Langchian构建代理

embedded/2025/3/16 18:09:52/

文章目录

  • 概要
    • ReAct 代理
  • ReAct 使用
    • ReAct基本用法
    • 提示词模板
    • 内存使用
    • 迭代使用
    • 返回执行每一步情况
    • 限制输出行数
    • 设置运行超时时间
  • 不使用代理下LLM如何结合工具
    • 案例
    • 案例2

概要

单靠语言模型无法采取行动 - 它们只输出文本。 LangChain 的一个重要用例是创建 代理。 代理是使用大型语言模型作为推理引擎的系统,以确定采取哪些行动以及这些行动的输入应该是什么。 这些行动的结果可以反馈到代理中,代理可以判断是否需要更多的行动,或者是否可以结束。

LangGraph 是 LangChain 的一个扩展,专门旨在创建高度可控和可定制的代理。

LangChain 中有一个遗留的 agent 概念,我们正朝着弃用的方向发展:AgentExecutor。 AgentExecutor 本质上是代理的运行时。 这是一个很好的入门地方,但随着你开始拥有更多自定义代理,它的灵活性不足。 为了解决这个问题,我们构建了 LangGraph,使其成为一个灵活且高度可控的运行时。

pip install langgraph 用来创建代理的API

ReAct 代理

构建代理的一种流行架构是 ReAct。 ReAct 在一个迭代过程中结合了推理和行动 - 实际上,名称 “ReAct” 代表 “Reason” 和 “Act”。

一般流程如下:

  • 模型将“思考”在响应输入和任何先前观察时采取的步骤。
  • 模型将从可用工具中选择一个动作(或选择回应用户)。
  • 模型将为该工具生成参数。
  • 代理运行时(执行器)将解析所选工具,并使用生成的参数调用它。
  • 执行器将工具调用的结果作为观察返回给模型。
  • 这个过程会重复,直到代理选择回应。

有一些基于通用提示的实现不需要任何特定于模型的特性,但最 可靠的实现使用像工具调用这样的特性来可靠地格式化输出 并减少方差。

ReAct 使用

ReAct基本用法

