Type something to search...
构建高效的多智能体rag管道:使用crew Ai和tavily实现智能问答系统

构建高效的多智能体rag管道:使用crew Ai和tavily实现智能问答系统

在当今智能系统时代

将多样的检索工具与强大的语言模型相结合的能力正在改变我们回答复杂查询的方式。

在本文中,我们探讨了Crew AI如何与Tavily等工具一起帮助构建一个多智能体、智能的Retrieval-Augmented Generation (RAG) pipeline。我们将深入了解Crew AI,解释传统RAG与智能RAG之间的区别,并向您展示如何集成PDF搜索和网络搜索,以创建一个动态的智能问答系统。

Crew AI

Crew AI是一个框架,协调多个专业代理和任务形成一个统一的工作流程。Crew AI并不依赖于单一的整体系统,而是将责任分配给各个代理——每个代理都被设计为承担特定角色(例如,路由、检索、评分)。这种模块化的方法提供了若干优势:

  • 灵活性: 可以轻松更换或更新单个代理,而无需重新设计整个系统。
  • 专业化: 每个代理可以专注于特定任务,例如评估检索相关性或过滤幻觉响应。
  • 透明性: 详细的日志记录和基于任务的架构使您能够追踪pipeline中的每个决策。

在我们的pipeline中,Crew AI用于将检索方法和评分步骤结合成一个智能的多智能体系统,从头到尾处理用户查询。

集成 Tavily 进行网页搜索

虽然我们的系统利用 PDF 搜索工具(通过 Crew AI 的 PDFSearchTool)进行文档内查询,但并非所有问题都能仅通过预先索引的文档来回答。这就是 Tavily 发挥作用的地方。

TavilySearchResults 是一个执行网页搜索并从互联网检索相关片段的工具。配置有自己的 API Key,Tavily 允许我们的系统访问更广泛的信息池,确保如果查询没有完全涵盖在我们的 vectorstore(PDF 文档)中,系统可以回退到实时网页搜索。

通过集成 Tavily,我们的 pipeline 无缝平衡本地(基于文档)检索和全球(基于网页)搜索——提高了覆盖范围和答案质量。

RAG vs. Agentic RAG: What’s the Difference?

Retrieval-Augmented Generation (RAG) 是一种强大的技术,它将语言模型的生成能力与外部检索系统相结合。在传统的 RAG 中:

  • 语言模型根据查询获取相关文档或片段。
  • 然后通过对检索到的上下文进行条件生成响应。

虽然传统的 RAG 系统有效,但在处理错误或适应新类型查询时往往显得单一且不够灵活。

Agentic RAG 则引入了专门代理的概念,进入检索和生成过程。它们的区别如下:

传统 RAG:

  • 使用单一的管道,检索和生成按固定顺序发生。
  • 缺乏动态决策能力;如果检索到的上下文不理想,系统可能会产生不佳的答案。

Agentic RAG:

  • 利用多个代理(例如 Router、Retriever、Grader 和 Answer Generator)执行不同的任务。
  • 每个代理可以验证、改进,甚至覆盖其他阶段的输出。例如,Grader Agent 可以评估检索数据的相关性,并在必要时触发回退到网页搜索。
  • 提供更高的适应性和透明度,因为每个决策点由专为该特定目的设计的代理管理。

通过采用代理化的方法,我们的管道变得更具抗错能力,并能够提供不仅准确而且有充分证据支持的答案。

Source — arxiv

构建 Agentic RAG 管道

我们的多代理管道集成了 Crew AI,一个用于 vectorstore 检索的 PDF 搜索工具,以及 Tavily 用于网络搜索。系统的工作原理如下:

  1. 路由: 路由代理检查问题,以确定是使用 vectorstore(PDF 搜索)还是进行网络搜索。例如,如果查询包含“self-attention”等关键词,它将把查询路由到我们的基于 PDF 的 vectorstore。
  2. 检索: 根据路由决定,检索代理使用适当的工具。如果选择了 vectorstore,则使用我们的 PDFSearchTool。否则,网络搜索工具(Tavily)从网络中检索相关片段。
  3. 评分和验证: 多个评分代理随后评估检索到的内容:
    • Grader Agent 评估信息是否相关。
    • Hallucination Grader 检查生成的答案是否有事实支持。
    • 最后,Answer Grader 提供最终裁决,如果答案缺乏依据,可能会触发另一次网络搜索。

最终答案生成: 根据这些评估,答案代理生成一个清晰简洁的最终答案。

