代码
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
base_url = 'https://api.siliconflow.cn/v1/'
model = 'Pro/deepseek-ai/DeepSeek-R1'
api_key = 'sk-cyhqy...dngbbdxp'
llm_conf = {
'base_url': base_url,
'model': model,
'api_key': api_key
}
# llm = ChatOpenAI(**llm_conf) # 配置方法1
llm = ChatOpenAI(model=model, base_url=base_url, api_key=api_key, temperature=0.1)
resp = llm.invoke('你是谁?')
print('-- resp : ', resp)
def get_weather(city: str) -> str: # (1)!
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
agent = create_react_agent(
name='agent1',
model=llm,
tools=[get_weather],
)
resp2 = agent.invoke(
{"messages": [{"role": "user", "content": "深圳的天气如何?"}]}
)
print('\n\n')
print(resp2 )
返回数据
用户输入后,
1、走到大模型,选择工具、生成参数
2、运行工具,返回结果
3、大模型根据结果,总结,返回给用户
{
'messages': [
// 1 - 用户提问
HumanMessage(
content = '深圳的天气如何?',
additional_kwargs = {},
response_metadata = {},
id = '7f0cb6c2-fe5b-4664-b38f-f5b32339d885'
),
// 2 - AI 判断、调用工具、传入参数
AIMessage(
name = 'agent1',
id = 'run--c0903412-47a7-4fa1-83f4-83a4fb9c0d5b-0',
content = '\n我需要查询深圳的天气情况。根据可用工具,我将使用`get_weather`工具,并传入城市参数"深圳"。以下是工具调用: \n\n',
additional_kwargs = {
'refusal': None,
'tool_calls': [{
'id': '0197677534ab7eed6dac12c2ef76696e',
'function': {
'arguments': '{"city": "\\u6df1\\u5733"}',
'name': 'get_weather'
},
'type': 'function',
'index': 0
}]
},
response_metadata = {
'model_name': 'Pro/deepseek-ai/DeepSeek-R1',
'system_fingerprint': '',
'id': '01976774e9b9d8f74af09bee5c9b8996',
'service_tier': None,
'finish_reason': 'tool_calls',
'logprobs': None,
'token_usage': {
'completion_tokens': 122,
'prompt_tokens': 169,
'total_tokens': 291,
'completion_tokens_details': {
'accepted_prediction_tokens': None,
'audio_tokens': None,
'reasoning_tokens': 85,
'rejected_prediction_tokens': None
},
'prompt_tokens_details': None
},
},
tool_calls = [{
'name': 'get_weather',
'args': {
'city': '深圳'
},
'id': '0197677534ab7eed6dac12c2ef76696e',
'type': 'tool_call'
}],
usage_metadata = {
'input_tokens': 169,
'output_tokens': 122,
'total_tokens': 291,
'input_token_details': {},
'output_token_details': {
'reasoning': 85
}
}
),
// 3 - 工具返回
ToolMessage(
content = "It's always sunny in 深圳!",
name = 'get_weather',
id = '87fd53b0-52b3-4814-9e68-7343dbf087ff',
tool_call_id = '0197677534ab7eed6dac12c2ef76696e'
),
// 4 - AI 总结回复
AIMessage(
name = 'agent1',
id = 'run--827c731c-a9b2-43c5-922d-3ebaf3868b2b-0',
content = '\n根据查询结果,深圳的天气总是晴朗的!',
additional_kwargs = {
'refusal': None
},
usage_metadata = {
'input_tokens': 258,
'output_tokens': 87,
'total_tokens': 345,
'input_token_details': {},
'output_token_details': {
'reasoning': 74
}
},
response_metadata = {
'token_usage': {
'completion_tokens': 87,
'prompt_tokens': 258,
'total_tokens': 345,
'completion_tokens_details': {
'accepted_prediction_tokens': None,
'audio_tokens': None,
'reasoning_tokens': 74,
'rejected_prediction_tokens': None
},
'prompt_tokens_details': None
},
'model_name': 'Pro/deepseek-ai/DeepSeek-R1',
'system_fingerprint': '',
'id': '019767753611d940e994d87b10b1ab26',
'service_tier': None,
'finish_reason': 'stop',
'logprobs': None
},
)
]
}
2025-06-13(五)