大模型调用工具

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': {}})]

相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
前端小万2 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝2 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋2 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
卡布鲁2 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
李少兄2 天前
JavaScript 隐式全局变量解析
javascript