{"id":5814,"date":"2026-04-15T11:20:16","date_gmt":"2026-04-15T03:20:16","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5814"},"modified":"2026-04-16T16:43:25","modified_gmt":"2026-04-16T08:43:25","slug":"15-llamaindex-%e6%96%87%e6%a1%a3%e8%a7%a3%e6%9e%90%e8%81%8a%e5%a4%a9agent%e9%9b%86%e6%88%90a2a","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5814","title":{"rendered":"15.LlamaIndex-\u6587\u6863\u89e3\u6790\u804a\u5929Agent\u96c6\u6210A2A"},"content":{"rendered":"<p>LlamaIndex\u96c6\u6210A2A\u52a9\u624b<\/p>\n<p>\u6211\u4eec\u901a\u8fc7LlamaIndex\u6765\u6784\u5efa\u6587\u6863\u89e3\u6790\u4ee5\u53ca\u804a\u5929Agent<\/p>\n<p>\u548cLangChain \u4e00\u6837\uff0cLlamaIndex\u4e5f\u662f\u4e00\u5f00\u59cb\u5c31\u6d41\u884c\u7684\u5927\u6a21\u578b\u5f00\u53d1\u6846\u67b6\u4e4b\u4e00\u3002\u5176\u66f4\u52a0\u4e13\u6ce8\u4e8e\u8fdb\u884c\u6587\u6863\u68c0\u7d22\u548c\u641c\u7d22\u76f8\u5173\u7684\u5927\u6a21\u578b\u5e94\u7528\u6784\u5efa\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u9996\u5148\u6784\u5efa\u4e00\u4e2aLlamaIndex\u7684\u4f7f\u7528\u5c0fDemo\u6765\u770b\u4e0b<\/p>\n<p>\u9996\u5148\u662f\u6839\u636e\u672c\u5730\u6587\u4ef6\u5939\uff0c\u8bfb\u53d6\u5e76\u5229\u7528LlamaIndex\u4e2d\u7684VectorStoreIndex\u6765\u8fdb\u884c\u7d22\u5f15\u751f\u6210\u3002<\/p>\n<table>\n<tr>\n<td>\n  from llama_index.core import SimpleDirectoryReader<\/p>\n<p>documents = SimpleDirectoryReader(&#8220;..\/data&#8221;).load_data()<\/p>\n<p>len(documents)<\/p>\n<p>from llama_index.core import VectorStoreIndex<\/p>\n<p>index = VectorStoreIndex.from_documents(documents)<\/p>\n<p>vars(index)<\/td>\n<\/tr>\n<\/table>\n<p>\u5728\u8fd9\u4e2aindex\u4e4b\u4e2d\uff0c\u4e3b\u8981\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff0c\u5206\u522b\u4e3a\u6587\u6863\u5b58\u50a8SimpleDocumentStore\u3002 \u7d22\u5f15\u5b58\u50a8SimpleIndexStore\u3002 \u5411\u91cf\u5b58\u50a8SimpleVectorStore\u3002<\/p>\n<p>\u5176\u4e2d\u6587\u6863\u5b58\u50a8\u4e2d\u4fdd\u5b58\u4e86\u8bfb\u53d6\u7684\u6587\u6863\uff0c\u4ee5Node\u4e3a\u5bf9\u8c61\u8868\u793a\uff0c\u8282\u70b9\u662f\u5bf9\u539f\u59cb\u6587\u6863\u8fdb\u884c\u5206\u5757\u5904\u7406\u7684\u7ed3\u679c\uff0c\u5305\u542b\u6587\u672c\u5185\u5bb9\u548c\u76f8\u5173\u5143\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<p>\u7d22\u5f15\u5b58\u50a8\uff0c\u5219\u662f\u5305\u542b\u8f7b\u91cf\u7ea7\u7684\u7d22\u5f15\u5143\u6570\u636e\uff0c\u6784\u5efa\u7d22\u5f15\u65f6\u5019\u8fdb\u884c\u521b\u5efa\u7684\u9644\u52a0\u72b6\u6001\u4fe1\u606f\u3002\u5b58\u50a8\u7d22\u5f15\u7ed3\u6784\u548c\u76f8\u5173\u5143\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<p>\u5411\u91cf\u5b58\u50a8\uff0c\u4e00\u79cd\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u7cfb\u7edf\uff0c\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u4fdd\u5b58\u5d4c\u5165\uff0c\u5c06\u8282\u70b9ID\u6620\u5c04\u5230\u76f8\u5173\u7684\u5d4c\u5165\u4e2d\u3002\u8fdb\u884c\u9ad8\u6548\u7684\u68c0\u7d22\u548c\u641c\u7d22\u3002<\/p>\n<p>\u5728\u6574\u4e2a\u7d22\u5f15\u4e2d\uff0cllama_index\u4f1a\u81ea\u52a8\u5c06\u8bfb\u5165\u7684\u6587\u6863\u5207\u5206\u4e3a\u4e00\u4e2a\u4e2a\u7684\u8282\u70b9\uff0c\u7136\u540e\u8fdb\u884c\u8282\u70b9\u5173\u8054\u3002<\/p>\n<p>\u4e4b\u540e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7storage_context\u8fdb\u884c\u7d22\u5f15\u7684\u4fdd\u5b58\uff0c\u5e76\u67e5\u770b\u7d22\u5f15\u6587\u4ef6\u7684\u673a\u6784\u3002<\/p>\n<p>index.storage_context.persist(persist_dir=&#8221;saved_index&#8221;) # \u4fdd\u5b58\u7d22\u5f15<\/p>\n<p>\u5728\u4fdd\u5b58\u7684\u76ee\u5f55\u4e0b\uff0c\u5305\u542b\u591a\u4e2aJSON\u6587\u4ef6\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u76f8\u5173\u6570\u636e<\/p>\n<p>default_vector_store.json \u4fdd\u5b58\u9ed8\u8ba4\u7684\u5411\u91cf\u5b58\u50a8\u914d\u7f6e\u6216\u6570\u636e\u3002\u8bf8\u5982\u7528\u6237\u7684\u4e0a\u4f20\u6587\u672c\u7684\u5411\u91cf\u5316\u8868\u793a\u3002<\/p>\n<p>docstore.json \u4fdd\u5b58\u6587\u6863\u5b58\u50a8\u7684\u5143\u6570\u636e\u6216\u8005\u5b9e\u9645\u5185\u5bb9\uff0c\u4f8b\u5982\u6587\u6863\u7684\u539f\u59cb\u5185\u5bb9<\/p>\n<p>graph_store.json \u4fdd\u5b58\u77e5\u8bc6\u56fe\u8c31\u6216\u8005\u5173\u7cfb\u56fe\u7684\u6570\u636e\uff0c\u8bb0\u5f55\u6587\u6863\u548c\u5411\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5f80\u5f80\u5728\u57fa\u4e8e\u8282\u70b9\u63a8\u7406\u7684\u65f6\u5019\u7528\u5230\u3002<\/p>\n<p>image_vector_store.json\uff1a\u4fdd\u5b58\u4e0e\u56fe\u50cf\u76f8\u5173\u7684\u5411\u91cf\u6570\u636e\u3002\u5982\u679c\u7cfb\u7edf\u652f\u6301\u591a\u6a21\u6001\u68c0\u7d22\uff08\u5982\u6587\u672c\u548c\u56fe\u50cf\u6df7\u5408\u68c0\u7d22\uff09\uff0c\u8fd9\u4e2a\u6587\u4ef6\u53ef\u80fd\u4fdd\u5b58\u4e86\u56fe\u50cf\u5d4c\u5165\uff08\u5411\u91cf\u5316\u8868\u793a\uff09\u7684\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e2a\u6587\u4ef6\uff0c\u7cfb\u7edf\u53ef\u4ee5\u8fdb\u884c\u57fa\u4e8e\u56fe\u50cf\u5185\u5bb9\u7684\u76f8\u4f3c\u6027\u641c\u7d22\u3002<\/p>\n<p>index_store.json \u4fdd\u5b58\u7d22\u5f15\u672c\u8eab\u7684\u7ed3\u6784\u6216\u8005\u4e91\u4fe1\u606f\uff0c\u5b58\u50a8\u6240\u6709\u5df2\u7ecf\u521b\u5efa\u7d22\u5f15\u7684\u5143\u6570\u636e\u3002\u6bd4\u5982\u6bcf\u4e2a\u7d22\u5f15\u7684\u5bf9\u5e94\u6587\u6863\u8303\u56f4\uff0c\u5d4c\u5165\u6a21\u578b\uff0c\u53c2\u6570\u914d\u7f6e\u7b49\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u9488\u5bf9docstore.json\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u4fe1\u606f\u8fdb\u884c\u5c55\u5f00\uff0c\u67e5\u770b\u5176\u4e2d\u5b58\u50a8\u7684\u5177\u4f53\u5185\u5bb9\uff0c\u5982\u56fe\u6240\u793a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"832\" height=\"1184\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-24.png\" class=\"wp-image-5815\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-24.png 832w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-24-211x300.png 211w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-24-720x1024.png 720w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-24-768x1093.png 768w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/p>\n<p>\u5176\u4e2d\u4e00\u65b9\u9762\u5b58\u50a8\u4e86\u6587\u672c\u5185\u5bb9\uff0c\u53e6\u4e00\u65b9\u9762\u5b58\u50a8\u4e86\u8bf8\u5982relationships\u8fd9\u6837\u7684\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<p>default_vector_store.json\u4e2d\u8fd8\u5b58\u50a8\u4e86\u4e00\u4e2a\u4e2a\u7684\u6587\u672c\u5757\u5d4c\u5165\u5411\u91cf\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1432\" height=\"1059\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-25.png\" class=\"wp-image-5816\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-25.png 1432w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-25-300x222.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-25-1024x757.png 1024w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/word-image-25-768x568.png 768w\" sizes=\"(max-width: 1432px) 100vw, 1432px\" \/><\/p>\n<p>\u6bcf\u4e00\u4e2a\u5747\u4e3a1536\u7ef4\u7684OpenAI\u7684\u5d4c\u5165\u5411\u91cf\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u56de\u6765\u5728\u7528\u6237\u8f93\u5165\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u8bb2\u6587\u672c\u8f93\u5165\u62c6\u5206\u4e3atoken\uff0c\u7136\u540e\u5c06token\u7edf\u4e00\u8d77\u6765\u8ba1\u7b97\u4e3a\u4e00\u4e2a\u5411\u91cf\u3002<\/p>\n<p>\u90a3\u4e48\u5728LlamaIndex\u6784\u5efa\u5b8c\u6210\u6587\u6863\u5e93\u5411\u91cf\u7d22\u5f15\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5f15\u5165LlamaIndex\u7684Agent\u548cWorkflow\u3002\u6700\u540e\u5b9e\u73b0\u6587\u6863\u52a0\u8f7d\uff0c\u591a\u8f6e\u95ee\u7b54\u8fd9\u6837\u7684\u804a\u5929Agent\u3002<\/p>\n<p>\u5982\u679c\u662f\u4f20\u7edfLlamaIndex\uff0c\u4e00\u822c\u662f\u4e00\u95ee\u4e00\u7b54\uff0c\u800c\u5982\u679c\u901a\u8fc7llama workflow \u63d0\u4f9b\u7684workflow\u673a\u5236\uff0c\u5219\u53ef\u4ee5\u5c06\u591a\u4e2a\u6b65\u9aa4\u5c01\u88c5\u4e3a\u4e8b\u4ef6\u9a71\u52a8\u7684\u72b6\u6001\u6d41\u7a0b\u3002\u5e76\u8fdb\u4e00\u6b65\u5f62\u6210\u4e00\u4e2aAgent\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u770b\u4e0b\u8fd9\u4e2aAgent\u76f8\u5173\u7684\u4ee3\u7801<\/p>\n<p>\u9996\u5148\u662froute\u51fd\u6570<\/p>\n<table>\n<tr>\n<td>\n  @step<\/p>\n<p>def route(self, ev: InputEvent) -&gt; LoadDocumentEvent | ChatEvent:<\/p>\n<p>&#8220;&#8221;&#8221;\u8def\u7531\u6b65\u9aa4\uff1a\u5224\u65ad\u662f\u52a0\u8f7d\u6587\u6863\u8fd8\u662f\u804a\u5929&#8221;&#8221;&#8221;<\/p>\n<p>if ev.file_path or ev.base64_content:<\/p>\n<p>return LoadDocumentEvent(<\/p>\n<p>file_path=ev.file_path,<\/p>\n<p>file_name=ev.file_name,<\/p>\n<p>base64_content=ev.base64_content,<\/p>\n<p>msg=ev.msg<\/p>\n<p>)<\/p>\n<p>return ChatEvent(msg=ev.msg)<\/td>\n<\/tr>\n<\/table>\n<p>\u4e0a\u9762\u7684step\u88c5\u9970\u5668\uff0c\u6807\u5fd7\u7740\u5de5\u4f5c\u6d41\u7684\u4e00\u4e2a\u6267\u884c\u6b65\u9aa4\uff0croute\u6839\u636e\u662f\u4e0d\u662f\u6587\u4ef6\uff0c\u6765\u51b3\u5b9a\u6267\u884c\u804a\u5929\u8fd8\u662f\u4e0a\u4f20\u6587\u4ef6\u3002<\/p>\n<p>\u5176\u6b21\u662f\u52a0\u8f7d\u6587\u6863\u5bf9\u8c61<\/p>\n<table>\n<tr>\n<td>\n  @step<\/p>\n<p>async def load_document(self, ctx: Context, ev: LoadDocumentEvent) -&gt; ChatEvent:<\/p>\n<p>&#8220;&#8221;&#8221;\u6587\u6863\u52a0\u8f7d\u6b65\u9aa4&#8221;&#8221;&#8221;<\/p>\n<p>try:<\/p>\n<p>if ev.file_path:<\/p>\n<p>ctx.write_event_to_stream(LogEvent(msg=f&#8221;\u6b63\u5728\u52a0\u8f7d\u6587\u6863: {ev.file_path}&#8221;))<\/p>\n<p># \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(ev.file_path, &#8216;r&#8217;, encoding=&#8217;utf-8&#8242;) as f:<\/p>\n<p>content = f.read()<\/p>\n<p>file_name = ev.file_name or ev.file_path<\/p>\n<p>elif ev.base64_content:<\/p>\n<p>ctx.write_event_to_stream(LogEvent(msg=f&#8221;\u6b63\u5728\u89e3\u6790base64\u6587\u6863: {ev.file_name}&#8221;))<\/p>\n<p># \u89e3\u7801base64\u5185\u5bb9<\/p>\n<p>content = base64.b64decode(ev.base64_content).decode(&#8216;utf-8&#8217;)<\/p>\n<p>file_name = ev.file_name<\/p>\n<p>else:<\/p>\n<p>raise ValueError(&#8220;\u5fc5\u987b\u63d0\u4f9b\u6587\u4ef6\u8def\u5f84\u6216base64\u5185\u5bb9&#8221;)<\/p>\n<p># \u521b\u5efa\u6587\u6863\u5bf9\u8c61<\/p>\n<p>doc = Document(<\/p>\n<p>text=content,<\/p>\n<p>metadata={&#8220;file_name&#8221;: file_name}<\/p>\n<p>)<\/p>\n<p># \u6dfb\u52a0\u5230\u6587\u6863\u5217\u8868<\/p>\n<p>self.documents.append(doc)<\/p>\n<p># \u91cd\u65b0\u6784\u5efa\u7d22\u5f15<\/p>\n<p>await self._build_index(ctx)<\/p>\n<p>ctx.write_event_to_stream(LogEvent(msg=f&#8221;\u2705 \u6587\u6863\u52a0\u8f7d\u6210\u529f: {file_name}&#8221;))<\/p>\n<p>except Exception as e:<\/p>\n<p>ctx.write_event_to_stream(LogEvent(msg=f&#8221;\u274c \u6587\u6863\u52a0\u8f7d\u5931\u8d25: {str(e)}&#8221;))<\/p>\n<p>return ChatEvent(msg=ev.msg)<\/td>\n<\/tr>\n<\/table>\n<table>\n<tr>\n<td>\n  @step<\/p>\n<p>async def chat(self, ctx: Context, ev: ChatEvent) -&gt; ChatResponseEvent:<\/p>\n<p>&#8220;&#8221;&#8221;\u804a\u5929\u6b65\u9aa4&#8221;&#8221;&#8221;<\/p>\n<p>try:<\/p>\n<p># \u6dfb\u52a0\u7528\u6237\u6d88\u606f\u5230\u5386\u53f2<\/p>\n<p>self.chat_history.append({&#8220;role&#8221;: &#8220;user&#8221;, &#8220;content&#8221;: ev.msg})<\/p>\n<p>if not self.index:<\/p>\n<p>response_content = &#8220;\u62b1\u6b49\uff0c\u8fd8\u6ca1\u6709\u52a0\u8f7d\u4efb\u4f55\u6587\u6863\u3002\u8bf7\u5148\u52a0\u8f7d\u4e00\u4e2a\u6587\u6863\u3002&#8221;<\/p>\n<p>self.chat_history.append({&#8220;role&#8221;: &#8220;assistant&#8221;, &#8220;content&#8221;: response_content})<\/p>\n<p>return ChatResponseEvent(<\/p>\n<p>response=response_content,<\/p>\n<p>citations={},<\/p>\n<p>has_documents=False<\/p>\n<p>)<\/p>\n<p># \u521b\u5efa\u67e5\u8be2\u5f15\u64ce<\/p>\n<p>query_engine = self.index.as_query_engine(<\/p>\n<p>similarity_top_k=3,<\/p>\n<p>response_mode=&#8221;compact&#8221;<\/p>\n<p>)<\/p>\n<p># \u6267\u884c\u67e5\u8be2<\/p>\n<p>ctx.write_event_to_stream(LogEvent(msg=&#8221;\u6b63\u5728\u67e5\u8be2\u6587\u6863&#8230;&#8221;))<\/p>\n<p>response = query_engine.query(ev.msg)<\/p>\n<p># \u63d0\u53d6\u5f15\u7528\u4fe1\u606f<\/p>\n<p>citations = {}<\/p>\n<p>if hasattr(response, &#8216;source_nodes&#8217;) and response.source_nodes:<\/p>\n<p>for i, node in enumerate(response.source_nodes, 1):<\/p>\n<p>citations[i] = {<\/p>\n<p>&#8220;content&#8221;: node.text[:200] + &#8220;&#8230;&#8221; if len(node.text) &gt; 200 else node.text,<\/p>\n<p>&#8220;metadata&#8221;: node.metadata<\/p>\n<p>}<\/p>\n<p>response_content = str(response)<\/p>\n<p># \u6dfb\u52a0\u52a9\u624b\u56de\u590d\u5230\u5386\u53f2<\/p>\n<p>self.chat_history.append({&#8220;role&#8221;: &#8220;assistant&#8221;, &#8220;content&#8221;: response_content})<\/p>\n<p>return ChatResponseEvent(<\/p>\n<p>response=response_content,<\/p>\n<p>citations=citations,<\/p>\n<p>has_documents=True<\/p>\n<p>)<\/p>\n<p>except Exception as e:<\/p>\n<p>error_msg = f&#8221;\u67e5\u8be2\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef: {str(e)}&#8221;<\/p>\n<p>self.chat_history.append({&#8220;role&#8221;: &#8220;assistant&#8221;, &#8220;content&#8221;: error_msg})<\/p>\n<p>return ChatResponseEvent(<\/p>\n<p>response=error_msg,<\/p>\n<p>citations={},<\/p>\n<p>has_documents=bool(self.index)<\/p>\n<p>)<\/td>\n<\/tr>\n<\/table>\n<p>\u5728chat\u8fc7\u7a0b\u4e2d\uff0c\u8c03\u7528\u4e86as_query_engine() \u65b9\u6cd5\uff0c\u5c06\u7d22\u5f15\u8f6c\u578b\u4e3a\u4e00\u4e2a\u53ef\u7528\u4e8e\u95ee\u7b54\u7684\u67e5\u8be2\u5f15\u64ce\u3002<\/p>\n<p>\u57fa\u4e8e\u8fd9\u4e2aAgent\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u5f97\u5c06\u5176\u5d4c\u5165\u5230A2A\u67b6\u6784\u4e2d\uff0c\u6bcf\u6b21\u4e0a\u4f20\u6587\u4ef6\uff0c\u89e6\u53d1load_document\u6d41\u7a0b\u3002<\/p>\n<p>\u90a3\u4e48\u5728A2A\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u8fdb\u4e00\u6b65\u5c06\u5176\u5c01\u88c5\u4e3a\u53ef\u90e8\u7f72\u53ef\u8c03\u5ea6\u7684\u667a\u80fd\u4f53Agent\uff0c\u63a5\u5165A2A\u534f\u8bae\u67b6\u6784\uff0c\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301\u591a\u8f6e\u95ee\u7b54\uff0c\u5f15\u7528\u8ffd\u6eaf\uff0c\u6d41\u5f0f\u76f8\u5e94\u7684\u5de5\u4f5c\u6d41\u7a0b\u3002<\/p>\n<p>Agent\u67b6\u6784\u4ee5Workflow\u7c7b\u4e3a\u57fa\u7840\u8fdb\u884c\u6784\u5efa\uff0c\u4f7f\u7528LlamaIndex\u7684step\u88c5\u9970\u5668\u5c06\u5404\u4e2a\u5904\u7406\u9636\u6bb5\u6a21\u5757\u5316\u3002<\/p>\n<p>\u9996\u5148\u662finit\u76f8\u5173\u4ee3\u7801<\/p>\n<table>\n<tr>\n<td>\n  class ParseAndChat(Workflow):<\/p>\n<p>def __init__(<\/p>\n<p>self,<\/p>\n<p>timeout: float | None = None,<\/p>\n<p>verbose: bool = False,<\/p>\n<p>**workflow_kwargs: Any,<\/p>\n<p>):<\/p>\n<p>super().__init__(timeout=timeout, verbose=verbose, **workflow_kwargs)<\/p>\n<p>self._sllm = GoogleGenAI(<\/p>\n<p>model=&#8217;gemini-2.0-flash&#8217;, api_key=os.getenv(&#8216;GOOGLE_API_KEY&#8217;)<\/p>\n<p>).as_structured_llm(ChatResponse)<\/p>\n<p>self._parser = LlamaParse(api_key=os.getenv(&#8216;LLAMA_CLOUD_API_KEY&#8217;))<\/p>\n<p>self._system_prompt_template = &#8220;&#8221;&#8221;\\<\/p>\n<p>\u4f60\u662f\u4e00\u4e2a\u6709\u7528\u7684\u52a9\u624b\uff0c\u53ef\u4ee5\u56de\u7b54\u5173\u4e8e\u6587\u6863\u7684\u95ee\u9898\uff0c\u63d0\u4f9b\u5f15\u7528\uff0c\u5e76\u8fdb\u884c\u5bf9\u8bdd\u3002<\/p>\n<p>\u8fd9\u662f\u5e26\u6709\u884c\u53f7\u7684\u6587\u6863\uff1a<\/p>\n<p>&lt;document_text&gt;<\/p>\n<p>{document_text}<\/p>\n<p>&lt;\/document_text&gt;<\/p>\n<p>\u5f53\u5f15\u7528\u6587\u6863\u5185\u5bb9\u65f6\uff1a<\/p>\n<p>1. \u4f60\u7684\u5185\u8054\u5f15\u7528\u5e94\u8be5\u4ece[1]\u5f00\u59cb\uff0c\u6bcf\u4e2a\u989d\u5916\u7684\u5185\u8054\u5f15\u7528\u9012\u589e1<\/p>\n<p>2. \u6bcf\u4e2a\u5f15\u7528\u7f16\u53f7\u5e94\u8be5\u5bf9\u5e94\u6587\u6863\u4e2d\u7684\u7279\u5b9a\u884c<\/p>\n<p>3. \u5982\u679c\u4e00\u4e2a\u5185\u8054\u5f15\u7528\u8986\u76d6\u591a\u4e2a\u8fde\u7eed\u884c\uff0c\u8bf7\u5c3d\u91cf\u4f18\u5148\u4f7f\u7528\u5355\u4e2a\u5185\u8054\u5f15\u7528\u6765\u8986\u76d6\u6240\u9700\u7684\u884c\u53f7<\/p>\n<p>4. \u5982\u679c\u4e00\u4e2a\u5f15\u7528\u9700\u8981\u8986\u76d6\u591a\u4e2a\u4e0d\u8fde\u7eed\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528[2, 3, 4]\u8fd9\u6837\u7684\u5f15\u7528\u683c\u5f0f<\/p>\n<p>5. \u4f8b\u5982\uff0c\u5982\u679c\u54cd\u5e94\u5305\u542b&#8221;The transformer architecture&#8230; [1].&#8221;\u548c&#8221;Attention mechanisms&#8230; [2].&#8221;\uff0c\u8fd9\u4e9b\u5206\u522b\u6765\u81ea\u7b2c10-12\u884c\u548c\u7b2c45-46\u884c\uff0c\u90a3\u4e48\uff1acitations = [[10, 11, 12], [45, 46]]<\/p>\n<p>6. \u59cb\u7ec8\u4ece[1]\u5f00\u59cb\u4f60\u7684\u5f15\u7528\uff0c\u6bcf\u4e2a\u989d\u5916\u7684\u5185\u8054\u5f15\u7528\u9012\u589e1\u3002\u4e0d\u8981\u4f7f\u7528\u884c\u53f7\u4f5c\u4e3a\u5185\u8054\u5f15\u7528\u7f16\u53f7\uff0c\u5426\u5219\u6211\u4f1a\u5931\u53bb\u5de5\u4f5c\u3002<\/p>\n<p>&#8220;&#8221;&#8221;<\/td>\n<\/tr>\n<\/table>\n<p>\u5728\u6574\u4e2aAgent\u4e4b\u4e2d\uff0c\u5229\u7528Event\u6765\u8fdb\u884c\u4f20\u9012\u7684\uff0c\u8bf8\u5982<\/p>\n<p>InputEvent\uff1a\u7528\u6237\u8f93\u5165\/\u6587\u4ef6\u4e0a\u4f20<\/p>\n<p>ParseEvent\uff1a\u89e6\u53d1\u6587\u6863\u89e3\u6790<\/p>\n<p>ChatEvent\uff1a\u63d0\u4ea4\u7528\u6237\u67e5\u8be2<\/p>\n<p>ChatResponseEvent\uff1a\u5305\u542b\u5f15\u7528\u7684\u7ed3\u6784\u5316\u54cd\u5e94<\/p>\n<p>\u8fd9\u91cc\u5176\u4e2droute\u51fd\u6570\u5c31\u4f1a\u8fd4\u56de\u4e24\u79cdEvent<\/p>\n<table>\n<tr>\n<td>\n  @step<\/p>\n<p>def route(self, ev: InputEvent) -&gt; ParseEvent | ChatEvent:<\/p>\n<p>if ev.attachment:<\/p>\n<p>return ParseEvent(<\/p>\n<p>attachment=ev.attachment, file_name=ev.file_name, msg=ev.msg<\/p>\n<p>)<\/p>\n<p>return ChatEvent(msg=ev.msg)<\/td>\n<\/tr>\n<\/table>\n<p>\u5229\u7528\u51fd\u6570\u58f0\u660e\u63a5\u53d7\u7684Event\u7c7b\u578b\u6765\u8fdb\u884c\u4f20\u9012<\/p>\n<p>\u5176\u6b21\u662f\u6587\u6863\u89e3\u6790\uff0c\u5229\u7528\u4e86LlamaParse<\/p>\n<p>\u5c06\u5176\u89e3\u6790\u4e3a\u4e86Markdown\u5e76\u5bf9\u540e\u7eed\u8fdb\u884c\u652f\u6301\u3002<\/p>\n<p>\u540e\u7eed\u5728chat\u9636\u6bb5\uff0c\u5c31\u4f1a\u9605\u8bfb\u4e0a\u6587\u89e3\u6790\u7684\u6587\u6863\uff0c\u5e76\u52a0\u5165\u5230\u76f8\u5173\u5927\u6a21\u578b\u7684\u8c03\u7528\u4e2d\uff0c\u5e76\u8fdb\u884c\u76f8\u5173\u7684\u5f15\u7528\u89e3\u6790\uff0c\u4f20\u9012\u7ed9\u4e0b\u6587<\/p>\n<table>\n<tr>\n<td>\n      @step<\/p>\n<p>async def chat(self, ctx: Context, event: ChatEvent) -&gt; ChatResponseEvent:<\/p>\n<p>current_messages = await ctx.get(&#8216;messages&#8217;, default=[])<\/p>\n<p>current_messages.append(ChatMessage(role=&#8217;user&#8217;, content=event.msg))<\/p>\n<p>ctx.write_event_to_stream(<\/p>\n<p>LogEvent(<\/p>\n<p>msg=f&#8217;\u6b63\u5728\u4e0e{len(current_messages)}\u6761\u521d\u59cb\u6d88\u606f\u8fdb\u884c\u5bf9\u8bdd\u3002&#8217;<\/p>\n<p>)<\/p>\n<p>)<\/p>\n<p>document_text = await ctx.get(&#8216;document_text&#8217;, default=&#8221;)<\/p>\n<p>if document_text:<\/p>\n<p>ctx.write_event_to_stream(<\/p>\n<p>LogEvent(msg=&#8217;\u6b63\u5728\u63d2\u5165\u7cfb\u7edf\u63d0\u793a&#8230;&#8217;)<\/p>\n<p>)<\/p>\n<p>input_messages = [<\/p>\n<p>ChatMessage(<\/p>\n<p>role=&#8217;system&#8217;,<\/p>\n<p>content=self._system_prompt_template.format(<\/p>\n<p>document_text=document_text<\/p>\n<p>),<\/p>\n<p>),<\/p>\n<p>*current_messages,<\/p>\n<p>]<\/p>\n<p>else:<\/p>\n<p>input_messages = current_messages<\/p>\n<p>response = await self._sllm.achat(input_messages)<\/p>\n<p>response_obj: ChatResponse = response.raw<\/p>\n<p>ctx.write_event_to_stream(<\/p>\n<p>LogEvent(msg=&#8217;\u6536\u5230LLM\u54cd\u5e94\uff0c\u6b63\u5728\u89e3\u6790\u5f15\u7528&#8230;&#8217;)<\/p>\n<p>)<\/p>\n<p>current_messages.append(<\/p>\n<p>ChatMessage(role=&#8217;assistant&#8217;, content=response_obj.response)<\/p>\n<p>)<\/p>\n<p>await ctx.set(&#8216;messages&#8217;, current_messages)<\/p>\n<p># \u4ece\u6587\u6863\u6587\u672c\u4e2d\u89e3\u6790\u5f15\u7528<\/p>\n<p>citations = {}<\/p>\n<p>if document_text:<\/p>\n<p>for citation in response_obj.citations:<\/p>\n<p>line_numbers = citation.line_numbers<\/p>\n<p>for line_number in line_numbers:<\/p>\n<p>start_idx = document_text.find(<\/p>\n<p>f&#8221;&lt;line idx='{line_number}&#8217;&gt;&#8221;<\/p>\n<p>)<\/p>\n<p>end_idx = document_text.find(<\/p>\n<p>f&#8221;&lt;line idx='{line_number + 1}&#8217;&gt;&#8221;<\/p>\n<p>)<\/p>\n<p>citation_text = (<\/p>\n<p>document_text[<\/p>\n<p>start_idx<\/p>\n<p>+ len(f&#8221;&lt;line idx='{line_number}&#8217;&gt;&#8221;) : end_idx<\/p>\n<p>]<\/p>\n<p>.replace(&#8216;&lt;\/line&gt;&#8217;, &#8221;)<\/p>\n<p>.strip()<\/p>\n<p>)<\/p>\n<p>if citation.citation_number not in citations:<\/p>\n<p>citations[citation.citation_number] = []<\/p>\n<p>citations[citation.citation_number].append(citation_text)<\/p>\n<p>return ChatResponseEvent(<\/p>\n<p>response=response_obj.response, citations=citations<\/p>\n<p>)<\/td>\n<\/tr>\n<\/table>\n<p>\u8bf4\u5b8c\u4e86\u8fd9\u4e2a\u7b80\u5355\u7684Agent\uff0c\u6211\u4eec\u770b\u4e0bTask Manger\u4e4b\u4e2d\uff0c\u662f\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2aAgent\u7684<\/p>\n<p>\u9996\u5148\u5728TaskManager\u4e4b\u4e2d\u7684 _run_streaming_agent<\/p>\n<table>\n<tr>\n<td>\n   async def _run_streaming_agent(self, request: SendTaskStreamingRequest):<\/p>\n<p>task_send_params: TaskSendParams = request.params<\/p>\n<p>task_id = task_send_params.id<\/p>\n<p>session_id = task_send_params.sessionId<\/p>\n<p>input_event = self._get_input_event(task_send_params)<\/p>\n<p>try:<\/p>\n<p>ctx = None<\/p>\n<p>handler = None<\/p>\n<p># \u68c0\u67e5\u6211\u4eec\u662f\u5426\u6709\u6b64\u4f1a\u8bdd\u7684\u4fdd\u5b58\u4e0a\u4e0b\u6587\u72b6\u6001<\/p>\n<p>print(f&#8217;\u4efb\u52a1\u6570\u91cf: {len(self.tasks)}&#8217;, flush=True)<\/p>\n<p>print(f&#8217;\u4e0a\u4e0b\u6587\u72b6\u6001\u6570\u91cf: {len(self.ctx_states)}&#8217;, flush=True)<\/p>\n<p>saved_ctx_state = self.ctx_states.get(session_id, None)<\/p>\n<p>if saved_ctx_state is not None:<\/p>\n<p># \u4f7f\u7528\u73b0\u6709\u4e0a\u4e0b\u6587\u6062\u590d<\/p>\n<p>logger.info(f&#8217;\u4f7f\u7528\u4fdd\u5b58\u7684\u4e0a\u4e0b\u6587\u6062\u590d\u4f1a\u8bdd {session_id}&#8217;)<\/p>\n<p>ctx = Context.from_dict(self.agent, saved_ctx_state)<\/p>\n<p>handler = self.agent.run(<\/p>\n<p>start_event=input_event,<\/p>\n<p>ctx=ctx,<\/p>\n<p>)<\/p>\n<p>else:<\/p>\n<p># \u65b0\u4f1a\u8bdd\uff01<\/p>\n<p>logger.info(f&#8217;\u542f\u52a8\u65b0\u4f1a\u8bdd {session_id}&#8217;)<\/p>\n<p>handler = self.agent.run(<\/p>\n<p>start_event=input_event,<\/p>\n<p>)<\/p>\n<p># \u6d41\u5f0f\u4f20\u8f93\u66f4\u65b0<\/p>\n<p>async for event in handler.stream_events():<\/p>\n<p>if isinstance(event, LogEvent):<\/p>\n<p># \u5c06\u65e5\u5fd7\u4e8b\u4ef6\u4f5c\u4e3a\u4e2d\u95f4\u6d88\u606f\u53d1\u9001<\/p>\n<p>content = event.msg<\/p>\n<p>parts = [{&#8216;type&#8217;: &#8216;text&#8217;, &#8216;text&#8217;: content}]<\/p>\n<p>task_status = TaskStatus(<\/p>\n<p>state=TaskState.WORKING,<\/p>\n<p>message=Message(role=&#8217;agent&#8217;, parts=parts),<\/p>\n<p>)<\/p>\n<p>latest_task = await self.update_store(<\/p>\n<p>task_id, task_status, None<\/p>\n<p>)<\/p>\n<p>await self.send_task_notification(latest_task)<\/p>\n<p># \u53d1\u9001\u72b6\u6001\u66f4\u65b0\u4e8b\u4ef6<\/p>\n<p>task_update_event = TaskStatusUpdateEvent(<\/p>\n<p>id=task_id, status=task_status, final=False<\/p>\n<p>)<\/p>\n<p>await self.enqueue_events_for_sse(<\/p>\n<p>task_id, task_update_event<\/p>\n<p>)<\/p>\n<p># \u5982\u679c\u6211\u4eec\u5230\u8fbe\u8fd9\u91cc\u800c\u6ca1\u6709\u9047\u5230\u8fd4\u56de\uff0c\u7b49\u5f85\u6700\u7ec8\u54cd\u5e94<\/p>\n<p>final_response = await handler<\/p>\n<p>if isinstance(final_response, ChatResponseEvent):<\/p>\n<p>content = final_response.response<\/p>\n<p>parts = [{&#8216;type&#8217;: &#8216;text&#8217;, &#8216;text&#8217;: content}]<\/p>\n<p>metadata = (<\/p>\n<p>final_response.citations<\/p>\n<p>if hasattr(final_response, &#8216;citations&#8217;)<\/p>\n<p>else None<\/p>\n<p>)<\/p>\n<p>if metadata is not None:<\/p>\n<p># \u786e\u4fdd\u5143\u6570\u636e\u662f\u5b57\u7b26\u4e32\u952e\u7684\u5b57\u5178<\/p>\n<p>metadata = {str(k): v for k, v in metadata.items()}<\/p>\n<p># \u4fdd\u5b58\u4e0a\u4e0b\u6587\u72b6\u6001\u4ee5\u6062\u590d\u5f53\u524d\u4f1a\u8bdd<\/p>\n<p>self.ctx_states[session_id] = handler.ctx.to_dict()<\/p>\n<p>artifact = Artifact(<\/p>\n<p>parts=parts, index=0, append=False, metadata=metadata<\/p>\n<p>)<\/p>\n<p>task_status = TaskStatus(state=TaskState.COMPLETED)<\/p>\n<p>latest_task = await self.update_store(<\/p>\n<p>task_id, task_status, [artifact]<\/p>\n<p>)<\/p>\n<p>await self.send_task_notification(latest_task)<\/p>\n<p># \u53d1\u9001\u5de5\u4ef6\u66f4\u65b0<\/p>\n<p>task_artifact_update_event = TaskArtifactUpdateEvent(<\/p>\n<p>id=task_id, artifact=artifact<\/p>\n<p>)<\/p>\n<p>await self.enqueue_events_for_sse(<\/p>\n<p>task_id, task_artifact_update_event<\/p>\n<p>)<\/p>\n<p># \u53d1\u9001\u6700\u7ec8\u72b6\u6001\u66f4\u65b0<\/p>\n<p>task_update_event = TaskStatusUpdateEvent(<\/p>\n<p>id=task_id, status=task_status, final=True<\/p>\n<p>)<\/p>\n<p>await self.enqueue_events_for_sse(task_id, task_update_event)<\/p>\n<p>except Exception as e:<\/p>\n<p>logger.error(f&#8217;\u6d41\u5f0f\u54cd\u5e94\u65f6\u53d1\u751f\u9519\u8bef: {e}&#8217;)<\/p>\n<p>logger.error(traceback.format_exc())<\/p>\n<p># \u5411\u5ba2\u6237\u7aef\u62a5\u544a\u9519\u8bef<\/p>\n<p>await self.enqueue_events_for_sse(<\/p>\n<p>task_id,<\/p>\n<p>InternalError(<\/p>\n<p>message=f&#8217;\u6d41\u5f0f\u54cd\u5e94\u65f6\u53d1\u751f\u9519\u8bef: {e}&#8217;<\/p>\n<p>),<\/p>\n<p>)<\/p>\n<p># \u5728\u9519\u8bef\u60c5\u51b5\u4e0b\u6e05\u7406\u4e0a\u4e0b\u6587<\/p>\n<p>if session_id in self.ctx_states:<\/p>\n<p>del self.ctx_states[session_id]<\/td>\n<\/tr>\n<\/table>\n<p>\u5728\u5176\u4e2d\uff0c\u4e00\u65b9\u9762\u652f\u6301\u4e0a\u4e0b\u6587\u7684\u6062\u590d\u673a\u5236<\/p>\n<p>\u72b6\u6001\u68c0\u67e5\uff1a\u68c0\u67e5\u662f\u5426\u5b58\u5728\u4fdd\u5b58\u7684\u4e0a\u4e0b\u6587\u3002\u4e0a\u4e0b\u6587\u91cd\u5efa\uff1a\u4ece\u5b57\u5178\u91cd\u5efa\u5b8c\u6574\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002\u65e0\u7f1d\u5207\u6362\uff1a\u65b0\u4f1a\u8bdd\u548c\u6062\u590d\u4f1a\u8bdd\u7684\u65e0\u7f1d\u5904\u7406\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\u652f\u6301\u6d41\u5f0f\u7684\u8fd4\u56de\u673a\u5236\uff0c\u53ef\u4ee5\u8ba9\u4efb\u52a1\u5728\u6267\u884c\u7684\u4e2d\u95f4\u72b6\u6001\u4e0d\u65ad\u5730\u63a8\u6d41\u8fdb\u884c\u8fd4\u56de\u3002<\/p>\n<p>\u5e76\u4e14\u5728\u6709\u9519\u8bef\u7684\u65f6\u5019\uff0c\u6e05\u9664\u4e0a\u4e0b\u6587\u5e76\u8fd4\u56de\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff0c\u6211\u4eec\u9996\u5148\u8bf4\u4e86\u4e0bLlamaIndex\u7684\u4f7f\u7528\uff0c\u5229\u7528\u5176\u6784\u5efa\u4e86\u4e00\u4e2a\u652f\u6301\u6587\u4ef6\u4e0a\u4f20\uff0c\u5411\u91cf\u7d22\u5f15\uff0c\u591a\u8f6e\u5bf9\u8bdd\u7684\u667a\u80fdAgent\u3002\u5e76\u5c06\u5176\u6700\u7ec8\u878d\u5165\u5230\u4e86A2A\u4e4b\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LlamaIndex\u96c6\u6210A2A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5814"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5814"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5814\/revisions"}],"predecessor-version":[{"id":5817,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5814\/revisions\/5817"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5814"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}