{"id":28630,"date":"2025-03-14T00:35:59","date_gmt":"2025-03-13T16:35:59","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28630"},"modified":"2025-03-14T00:35:59","modified_gmt":"2025-03-13T16:35:59","slug":"shiyong-ollamalangch-2","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/shiyong-ollamalangch-2\/","title":{"rendered":"\u4f7f\u7528 Ollama+LangChain \u5b9e\u73b0\u672c\u5730 Agent"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>ReAct\uff08Reasoning and Acting\uff09\u662f\u4e00\u79cd\u5c06\u63a8\u7406\u4e0e\u884c\u52a8\u76f8\u7ed3\u5408\u7684\u6846\u67b6\uff0c\u7528\u4e8e\u589e\u5f3a\u667a\u80fd\u4f53\u5728\u590d\u6742\u4efb\u52a1\u4e2d\u7684\u8868\u73b0\u3002\u8be5\u6846\u67b6\u901a\u8fc7\u5c06\u903b\u8f91\u63a8\u7406\u4e0e\u5b9e\u9645\u884c\u52a8\u7d27\u5bc6\u7ed3\u5408\uff0c\u4f7f\u667a\u80fd\u4f53\u80fd\u591f\u5728\u52a8\u6001\u73af\u5883\u4e2d\u66f4\u6709\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28631\" title=\"\u4f7f\u7528 Ollama+LangChain \u5b9e\u73b0\u672c\u5730 Agent-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04.png\" alt=\"\u4f7f\u7528 Ollama+LangChain \u5b9e\u73b0\u672c\u5730 Agent-1\" width=\"3070\" height=\"828\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04.png 3070w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04-768x207.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04-1536x414.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04-2048x552.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aee3e39b5cc6a04-18x5.png 18w\" sizes=\"auto, (max-width: 3070px) 100vw, 3070px\" \/><\/p>\n<p>\u6765\u6e90\uff1a<a href=\"https:\/\/www.kdjingpai.com\/react\/\">ReAct: \u5927\u8bed\u8a00\u6a21\u578b\u4e2d\u63a8\u7406\u548c\u884c\u52a8\u534f\u540c\u5de5\u4f5c<\/a><\/p>\n<p>\u672c\u6587\u6863\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 <a href=\"https:\/\/www.kdjingpai.com\/ja\/react\/\">ReAct<\/a> \u6846\u67b6\u5728 <a href=\"https:\/\/www.kdjingpai.com\/ja\/ollama\/\">Ollama<\/a> \u4e2d\u5b9e\u73b0\u672c\u5730\u4ee3\u7406\uff08Agent\uff09\u3002\u901a\u8fc7\u7ed3\u5408 Ollama \u7684\u529f\u80fd\u4e0e ReAct \u7684\u7075\u6d3b\u6027\uff0c\u7528\u6237\u80fd\u591f\u5728\u672c\u5730\u73af\u5883\u4e2d\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u4ea4\u4e92\u5f0f\u4ee3\u7406\u3002\u6b64\u5b9e\u73b0\u80fd\u591f\u5904\u7406\u590d\u6742\u4efb\u52a1\uff0c\u652f\u6301\u591a\u79cd\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5e76\u4e14\u4f18\u5316\u4e86\u4efb\u52a1\u81ea\u52a8\u5316\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u9002\u5408\u9700\u8981\u9ad8\u5b9e\u65f6\u6027\u7684\u672c\u5730\u5e94\u7528\u573a\u666f\u3002<\/p>\n<blockquote><p>\u6ce8: \u672c\u6587\u6863\u5305\u542b\u6838\u5fc3\u4ee3\u7801\u7247\u6bb5\u548c\u8be6\u7ec6\u89e3\u91ca\u3002\u5b8c\u6574\u4ee3\u7801\u53ef\u89c1\u00a0<a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/notebook\/C7\/LangChain_Agent\/%E4%BD%BF%E7%94%A8LangChain%E5%AE%9E%E7%8E%B0%E6%9C%AC%E5%9C%B0Agent.ipynb\">notebook<\/a>\u00a0\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>1. \u5bfc\u5165\u4f9d\u8d56<\/h2>\n<pre><code>from langchain_core.tools import tool  \r\nfrom langchain.pydantic_v1 import BaseModel, Field\r\nfrom langchain_core.tools import render_text_description\r\nfrom langchain.agents import AgentExecutor, create_react_agent\r\nfrom langchain import hub\r\nfrom langchain_community.chat_models import <a href=\"https:\/\/www.kdjingpai.com\/ja\/chatollamajiyun\/\">ChatOllama<\/a>\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>2. \u521d\u59cb\u5316 Agent \u5de5\u5177<\/h2>\n<p>\u5b9a\u4e49\u00a0<code>SearchInput<\/code>\u00a0\u7c7b\u7ee7\u627f\u00a0<code>BaseModel<\/code>\uff0c\u7528\u4e8e\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u7684\u6a21\u5f0f\u3002<\/p>\n<p><code>@tool(args_schema=SearchInput)<\/code>\u00a0\u4f7f\u7528\u5de5\u5177\u88c5\u9970\u5668\u88c5\u9970\u00a0<code>weather_forecast<\/code>\u00a0\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u5176\u8f93\u5165\u6a21\u5f0f\u4e3a\u00a0<code>SearchInput<\/code>\u3002<\/p>\n<pre><code>class SearchInput(BaseModel):\r\nlocation: str = Field(description=\"location to search for\")  # \u5b9a\u4e49\u4e00\u4e2a Pydantic \u6a21\u578b\uff0c\u7528\u4e8e\u63cf\u8ff0\u8f93\u5165\u6a21\u5f0f\uff0c\u5e76\u63d0\u4f9b\u63cf\u8ff0\u4fe1\u606f\r\n@tool(args_schema=SearchInput)\r\ndef weather_forecast(location: str):\r\n\"\"\"\u5929\u6c14\u9884\u62a5\u5de5\u5177\u3002\"\"\"\r\nprint(f\"Weather for {location}\")  # \u6253\u5370\u8981\u9884\u62a5\u5929\u6c14\u7684\u4f4d\u7f6e\r\nreturn f\"A dummy forecast for {location}\"  # \u8fd4\u56de\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u865a\u62df\u5929\u6c14\u9884\u62a5\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>3. \u672c\u5730\u8fd0\u884c<\/h2>\n<p>\u5728\u672c\u4f8b\u4e2d\u4f7f\u7528\u00a0<code>gemma:2b<\/code>\u00a0\u6a21\u578b\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u6a21\u578b\uff0c\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u5f88\u4e0d\u4e00\u6837\uff08\u968f\u673a\u6027\u6bd4\u8f83\u5927\uff09\u3002<\/p>\n<pre><code>llm = ChatOllama(model=\"gemma:2b\")  # \u521d\u59cb\u5316 ChatOllama \u6a21\u578b\uff0c\u4f7f\u7528 \"gemma:2b\"\r\ntools = [weather_forecast]  # \u4f7f\u7528 weather_forecast \u5de5\u5177\r\nprompt = hub.pull(\"hwchase17\/react-json\")  # \u4ece hub \u62c9\u53d6\u7279\u5b9a\u63d0\u793a\r\nprompt = prompt.partial(\r\ntools=render_text_description(tools),  # \u4e3a\u63d0\u793a\u5448\u73b0\u5de5\u5177\u7684\u6587\u672c\u63cf\u8ff0\r\ntool_names=\", \".join([t.name for t in tools]),  # \u5c06\u5de5\u5177\u540d\u79f0\u8fde\u63a5\u6210\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\r\n)\r\nagent = create_react_agent(llm, tools, prompt)  # \u4f7f\u7528 llm\u3001\u5de5\u5177\u548c\u81ea\u5b9a\u4e49\u63d0\u793a\u521b\u5efa\u4ee3\u7406\r\nagent_executor = AgentExecutor(agent=agent, tools=tools, handle_parsing_errors=True, verbose=False, format=\"json\")  # \u4f7f\u7528\u6307\u5b9a\u53c2\u6570\u521d\u59cb\u5316 AgentExecutor\r\nprint(agent_executor.invoke({\"input\":\"What is the weather in Paris?\"}))  # \u4f7f\u7528\u6d4b\u8bd5\u8f93\u5165\u8c03\u7528\u4ee3\u7406\u5e76\u6253\u5370\u7ed3\u679c\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5bf9\u8bdd\u5386\u53f2<\/h2>\n<p>\u5728\u4f7f\u7528\u5bf9\u8bdd\u5386\u53f2\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u00a0<code>react-chat<\/code>\u00a0Prompt \u6a21\u677f\u3002\u5728 invoke \u65f6\uff0c\u52a0\u5165\u00a0<code>chat_history<\/code>\u3002<\/p>\n<pre><code># \u62c9\u53bb\u7279\u5b9a\u63d0\u793a\uff0c\u6ce8\u610f\u6b64\u5904\u4f7f\u7528\u7684\u662f react-chat\r\nprompt = hub.pull(\"hwchase17\/react-chat\")\r\n# \u6784\u5efa ReAct agent\r\nagent_history = create_react_agent(llm, tools, prompt)\r\nagent_executor = AgentExecutor(agent=agent_history, tools=tools, verbose=False)\r\nagent_executor.invoke(\r\n{\r\n\"input\": \"what's my name? Only use a tool if needed, otherwise respond with Final Answer\",\r\n\"chat_history\": \"Human: Hi! My name is Bob\\nAI: Hello Bob! Nice to meet you\",\r\n}\r\n)\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u53c2\u8003\u8d44\u6599<\/strong><\/p>\n<p>https:\/\/react-lm.github.io\/<\/p>\n<p>https:\/\/python.langchain.com\/v0.1\/docs\/modules\/agents\/agent_types\/react\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb ReAct\uff08Reasoning and Acting\uff09\u662f\u4e00\u79cd\u5c06\u63a8\u7406\u4e0e\u884c\u52a8\u76f8\u7ed3\u5408\u7684\u6846\u67b6\uff0c\u7528\u4e8e\u589e\u5f3a\u667a\u80fd\u4f53\u5728\u590d\u6742\u4efb\u52a1\u4e2d\u7684\u8868\u73b0\u3002\u8be5\u6846\u67b6\u901a\u8fc7\u5c06\u903b\u8f91\u63a8\u7406\u4e0e\u5b9e\u9645\u884c\u52a8\u7d27\u5bc6\u7ed3\u5408\uff0c\u4f7f\u667a\u80fd\u4f53\u80fd\u591f\u5728\u52a8\u6001\u73af\u5883\u4e2d\u66f4\u6709\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002 \u6765\u6e90\uff1aReAct: \u5927\u8bed\u8a00\u6a21\u578b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182],"tags":[],"class_list":["post-28630","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=28630"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28630\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=28630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=28630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=28630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}