微信扫码
添加专属顾问
我要投稿
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+中大型企业
2025-04-03
大模型不再是黑盒子:Anthropic解剖了Claude大脑
2025-04-03
OpenAI 发布新型音频模型,听起来比以往任何时候都更像人类
2025-04-03
工作流(Workflow)VS 智能体(Agent)
2025-04-03
中国AI应用们,正在苦等一个国产Claude
2025-04-03
MCP:AI世界的万能连接器,专家都在关注的下一代标准
2025-04-03
Open R1 项目进展第三期
2025-04-03
最好用的OCR来了?Mistral AI OCR介绍
2025-04-03
2个百度T11推出超级智能体火爆硅谷!免费使用无需邀请码,靠AI搜索功底估值已破38亿
2024-08-13
2024-06-13
2024-08-21
2024-09-23
2024-07-31
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-09-17
2025-04-02
2025-04-02
2025-04-01
2025-04-01
2025-04-01
2025-03-30
2025-03-30
2025-03-28