掘金 人工智能 05月13日 15:38
使用PocketFlow构建Web Search Agent
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文深入解析了PocketFlow框架中Web Search Agent的实现原理和架构。该Agent通过判断用户问题是否需要网络搜索,并结合网络搜索结果进行回答,从而实现智能问答功能。文章详细介绍了Agent的三个核心节点:判断节点、网络搜索节点和回答节点,并阐述了它们之间的协作流程。此外,还分析了每个节点的关键步骤,如提示词设计、数据获取和结果处理,帮助读者理解Agent的工作机制。文章还提及作者使用C#复刻该Agent的计划,预示着后续的技术分享。

💡Web Search Agent包含三个关键节点:**判断节点**(DecideAction)、**网络搜索节点**(SearchWeb)和**回答节点**(AnswerQuestion),它们协同工作完成智能问答。

🔍**判断节点**(DecideAction)负责分析用户问题,决定是否需要进行网络搜索。它通过提示词引导模型判断,并以YAML格式返回决策,选择“search”或“answer”。

🌐**网络搜索节点**(SearchWeb)在判断节点决定搜索后执行,利用DuckDuckGo等工具进行网络搜索,并将搜索结果存储在共享上下文中,供后续回答节点使用。

💬**回答节点**(AnswerQuestion)负责根据问题和上下文(包括网络搜索结果)生成最终答案。它同样依赖提示词引导模型,并从共享存储中提取最终答案呈现给用户。

前言

本文介绍的是PocketFlow的cookbook中的pocketflow-agent部分。

回顾一下PocketFlow的核心架构:

每一个节点的架构:

具体介绍可以看上一篇文章:

“Pocket Flow,一个仅用 100 行代码实现的 LLM 框架”

实现效果

这个Web Search Agent是干嘛的呢?

这个Agent会判断你输入的问题是否需要搜索网络,如果需要会搜索网络之后回答。

效果如下图所示:

现在就让我们看看是怎么实现的吧!!

Web Search Agent实现

先看看这个Web Search Agent的整体架构。

Web Search Agent包含三个节点:

Web Search Agent判断节点网络搜索节点回答节点

这三个节点的关系如下图所示:

searchanswercontext判断节点网络搜索节点回答节点

现在来看整体流程。

首先创建节点并连接:

def create_agent_flow():    """    Create and connect the nodes to form a complete agent flow.        The flow works like this:    1. DecideAction node decides whether to search or answer    2. If search, go to SearchWeb node    3. If answer, go to AnswerQuestion node    4. After SearchWeb completes, go back to DecideAction        Returns:        Flow: A complete research agent flow    """    # Create instances of each node    decide = DecideAction()    search = SearchWeb()    answer = AnswerQuestion()        # Connect the nodes    # If DecideAction returns "search", go to SearchWeb    decide - "search" >> search        # If DecideAction returns "answer", go to AnswerQuestion    decide - "answer" >> answer        # After SearchWeb completes and returns "decide", go back to DecideAction    search - "decide" >> decide        # Create and return the flow, starting with the DecideAction node    return Flow(start=decide) 

决定节点的prep:

获取上下文(当前还没有上下文)与问题。

决定节点的exec:

获取prep的问题与上下文,判断是搜索还是回答。

看看这里的提示词:

 prompt = f"""### CONTEXTYou are a research assistant that can search the web.Question: {question}Previous Research: {context}### ACTION SPACE[1] search  Description: Look up more information on the web  Parameters:    - query (str): What to search for[2] answer  Description: Answer the question with current knowledge  Parameters:    - answer (str): Final answer to the question## NEXT ACTIONDecide the next action based on the context and available actions.Return your response in this format:```yamlthinking: |    <your step-by-step reasoning process>action: search OR answerreason: <why you chose this action>answer: <if action is answer>search_query: <specific search query if action is search>```IMPORTANT: Make sure to:1. Use proper indentation (4 spaces) for all multi-line fields2. Use the | character for multi-line text fields3. Keep single-line fields without the | character"""

注意这里作者使用的是yaml返回而不是json,这是因为大模型返回json出错的概率更高一点,我也遇到过好多次json返回错误。

返回大模型的决定:

决定节点的post:

会返回search,转到Search节点。

Search节点的prep:

从共享存储中获取要搜索的内容。

Search节点的exec:

开始执行网络搜索,这里作者使用的是duckduckgo,直接使用很方便,当然也可以使用brave,免费订阅一个月2000次的额度。

将结果合并之后返回。

Search节点的post:

将网络搜索的结果放到共享存储的context中。

返回决定节点。

根据获取的上下文做判断:

模型决定回答:

回答节点的prep:

从共享存储中获取问题与上下文。

回答节点的exec:

获取问题与上下文,现在的上下文变成之前的答案:

回答的提示词:

 prompt = f"""### CONTEXTBased on the following information, answer the question.Question: {question}Research: {context}## YOUR ANSWER:Provide a comprehensive answer using the research results."""

回答节点的post:

最终回答从共享存储中取出:

print(shared.get("answer", "No answer found"))

以上就将这个流程走通了。

整体的流程图大概如下所示:

searchanswer开始决定节点的prep:获取上下文(当前还没有上下文)与问题。决定节点的exec:获取prep的问题与上下文,判断是搜索还是回答。决定节点的post:返回search或answer网络搜索节点的prep:从共享存储中获取要搜索的内容。回答节点的prep:从共享存储中获取问题与上下文。网络搜索节点的exec:获取网络搜索结果网络搜索节点的post:将网络搜索的结果放到共享存储的context中。回答节点的exec:根据获取的上下文回答问题回答节点的post:从共享存储中取出最终回答结束

以上就是这个Web Search Agent的工作流。

由于我个人比较喜欢C#,我也用C#复刻了一个,下期介绍如何使用C#复刻一个。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

PocketFlow Web Search Agent LLM框架 智能问答
相关文章