大模型调用工具

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

相关推荐
不正经学生17 分钟前
C语言预处理详解:编译器真正动手之前的那些事
c语言·开发语言·算法·面试·bug
门思科技26 分钟前
LoRaWAN 设备类别:Class A、B、C 对比与选型指南
c语言·开发语言·php
ℋᙚᵐⁱᒻᵉ鲸落3 小时前
移动端滑动手势冲突:overflow: auto导致外层横向滑动失效
前端·javascript·css·vue.js·html
恋恋西风3 小时前
C++ 理解 std::thread 在单核和多核上的行为差异
开发语言·c++
Brilliantwxx3 小时前
【Linux】 进程(9)程序与进程地址空间(基础+进阶+面试题)
linux·运维·服务器·开发语言·c++
BizzZ_4 小时前
C++(22)——类型转换和IO流
开发语言·c++
默_笙4 小时前
🎃 前端学了 Next.js,后端该学啥?NestJS 就是 Node 版的蜜雪冰城
前端·javascript
小范同学_4 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
geovindu4 小时前
CSharp: 万年历
开发语言·后端·c#·.net
我找到地球的支点啦5 小时前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信