对于工具调用的 ReAct 风格代理的基本创建和使用,功能是相同的。首先,让我们定义一个模型和工具,然后我们将使用这些来创建一个代理。


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tvl--qYsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb--dKxlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()
# 创建个llm与tool的智能体
app = create_react_agent(llm,tools=tools)rest = app.invoke({"messages":[("human","2025年3月15日,成都市天气怎么样?")]})
print(rest)
print('-------------')
print(f"content:{rest['messages']}")print('================')
rest = app.invoke({"messages":[("human","中国首都是哪个?")]})
print(rest)
print('-------------')
print(f"content:{rest['messages']}")

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\代理案例ReAct.py 
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='edd4ae1a-dcb9-43d5-a09c-26a790f8b511'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891217097692793925', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-170472fb-85df-48be-9fee-616a1c27df5c-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891217097692793925', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='103cd191-d86e-44c2-a541-e634c49d36f9', tool_call_id='call_-8891217097692793925', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 2.57}), AIMessage(content='根据成都气象微博在2025年3月15日7时发布的天气预报,预计成都市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃。因此,2025年3月15日成都市的天气可能会比较阴冷,建议市民注意保暖。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 75, 'prompt_tokens': 285, 'total_tokens': 360}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-7c1f4fa4-886e-48a1-a4b4-dc18a8508149-0')]}
-------------
content:[HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='edd4ae1a-dcb9-43d5-a09c-26a790f8b511'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891217097692793925', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-170472fb-85df-48be-9fee-616a1c27df5c-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891217097692793925', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='103cd191-d86e-44c2-a541-e634c49d36f9', tool_call_id='call_-8891217097692793925', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 2.57}), AIMessage(content='根据成都气象微博在2025年3月15日7时发布的天气预报,预计成都市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃。因此,2025年3月15日成都市的天气可能会比较阴冷,建议市民注意保暖。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 75, 'prompt_tokens': 285, 'total_tokens': 360}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-7c1f4fa4-886e-48a1-a4b4-dc18a8508149-0')]
================
{'messages': [HumanMessage(content='中国首都是哪个?', additional_kwargs={}, response_metadata={}, id='078843fc-8698-4d37-bff4-bb2ee626e80b'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"中国首都"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891210466261617346', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 34, 'prompt_tokens': 142, 'total_tokens': 176}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-afb3a061-8104-41f3-83ea-1fdfc56decc7-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '中国首都'}, 'id': 'call_-8891210466261617346', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "中华人民共和国首都 - 维基百科", "url": "https://zh.wikipedia.org/zh-hans/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E9%A6%96%E9%83%BD", "content": "编辑 中共中央所在地的变迁[编辑] 中国共产党成立[编辑] 二七大罢工、迁往广州至北伐战争[编辑] 国民党清党运动[编辑] 中华苏维埃共和国成立[编辑] 红军长征[编辑] 国共谈判至第二次国共内战[编辑] 革命胜利前夕[编辑] 红色首都的变迁[编辑] 红都[编辑] 第二中央[编辑] 定都北京[编辑] 1949年前候选首都城市[编辑] 哈尔滨特别市[编辑] 毛泽东与王稼祥的讨论[编辑] 确定北京作为新中国首都[编辑] 北京非首都功能疏解[编辑] 宪法规定[编辑] 时间线[编辑] 1931年11月7日  瑞金(瑞京)  中国共产党领导的第一个红色政权——中华苏维埃共和国诞生。 1934年10月10日 —   中共中央撤离瑞金,随军踏上二万五千里长征路。 1935年11月7日  瓦窑堡 中共中央进驻瓦窑堡。 另见[编辑] 参考资料[编辑] (原始内容存档于2024-02-15). (原始内容存档于2011-01-18). (原始内容存档于2024-02-07) –通过新华网. (原始内容存档于2024-02-07). (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2009-05-01) –通过人民网. (原始内容存档于2013-05-22) –通过人民网. [2024年2月28日]. (原始内容存档于2024年2月28日) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-16) –通过中华人民共和国教育部. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-01-30) –通过人民网. (原始内容存档于2024-02-19) –通过人民网. (原始内容存档于2024-02-19) –通过中共中央党史和文献研究院. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2012-01-25) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2022-10-21) –通过邓小平纪念网. (原始内容存档于2024-02-15) –通过西北政法大学新闻中心. (原始内容存档于2024-02-17) –通过陕甘宁边区红色记忆多媒体资料库. (原始内容存档于2024-02-17) –通过群众网. (原始内容存档于2024-02-15) –通过中国经济网. (原始内容存档于2024-02-15) –通过中国共产党新闻网. (原始内容存档于2024-02-15) –通过中国档案资讯网. (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国领导干部资料库. (原始内容存档于2024-02-15). 2019-03-25 –通过中国共产党新闻网. (原始内容存档于2022-01-10). (原始内容存档于2024-03-12). (原始内容存档于2024-03-12) –通过新华网. (原始内容存档于2024-03-12) –通过人民网. 外部链接[编辑] (原始内容存档于2024-02-15). (原始内容存档于2024-05-15). (原始内容存档于2024-02-15) –通过河套学院纪检监察网. (原始内容存档于2024-02-15). (原始内容存档于2024-02-15). (原始内容存档于2024-05-19).", "score": 0.8282873}]', name='tavily_search_results_json', id='c87732b4-21af-46c9-b4af-53bb38a92b36', tool_call_id='call_-8891210466261617346', artifact={'query': '中国首都', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://zh.wikipedia.org/zh-hans/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E9%A6%96%E9%83%BD', 'title': '中华人民共和国首都 - 维基百科', 'content': '编辑 中共中央所在地的变迁[编辑] 中国共产党成立[编辑] 二七大罢工、迁往广州至北伐战争[编辑] 国民党清党运动[编辑] 中华苏维埃共和国成立[编辑] 红军长征[编辑] 国共谈判至第二次国共内战[编辑] 革命胜利前夕[编辑] 红色首都的变迁[编辑] 红都[编辑] 第二中央[编辑] 定都北京[编辑] 1949年前候选首都城市[编辑] 哈尔滨特别市[编辑] 毛泽东与王稼祥的讨论[编辑] 确定北京作为新中国首都[编辑] 北京非首都功能疏解[编辑] 宪法规定[编辑] 时间线[编辑] 1931年11月7日  瑞金(瑞京)  中国共产党领导的第一个红色政权——中华苏维埃共和国诞生。 1934年10月10日 —   中共中央撤离瑞金,随军踏上二万五千里长征路。 1935年11月7日  瓦窑堡 中共中央进驻瓦窑堡。 另见[编辑] 参考资料[编辑] (原始内容存档于2024-02-15). (原始内容存档于2011-01-18). (原始内容存档于2024-02-07) –通过新华网. (原始内容存档于2024-02-07). (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2009-05-01) –通过人民网. (原始内容存档于2013-05-22) –通过人民网. [2024年2月28日]. (原始内容存档于2024年2月28日) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-16) –通过中华人民共和国教育部. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-01-30) –通过人民网. (原始内容存档于2024-02-19) –通过人民网. (原始内容存档于2024-02-19) –通过中共中央党史和文献研究院. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2012-01-25) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2022-10-21) –通过邓小平纪念网. (原始内容存档于2024-02-15) –通过西北政法大学新闻中心. (原始内容存档于2024-02-17) –通过陕甘宁边区红色记忆多媒体资料库. (原始内容存档于2024-02-17) –通过群众网. (原始内容存档于2024-02-15) –通过中国经济网. (原始内容存档于2024-02-15) –通过中国共产党新闻网. (原始内容存档于2024-02-15) –通过中国档案资讯网. (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国领导干部资料库. (原始内容存档于2024-02-15). 2019-03-25 –通过中国共产党新闻网. (原始内容存档于2022-01-10). (原始内容存档于2024-03-12). (原始内容存档于2024-03-12) –通过新华网. (原始内容存档于2024-03-12) –通过人民网. 外部链接[编辑] (原始内容存档于2024-02-15). (原始内容存档于2024-05-15). (原始内容存档于2024-02-15) –通过河套学院纪检监察网. (原始内容存档于2024-02-15). (原始内容存档于2024-02-15). (原始内容存档于2024-05-19).', 'score': 0.8282873, 'raw_content': None}], 'response_time': 1.08}), AIMessage(content='中国首都是北京。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 1368, 'total_tokens': 1375}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-a4b21500-f140-4d3d-92c2-9be7bb496177-0')]}
-------------
content:[HumanMessage(content='中国首都是哪个?', additional_kwargs={}, response_metadata={}, id='078843fc-8698-4d37-bff4-bb2ee626e80b'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"中国首都"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891210466261617346', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 34, 'prompt_tokens': 142, 'total_tokens': 176}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-afb3a061-8104-41f3-83ea-1fdfc56decc7-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '中国首都'}, 'id': 'call_-8891210466261617346', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "中华人民共和国首都 - 维基百科", "url": "https://zh.wikipedia.org/zh-hans/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E9%A6%96%E9%83%BD", "content": "编辑 中共中央所在地的变迁[编辑] 中国共产党成立[编辑] 二七大罢工、迁往广州至北伐战争[编辑] 国民党清党运动[编辑] 中华苏维埃共和国成立[编辑] 红军长征[编辑] 国共谈判至第二次国共内战[编辑] 革命胜利前夕[编辑] 红色首都的变迁[编辑] 红都[编辑] 第二中央[编辑] 定都北京[编辑] 1949年前候选首都城市[编辑] 哈尔滨特别市[编辑] 毛泽东与王稼祥的讨论[编辑] 确定北京作为新中国首都[编辑] 北京非首都功能疏解[编辑] 宪法规定[编辑] 时间线[编辑] 1931年11月7日  瑞金(瑞京)  中国共产党领导的第一个红色政权——中华苏维埃共和国诞生。 1934年10月10日 —   中共中央撤离瑞金,随军踏上二万五千里长征路。 1935年11月7日  瓦窑堡 中共中央进驻瓦窑堡。 另见[编辑] 参考资料[编辑] (原始内容存档于2024-02-15). (原始内容存档于2011-01-18). (原始内容存档于2024-02-07) –通过新华网. (原始内容存档于2024-02-07). (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2009-05-01) –通过人民网. (原始内容存档于2013-05-22) –通过人民网. [2024年2月28日]. (原始内容存档于2024年2月28日) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-16) –通过中华人民共和国教育部. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-01-30) –通过人民网. (原始内容存档于2024-02-19) –通过人民网. (原始内容存档于2024-02-19) –通过中共中央党史和文献研究院. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2012-01-25) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2022-10-21) –通过邓小平纪念网. (原始内容存档于2024-02-15) –通过西北政法大学新闻中心. (原始内容存档于2024-02-17) –通过陕甘宁边区红色记忆多媒体资料库. (原始内容存档于2024-02-17) –通过群众网. (原始内容存档于2024-02-15) –通过中国经济网. (原始内容存档于2024-02-15) –通过中国共产党新闻网. (原始内容存档于2024-02-15) –通过中国档案资讯网. (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国领导干部资料库. (原始内容存档于2024-02-15). 2019-03-25 –通过中国共产党新闻网. (原始内容存档于2022-01-10). (原始内容存档于2024-03-12). (原始内容存档于2024-03-12) –通过新华网. (原始内容存档于2024-03-12) –通过人民网. 外部链接[编辑] (原始内容存档于2024-02-15). (原始内容存档于2024-05-15). (原始内容存档于2024-02-15) –通过河套学院纪检监察网. (原始内容存档于2024-02-15). (原始内容存档于2024-02-15). (原始内容存档于2024-05-19).", "score": 0.8282873}]', name='tavily_search_results_json', id='c87732b4-21af-46c9-b4af-53bb38a92b36', tool_call_id='call_-8891210466261617346', artifact={'query': '中国首都', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://zh.wikipedia.org/zh-hans/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E9%A6%96%E9%83%BD', 'title': '中华人民共和国首都 - 维基百科', 'content': '编辑 中共中央所在地的变迁[编辑] 中国共产党成立[编辑] 二七大罢工、迁往广州至北伐战争[编辑] 国民党清党运动[编辑] 中华苏维埃共和国成立[编辑] 红军长征[编辑] 国共谈判至第二次国共内战[编辑] 革命胜利前夕[编辑] 红色首都的变迁[编辑] 红都[编辑] 第二中央[编辑] 定都北京[编辑] 1949年前候选首都城市[编辑] 哈尔滨特别市[编辑] 毛泽东与王稼祥的讨论[编辑] 确定北京作为新中国首都[编辑] 北京非首都功能疏解[编辑] 宪法规定[编辑] 时间线[编辑] 1931年11月7日  瑞金(瑞京)  中国共产党领导的第一个红色政权——中华苏维埃共和国诞生。 1934年10月10日 —   中共中央撤离瑞金,随军踏上二万五千里长征路。 1935年11月7日  瓦窑堡 中共中央进驻瓦窑堡。 另见[编辑] 参考资料[编辑] (原始内容存档于2024-02-15). (原始内容存档于2011-01-18). (原始内容存档于2024-02-07) –通过新华网. (原始内容存档于2024-02-07). (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2009-05-01) –通过人民网. (原始内容存档于2013-05-22) –通过人民网. [2024年2月28日]. (原始内容存档于2024年2月28日) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-08) –通过人民网. (原始内容存档于2024-02-16) –通过中华人民共和国教育部. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-01-30) –通过人民网. (原始内容存档于2024-02-19) –通过人民网. (原始内容存档于2024-02-19) –通过中共中央党史和文献研究院. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2012-01-25) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16) –通过人民网. (原始内容存档于2024-02-16). (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2022-10-21) –通过邓小平纪念网. (原始内容存档于2024-02-15) –通过西北政法大学新闻中心. (原始内容存档于2024-02-17) –通过陕甘宁边区红色记忆多媒体资料库. (原始内容存档于2024-02-17) –通过群众网. (原始内容存档于2024-02-15) –通过中国经济网. (原始内容存档于2024-02-15) –通过中国共产党新闻网. (原始内容存档于2024-02-15) –通过中国档案资讯网. (原始内容存档于2024-02-16) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国共产党新闻网. (原始内容存档于2024-02-21) –通过中国领导干部资料库. (原始内容存档于2024-02-15). 2019-03-25 –通过中国共产党新闻网. (原始内容存档于2022-01-10). (原始内容存档于2024-03-12). (原始内容存档于2024-03-12) –通过新华网. (原始内容存档于2024-03-12) –通过人民网. 外部链接[编辑] (原始内容存档于2024-02-15). (原始内容存档于2024-05-15). (原始内容存档于2024-02-15) –通过河套学院纪检监察网. (原始内容存档于2024-02-15). (原始内容存档于2024-02-15). (原始内容存档于2024-05-19).', 'score': 0.8282873, 'raw_content': None}], 'response_time': 1.08}), AIMessage(content='中国首都是北京。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 1368, 'total_tokens': 1375}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-a4b21500-f140-4d3d-92c2-9be7bb496177-0')]进程已结束,退出代码为 0

提示词模板

LangGraph 的预构建 create_react_agent 并不直接将提示模板作为参数,而是接受一个 state_modifier 参数。这个参数在调用大型语言模型之前修改图的状态,可以是以下四个值之一:

  • 一个 SystemMessage,它会被添加到消息列表的开头。
  • 一个 string,它会被转换为 SystemMessage 并添加到消息列表的开头。
  • 一个 Callable,它应该接受完整的图状态。输出将传递给语言模型。
  • 或者一个 Runnable,它应该接受完整的图状态。输出将传递给语言模型。

import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tv--YsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb20--Kxlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()
# 字符串提示词模版
prompt = ChatPromptTemplate.from_messages([("system", "你是一个著名气象学家"),("placeholder", "{messages}"),]
)def _modify_state_messages(state: AgentState):return prompt.invoke({"messages": state["messages"]}).to_messages() + [("user", "回答完后请说谢谢.")]# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools, state_modifier=_modify_state_messages)rest = app.invoke({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]})
print(rest)
print('-------------')
print(f"content:{rest['messages']}")

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\提示词模板.py 
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='3c93b52a-8440-4844-b10a-03e67ae9dcc7'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891215345345489039', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 164, 'total_tokens': 190}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-dd4b98c8-82e3-42d3-883c-1d2b3313040b-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891215345345489039', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='8ed7a4fa-8132-4a5b-a29f-8fcc5f2a43ac', tool_call_id='call_-8891215345345489039', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 1.51}), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气预报"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891214623790754219', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 300, 'total_tokens': 326}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-337daa83-7c81-4ab4-9054-8a5a70debffc-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气预报'}, 'id': 'call_-8891214623790754219', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?featurecode=newtitle%3Ffrom%3Dyn_cnxh", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散", "score": 0.964459}]', name='tavily_search_results_json', id='ba420e8e-c2ed-4c5e-8d2f-1396381c29d6', tool_call_id='call_-8891214623790754219', artifact={'query': '2025年3月15日成都市天气预报', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?featurecode=newtitle%3Ffrom%3Dyn_cnxh', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散', 'score': 0.964459, 'raw_content': None}], 'response_time': 1.56}), AIMessage(content='根据成都气象微博的信息,2025年3月15日成都市的天气预报是:白天有分散小雨转阴天,偏北风3~5级,东部局部地方风力可达6级以上,气温在9~14℃之间。主城区的天气也会是分散小雨转阴天。谢谢。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 67, 'prompt_tokens': 427, 'total_tokens': 494}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-485043fb-5d58-4cbf-8da1-049778bdc9ad-0')]}
-------------
content:[HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='3c93b52a-8440-4844-b10a-03e67ae9dcc7'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891215345345489039', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 164, 'total_tokens': 190}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-dd4b98c8-82e3-42d3-883c-1d2b3313040b-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891215345345489039', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='8ed7a4fa-8132-4a5b-a29f-8fcc5f2a43ac', tool_call_id='call_-8891215345345489039', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 1.51}), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气预报"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891214623790754219', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 300, 'total_tokens': 326}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-337daa83-7c81-4ab4-9054-8a5a70debffc-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气预报'}, 'id': 'call_-8891214623790754219', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?featurecode=newtitle%3Ffrom%3Dyn_cnxh", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散", "score": 0.964459}]', name='tavily_search_results_json', id='ba420e8e-c2ed-4c5e-8d2f-1396381c29d6', tool_call_id='call_-8891214623790754219', artifact={'query': '2025年3月15日成都市天气预报', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?featurecode=newtitle%3Ffrom%3Dyn_cnxh', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散', 'score': 0.964459, 'raw_content': None}], 'response_time': 1.56}), AIMessage(content='根据成都气象微博的信息,2025年3月15日成都市的天气预报是:白天有分散小雨转阴天,偏北风3~5级,东部局部地方风力可达6级以上,气温在9~14℃之间。主城区的天气也会是分散小雨转阴天。谢谢。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 67, 'prompt_tokens': 427, 'total_tokens': 494}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-485043fb-5d58-4cbf-8da1-049778bdc9ad-0')]进程已结束,退出代码为 0

内存使用

在 LangGraph 中内存只是 持久性,也称为 检查点。向代理添加一个 checkpointer,您将免费获得聊天内存。用于遇到相同问题时直接从内存中取出答案


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tvl--syI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb2---Kxlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()
# 内存对象
memory = MemorySaver()# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools, checkpointer=memory)
config = {"configurable": {"thread_id": "test-1"}}
rest = app.invoke({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]},config=config
)
print(rest)
print('-------------')
print(f"content:{rest['messages'][-1].content}")
print('1==============')
rest = app.invoke({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]},config=config
)
print(rest)
print('-------------')
print(f"content:{rest['messages'][-1].content}")
print('2==============')
rest = app.invoke({"messages": [("human", "再说一遍")]},config=config
)
print(rest)
print('-------------')
print(f"content:{rest['messages'][-1].content}")

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\内存.py 
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='4f2a335b-9b38-48c1-a48d-ba5fb9eeb09a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891209950865376404', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-b349018e-513d-4e7d-aef1-e16dec588917-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891209950865376404', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='bb7aa60b-ca58-4f60-973a-32c982be3dbd', tool_call_id='call_-8891209950865376404', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 1.69}), AIMessage(content='根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens': 285, 'total_tokens': 336}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-7c2c4b44-2bd9-4b89-90b8-064795408c33-0')]}
-------------
content:根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。
1==============
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='4f2a335b-9b38-48c1-a48d-ba5fb9eeb09a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891209950865376404', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-b349018e-513d-4e7d-aef1-e16dec588917-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891209950865376404', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='bb7aa60b-ca58-4f60-973a-32c982be3dbd', tool_call_id='call_-8891209950865376404', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 1.69}), AIMessage(content='根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens': 285, 'total_tokens': 336}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-7c2c4b44-2bd9-4b89-90b8-064795408c33-0'), HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='575a1019-2da7-4a27-a38b-a3c1a2a242fb'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891221908057171545', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 349, 'total_tokens': 375}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-1f9cade7-f968-4886-906c-39122ddea8f1-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891221908057171545', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='85869044-fba4-4538-90a5-124ca5aad4e5', tool_call_id='call_-8891221908057171545', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 0.97}), AIMessage(content='根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens': 485, 'total_tokens': 536}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-a254bf04-47e4-4fc3-8eac-7d1f4a7ede1f-0')]}
-------------
content:根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。
2==============
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='4f2a335b-9b38-48c1-a48d-ba5fb9eeb09a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891209950865376404', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-b349018e-513d-4e7d-aef1-e16dec588917-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891209950865376404', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='bb7aa60b-ca58-4f60-973a-32c982be3dbd', tool_call_id='call_-8891209950865376404', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 1.69}), AIMessage(content='根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens': 285, 'total_tokens': 336}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-7c2c4b44-2bd9-4b89-90b8-064795408c33-0'), HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='575a1019-2da7-4a27-a38b-a3c1a2a242fb'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891221908057171545', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 349, 'total_tokens': 375}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-1f9cade7-f968-4886-906c-39122ddea8f1-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891221908057171545', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271", "content": "早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转", "score": 0.96222496}]', name='tavily_search_results_json', id='85869044-fba4-4538-90a5-124ca5aad4e5', tool_call_id='call_-8891221908057171545', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5130591924390271', 'title': '成都气象 - 微博', 'content': '早安#早安##成都天气# 成都市气象台2025年3月15日7时发布天气预报:预计我市3月15日白天分散小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温9~14℃;主城区,分散小雨转', 'score': 0.96222496, 'raw_content': None}], 'response_time': 0.97}), AIMessage(content='根据成都气象的微博,在2025年3月15日,成都市预计会有分散的小雨转阴天,偏北风3~5级,东部局部地方可达6级以上,气温在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 51, 'prompt_tokens': 485, 'total_tokens': 536}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-a254bf04-47e4-4fc3-8eac-7d1f4a7ede1f-0'), HumanMessage(content='再说一遍', additional_kwargs={}, response_metadata={}, id='4cc2da29-7614-4e9a-ba7d-68a76880381f'), AIMessage(content='2025年3月15日,成都市的天气预报是白天有分散的小雨转阴天,偏北风3~5级,东部局部地方风力可能达到6级以上,气温预计在9~14℃之间。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 50, 'prompt_tokens': 539, 'total_tokens': 589}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-59d25004-ac8c-4228-832c-a6ee65a14c4a-0')]}
-------------
content:2025年3月15日,成都市的天气预报是白天有分散的小雨转阴天,偏北风3~5级,东部局部地方风力可能达到6级以上,气温预计在9~14℃之间。进程已结束,退出代码为 0