这种模块化设计确保了过程中的每一步都经过优化,并可以独立改进。系统的灵活性使其能够处理特定领域的查询(使用预先索引的 PDF)和一般知识查询(使用实时网络搜索)。

实施

前提条件:安装所需的库

在深入代码之前,您需要安装几个支持我们pipeline的Python库。您可以使用pip安装这些库。以下是所需的包及其版本:

pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29 sentence-transformers langchain-groq langchain_huggingface
  • crewai==0.28.8: 用于协调多个agents的核心框架。
  • crewai_tools==0.1.6: 与Crew AI集成的附加工具。
  • langchain_community==0.0.29: 提供与语言模型交互的社区工具。
  • sentence-transformers: 用于生成检索任务的embedding。
  • langchain-groq: 与GROQ API集成以进行语言模型操作。
  • langchain_huggingface: 提供Hugging Face集成,例如用于embedding模型。

确保您安装了Python 3.7或更高版本,然后在终端中运行上述命令以安装所有依赖项。

Obtaining API Keys: GROQ and Tavily

To power our multi-agent RAG pipeline, you’ll need to obtain API keys for both the GROQ and Tavily services. 这些密钥使您能够验证请求并访问我们系统所需的高级功能。

GROQ API Key

它是什么? GROQ API 提供对 OpenAI 兼容的语言模型服务的访问。在我们的设置中,我们将其用作我们的 ChatOpenAI 模型的后端。

