大模型调用工具

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

相关推荐
.千余1 分钟前
【C++】模板进阶全解:非类型参数|全特化|偏特化|分离编译完全指南
开发语言·c++·笔记·学习·其他
代码改善世界9 分钟前
【C++进阶】C++11:列表初始化、右值引用与移动语义、完美转发全解析
java·开发语言·c++
饼饼饼12 分钟前
React19 状态解惑:State 没那么神秘,一文读懂 React 状态不可变原则与 Hooks 底层链表
前端·react.js
scx_link19 分钟前
通过git bash在本地创建分支,并推送到远程仓库中
开发语言·git·bash
GZ同学28 分钟前
单双变量Ripley’s K函数 R 语言实现
开发语言·r语言
Channing Lewis29 分钟前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
小小龙学IT36 分钟前
Apache Airflow 2.x 深度指南:用 Python 编排一切的现代化工作流引擎
开发语言·python·apache
難釋懷36 分钟前
Nginx获取客户端真实IP
服务器·前端·nginx
少爷晚安。38 分钟前
Java基础02_JDK&JRE下载安装及环境配置
java·开发语言
小冷爱读书39 分钟前
allocator
开发语言·c++