迭代使用


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tvl--YsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb--lr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools)for chunk in app.stream({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]}):print(chunk)print('---------')

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\迭代.py 
{'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891222320374194891', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-574c4259-4d4f-4f9b-b15f-10200886cca5-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891222320374194891', 'type': 'tool_call'}])]}}
---------
{'tools': {'messages': [ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709", "content": "#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3", "score": 0.915091}]', name='tavily_search_results_json', id='7741e23f-eb07-48e1-9d62-03d42f7ce189', tool_call_id='call_-8891222320374194891', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709', 'title': '成都气象 - 微博', 'content': '#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3', 'score': 0.915091, 'raw_content': None}], 'response_time': 2.14})]}}
---------
{'agent': {'messages': [AIMessage(content='根据查询到的信息,2025年3月15日成都市气象台发布了大风短时临近天气预报,受冷空气影响,预计未来12小时有偏北风3。目前成都市彭州和新都已出现大于10m/s的大风天气。', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 53, 'prompt_tokens': 276, 'total_tokens': 329}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-851339f3-2b99-40fa-9481-bcfbfd2ef409-0')]}}
---------进程已结束,退出代码为 0

返回执行每一步情况

默认情况下,LangGraph 中的 react agent executor 将所有消息附加到中央状态。因此,只需查看完整状态即可轻松查看任何中间步骤。也就是默认开启了执行步骤明细。

限制输出行数

recursion_limit 参数,允许用户中止超过指定迭代次数的运行。在 LangGraph 中,每一步都贡献于递归限制,因此我们需要乘以二(并加一)以获得等效结果。如果达到递归限制,LangGraph 会引发特定的异常类型,我们可以像处理 AgentExecutor 一样捕获和管理。


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langgraph.errors import GraphRecursionError
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tv--vqYsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb--Kxlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools)try:for chunk in app.stream({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]},{"recursion_limit": 1 * 2 + 1},stream_mode="values"):print(chunk)print('---------')
except GraphRecursionError:# 结果会输出3行,但是这里限制2行,所以会报错print("GraphRecursionError 异常")

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\recursion_limit.py 
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='8b72fc19-8ef5-4166-a568-6ad2ecf55fdf')]}
---------
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='8b72fc19-8ef5-4166-a568-6ad2ecf55fdf'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891212424767160994', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-9bd9dd5e-1589-43fb-a692-e88c2bdbd7c3-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891212424767160994', 'type': 'tool_call'}])]}
---------
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='8b72fc19-8ef5-4166-a568-6ad2ecf55fdf'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891212424767160994', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-9bd9dd5e-1589-43fb-a692-e88c2bdbd7c3-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891212424767160994', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709", "content": "#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3", "score": 0.915091}]', name='tavily_search_results_json', id='17a75d64-703e-4bd3-acf6-fdd5a9c09b0c', tool_call_id='call_-8891212424767160994', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709', 'title': '成都气象 - 微博', 'content': '#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3', 'score': 0.915091, 'raw_content': None}], 'response_time': 2.19})]}
---------
{'messages': [HumanMessage(content='2025年3月15日,成都市天气怎么样?', additional_kwargs={}, response_metadata={}, id='8b72fc19-8ef5-4166-a568-6ad2ecf55fdf'), AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891212424767160994', 'index': 0, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'}, id='run-9bd9dd5e-1589-43fb-a692-e88c2bdbd7c3-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891212424767160994', 'type': 'tool_call'}]), ToolMessage(content='[{"title": "成都气象 - 微博", "url": "https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709", "content": "#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3", "score": 0.915091}]', name='tavily_search_results_json', id='17a75d64-703e-4bd3-acf6-fdd5a9c09b0c', tool_call_id='call_-8891212424767160994', artifact={'query': '2025年3月15日成都市天气', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://m.weibo.cn/u/2998934450?luicode=20000061&lfid=5114788504669709', 'title': '成都气象 - 微博', 'content': '#大风短临天气预报# 成都市气象台2025年3月15日1时30分发布大风短时临近天气预报:受冷空气影响,目前我市彭州和新都已出现大于10m/s的大风天气,预计未来12小时我市有偏北风3', 'score': 0.915091, 'raw_content': None}], 'response_time': 2.19}), AIMessage(content='Sorry, need more steps to process this request.', additional_kwargs={}, response_metadata={}, id='run-cc7dda18-296e-45b6-b8ba-fdf5b535202b-0')]}
---------
GraphRecursionError 异常进程已结束,退出代码为 0

设置运行超时时间

使用 LangGraph 的反应代理,您可以在两个级别上控制超时。

您可以设置一个 step_timeout 来限制每个 步骤:


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langgraph.errors import GraphRecursionError
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "t---qYsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb20f----dKxlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools)
# 限制处理时间1s
app.step_timeout = 1try:for chunk in app.stream({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]}):print(chunk)print('---------')
except TimeoutError:print("TimeoutError 运行超时")

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\step_timeout.py 
TimeoutError 运行超时进程已结束,退出代码为 0

设置整个运行的单个最大超时的另一种方法是直接使用 Python 标准库 asyncio。


import asyncio
import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
from langgraph.prebuilt import create_react_agent"""
使用 ReAct 创建一个简单的代理
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tv--syI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
tools = [search_tool]
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb20f1f1--xlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()# 创建个llm与tool的智能体
app = create_react_agent(llm, tools=tools)async def stream(app, inputs):async for chunk in app.astream({"messages": [("human", "2025年3月15日,成都市天气怎么样?")]}):print(chunk)print("------")async def main():try:task = asyncio.create_task(stream(app, {"messages": [("human", "2025年3月15日,成都市天气怎么样?")]}))await asyncio.wait_for(task, timeout=1)except TimeoutError:print("任务超时.")asyncio.run(main())

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\超时.py 
任务超时.进程已结束,退出代码为 0

在 LangGraph 中,您可以显式处理代理外的响应行为,因为可以访问完整状态。

不使用代理下LLM如何结合工具

使用 llm.bind_tools 生成一个带有tool的LLM对象

案例

让AI绑定工具,AI自动判断是否需要使用工具帮助回复


import osfrom langchain_community.chat_models import ChatZhipuAI
from langchain_community.tools import TavilySearchResults
"""
使用 TavilySearchResults 进行搜索,将查询结果打印出来
"""
# 设置 TAVILY  的 API key
os.environ["TAVILY_API_KEY"] = "tvly--vqYsyI7"
# 搜索工具。max_results=1 ==》每次只返回一个结果
search_tool = TavilySearchResults(max_results=1)
# 设置智普 AI 的 API 密钥
os.environ["ZHIPUAI_API_KEY"] = "5eb2---xlr"
# 初始化智普 AI 模型
llm = ChatZhipuAI()
# 带有搜索工具的LLM
llm_with_tool = llm.bind_tools([search_tool])rest = llm_with_tool.invoke("2025年3月15日,成都市天气怎么样?")
print(rest)
print('-------------')
print(f'content:{rest.content}')
print('-------------')
print(f'tool_calls:{rest.tool_calls}')print('================')
rest = llm_with_tool.invoke("中国首都是哪个?")
print(rest)
print('-------------')
print(f'content:{rest.content}')
print('-------------')
print(f'tool_calls:{rest.tool_calls}')

结果:


E:\learn_work_spaces\PythonProject1\.venv\Scripts\python.exe E:\learn_work_spaces\PythonProject1\agent代理\代理案例1.py 
content='' additional_kwargs={'tool_calls': [{'function': {'arguments': '{"query":"2025年3月15日成都市天气"}', 'name': 'tavily_search_results_json'}, 'id': 'call_-8891216066900221760', 'index': 0, 'type': 'function'}]} response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 149, 'total_tokens': 175}, 'model_name': 'glm-4', 'finish_reason': 'tool_calls'} id='run-3494341c-9a44-48b8-8825-ed8ba86254ea-0' tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891216066900221760', 'type': 'tool_call'}]
-------------
content:
-------------
tool_calls:[{'name': 'tavily_search_results_json', 'args': {'query': '2025年3月15日成都市天气'}, 'id': 'call_-8891216066900221760', 'type': 'tool_call'}]
================
content='中国首都是北京。' additional_kwargs={} response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 142, 'total_tokens': 149}, 'model_name': 'glm-4', 'finish_reason': 'stop'} id='run-4881ec52-40cb-4dbc-8fba-f12eca9b9ee8-0'
-------------
content:中国首都是北京。
-------------
tool_calls:[]进程已结束,退出代码为 0

content为空说明回复时使用了工具帮助回复
tool_calls为空说明回复来源于AI

案例2


http://www.ppmy.cn/embedded/173125.html

相关文章

MySQL进阶——数据备份与恢复(超详细!!!)

本文将详细介绍 MySQL 数据备份与恢复的相关知识点。 本文目录 1. 备份类型2. 备份策略设计3. 恢复方法4. 备份工具对比5. 逻辑备份6. 恢复 1. 备份类型 逻辑备份:导出数据库的逻辑结构,以SQL语句的形式呈现,可读性强。适用于小数据量或跨版…

【论文阅读方法】沐神课程:如何读论文

一篇论文的一般结构 titleabstractintroductionmethodexperienceconclusion 三明治论文阅读法 第一遍:海选 titleabstractconclusion——确定要不要读第二遍:精读 对整个文章过一遍,知道每一块在做什么 可以从标题开始读到最后,注…

RabbitMQ (Java)学习笔记

目录 一、概述 ①核心组件 ②工作原理 ③优势 ④应用场景 二、入门 1、docker 安装 MQ 2、Spring AMQP 3、代码实现 pom 依赖 配置RabbitMQ服务端信息 发送消息 接收消息 三、基础 work Queue 案例 消费者消息推送限制(解决消息堆积方案之一&#…

iOS底层原理系列04-并发编程

在移动应用开发中,流畅的用户体验至关重要,而并发编程是实现这一目标的关键技术。本文将深入探讨iOS平台上的并发编程和多线程架构,帮助你构建高性能、响应迅速的应用程序。 1. iOS线程调度机制 1.1 线程本质和iOS线程调度机制 线程是操作…

【Java】——数据类型和变量

个人主页:User_芊芊君子 🎉欢迎大家点赞👍评论📝收藏⭐文章 文章目录: 1.Java中的注释1.1.基本规则1.2.注释规范 2.标识符3.关键字4.字面常量5.数据类型6.变量6.1变量的概念6.2语法6.3整型变量6.3.1整型变量6.3.2长整…

Java构造方法详解:从入门到实战

目录 一、什么是构造方法? 二、构造方法的作用 三、构造方法分类与使用 1. 默认构造方法 2. 有参构造方法 3. 构造方法重载 四、注意事项(避坑指南) 五、经典面试题解析 六、实战应用场景 七、总结 一、什么是构造方法? …

2025-03-15 学习记录--C/C++-PTA 练习3-4 统计字符

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。💪🏻 一、题目描述 ⭐️ 练习3-4 统计字符 本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、…

软考中级设计模式实战宝典:核心意图解析+适用场景+完整源码下载+高频题库

简介: 备战软考中级软件设计师,设计模式模块占分高、考点深?本文专为高效备考打造! 直击考点:逐条解析23种设计模式的核心意图与适用场景,搭配UML类图快速理解模式本质,告别抽象理论。代码实战…