{"id":28681,"date":"2025-03-14T23:33:37","date_gmt":"2025-03-14T15:33:37","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28681"},"modified":"2025-03-14T23:35:43","modified_gmt":"2025-03-14T15:35:43","slug":"shiyong-llamaindex-an","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/pt\/shiyong-llamaindex-an\/","title":{"rendered":"\u4f7f\u7528 Ollama+LlamaIndex \u5b9e\u73b0\u672c\u5730 Agent"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>\u672c\u6587\u6863\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 LlamaIndex \u4e2d\u7684\u00a0<code>ReActAgent<\/code>\u00a0\u7ed3\u5408 <a href=\"https:\/\/www.kdjingpai.com\/ollama\/\">Ollama<\/a> \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u672c\u5730 Agent\u3002<\/p>\n<p>\u672c\u6587\u6863\u4e2d\u4f7f\u7528\u7684 LLM \u4e3a qwen2:0.5b \u6a21\u578b\uff0c\u7531\u4e8e\u4e0d\u540c\u6a21\u578b\u8c03\u7528 tools \u7684\u80fd\u529b\u4e0d\u540c\uff0c\u53ef\u4ee5\u81ea\u884c\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u578b\u5b9e\u73b0 Agent\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\/LlamaIndex_Agent\/%E4%BD%BF%E7%94%A8LlamaIndex%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 llama_index.core.tools import FunctionTool\r\nfrom llama_index.core.agent import ReActAgent\r\nfrom llama_index.llms.ollama import Ollama\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>2. \u521d\u59cb\u5316 Agent \u5de5\u5177<\/h2>\n<pre><code># Define tools\r\ndef multiply(a: float, b: float) -&gt; float:\r\n\"\"\"Multiply two integers and return the result integer\"\"\"\r\nreturn a * b\r\n# Create FunctionTool instances\r\nmultiply_tool = FunctionTool.from_defaults(\r\nfn=multiply,\r\nname=\"MultiplyTool\",\r\ndescription=\"A tool that multiplies two floats.\",\r\nreturn_direct=True\r\n)\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>3. \u521d\u59cb\u5316 LLM \u548c Agent<\/h2>\n<pre><code># Initialize LLM\r\nllm = Ollama(model=\"qwen2:0.5b\", request_timeout=360.0)\r\n# Initialize <a href=\"https:\/\/www.kdjingpai.com\/react\/\">ReAct<\/a> agent with tools\r\nagent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>4. \u8fdb\u884c\u5bf9\u8bdd<\/h2>\n<ul>\n<li>\u76f4\u63a5\u4f7f\u7528 LLM \u5bf9\u8bdd\u3002<\/li>\n<\/ul>\n<pre><code># direct response\r\nres_llm = llm.complete(\"What is 2.3 \u00d7 4.8 ? Calculate step by step\")\r\nprint(res_llm)\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>To calculate \\( 2.3 \\times 4.8 \\), you can follow these steps:\r\n1. **Perform the multiplication:** When multiplying decimals, simply multiply the numerators (the top numbers) to get the numerator of the product.\r\n\\[\r\n2.3 \\times 4.8 = 9.44\r\n\\]\r\n2. **Multiply the denominators (bottom numbers)**\r\nThe denominator of \\(4.8\\) is not affected by the multiplication because it does not contain a factor that can affect its value or determine the result.\r\n3. **Calculate the product**  \r\nSince there are no common factors between the numerator and the denominator, the calculation is:\r\n\\[\r\n9.44 = 2.3 \\times 2.3\r\n\\]\r\nThis multiplication does not give you a new number because \\(2.3\\) and \\(2.3\\) are already multiplied to get 5.6.\r\nSo, \\(2.3 \\times 4.8 = 9.44\\).\r\n<\/code><\/pre>\n<ul>\n<li>LLM \u8c03\u7528 Agent \u5bf9\u8bdd\u3002<\/li>\n<\/ul>\n<pre><code># use agent\r\nresponse = agent.chat(\"What is 2.3 \u00d7 4.8 ? Calculate step by step\")\r\nresponse.response\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>&gt; Running step 9227846e-d630-4ce2-a760-c8e90366dc6c. Step input: What is 2.3 \u00d7 4.8 ? Calculate step by step\r\nThought: The task is asking to multiply two numbers, 2.3 and 4.8, then to calculate this multiplication step by step.\r\nAction: MultiplyTool\r\nAction Input: {'a': 2.3, 'b': 4.8}\r\nObservation: 11.04\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/agent\/react_agent\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u672c\u6587\u6863\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 LlamaIndex \u4e2d\u7684\u00a0ReActAgent\u00a0\u7ed3\u5408 Ollama \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u672c\u5730 Agent\u3002 \u672c\u6587\u6863\u4e2d\u4f7f\u7528\u7684 LLM \u4e3a qwen2:0.5b \u6a21\u578b\uff0c\u7531\u4e8e\u4e0d\u540c\u6a21\u578b\u8c03\u7528 tools \u7684\u80fd\u529b\u4e0d\u540c\uff0c\u53ef\u4ee5\u81ea\u884c\u5c1d&#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-28681","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/28681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/comments?post=28681"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/28681\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/media?parent=28681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/categories?post=28681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/tags?post=28681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}