test_agent.py
1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# pip install -qU "langchain[openai]" langgraph to call the model
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
import os
from typing import Dict, Any
# 设置 DEEPSEEK API 配置
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "sk-e59da2fbc73240ea8d5ef8fb12657e4b")
os.environ["OPENAI_BASE_URL"] = os.getenv("OPENAI_BASE_URL", "https://api.deepseek.com/v1")
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
# 创建 DEEPSEEK 聊天模型
model = ChatOpenAI(
model="deepseek-chat", # 使用 DEEPSEEK 模型
temperature=0.7
)
# 创建 ReAct 智能体
agent = create_react_agent(
model=model,
tools=[get_weather]
)
# LangGraph 服务端点
def weather_agent_endpoint(input_data: Dict[str, Any]) -> Dict[str, Any]:
"""LangGraph 服务端点"""
try:
result = agent.invoke(input_data)
return {
"status": "success",
"data": result,
"error": None
}
except Exception as e:
return {
"status": "error",
"data": None,
"error": str(e)
}
# 如果直接运行此文件
if __name__ == "__main__":
# Run the agent
result = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
print(result)