ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

【Agent】15. 多智能体工作流与Weaviate QueryAgent分析报告

【Agent】15. 多智能体工作流与Weaviate QueryAgent分析报告 1. 案例目标本案例展示了如何结合多智能体工作流与Weaviate QueryAgent构建一个智能文档管理系统。该系统能够将网页内容写入Weaviate向量数据库的不同集合WeaviateDocs和LlamaIndexDocs使用QueryAgent对存储的文档进行智能查询通过工作流自动分类用户请求并执行相应操作支持并行处理多个任务提高系统效率2. 技术栈与核心依赖本案例使用的主要技术栈和依赖包括LlamaIndex: 用于构建智能体和工作流的核心框架Weaviate: 向量数据库用于存储和检索文档OpenAI: 提供语言模型支持GPT-4o-miniPydantic: 用于数据验证和结构化输出核心依赖包llama-index-core llama-index-llms-openai weaviate-client[agents] pydantic3. 环境配置在运行本案例之前需要完成以下环境配置3.1 安装依赖pip install llama-index-core llama-index-llms-openai weaviate-client[agents] pydantic3.2 配置API密钥import os from getpass import getpass if OPENAI_API_KEY not in os.environ: os.environ[OPENAI_API_KEY] getpass(openai-key)3.3 配置Weaviate客户端import weaviate from weaviate.collections.classes.config import Configure client weaviate.connect_to_local() client.collections.delete(WeaviateDocs) client.collections.delete(LlamaIndexDocs)4. 案例实现4.1 初始化Weaviate客户端首先我们连接到本地Weaviate实例并创建必要的集合def fresh_setup_weaviate(client): client.collections.delete(WeaviateDocs) client.collections.delete(LlamaIndexDocs) client.collections.create( nameWeaviateDocs, vectorizer_configConfigure.Vectorizer.none(), ) client.collections.create( nameLlamaIndexDocs, vectorizer_configConfigure.Vectorizer.none(), ) weaviate_agent QueryAgent( clientclient, collections[WeaviateDocs, LlamaIndexDocs], llmOpenAI(modelgpt-4o-mini), system_promptYou are a helpful assistant that can answer questions about Weaviate and LlamaIndex, ) return weaviate_agent4.2 创建QueryAgentQueryAgent用于对Weaviate中的文档进行智能查询from weaviate.agents.query import QueryAgent weaviate_agent fresh_setup_weaviate(client)4.3 创建FunctionAgent我们创建一个FunctionAgent它可以执行以下操作将网页内容写入WeaviateDocs集合将网页内容写入LlamaIndexDocs集合使用QueryAgent回答问题def write_to_weaviate_collection(urlslist[str]): Useful for writing new content to the WeaviateDocs collection write_webpages_to_weaviate(client, urls, WeaviateDocs) def write_to_li_collection(urlslist[str]): Useful for writing new content to the LlamaIndexDocs collection write_webpages_to_weaviate(client, urls, LlamaIndexDocs) def query_agent(query: str) - str: Useful for asking questions about Weaviate and LlamaIndex response weaviate_agent.run(query) return response.final_answer agent FunctionAgent( tools[write_to_weaviate_collection, write_to_li_collection, query_agent], llmllm, system_promptYou are a helpful assistant that can write the contents of urls to WeaviateDocs and LlamaIndexDocs collections, as well as forwarding questions to a QueryAgent, )4.4 构建工作流事件模型我们定义了以下事件类型来构建工作流class EvaluateQuery(Event): query: str class WriteLlamaIndexDocsEvent(Event): urls: list[str] class WriteWeaviateDocsEvent(Event): urls: list[str] class QueryAgentEvent(Event): query: str4.5 实现分支工作流我们创建一个简单的工作流根据查询类型决定执行哪个分支class DocsAssistantWorkflow(Workflow): step async def start(self, ctx: Context, ev: StartEvent) - EvaluateQuery: return EvaluateQuery(queryev.query) step async def evaluate_query( self, ctx: Context, ev: EvaluateQuery ) - QueryAgentEvent | WriteLlamaIndexDocsEvent | WriteWeaviateDocsEvent | StopEvent: if ev.query llama: return WriteLlamaIndexDocsEvent(urls[ev.query]) if ev.query weaviate: return WriteWeaviateDocsEvent(urls[ev.query]) if ev.query question: return QueryAgentEvent(queryev.query) return StopEvent() step async def write_li_docs( self, ctx: Context, ev: WriteLlamaIndexDocsEvent ) - StopEvent: print(fGot a request to write something to LlamaIndexDocs) return StopEvent() step async def write_weaviate_docs( self, ctx: Context, ev: WriteWeaviateDocsEvent ) - StopEvent: print(fGot a request to write something to WeaviateDocs) return StopEvent() step async def query_agent( self, ctx: Context, ev: QueryAgentEvent ) - StopEvent: print(fGot a request to forward a query to the QueryAgent) return StopEvent()4.6 使用结构化输出分类查询为了更智能地分类查询我们使用Pydantic模型和结构化输出class SaveToLlamaIndexDocs(BaseModel): The URLs to parse and save into a llama-index specific docs collection. llama_index_urls: List[str] Field(default_factorylist) class SaveToWeaviateDocs(BaseModel): The URLs to parse and save into a weaviate specific docs collection. weaviate_urls: List[str] Field(default_factorylist) class Ask(BaseModel): The natural language questions that can be asked to a QA agent. queries: List[str] Field(default_factorylist) class Actions(BaseModel): Actions to take based on the latest user message. actions: List[ Union[SaveToLlamaIndexDocs, SaveToWeaviateDocs, Ask] ] Field(default_factorylist)4.7 实现多分支工作流最后我们实现一个支持并行处理多个任务的工作流class DocsAssistantWorkflow(Workflow): def __init__(self, *args, **kwargs): self.llm OpenAIResponses(modelgpt-4.1-mini) self.system_prompt You are a docs assistant. You evaluate incoming queries and break them down to subqueries when needed. You decide on the next best course of action. Overall, here are the options: - You can write the contents of a URL to llamaindex docs (if its a llamaindex url) - You can write the contents of a URL to weaviate docs (if its a weaviate url) - You can answer a question about llamaindex and weaviate using the QueryAgent super().__init__(*args, **kwargs) step async def start(self, ctx: Context, ev: StartEvent) - EvaluateQuery: return EvaluateQuery(queryev.query) step async def evaluate_query( self, ctx: Context, ev: EvaluateQuery ) - QueryAgentEvent | WriteLlamaIndexDocsEvent | WriteWeaviateDocsEvent | None: await ctx.store.set(results, []) sllm self.llm.as_structured_llm(Actions) response await sllm.achat( [ ChatMessage(rolesystem, contentself.system_prompt), ChatMessage(roleuser, contentev.query), ] ) actions response.raw.actions await ctx.store.set(num_events, len(actions)) await ctx.store.set(results, []) print(actions) for action in actions: if isinstance(action, SaveToLlamaIndexDocs): ctx.send_event( WriteLlamaIndexDocsEvent(urlsaction.llama_index_urls) ) elif isinstance(action, SaveToWeaviateDocs): ctx.send_event( WriteWeaviateDocsEvent(urlsaction.weaviate_urls) ) elif isinstance(action, Ask): for query in action.queries: ctx.send_event(QueryAgentEvent(queryquery)) step async def write_li_docs( self, ctx: Context, ev: WriteLlamaIndexDocsEvent ) - ActionCompleted: print(fWriting {ev.urls} to LlamaIndex Docs) write_webpages_to_weaviate( client, urlsev.urls, collection_nameLlamaIndexDocs ) results await ctx.store.get(results) results.append(fWrote {ev.urls} it LlamaIndex Docs) return ActionCompleted(resultfWriting {ev.urls} to LlamaIndex Docs) step async def write_weaviate_docs( self, ctx: Context, ev: WriteWeaviateDocsEvent ) - ActionCompleted: print(fWriting {ev.urls} to Weaviate Docs) write_webpages_to_weaviate( client, urlsev.urls, collection_nameWeaviateDocs ) results await ctx.store.get(results) results.append(fWrote {ev.urls} it Weavite Docs) return ActionCompleted(resultfWriting {ev.urls} to Weaviate Docs) step async def query_agent( self, ctx: Context, ev: QueryAgentEvent ) - ActionCompleted: print(fSending {ev.query} to agent) response weaviate_agent.run(ev.query) results await ctx.store.get(results) results.append(fQueryAgent responded with:\n {response.final_answer}) return ActionCompleted(resultfSending {ev.query} to agent) step async def collect( self, ctx: Context, ev: ActionCompleted ) - StopEvent | None: num_events await ctx.store.get(num_events) evs ctx.collect_events(ev, [ActionCompleted] * num_events) if evs is None: return None return StopEvent(result[ev.result for ev in evs])5. 案例效果本案例实现了一个智能文档管理系统具有以下效果功能演示系统能够根据用户输入自动分类并执行相应操作当用户提供LlamaIndex相关URL时系统会自动将内容写入LlamaIndexDocs集合当用户提供Weaviate相关URL时系统会自动将内容写入WeaviateDocs集合当用户提出问题时系统会使用QueryAgent查询已存储的文档并提供答案系统支持并行处理多个任务提高效率示例输出[SaveToLlamaIndexDocs(llama_index_urls[https://docs.llamaindex.ai/en/stable/understanding/workflows/]), SaveToLlamaIndexDocs(llama_index_urls[https://docs.llamaindex.ai/en/stable/understanding/workflows/branches_and_loops/])] Writing [https://docs.llamaindex.ai/en/stable/understanding/workflows/] to LlamaIndex Docs Writing [https://docs.llamaindex.ai/en/stable/understanding/workflows/branches_and_loops/] to LlamaIndex Docs Wrote [https://docs.llamaindex.ai/en/stable/understanding/workflows/] it LlamaIndex Docs Wrote [https://docs.llamaindex.ai/en/stable/understanding/workflows/branches_and_loops/] it LlamaIndex Docs6. 案例实现思路本案例的实现思路可以分为以下几个关键步骤环境初始化: 设置Weaviate客户端创建必要的集合并初始化QueryAgent工具定义: 定义写入不同集合和查询文档的工具函数事件模型设计: 设计工作流中的事件类型用于在不同步骤间传递信息工作流构建: 实现基于事件的分支工作流能够根据查询类型执行不同操作结构化输出: 使用Pydantic模型和结构化输出使系统能够智能解析用户意图并行处理: 实现支持并行执行多个任务的工作流提高系统效率关键技术点使用Weaviate的QueryAgent实现智能文档检索通过LlamaIndex工作流框架实现复杂的分支逻辑利用结构化输出提高系统对用户意图的理解能力通过事件驱动架构实现灵活的任务调度7. 扩展建议基于本案例的实现可以考虑以下扩展方向7.1 增强文档处理能力支持更多文档格式PDF、Word、Excel等实现文档内容的智能摘要和关键信息提取添加文档版本控制和历史追踪功能7.2 优化查询体验实现更高级的查询意图识别添加查询历史记录和个性化推荐支持多语言查询和跨语言检索7.3 扩展工作流功能实现更复杂的条件分支和循环逻辑添加人机交互节点支持工作流中的用户确认集成更多外部工具和服务7.4 提升系统性能实现文档处理的并行化和批处理优化向量索引策略提高检索速度添加缓存机制减少重复计算7.5 增强安全性实现基于角色的访问控制添加文档内容的安全扫描和过滤支持数据加密和安全传输8. 总结本案例展示了如何结合LlamaIndex工作流框架和Weaviate QueryAgent构建一个智能文档管理系统。通过事件驱动的工作流设计系统能够自动识别用户意图并执行相应操作实现了文档的智能存储和检索。该系统的核心优势在于自动化处理能够自动分类用户请求并执行相应操作并行执行支持同时处理多个任务提高效率智能检索利用QueryAgent实现基于语义的文档检索灵活扩展基于事件驱动架构易于添加新功能和工具这种多智能体工作流的设计模式可以广泛应用于各种需要自动化处理和智能决策的场景如客户服务、知识管理、内容分析等领域。通过结合不同类型的智能体和工作流可以构建出更加强大和灵活的AI系统。
返回列表