如何获取:

  1. 访问 GROQ 开发者门户: 前往 GROQ 网站或他们的开发者门户(通常位于 https://api.groq.com 或类似的 URL)。

  2. 创建账户: 如果您尚未注册,请注册一个新账户。

  3. 生成 API Key: 注册后,导航到您的仪表板或 API 部分并生成一个新的 API Key。

  4. 配置您的环境: 在您的环境变量中设置该 Key。例如,在您的 Python 代码中,您可以这样做:

    os.environ["GROQ_API_KEY"] = "your_groq_api_key_here"
    

安全提示: 确保将您的 API Key 保持安全,避免在公共代码库中暴露它。

Tavily API Key

什么是它? Tavily 提供了一种网页搜索功能,通过从互联网获取实时数据来增强我们的检索过程。当查询需要超出预先索引文档的更广泛上下文时,此工具至关重要。

如何获取:

  1. 访问 Tavily 平台: 前往 Tavily 的网站或开发者门户(在他们的官方网站上查找适当的 URL)。

  2. 注册或登录: 创建一个账户或如果您已经有一个账户则登录。

  3. 生成 API Key: 登录后,导航到仪表板中的 API keys 部分并生成一个新的密钥。

  4. 设置 API Key: 在您的代码中,按如下方式配置密钥:

    os.environ['TAVILY_API_KEY'] = "your_tavily_api_key_here"
    

保持私密: 与 GROQ API key 一样,确保您的 Tavily API key 保持机密。

通过遵循这些步骤,您将拥有运行我们的 multi-agent RAG pipeline 所需的 API keys。这些密钥允许我们的系统通过 GROQ 利用先进的语言模型,并通过 Tavily 进行实时网页搜索,从而确保一个强大且灵活的问题回答框架。

1. 环境设置和依赖项

我们的代码开始于导入必要的库并设置环境变量。我们使用几个专用包:

  • langchain_openai.ChatOpenAI: 与兼容OpenAI的语言模型进行接口。
  • crewai_tools.PDFSearchTool: 允许在PDF中进行搜索。
  • TavilySearchResults: 一个用于网络搜索的工具。
  • crewai (Crew, Task, Agent): 提供构建多代理工作流的框架。

我们还通过环境变量设置API密钥,以安全地访问我们的外部服务。

from langchain_openai import ChatOpenAI
from crewai_tools import PDFSearchTool
from langchain_community.tools.tavily_search import TavilySearchResults
from crewai_tools import tool
from crewai import Crew
from crewai import Task
from crewai import Agent

import os

os.environ["GROQ_API_KEY"] = ""

2. 配置语言模型 (LLM)

我们系统的核心是一个语言模型。我们配置 ChatOpenAI 以使用指定的 API 端点和模型 (“llama3-8b-8192”),并控制温度和令牌限制。

llm = ChatOpenAI(
    openai_api_base="https://api.groq.com/openai/v1",
    openai_api_key=os.environ['GROQ_API_KEY'],
    model_name="llama3-8b-8192",
    temperature=0.1,
    max_tokens=1000,
)

这个 LLM 不仅处理我们的查询,还支持各种 agent 在整个 pipeline 中生成、评分和完善答案。

3. 设置 PDF 搜索工具

我们的一种关键检索方法涉及在 PDF 文档中进行搜索。在这个例子中,我们下载了一篇关于自注意力的开创性论文,并使用 LLM 进行语言理解和 Hugging Face 嵌入器生成嵌入,配置我们的 PDFSearchTool

import requests

pdf_url = "https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf"
response = requests.get(pdf_url)

with open("attenstion_is_all_you_need.pdf", "wb") as file:
    file.write(response.content)

rag_tool = PDFSearchTool(pdf='attenstion_is_all_you_need.pdf',
    config=dict(
        llm=dict(
            provider="groq", 
            config=dict(
                model="llama3-8b-8192",
            ),
        ),
        embedder=dict(
            provider="huggingface", 
            config=dict(
                model="BAAI/bge-small-en-v1.5",
            ),
        ),
    )
)

然后,我们对 PDF 工具运行一个测试查询,以查看它如何处理与自注意力机制相关的问题。

rag_tool.run("How did self-attention mechanism evolve in large language models?")

4. 设置网络搜索工具

对于可能受益于更广泛上下文的问题,我们还集成了一个使用 TavilySearchResults 的网络搜索工具。该工具配置了特定的 API Key,并返回有限数量的搜索结果。

import os

os.environ['TAVILY_API_KEY'] = ''
web_search_tool = TavilySearchResults(k=3)
web_search_tool.run("What is self-attention mechansim in large language models?")

这种双重方法使我们的系统能够动态决定是使用文档内搜索还是更广泛的网络搜索。

5. 创建路由工具

接下来,我们实现一个简单的路由工具来决定检索方法。该函数检查问题,如果它包含关键字“self-attention”,则将查询发送到 vectorstore(即 PDF 搜索);否则,它选择网页搜索。

@tool
def router_tool(question):
  """Router Function"""
  if 'self-attention' in question:
    return 'vectorstore'
  else:
    return 'web_search'

@tool 装饰器注册此函数,使其在后续过程中可以被我们的 agent 访问。

6. 定义专门代理

我们现在定义几个代理,每个代理在我们的multi-agent system中扮演特定角色:

6.1. 代理 A

  • 角色:数据收集
  • 职责
    • 从各种来源收集数据
    • 预处理数据以便分析

6.2. 代理 B

  • 角色:数据分析
  • 职责
    • 分析收集到的数据
    • 生成洞察和报告

6.3. 代理 C

  • 角色:决策制定
  • 职责
    • 使用代理 B 的洞察来做出决策
    • 根据数据分析实施策略

6.4. 代理 D

  • 角色:沟通
  • 职责
    • 促进代理之间的沟通
    • 确保所有代理都更新最新信息

6.5. 代理 E

  • 角色:监控
  • 职责
    • 监控系统性能
    • 识别任何问题或瓶颈

6.6. 代理 F

  • 角色:反馈循环
  • 职责
    • 收集系统反馈
    • 根据反馈建议改进
def agent_a_function(data):
    # Process data for analysis
    processed_data = preprocess(data)
    return processed_data
def agent_b_function(processed_data):
    # Analyze the processed data
    insights = analyze(processed_data)
    return insights
def agent_c_function(insights):
    # Make decisions based on insights
    decision = make_decision(insights)
    return decision
def agent_d_function(decision):
    # Communicate the decision
    communicate(decision)
def agent_e_function():
    # Monitor system performance
    performance = monitor_system()
    return performance
def agent_f_function(feedback):
    # Suggest improvements based on feedback
    improvements = suggest_improvements(feedback)
    return improvements

根据这些代理的定义,我们可以实现一个高效且有效的 cohesive multi-agent system。

路由代理

负责根据关键词将查询路由到向量存储或网络搜索。

Router_Agent = Agent(
  role='Router',
  goal='Route user question to a vectorstore or web search',
  backstory=(
    "You are an expert at routing a user question to a vectorstore or web search."
    "Use the vectorstore for questions on concept related to Retrieval-Augmented Generation."
    "You do not need to be stringent with the keywords in the question related to these topics. Otherwise, use web-search."
  ),
  verbose=True,
  allow_delegation=False,
  llm=llm,
)

Retriever Agent

从适当的工具(PDF或网页搜索)中检索相关信息。

Retriever_Agent = Agent(
    role="Retriever",
    goal="Use the information retrieved from the vectorstore to answer the question",
    backstory=(
        "You are an assistant for question-answering tasks."
        "Use the information present in the retrieved context to answer the question."
        "You have to provide a clear concise answer."
    ),
    verbose=True,
    allow_delegation=False,
    llm=llm,
)

Grader 和幻觉代理

这些代理评估检索的质量:

  • Grader_agent 评估相关性。
  • Hallucination_grader 检查答案是否基于事实,而不是幻觉。
  • Answer_grader 提供最终过滤,必要时可能触发网络搜索。

每个代理都配置了角色、明确的目标和指导其行为的背景故事。

Grader_agent =  Agent(
  role='Answer Grader',
  goal='Filter out erroneous retrievals',
  backstory=(
    "You are a grader assessing relevance of a retrieved document to a user question."
    "If the document contains keywords related to the user question, grade it as relevant."
    "It does not need to be a stringent test. You have to make sure that the answer is relevant to the question."
  ),
  verbose=True,
  allow_delegation=False,
  llm=llm,
)
hallucination_grader = Agent(
    role="Hallucination Grader",
    goal="Filter out hallucination",
    backstory=(
        "You are a hallucination grader assessing whether an answer is grounded in / supported by a set of facts."
        "Make sure you meticulously review the answer and check if the response provided is in alignment with the question asked"
    ),
    verbose=True,
    allow_delegation=False,
    llm=llm,
)
answer_grader = Agent(
    role="Answer Grader",
    goal="Filter out hallucination from the answer.",
    backstory=(
        "You are a grader assessing whether an answer is useful to resolve a question."
        "Make sure you meticulously review the answer and check if it makes sense for the question asked."
        "If the answer is relevant generate a clear and concise response."
        "If the answer generated is not relevant then perform a websearch using 'web_search_tool'."
    ),
    verbose=True,
    allow_delegation=False,
    llm=llm,
)

7. 使用任务协调工作流程

任务定义了过程每个步骤的责任和预期结果。它们将代理连接成一个连贯的 pipeline:

路由任务

通过决定使用 vectorstore 还是 web 搜索来路由查询。

router_task = Task(
    description=("Analyse the keywords in the question {question}"
    "Based on the keywords decide whether it is eligible for a vectorstore search or a web search."
    "Return a single word 'vectorstore' if it is eligible for vectorstore search."
    "Return a single word 'websearch' if it is eligible for web search."
    "Do not provide any other premable or explaination."
    ),
    expected_output=("Give a binary choice 'websearch' or 'vectorstore' based on the question"
    "Do not provide any other premable or explaination."),
    agent=Router_Agent,
    tools=[router_tool],
)

Retriever Task

使用路由器的决策来提取相关信息。

retriever_task = Task(
    description=("Based on the response from the router task extract information for the question {question} with the help of the respective tool."
    "Use the web_search_tool to retrieve information from the web in case the router task output is 'websearch'."
    "Use the rag_tool to retrieve information from the vectorstore in case the router task output is 'vectorstore'."
    ),
    expected_output=("You should analyse the output of the 'router_task'"
    "If the response is 'websearch' then use the web_search_tool to retrieve information from the web."
    "If the response is 'vectorstore' then use the rag_tool to retrieve information from the vectorstore."
    "Return a claer and consise text as response."),
    agent=Retriever_Agent,
    context=[router_task],
)

Grader, Hallucination, and Answer Tasks

这些任务逐步评估检索到的内容并最终确定答案。

grader_task = Task(
    description=("根据检索任务对问题 {question} 的响应,评估检索到的内容是否与问题相关。"
    ),
    expected_output=("二进制评分 'yes' 或 'no',以指示文档是否与问题相关。"
    "如果 'retriever_task' 的响应与所提问的问题一致,则必须回答 'yes'。"
    "如果 'retriever_task' 的响应与所提问的问题不一致,则必须回答 'no'。"
    "除了 'yes' 或 'no' 之外,不要提供任何前言或解释。"),
    agent=Grader_agent,
    context=[retriever_task],
)
hallucination_task = Task(
    description=("根据评分任务对问题 {question} 的响应,评估答案是否基于/由一组事实支持。"),
    expected_output=("二进制评分 'yes' 或 'no',以指示答案是否与所提问的问题同步。"
    "如果答案有用并包含关于所提问问题的事实,则回答 'yes'。"
    "如果答案没有用并且不包含关于所提问问题的事实,则回答 'no'。"
    "除了 'yes' 或 'no' 之外,不要提供任何前言或解释。"),
    agent=hallucination_grader,
    context=[grader_task],
)
answer_task = Task(
    description=("根据幻觉任务对问题 {question} 的响应,评估答案是否有助于解决该问题。"
    "如果答案是 'yes',返回一个清晰简洁的答案。"
    "如果答案是 'no',则执行 'websearch' 并返回响应。"),
    expected_output=("如果 'hallucination_task' 的响应是 'yes',则返回一个清晰简洁的响应。"
    "仅当 'hallucination_task' 的响应是 'no' 时,使用 'web_search_tool' 执行网络搜索并返回一个清晰简洁的响应。"
    "否则回答 '抱歉!无法找到有效的响应'。"),
    context=[hallucination_task],
    agent=answer_grader,
)

8. 组建团队并执行管道

最后,我们将代理和任务组合成一个 Crew,它协调整个多代理工作流程。然后,我们使用示例查询测试系统。

rag_crew = Crew(
    agents=[Router_Agent, Retriever_Agent, Grader_agent, hallucination_grader, answer_grader],
    tasks=[router_task, retriever_task, grader_task, hallucination_task, answer_task],
    verbose=True,
)

inputs = {"question": "Tell me about self-attention mechanism in Transformers?"}
result = rag_crew.kickoff(inputs=inputs)
print(result)
[DEBUG]: == Working Agent: Router
[INFO]: == Starting Task: Analyse the keywords in the question Tell me about self-attention mechanism in Transformers? Based on the keywords decide whether it is eligible for a vectorstore search or a web search. Return a single word 'vectorstore' if it is eligible for vectorstore search. Return a single word 'websearch' if it is eligible for web search. Do not provide any other preamble or explanation.

Entering new CrewAgentExecutor chain…
Thought: The question is about the self-attention mechanism in Transformers, which is a concept related to Retrieval-Augmented Generation.
Action: router_tool
Action Input: {“question”: “Tell me about self-attention mechanism in Transformers?”}

vectorstore

Final Answer: vectorstore

Finished chain.

[DEBUG]: == [Router] Task output: vectorstore
[DEBUG]: == Working Agent: Retriever
[INFO]: == Starting Task: Based on the response from the router task extract information for the question Tell me about self-attention mechanism in Transformers? with the help of the respective tool. Use the web_search_tool to retrieve information from the web in case the router task output is 'websearch'. Use the rag_tool to retrieve information from the vectorstore in case the router task output is 'vectorstore'.

Entering new CrewAgentExecutor chain…
Thought: I now have the context and the task to extract information about the self-attention mechanism in Transformers from the vectorstore. I will use the RAG tool to retrieve the relevant information.

Final Answer:

自注意力机制是 Transformers 中的一个关键组件,允许模型在计算输出时权衡不同输入元素之间的重要性。它基于这样一个理念:不同的输入元素对输出的影响可能不一样,模型应该能够学习关注最重要的元素。

自注意力机制通过对输入元素进行加权求和来实现,权重在训练过程中学习。权重是根据输入元素与一组学习到的查询向量之间的相似性计算的。查询向量在训练过程中学习,用于表示输入元素被处理时的上下文。

自注意力机制在 Transformer 模型的编码器和解码器组件中使用。在编码器中,它用于生成输入序列的表示,捕捉输入元素之间的关系。在解码器中,它用于生成输出序列,每次一个元素,基于输入序列和输出序列中的前一个元素。

自注意力机制相较于传统的递归神经网络 (RNN) 有几个优势。它允许模型处理任意长度的输入序列,并且更具并行性,使训练更快更高效。它还允许模型捕捉输入序列中的长距离依赖关系,这对许多自然语言处理任务至关重要。

总体而言,自注意力机制是 Transformer 模型的一个关键组件,使其能够并行处理输入序列并捕捉长距离依赖关系。这一关键创新使 Transformer 模型在许多自然语言处理任务中取得了最先进的结果。

Finished chain.

[DEBUG]: == [Retriever] Task output: The self-attention mechanism in Transformers is a key component that allows the model to weigh the importance of different input elements relative to each other. It is based on the idea that different input elements may be more or less important for the output, and that the model should be able to learn to focus on the most important elements.

The self-attention mechanism is implemented as a weighted sum of the input elements, where the weights are learned during training. The weights are calculated based on the similarity between the input elements and a set of learned query vectors. The query vectors are learned during training and are used to represent the context in which the input elements are being processed.

The self-attention mechanism is used in the encoder and decoder components of the Transformer model. In the encoder, it is used to generate a representation of the input sequence that captures the relationships between the input elements. In the decoder, it is used to generate the output sequence, one element at a time, based on the input sequence and the previous elements in the output sequence.

The self-attention mechanism has several advantages over traditional recurrent neural networks (RNNs). It allows the model to process input sequences of arbitrary length, and it is more parallelizable, which makes it faster and more efficient to train. It also allows the model to capture long-range dependencies in the input sequence, which is important for many natural language processing tasks.

Overall, the self-attention mechanism is a key component of the Transformer model that allows it to process input sequences in parallel and capture long-range dependencies. It is a key innovation that has enabled the Transformer model to achieve state-of-the-art results in many natural language processing tasks.
[DEBUG]: == Working Agent: Answer Grader
[INFO]: == Starting Task: Based on the response from the retriever task for the question Tell me about self-attention mechanism in Transformers? evaluate whether the retrieved content is relevant to the question.

Entering new CrewAgentExecutor chain…
Thought: I now can give a great answer

Final Answer: yes

Finished chain.

[DEBUG]: == [Answer Grader] Task output: yes
[DEBUG]: == Working Agent: Hallucination Grader
[INFO]: == Starting Task: Based on the response from the grader task for the question Tell me about self-attention mechanism in Transformers? evaluate whether the answer is grounded in / supported by a set of facts.

Entering new CrewAgentExecutor chain…
Thought: I now can give a great answer
Final Answer: yes

Finished chain.

[DEBUG]: == [Hallucination Grader] Task output: yes
[DEBUG]: == Working Agent: Answer Grader
[INFO]: == Starting Task: Based on the response from the hallucination task for the question Tell me about self-attention mechanism in Transformers? evaluate whether the answer is useful to resolve the question. If the answer is 'yes' return a clear and concise answer. If the answer is 'no' then perform a 'websearch' and return the response

Entering new CrewAgentExecutor chain…
Thought: I now can give a great answer

Action: Since the response from the hallucination task is “yes”, I will return a clear and concise answer.

Final Answer: 自注意力机制是 Transformers 中的一个关键组件,允许模型在计算输出时关注输入序列的特定部分。它通过生成输入序列的加权和来实现,其中的权重在训练期间学习,并表示每个输入元素的重要性。这使得模型能够在不同时间关注输入序列的不同部分,这对于机器翻译和文本摘要等任务特别有用。

Finished chain.

[DEBUG]: == [Answer Grader] Task output: The self-attention mechanism in Transformers is a key component that allows the model to focus on specific parts of the input sequence when computing the output. It does this by generating a weighted sum of the input sequence, where the weights are learned during training and represent the importance of each input element. This allows the model to attend to different parts of the input sequence at different times, which is particularly useful for tasks such as machine translation and text summarization.

Transformers中的自注意力机制

Transformers中的自注意力机制是一个关键组件,它允许模型在计算输出时关注输入序列的特定部分。它通过生成输入序列的加权和来实现这一点,其中权重在训练过程中学习,并表示每个输入元素的重要性。这使得模型能够在不同时间关注输入序列的不同部分,这对于机器翻译和文本摘要等任务特别有用。

inputs = {"question": "Tell me about LLMs using web_search?"}
result = rag_crew.kickoff(inputs=inputs)
[DEBUG]: == Working Agent: Router
[INFO]: == Starting Task: Analyse the keywords in the question Tell me about LLMs using web_search? Based on the keywords decide whether it is eligible for a vectorstore search or a web search. Return a single word 'vectorstore' if it is eligible for vectorstore search. Return a single word 'websearch' if it is eligible for web search. Do not provide any other premable or explanation.

Entering new CrewAgentExecutor chain…
Thought: The question is about LLMs, which is a concept related to Retrieval-Augmented Generation, and the user is asking about it using web_search.
Action: router_tool
Action Input: {“question”: “Tell me about LLMs using web_search?”}

web_search

Thought: I now know the final answer
Final Answer: websearch

Finished chain.

[DEBUG]: == [Router] Task output: websearch
[DEBUG]: == Working Agent: Retriever
[INFO]: == Starting Task: Based on the response from the router task extract information for the question Tell me about LLMs using web_search? with the help of the respective tool. Use the web_search_tool to retrieve information from the web in case the router task output is 'websearch'. Use the rag_tool to retrieve information from the vectorstore in case the router task output is 'vectorstore'.

Entering new CrewAgentExecutor chain…
Thought: I now have the router task output as ‘websearch’, which means I need to use the web_search_tool to retrieve information from the web to answer the question “Tell me about LLMs using web_search?”.

Final Answer:

LLMs(大型语言模型)是一种人工智能(AI)模型,经过大量文本数据的训练,以生成类似人类的语言。它们旨在理解和生成自然语言,通常用于语言翻译、文本摘要和聊天机器人等任务。

以下是LLMs的一些关键特性:

  • 它们在大型文本数据集上进行训练,这使它们能够学习语言中的模式和关系。
  • 它们使用一种称为Transformer的神经网络来处理和生成文本。
  • 它们能够生成与人类撰写的文本相似的文本,甚至可以用来生成整篇文章或书籍。
  • 它们通常用于语言翻译、文本摘要和聊天机器人等任务。

以下是LLMs的使用示例:

  • Google的BERT模型是一个用于自然语言处理任务(如语言翻译和文本摘要)的LLM示例。
  • Facebook的语言模型是一个用于聊天机器人和其他自然语言处理任务的LLM示例。
  • LLMs还用于客户服务聊天机器人,可以用于生成对客户询问的响应。

Finished chain.

[DEBUG]: == [Retriever] Task output: LLMs (Large Language Models) are a type of artificial intelligence (AI) model that are trained on large amounts of text data to generate human-like language. They are designed to understand and generate natural language, and are often used for tasks such as language translation, text summarization, and chatbots.

以下是LLMs的一些关键特性:

  • 它们在大型文本数据集上进行训练,这使它们能够学习语言中的模式和关系。
  • 它们使用一种称为Transformer的神经网络来处理和生成文本。
  • 它们能够生成与人类撰写的文本相似的文本,甚至可以用来生成整篇文章或书籍。
  • 它们通常用于语言翻译、文本摘要和聊天机器人等任务。

以下是LLMs的使用示例:

  • Google的BERT模型是一个用于自然语言处理任务(如语言翻译和文本摘要)的LLM示例。
  • Facebook的语言模型是一个用于聊天机器人和其他自然语言处理任务的LLM示例。
  • LLMs还用于客户服务聊天机器人,可以用于生成对客户询问的响应。
[DEBUG]: == Working Agent: Answer Grader
[INFO]: == Starting Task: Based on the response from the retriever task for the question Tell me about LLMs using web_search? evaluate whether the retrieved content is relevant to the question.

Entering new CrewAgentExecutor chain…
Thought: I now have a great understanding of the context and the question “Tell me about LLMs using web_search?” I will carefully evaluate the retrieved content to determine its relevance to the question.

Final Answer: yes

Finished chain.

[DEBUG]: == [Answer Grader] Task output: yes
[DEBUG]: == Working Agent: Hallucination Grader
[INFO]: == Starting Task: Based on the response from the grader task for the question Tell me about LLMs using web_search? evaluate whether the answer is grounded in / supported by a set of facts.

Entering new CrewAgentExecutor chain…
Thought: I will carefully review the response and assess whether it is grounded in/supported by a set of facts related to the question “Tell me about LLMs using web_search?”.

Final Answer: yes

Finished chain.

[DEBUG]: == [Hallucination Grader] Task output: yes
[DEBUG]: == Working Agent: Answer Grader
[INFO]: == Starting Task: Based on the response from the hallucination task for the question Tell me about LLMs using web_search? evaluate whether the answer is useful to resolve the question. If the answer is 'yes' return a clear and concise answer. If the answer is 'no' then perform a 'websearch' and return the response

Entering new CrewAgentExecutor chain…
Thought: I now can give a great answer

Action: Since the response from the hallucination task is “yes”, I will return a clear and concise answer.

Final Answer: 大型语言模型(LLMs)是一种人工智能(AI),旨在处理和生成类似人类的语言。它们在大量文本数据上进行训练,可用于多种任务,如语言翻译、文本摘要和聊天机器人。LLMs是自然语言处理(NLP)发展的关键技术,在客户服务、市场营销和教育等领域具有许多潜在应用。

Finished chain.

[DEBUG]: == [Answer Grader] Task output: Large Language Models (LLMs) are a type of artificial intelligence (AI) designed to process and generate human-like language. They are trained on vast amounts of text data and can be used for a variety of tasks such as language translation, text summarization, and chatbots. LLMs are a key technology in the development of natural language processing (NLP) and have many potential applications in fields such as customer service, marketing, and education.

大型语言模型(LLMs)是一种人工智能(AI),旨在处理和生成类似人类的语言。它们在大量文本数据上进行训练,可用于多种任务,如语言翻译、文本摘要和聊天机器人。LLMs是自然语言处理(NLP)发展的关键技术,在客户服务、市场营销和教育等领域具有许多潜在应用。

kickoff方法通过每个任务处理输入问题:

  1. 路由任务: 决定检索方法。
  2. 检索任务: 从相应工具中获取信息。
  3. 评分和幻觉任务: 验证检索内容的质量和真实性。
  4. 回答任务: 生成最终答案或触发后备网页搜索。

结论

通过整合 Crew AI、Tavily 和先进的检索技术,我们建立了一个强大的代理 RAG pipeline,能够动态选择每个 query 的最佳信息来源。这种多代理架构不仅增强了可靠性和准确性,还提供了一个透明的、模块化的框架,可以根据您的需求不断演变。

无论您是对开发先进的 QA 系统感兴趣,还是想尝试检索增强生成,或探索基于代理的架构的力量,这种方法都为智能信息检索的未来提供了一个引人注目的蓝图。

祝您编码愉快,欢迎来到智能、可适应的 AI pipeline 时代!

关于我 你好!我是 Toni Ramchandani。我对所有与技术相关的事物充满热情!我的旅程是探索广阔而动态的科技世界,从尖端创新到实用的商业解决方案。我相信技术的力量能够改变我们的生活和工作。

让我们在 https://www.linkedin.com/in/toni-ramchandani/ 连接,交流关于最新科技趋势和进展的想法!

Related Posts

结合chatgpt-o3-mini与perplexity Deep Research的3步提示:提升论文写作质量的终极指南

结合chatgpt-o3-mini与perplexity Deep Research的3步提示:提升论文写作质量的终极指南

AI 研究报告和论文写作 合并两个系统指令以获得两个模型的最佳效果 Perplexity AI 的 Deep Research 工具提供专家级的研究报告,而 OpenAI 的 ChatGPT-o3-mini-high 擅长推理。我发现你可以将它们结合起来生成令人难以置信的论文,这些论文比任何一个模型单独撰写的都要好。你只需要将这个一次性提示复制到 **

阅读更多
让 Excel 过时的 10 种 Ai 工具:实现数据分析自动化,节省手工作业时间

让 Excel 过时的 10 种 Ai 工具:实现数据分析自动化,节省手工作业时间

Non members click here作为一名软件开发人员,多年来的一个发现总是让我感到惊讶,那就是人们还在 Excel

阅读更多
使用 ChatGPT 搜索网络功能的 10 种创意方法

使用 ChatGPT 搜索网络功能的 10 种创意方法

例如,提示和输出 你知道可以使用 ChatGPT 的“搜索网络”功能来完成许多任务,而不仅仅是基本的网络搜索吗? 对于那些不知道的人,ChatGPT 新的“搜索网络”功能提供实时信息。 截至撰写此帖时,该功能仅对使用 ChatGPT 4o 和 4o-mini 的付费会员开放。 ![](https://images.weserv.nl/?url=https://cdn-im

阅读更多
掌握Ai代理:解密Google革命性白皮书的10个关键问题解答

掌握Ai代理:解密Google革命性白皮书的10个关键问题解答

10 个常见问题解答 本文是我推出的一个名为“10 个常见问题解答”的新系列的一部分。在本系列中,我旨在通过回答关于该主题的十个最常见问题来分解复杂的概念。我的目标是使用简单的语言和相关的类比,使这些想法易于理解。 图片来自 [Solen Feyissa](https://unsplash.com/@solenfeyissa?utm_source=medium&utm_medi

阅读更多
在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和科技这样一个动态的行业中,保持领先意味着不断提升你的技能。无论你是希望深入了解人工智能模型性能、掌握数据分析,还是希望通过人工智能转变传统领域如法律,这些课程都是你成功的捷径。以下是一个精心策划的高价值课程列表,可以助力你的职业发展,并让你始终处于创新的前沿。 1. 生成性人工智能简介课程: [生成性人工智能简介](https://genai.works

阅读更多
揭开真相!深度探悉DeepSeek AI的十大误区,您被误导了吗?

揭开真相!深度探悉DeepSeek AI的十大误区,您被误导了吗?

在AI军备竞赛中分辨事实与虚构 DeepSeek AI真的是它所宣传的游戏规则改变者,还是仅仅聪明的营销和战略炒作?👀 虽然一些人将其视为AI效率的革命性飞跃,但另一些人则认为它的成功建立在借用(甚至窃取的)创新和可疑的做法之上。传言称,DeepSeek的首席执行官在疫情期间像囤积卫生纸一样囤积Nvidia芯片——这只是冰山一角。 从其声称的550万美元培训预算到使用Open

阅读更多
Type something to search...