想象您正在建立一个客户支持AI,需要回答有关您的产品的问题。有时,它需要从您的文档中获取信息,而其他时间则需要搜索网络以获取最新更新。代理抹布系统在此类复杂的AI应用程序中派上用场。将他们视为聪明的研究助理,他们不仅知道您的内部文档,而且决定何时搜索网络。在本指南中,我们将使用Haystack Framework进行构建代理QA抹布系统的过程。
> > data Science Blogathon的一部分。 目录的>>什么是代理llm?块
>传统的抹布系统遵循线性过程。 收到查询时,系统首先标识请求中的密钥元素。然后,它搜索知识库,扫描相关信息,以帮助设计准确的响应。一旦检索了相关信息或数据,系统就会对其进行处理以生成有意义且具有上下文相关的响应。
您可以通过下图轻松理解这些过程。
现在,一个代理抹布系统通过以下方式增强了此过程
评估查询要求
您可以使用Haystack构建什么?
>使用强大的检索和发电技术易于促进数据的抹布。
>
在混合类型(图像,文本,音频和表)知识库上生成多模式提问系统。
>组件是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)
定义组件之间的数据流
>
>管理输入/输出关系
>
创建灵活的处理途径。
响应:
template = """ Given only the following information, answer the question. Ignore your own knowledge. Context: {% for document in documents %} {{ document.content }} {% endfor %} Question: {{ query }}? """
这个抹布对新来者来说很简单,但在概念上很有价值。现在,我们已经了解了大多数Haystack框架的概念,我们可以深入研究我们的主要项目。如果有什么新事物出现,我将在此过程中解释。
>
local llama3.2:3b或llama3.2:1b
我们将设置一个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
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
文件。> 导入必要的库
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
该解决方案是矢量数据库,例如Pinecode,Weaviate,Postgres Vector DB或Chromadb。我之所以使用chromadb,是因为免费,开源,易于使用且健壮。>
persist_pathquery = "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转换为文档。 作者:>它将存储文档要存储文档和重复文档的文档,它将与先前的文档覆盖。
嵌入:nomic嵌入文本>
>
在运行索引管道之前,请打开终端并在下面键入sumic-embed-text和llama3.2:3b模型从Ollama模型商店
来启动Ollama
现在嵌入组件
我们使用 ollamatextemtembedder。
创建索引管道
就像我们以前的玩具抹布示例一样,我们将首先启动管道类别。>
将组件连接到管道图 >在这里,订单很重要,因为如何连接组件告诉管道数据将如何流过管道。就像,在哪个顺序或购买水管物品的位置都没关系,但是如何将它们放在一起会决定您是否获得水。 绘制索引管道
实现路由器
系统从嵌入式商店上下文中获得NO_ANSWER回复时,它将转到Web搜索工具以从Internet收集相关数据。 >用于网络搜索,我们将使用DuckDuckgo API或Tavely,在这里我使用了DuckDuckgo。
创建提示模板 首先,我们将为QA
>它将有助于系统进入Web搜索并尝试回答查询。 的提示构建器创建提示
我们将使用HayStack提示Joiner一起加入提示的分支。
>实现查询管道 它类似于索引管道。 >在查询管道中添加组件
在这里,对于LLM生成,我们使用ollamagenerator组件使用Llama3.2:3b或1b或您喜欢使用工具调用的任何LLM生成答案。
猎犬将数据发送到提示_builder的文档。
>
no_answer 我知道这是一个巨大的图表,但它会向您显示野兽腹部下发生的事情。 现在是时候享受我们辛勤工作的果实了。
>
现在运行您的主要脚本以索引NCERT物理书
和文件的底部,我们为查询
关于本书知识的电阻率的 书中不在书中的另一个问题
输出
>
>我们的代理抹布系统展示了Haystack框架与组合组件和管道的功能的灵活性和鲁棒性。可以通过部署到Web服务平台,并使用付费更好的LLM(例如OpenAI和Nththththopic)来准备生产。您可以使用简化或基于React的Web Spa构建UI,以获得更好的用户体验。
代理抹布系统提供了更聪明,更灵活的响应。
Haystack的管道体系结构启用复杂的模块化工作流程。 Q4。我可以使用其他LLM API?是的,很容易只为各自的LLM API安装必要的集成包,例如Gemini,Anthropic和Groq,然后将其与API键一起使用。$ 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."
),
]
ollama serve 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")
)
>转换器将PDF转换并发送清洁以进行清洁。然后,清洁工将清洁的文档发送到分离器以进行分解。然后,这些块将传递到嵌入式矢量化,最后嵌入的嵌入将把这些嵌入到作者的存储中。
。
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 }}?
"""
$ 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
我们将使用Haystack提示式布置组件来从模板
构建提示
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提示模板
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)
>查询管道将嵌入嵌入的查询收集上下文资源,并使用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")
)
启动管道
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 }}?
"""
query = "How Goku eliminate people?"
response = pipe.run({"prompt_builder": {"query": query}, "retriever": {"query": query}})
print(response["llm"]["replies"])
llm的答复转到路由器以检查答复是否具有 Web搜索将数据发送到Web搜索提示
$ 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."
),
]
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)
>所以,它正在工作!我们可以使用更多数据,书籍或PDF来嵌入,这将产生更多的上下文感知答案。此外,诸如GPT-4O,Anthropic的Claude或其他Cloud LLM等LLM会更好地完成工作。结论
>
钥匙要点>
连接图提供了灵活且可维护的组件交互。>
以上是如何使用Haystack Framework构建代理QA抹布系统的详细内容。更多信息请关注PHP中文网其他相关文章!