微信扫码
与创始人交个朋友
我要投稿
import os
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
from langchain_core.messages import HumanMessage
# 加载环境变量和设置模型
load_dotenv()
model = AzureChatOpenAI(
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
)
# 第一次对话
message = HumanMessage(content="I am Bob")
response = model.invoke([message])
print("Model's response:")
print(response.content)
# 第二次对话
message = HumanMessage(content="What's my name?")
response = model.invoke([message])
print("Model's response:")
print(response.content)
Model's response:
Hello Bob! It's nice to meet you. Is there anything I can help you with today?
Model's response:
I apologize, but I don't have any prior context or information about your name. Each interaction with me starts fresh, and I don't retain information from previous conversations. If you'd like me to know your name, you'll need to tell me in this current conversation. So, may I ask what your name is?
import os
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
# 加载环境变量和设置模型
load_dotenv()
model = AzureChatOpenAI(
model_name="gpt-4",
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
)
# 设置对话图和记忆
workflow = StateGraph(state_schema=MessagesState)
def call_model(state: MessagesState):
response = model.invoke(state["messages"])
return {"messages": response}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)
# 进行对话
config = {"configurable": {"thread_id": "tom"}}
# 第一次对话
query = "Hi! I'm Bob."
input_messages = [HumanMessage(query)]
output = app.invoke({"messages": input_messages}, config)
output["messages"][-1].pretty_print()
# 第二次对话
query = "What's my name?"
input_messages = [HumanMessage(query)]
output = app.invoke({"messages": input_messages}, config)
output["messages"][-1].pretty_print()
Human: Hi! I'm Bob.
AI: Hello Bob! It's nice to meet you. How can I assist you today?
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-10-31
大模型生成RPG游戏,情节角色全自定义!谷歌出品,一作上海交大校友
2024-10-31
360AI搜索接入腾讯混元大模型&切换大模型使用教程
2024-10-31
探索LLM推理全阶段的JSON格式输出限制方法
2024-10-31
大模型时代的对话分析:阿里最新综述全面解析对话分析的必要性
2024-10-31
MIT大牛新发现:LLM和人类大脑结构类似,存在脑叶分区!
2024-10-30
整合长期记忆,AI实现自我进化,探索大模型这一可能性
2024-10-30
OpenAI o1模型的前世今生
2024-10-30
西湖大学发布 「 AI 科学家」 Nova,效果相比 SOTA 提升 2.5 倍
2024-03-30
2024-07-18
2024-05-28
2024-04-26
2024-05-06
2024-04-11
2024-07-25
2024-07-09
2024-08-21
2024-06-12
2024-10-31
2024-10-30
2024-10-30
2024-10-30
2024-10-30
2024-10-30
2024-10-30
2024-10-30