大模型调用工具

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 分钟前
工厂模式实战:消灭魔法数字,把写死的 new 收进一个厂里
开发语言·python
niucloud-admin12 分钟前
NIUCLOUD LITE AI开发文档--后台系统结构
开发语言
Wang's Blog17 分钟前
Java 项目实战: 交友项目 - 登录发送验证码的测试验证与经验复盘
java·开发语言·交友
u01110267521 分钟前
WebP 转成 PNG 后为什么更大?图片格式转换不等于压缩
前端·javascript·vue.js
万少22 分钟前
Qoder sonus模型 20分钟,复刻 马斯克 飞天汽车
前端·后端·算法
马优晨42 分钟前
Java中的dto是什么
java·开发语言·java中的dto是什么·java的dto是什么·dto在java中是什么
java1234_小锋44 分钟前
【技术专题】HTML5 网页技术 - HTML5 列表标签
前端·html·html5
强二小哥帅1 小时前
Kotlin 协程复习笔记
前端
不可能片场1 小时前
工程实践 原子替换改了 ctime 怎么修
前端·electron
暧暧内含光1 小时前
HTTP 文件传输体系全解:从协议标头、OSS 架构到流媒体分片
前端