大模型调用工具

from langchain.agents import create_agent,AgentState

from langchain_core.messages import HumanMessage

from langgraph.checkpoint.memory import InMemorySaver

from langgraph.store.memory import InMemoryStore

from langgraph.prebuilt import ToolRuntime

from langchain_deepseek import ChatDeepSeek

from typing import TypedDict, Annotated

from env_utils import DEEPSEEK_API_KEY, DEEPSEEK_BASE_URL

from langchain_core.tools import tool

@tool

def get_weather(city: str) -> str:

"""查询指定城市的天气信息"""

return f"The weather in {city} is sunny with a temperature of 25 degrees Celsius"

deepseek_llm = ChatDeepSeek(

model='deepseek-chat',

api_key=DEEPSEEK_API_KEY,

base_url=DEEPSEEK_BASE_URL

)

model_bind_tool=deepseek_llm.bind_tools([get_weather])

resp=model_bind_tool.invoke("北京的天气")

messages=[]

human_message=HumanMessage(content="北京的天气")

print(type(human_message))

print(human_message)

messages.append(human_message)

resp=model_bind_tool.invoke(messages)

print(type(resp))

print(resp)

messages.append(resp)

for tool_call in resp.tool_calls:

if tool_call['name'] == 'get_weather':

tool_result= get_weather.invoke(tool_call)

messages.append(tool_result)

print(type(tool_result))

print(tool_result)

resp=model_bind_tool.invoke(messages)

messages.append(resp)

print('3333333333333333333333')

print(type(resp))

print(resp)

print('44444444444444444444444')

print(messages)

HumanMessage(content='北京的天气', additional_kwargs={}, response_metadata={}), AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 44, 'prompt_tokens': 274, 'total_tokens': 318, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 256}, 'prompt_cache_hit_tokens': 256, 'prompt_cache_miss_tokens': 18}, 'model_provider': 'deepseek', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '0d85cad8-de94-446b-b5a1-0a78fa84b7ab', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e643d-b9b9-71f3-b8c4-38b1a9726ecd-0', tool_calls=\[{'name': 'get_weather', 'args': {'city': '北京'}, 'id': 'call_00_6DRAcna86ZrbvZJL0SGz5418', 'type': 'tool_call'}\], invalid_tool_calls=\[\], usage_metadata={'input_tokens': 274, 'output_tokens': 44, 'total_tokens': 318, 'input_token_details': {'cache_read': 256}, 'output_token_details': {}}), ToolMessage(content='The weather in 北京 is sunny with a temperature of 25 degrees Celsius', name='get_weather', tool_call_id='call_00_6DRAcna86ZrbvZJL0SGz5418'), AIMessage(content='北京的天气情况如下:\\n\\n- \*\*天气状况\*\*:☀️ 晴天\\n- \*\*温度\*\*:🌡️ 25°C\\n\\n是一个阳光明媚的好天气,温度舒适宜人,适合外出活动。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 45, 'prompt_tokens': 344, 'total_tokens': 389, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 256}, 'prompt_cache_hit_tokens': 256, 'prompt_cache_miss_tokens': 88}, 'model_provider': 'deepseek', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': 'c85e83f0-f71c-462a-8cb7-a5255cdf1005', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019e643d-bd3d-7183-982e-668f43917cf6-0', tool_calls=\[\], invalid_tool_calls=\[\], usage_metadata={'input_tokens': 344, 'output_tokens': 45, 'total_tokens': 389, 'input_token_details': {'cache_read': 256}, 'output_token_details': {}})

相关推荐
Ws_5 分钟前
C#学习 Day2
开发语言·学习·c#
杰克尼25 分钟前
天机学堂复习总结(day03-day04)
java·开发语言·redis·elasticsearch·spring cloud
tedcloud12332 分钟前
RTK部署教程:构建稳定的AI Workflow环境
服务器·javascript·人工智能·typescript·ocr
x***r1511 小时前
jdk-11.0.16.1_windows使用步骤详解(附JDK 11环境变量配置与验证教程)
java·开发语言·windows
ZC跨境爬虫1 小时前
跟着 MDN 学CSS day_16:(深入掌握背景与边框的艺术)
前端·css·ui·html·tensorflow
luck_bor2 小时前
File类&递归作业
java·开发语言
道里4 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验
前端·人工智能
Royzst4 小时前
xml知识点
java·服务器·前端
IT_陈寒5 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
努力努力再努力wz5 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github