首页 > 科技周边 > 人工智能 > 如何使用Haystack Framework构建代理QA抹布系统

如何使用Haystack Framework构建代理QA抹布系统

尊渡假赌尊渡假赌尊渡假赌
发布: 2025-03-03 18:35:10
原创
648 人浏览过

想象您正在建立一个客户支持AI,需要回答有关您的产品的问题。有时,它需要从您的文档中获取信息,而其他时间则需要搜索网络以获取最新更新。代理抹布系统在此类复杂的AI应用程序中派上用场。将他们视为聪明的研究助理,他们不仅知道您的内部文档,而且决定何时搜索网络。在本指南中,我们将使用Haystack Framework进行构建代理QA抹布系统的过程。

学习目标

  • 知道什么是代理LLM,并且了解它与抹布系统有何不同。>
  • 熟悉Agesic LLM应用程序的Haystack框架。
  • >了解从模板提示构建的过程,并学习如何将不同的提示连接在一起。
  • 学习如何在Haystack中使用Chromadb创建嵌入。
  • >
  • 学习如何建立从嵌入到世代的完整本地开发系统。
  • >本文是

> > data Science Blogathon的一部分。 目录的>>什么是代理llm?块

组件

    • 管道
    >连接图形
    • Question-Asswer rag项目,用于高级物理学的
    • >管道
    • >>实现路由器
    >>创建提示模板
  • >实现Query Pipeline
    • 绘制查询管道图形
    • 结论
    • 常见问题问题。
    • 什么是代理LLM?
    • >
    • >代理LLM是一个可以自主做出决定并根据其对任务的理解采取行动的AI系统。与主要产生文本响应的传统LLM不同,代理LLM可以做更多的事情。
    • >它可以思考,计划和行动,以最少的人类投入。它评估其知识,认识到何时需要更多信息或外部工具。
    • >代理LLMS
    不依赖静态数据或索引知识,他们决定要信任哪些来源以及如何收集最佳见解。
  • >
  • 这种类型的系统还可以为作业选择正确的工具。它可以决定何时需要检索文档,运行计算或自动化任务。使它们与众不同的是它可以将复杂问题分解为步骤并独立执行它们的能力,从而使其对于研究,分析和工作流动自动化很有价值。

    rag vs agentic rag

    >传统的抹布系统遵循线性过程。 收到查询时,系统首先标识请求中的密钥元素。然后,它搜索知识库,扫描相关信息,以帮助设计准确的响应。一旦检索了相关信息或数据,系统就会对其进行处理以生成有意义且具有上下文相关的响应。

    您可以通过下图轻松理解这些过程。

    如何使用Haystack Framework构建代理QA抹布系统现在,一个代理抹布系统通过以下方式增强了此过程

    评估查询要求

      在多个知识源之间决定
    • 可能将来自不同来源的信息
    • 组合
    • >就响应策略做出自主决定
    • 提供源代理响应
    • >关键区别在于系统对如何处理查询做出明智决定的能力,而不是遵循固定的检索生成模式。
    • 了解干草框架组件
    Haystack是一个开源框架,用于构建可准备生产的AI,LLM应用程序,RAG管道和搜索系统。它为构建LLM应用程序提供了一个强大而灵活的框架。它使您可以整合来自Huggingface,OpenAI,Cohere,Mistral和Local Ollama等各种平台的模型。您还可以在AWS Sagemaker,Bedrock,Azure和GCP等云服务上部署模型。

    > Haystack提供可靠的文档存储,以进行有效的数据管理。它还配备了一套全面的评估,监视和数据集成工具,可确保您应用程序的所有层次的平稳性能。它还具有强大的社区合作,可以定期从各种服务提供商那里进行新的服务集成。

    您可以使用Haystack构建什么?

    >使用强大的检索和发电技术易于促进数据的抹布。

    > 聊天机器人和代理使用最新的Genai模型,例如GPT-4,Llama3.2,DeepSeek-r1。

    在混合类型(图像,文本,音频和表)知识库上如何使用Haystack Framework构建代理QA抹布系统生成多模式提问系统。

    >从文档或构建知识图中提取信息。

    • > HAYSTACK构建块
    • Haystack有两个主要概念,用于构建功能齐全的Genai LLM系统 - 组件和管道。让我们以日语动漫角色的抹布的简单示例来理解它们。

      组件

      >组件是Haystack的核心构建块。他们可以执行诸如文档存储,文档检索,文本生成和嵌入之类的任务。 Haystack有许多组件,您可以在安装后直接使用,它还提供了通过编写Python类制造自己组件的API。

      有合作伙伴公司和社区的集成集合。>

      >

      >安装库,并设置Ollama

      >

      $ pip install haystack-ai ollama-haystack
      
      # On you system download Ollama and install LLM
      
      ollama pull llama3.2:3b
      
      ollama pull nomic-embed-text
      
      
      # And then start ollama server
      ollama serve
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      导入一些组件

      创建文档和文档存储
      from haystack import Document, Pipeline
      from haystack.components.builders.prompt_builder import PromptBuilder
      from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
      from haystack.document_stores.in_memory import InMemoryDocumentStore
      from haystack_integrations.components.generators.ollama import OllamaGenerator
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      管道
      document_store = InMemoryDocumentStore()
      documents = [
          Document(
              content="Naruto Uzumaki is a ninja from the Hidden Leaf Village and aspires to become Hokage."
          ),
          Document(
              content="Luffy is the captain of the Straw Hat Pirates and dreams of finding the One Piece."
          ),
          Document(
              content="Goku, a Saiyan warrior, has defended Earth from numerous powerful enemies like Frieza and Cell."
          ),
          Document(
              content="Light Yagami finds a mysterious Death Note, which allows him to eliminate people by writing their names."
          ),
          Document(
              content="Levi Ackerman is humanity’s strongest soldier, fighting against the Titans to protect mankind."
          ),
      ]
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      管道是Haystack框架的骨干。它们定义了不同组件之间的数据流。管道本质上是有向的无环图(DAG)。一个具有多个输出的单个组件可以连接到具有多个输入的另一个单个组件。

      >

      您可以通过

      >定义管道

      您可以可视化管道

      pipe = Pipeline()
      
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      pipe.connect("retriever", "prompt_builder.documents")
      pipe.connect("prompt_builder", "llm")
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      管道提供:

      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      模块化工作流程管理

        灵活组件布置
      • >轻松调试和监视
      • 可扩展的处理体系结构
      • >节点
      • 节点是可以在管道中连接的基本处理单元,这些节点是执行特定任务的组件。 来自上述管道的节点的示例

      连接图

      连接图定义了组件如何相互作用。

      从上面的管道中,您可以可视化连接图。
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      >

      这个图形结构:
      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      定义组件之间的数据流

      > 如何使用Haystack Framework构建代理QA抹布系统>管理输入/输出关系

      在可能的情况下启用并行处理

      >

      创建灵活的处理途径。
      • 现在,我们可以使用提示来查询动漫知识库。>
      • 创建一个提示模板
      • 此提示将提供一个从文档库中获取信息的答案。
      • >
      >使用提示和检索器查询

      响应:

      template = """
      Given only the following information, answer the question.
      Ignore your own knowledge.
      
      Context:
      {% for document in documents %}
          {{ document.content }}
      {% endfor %}
      
      Question: {{ query }}?
      """
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      这个抹布对新来者来说很简单,但在概念上很有价值。现在,我们已经了解了大多数Haystack框架的概念,我们可以深入研究我们的主要项目。如果有什么新事物出现,我将在此过程中解释。

      >

      >高中物理学的问答抹布项目

      >我们将为高中生建立一个基于NCERT物理书籍的问题答案抹布。它将通过从NCERT书籍中获取信息来提供查询的答案,如果不存在信息,它将搜索网络以获取该信息。


      local llama3.2:3b或llama3.2:1b

        Chromadb用于嵌入存储
      • 用于本地嵌入
      • 的通用嵌入文本模型
      • duckduckgo搜索网络搜索或tavily搜索(可选)
      • >
      • 我使用一个免费的,完全局部的系统。
      • >
      设置开发人员环境

      我们将设置一个conda env python 3.12

      安装必要的软件包

      $ pip install haystack-ai ollama-haystack
      
      # On you system download Ollama and install LLM
      
      ollama pull llama3.2:3b
      
      ollama pull nomic-embed-text
      
      
      # And then start ollama server
      ollama serve
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      现在创建一个名为

      QAGENT
      from haystack import Document, Pipeline
      from haystack.components.builders.prompt_builder import PromptBuilder
      from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
      from haystack.document_stores.in_memory import InMemoryDocumentStore
      from haystack_integrations.components.generators.ollama import OllamaGenerator
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      的项目目录。

      >您可以为项目或jupyter笔记本使用普通的Python文件,这无关紧要。我将使用一个普通的python文件。

      document_store = InMemoryDocumentStore()
      documents = [
          Document(
              content="Naruto Uzumaki is a ninja from the Hidden Leaf Village and aspires to become Hokage."
          ),
          Document(
              content="Luffy is the captain of the Straw Hat Pirates and dreams of finding the One Piece."
          ),
          Document(
              content="Goku, a Saiyan warrior, has defended Earth from numerous powerful enemies like Frieza and Cell."
          ),
          Document(
              content="Light Yagami finds a mysterious Death Note, which allows him to eliminate people by writing their names."
          ),
          Document(
              content="Levi Ackerman is humanity’s strongest soldier, fighting against the Titans to protect mankind."
          ),
      ]
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      在项目根上创建

      main.py

      文件。

      > 导入必要的库

      >系统软件包

        > Core Haystack组件
      • ChromAdb用于嵌入组件
      • 元素推断的ollama组件
      • >
      • 和用于Web搜索的DuckDuckgo
      pipe = Pipeline()
      
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      pipe.connect("retriever", "prompt_builder.documents")
      pipe.connect("prompt_builder", "llm")
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      创建文档商店
      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      template = """
      Given only the following information, answer the question.
      Ignore your own knowledge.
      
      Context:
      {% for document in documents %}
          {{ document.content }}
      {% endfor %}
      
      Question: {{ query }}?
      """
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      Document store is the most important here we will store our embedding for retrieval, we use

      ChromaDB

      for the embedding store, and as you may see in the earlier example, we use InMemoryDocumentStore for fast retrieval because then our data was tiny but for a robust system of retrieval we don’t rely on the InMemoryStore, it will hog the memory and we will have creat embeddings every time we start系统。

      该解决方案是矢量数据库,例如Pinecode,Weaviate,Postgres Vector DB或Chromadb。我之所以使用chromadb,是因为免费,开源,易于使用且健壮。>

      persist_path
      query = "How Goku eliminate people?"
      response = pipe.run({"prompt_builder": {"query": query}, "retriever": {"query": query}})
      print(response["llm"]["replies"])
      登录后复制
      登录后复制
      是您要存储嵌入的位置。

      pdf文件路径

      >它将从数据文件夹中创建文件列表,该文件由我们的PDF文件组成。>

      文档预处理组件
      $conda create --name agenticlm python=3.12
      
      $conda activate agenticlm
      登录后复制

      >我们将使用Haystack的内置文档预处理器,例如清洁器,分离器和文件转换器,然后使用Writer将数据写入商店。

      清洁器:

      它将清除文档中的额外空间,重复的线条,空线等。

      分离器:>它将以各种方式分开文档,例如单词,句子,para,pages。

      >
      $pip install haystack-ai ollama-haystack pypdf
      
      $pip install chroma-haystack duckduckgo-api-haystack
      登录后复制

      >>文件转换器:它将使用PYPDF将PDF转换为文档。

      $ pip install haystack-ai ollama-haystack
      
      # On you system download Ollama and install LLM
      
      ollama pull llama3.2:3b
      
      ollama pull nomic-embed-text
      
      
      # And then start ollama server
      ollama serve
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      作者:>它将存储文档要存储文档和重复文档的文档,它将与先前的文档覆盖。>

      from haystack import Document, Pipeline
      from haystack.components.builders.prompt_builder import PromptBuilder
      from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
      from haystack.document_stores.in_memory import InMemoryDocumentStore
      from haystack_integrations.components.generators.ollama import OllamaGenerator
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      现在设置文档索引的嵌入式。

      >

      嵌入:nomic嵌入文本>

      >我们将使用somic-embed-text嵌入器,这是非常有效且免费的插入面和ollama。

      > 在运行索引管道之前,请打开终端并在下面键入sumic-embed-text和llama3.2:3b模型从Ollama模型商店

      >通过在您的终端中键入命令
      document_store = InMemoryDocumentStore()
      documents = [
          Document(
              content="Naruto Uzumaki is a ninja from the Hidden Leaf Village and aspires to become Hokage."
          ),
          Document(
              content="Luffy is the captain of the Straw Hat Pirates and dreams of finding the One Piece."
          ),
          Document(
              content="Goku, a Saiyan warrior, has defended Earth from numerous powerful enemies like Frieza and Cell."
          ),
          Document(
              content="Light Yagami finds a mysterious Death Note, which allows him to eliminate people by writing their names."
          ),
          Document(
              content="Levi Ackerman is humanity’s strongest soldier, fighting against the Titans to protect mankind."
          ),
      ]
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      ollama serve

      来启动Ollama 现在嵌入组件

      我们使用

      > ollamadocumentembedder
      pipe = Pipeline()
      
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      pipe.connect("retriever", "prompt_builder.documents")
      pipe.connect("prompt_builder", "llm")
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      组件嵌入文档,但是如果您要嵌入文本字符串,则必须使用

      ollamatextemtembedder。 创建索引管道 就像我们以前的玩具抹布示例一样,我们将首先启动管道类别。>

      现在,我们将一一

      一个添加到管道中的组件

      >在管道中添加组件并不关心订单,因此您可以按任何顺序添加组件。但是连接是重要的。
      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      将组件连接到管道图

      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      >在这里,订单很重要,因为如何连接组件告诉管道数据将如何流过管道。就像,在哪个顺序或购买水管物品的位置都没关系,但是如何将它们放在一起会决定您是否获得水。

      >

      >转换器将PDF转换并发送清洁以进行清洁。然后,清洁工将清洁的文档发送到分离器以进行分解。然后,这些块将传递到嵌入式矢量化,最后嵌入的嵌入将把这些嵌入到作者的存储中。

      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      理解!好的,让我给您索引的视觉图,以便您可以检查数据流。

      绘制索引管道

      是的,您可以轻松地从Haystack管道中创建一个漂亮的美人鱼图。

      索引管道的图

      >
      template = """
      Given only the following information, answer the question.
      Ignore your own knowledge.
      
      Context:
      {% for document in documents %}
          {{ document.content }}
      {% endfor %}
      
      Question: {{ query }}?
      """
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      我假设您现在已经完全掌握了Haystack管道背后的想法。感谢您的水管工。

      实现路由器

      >现在,我们需要创建一个路由器来通过其他路径路由数据。在这种情况下,我们将使用条件路由器,该路由器将在某些条件下完成路由工作。条件路由器将根据组件输出评估条件。它将通过不同的管道分支来指导数据流,从而实现动态决策。它也将具有强大的后备策略。
      $ pip install haystack-ai ollama-haystack
      
      # On you system download Ollama and install LLM
      
      ollama pull llama3.2:3b
      
      ollama pull nomic-embed-text
      
      
      # And then start ollama server
      ollama serve
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      系统从嵌入式商店上下文中获得NO_ANSWER回复时,它将转到Web搜索工具以从Internet收集相关数据。

      >用于网络搜索,我们将使用DuckDuckgo API或Tavely,在这里我使用了DuckDuckgo。

      好的,大多数繁重的举重已经完成。现在,是时候及时工程
      from haystack import Document, Pipeline
      from haystack.components.builders.prompt_builder import PromptBuilder
      from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
      from haystack.document_stores.in_memory import InMemoryDocumentStore
      from haystack_integrations.components.generators.ollama import OllamaGenerator
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      创建提示模板

      我们将使用Haystack提示式布置组件来从模板

      构建提示

      首先,我们将为QA

      创建一个提示

      >它将从文档中获取上下文,并尝试回答问题。但是,如果它在文档中找不到相关上下文,它将回复no_answer。
      document_store = InMemoryDocumentStore()
      documents = [
          Document(
              content="Naruto Uzumaki is a ninja from the Hidden Leaf Village and aspires to become Hokage."
          ),
          Document(
              content="Luffy is the captain of the Straw Hat Pirates and dreams of finding the One Piece."
          ),
          Document(
              content="Goku, a Saiyan warrior, has defended Earth from numerous powerful enemies like Frieza and Cell."
          ),
          Document(
              content="Light Yagami finds a mysterious Death Note, which allows him to eliminate people by writing their names."
          ),
          Document(
              content="Levi Ackerman is humanity’s strongest soldier, fighting against the Titans to protect mankind."
          ),
      ]
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      现在,在从LLM获取NO_ANSWER之后的第二个提示中,系统将使用Web搜索工具从Internet收集上下文。

      > duckduckgo提示模板

      >它将有助于系统进入Web搜索并尝试回答查询。>

      使用Haystack
      pipe = Pipeline()
      
      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      pipe.connect("retriever", "prompt_builder.documents")
      pipe.connect("prompt_builder", "llm")
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      的提示构建器创建提示

      我们将使用HayStack提示Joiner一起加入提示的分支。 >实现查询管道

      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      >查询管道将嵌入嵌入的查询收集上下文资源,并使用LLM或Web搜索工具回答我们的查询。

      它类似于索引管道。

      pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
      pipe.add_component("prompt_builder", PromptBuilder(template=template))
      pipe.add_component(
          "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
      )
      
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      启动管道

      >在查询管道中添加组件

      在这里,对于LLM生成,我们使用ollamagenerator组件使用Llama3.2:3b或1b或您喜欢使用工具调用的任何LLM生成答案。>

      将所有组件连接在一起以进行查询流并回答生成

      image_param = {
          "format": "img",
          "type": "png",
          "theme": "forest",
          "bgColor": "f2f3f4",
      }
      pipe.show(params=image_param)
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      登录后复制
      总结上述连接:>

      发送到回猎犬的查询嵌入的text_embedder的嵌入。
      template = """
      Given only the following information, answer the question.
      Ignore your own knowledge.
      
      Context:
      {% for document in documents %}
          {{ document.content }}
      {% endfor %}
      
      Question: {{ query }}?
      """
      登录后复制
      登录后复制
      登录后复制
      登录后复制

      猎犬将数据发送到提示_builder的文档。

      提示构建器转到提示木器以加入其他提示。

      >

      提示木匠将数据传递给LLM发电。
      query = "How Goku eliminate people?"
      response = pipe.run({"prompt_builder": {"query": query}, "retriever": {"query": query}})
      print(response["llm"]["replies"])
      登录后复制
      登录后复制
      llm的答复转到路由器以检查答复是否具有

      no_answer

      >> no_answer。
        Web搜索将数据发送到Web搜索提示
      1. Web搜索文档将数据发送到Web搜索文档。
      2. Web搜索提示符将数据发送到提示点。
      3. >提示木器将将数据发送到LLM以进行答案。
      4. 为什么不看自己?
      5. >
      6. 绘制查询管道图
      7. QUERY GRAPH
      8. 我知道这是一个巨大的图表,但它会向您显示野兽腹部下发生的事情。

        现在是时候享受我们辛勤工作的果实了。

        创建一个用于易于查询的函数。

        >

        $ pip install haystack-ai ollama-haystack
        
        # On you system download Ollama and install LLM
        
        ollama pull llama3.2:3b
        
        ollama pull nomic-embed-text
        
        
        # And then start ollama server
        ollama serve
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        这是一个简单的简单函数。

        现在运行您的主要脚本以索引NCERT物理书

        >

        >这是一项一次性工作,在索引之后,您必须对此行评论,否则它将开始重新索引书籍。
        from haystack import Document, Pipeline
        from haystack.components.builders.prompt_builder import PromptBuilder
        from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
        from haystack.document_stores.in_memory import InMemoryDocumentStore
        from haystack_integrations.components.generators.ollama import OllamaGenerator
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制

        和文件的底部,我们为查询

        编写驱动程序代码

        关于本书知识的电阻率的

        > MCQ
        document_store = InMemoryDocumentStore()
        documents = [
            Document(
                content="Naruto Uzumaki is a ninja from the Hidden Leaf Village and aspires to become Hokage."
            ),
            Document(
                content="Luffy is the captain of the Straw Hat Pirates and dreams of finding the One Piece."
            ),
            Document(
                content="Goku, a Saiyan warrior, has defended Earth from numerous powerful enemies like Frieza and Cell."
            ),
            Document(
                content="Light Yagami finds a mysterious Death Note, which allows him to eliminate people by writing their names."
            ),
            Document(
                content="Levi Ackerman is humanity’s strongest soldier, fighting against the Titans to protect mankind."
            ),
        ]
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制

        书中不在书中的另一个问题 如何使用Haystack Framework构建代理QA抹布系统

        输出

        pipe = Pipeline()
        
        pipe.add_component("retriever", InMemoryBM25Retriever(document_store=document_store))
        pipe.add_component("prompt_builder", PromptBuilder(template=template))
        pipe.add_component(
            "llm", OllamaGenerator(model="llama3.2:1b", url="http://localhost:11434")
        )
        pipe.connect("retriever", "prompt_builder.documents")
        pipe.connect("prompt_builder", "llm")
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制

        让我们尝试另一个问题。

        如何使用Haystack Framework构建代理QA抹布系统

        image_param = {
            "format": "img",
            "type": "png",
            "theme": "forest",
            "bgColor": "f2f3f4",
        }
        pipe.show(params=image_param)
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        登录后复制
        >所以,它正在工作!我们可以使用更多数据,书籍或PDF来嵌入,这将产生更多的上下文感知答案。此外,诸如GPT-4O,Anthropic的Claude或其他Cloud LLM等LLM会更好地完成工作。

        > 如何使用Haystack Framework构建代理QA抹布系统结论

        >我们的代理抹布系统展示了Haystack框架与组合组件和管道的功能的灵活性和鲁棒性。可以通过部署到Web服务平台,并使用付费更好的LLM(例如OpenAI和Nththththopic)来准备生产。您可以使用简化或基于React的Web Spa构建UI,以获得更好的用户体验。

        >您可以在此处找到本文中使用的所有代码。

        >

        钥匙要点

        与传统的抹布相比,

        代理抹布系统提供了更聪明,更灵活的响应。 Haystack的管道体系结构启用复杂的模块化工作流程。

        路由器在响应生成中启用动态决策。

        >

        连接图提供了灵活且可维护的组件交互。
          >
        • 多个知识来源的集成增强了响应质量。
        • >
        • 本文所示的媒体不归Analytics Vidhya拥有,并由作者的酌情决定
        • 常见问题
        • > Q1。系统如何处理未知查询?当本地知识不足时,系统使用其路由器组件自动返回Web搜索,从而确保全面的覆盖范围。管道架构提供了哪些优点?管道体系结构可实现模块化开发,易于测试和灵活的组件布置,使系统可维护和扩展。 Q3。连接图如何增强系统功能?该连接图可实现复杂的数据流和并行处理,提高系统效率以及在处理不同类型的查询时的灵活性。

          Q4。我可以使用其他LLM API?是的,很容易只为各自的LLM API安装必要的集成包,例如Gemini,Anthropic和Groq,然后将其与API键一起使用。

          >

以上是如何使用Haystack Framework构建代理QA抹布系统的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板