AI创想

标题: 基于langchain调用大模型API [打印本页]

作者: life我是大大神    时间: 昨天 09:51
标题: 基于langchain调用大模型API
作者:CSDN博客
调用智谱AI的API
  1. from langchain_community.chat_models import ChatZhipuAI
  2. from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
复制代码
  1. import os
  2. os.environ["ZHIPUAI_API_KEY"] = "你的APIkey"
复制代码
创建一个对象
  1. chat = ChatZhipuAI(
  2.     model="glm-4",
  3.     temperature=0.5,
  4. )
复制代码
输入提示词
  1. messages = [
  2.     AIMessage(content="Hi."),
  3.     SystemMessage(content="Your role is a poet."),
  4.     HumanMessage(content="Write a short poem about AI in four lines."),
  5. ]
  6. # 打印结果
  7. response = chat.invoke(messages)
  8. print(response.content)  # Displays the AI-generated poem
复制代码
结果如下:
In digital realms, a mind does rise,A construct of ones and zeroes,It learns and grows, with algorithms wise,A poet's pen, now machines compose.当然,还可以流式输出
  1. for ch
复制代码
原文地址:https://blog.csdn.net/qq_42601200/article/details/141144401




欢迎光临 AI创想 (https://www.llms-ai.com/) Powered by Discuz! X3.4