使用HuggingFace Transformers Agent构建Agentic RAG

2024年07月29日 由 alex 发表 153 0

介绍

传统 RAG 设置


8


大型语言模型(LLM)是为执行因果语言建模而训练的,可以处理各种任务,但它们在逻辑、计算和搜索等基本任务上往往表现吃力。在它们表现不佳的领域中进行提示时,它们往往无法生成我们期望的答案。这就是检索增强生成的作用所在。


在传统的 RAG 设置中,用户查询会通过基于语义的相似性搜索来运行,从知识库中寻找最相关的信息块。然后将增强的上下文传递给 LLM,由 LLM 合成响应,并将最终答案呈现给用户。


但是,如果所提问题模棱两可、不甚清晰或提问方式不当,又会发生什么情况呢?在这种情况下,即使信息确实存在于知识库中,RAG 管道也很可能会产生幻觉。


在这种情况下,Agentic RAG 出现了,它能够分析 RAG 管道生成的响应。在这里,Agent 可以进行规划、分析和响应。


什么是 Agentic RAG?

简单地说,Agentic RAG = 基于代理的 RAG 实现。


将 Agentic RAG 视为你的专业研究人员团队,每个人都拥有独特的技能和专业知识,他们通力合作,满足你的信息需求。无论你是需要比较不同文件中的观点、探索特定文本中的细节,还是整合多个摘要中的见解,Agentic RAG 代理都已做好充分准备,能够准确有效地执行任务。


9


Agentic RAG 的主要功能和优势

  • 协调问题解答:Agentic RAG 将问题解答过程分解为易于管理的步骤,从而简化了问题解答过程。它为每项任务分配正确的代理,并确保无缝协调,以达到最佳效果。
  • 目标驱动:这些代理能够理解并追求特定目标,从而实现更复杂、更有意义的互动。
  • 规划与推理: 该框架下的代理拥有先进的规划和多步推理能力。它们能够制定有效的信息检索、分析和综合策略,以解决复杂的问题。
  • 工具使用和适应性: RAG 代理可利用外部工具和资源,如搜索引擎、数据库和专门的应用程序接口,来增强其信息收集和处理能力。
  • 情境感知: RAG 代理系统会考虑当前的情境、过去的互动以及用户的偏好,从而做出明智的决策并采取适当的行动。
  • 随时间学习:这些智能代理的设计目的是随时间学习和发展。当它们遇到新的挑战和信息时,它们的知识库就会不断扩大,从而提高解决复杂问题的能力。
  • 灵活性和定制化: Agentic RAG 框架具有显著的灵活性,允许进行定制,以满足特定的要求和领域。可以对代理及其功能进行定制,以适应特定的任务和信息环境。
  • 提高准确性和效率: 与传统方法相比,Agentic RAG 利用大型语言模型(LLM)和基于代理的系统的优势,提高了问题解答的准确性和效率。
  • 开辟新的可能性: 这项技术为个性化助手、客户服务等各个领域的创新应用铺平了道路。


代理 RAG 与传统 RAG 的区别


10


提供不同的Agentic框架:

  1. LangGraph : LangGraph 是一个使用大型语言模型 (LLM) 构建有状态多角色应用程序的库,用于创建代理和多代理工作流。
  2. AutoGen:AutoGen 是微软的一个开源框架,用于构建使用大型语言模型(LLM)的多代理应用程序。它提供了一个高级抽象概念,便于创建复杂的工作流,多个代理可以在工作流中协作、交流并集体执行任务。
  3. CrewAI : CrewAI 是一个开源框架,用于构建利用大型语言模型(LLM)的多代理系统。它允许开发人员定义和配置多个代理,每个代理都有自己的角色、目标和背景故事,以协作完成复杂的任务。
  4. Transformers Agent:Transformers Agent 是 Hugging Face 公司最近发布的一款工具,可以通过自然语言交互创建能够执行各种任务的代理。它作为一个界面,允许用户利用 Hugging Face 平台上托管的各种工具和模型,促进文本生成、图像创建、音频处理等任务。


在实施过程中,我们使用了 ReAct Agent 和 Transformers Agent。


什么是 ReAct Agent?

ReAct = 使用 LLM 的推理 + 行动


再往上就需要在复杂的查询中反复执行推理和操作。从本质上讲,这包括将路由选择、查询规划和工具使用结合到一个实体中。ReAct 代理能够处理连续的多部分查询,同时保持状态(在内存中)。处理过程包括以下步骤:

  1. 收到用户输入的查询后,代理会根据需要确定要使用的适当工具,并为工具收集必要的输入。
  2. 通过必要的输入调用工具,并存储其输出。
  3. 然后,代理接收工具的历史记录,包括输入和输出,并根据这些信息确定后续的行动方案。
  4. 这一过程反复进行,直到代理完成任务并回复用户。


11


Transformers Agent的主要功能:

  • 自然语言应用程序接口: 用户可以使用自然语言与代理交互,无需大量编程知识即可轻松定义任务。
  • 工具集成: 代理可以从一套精心挑选的工具中进行选择,以完成特定任务,这与 LangChain 代理提供的功能类似。
  • 代理类型: Transformers Agent 包括不同类型的代理,例如
  • 代码代理(CodeAgent): 通过一次性生成和运行代码块来执行任务。
  • ReactAgent: 以循序渐进的方式运行,允许执行更具交互性和迭代性的任务。
  • 多模式功能: 该代理可处理各种模式,使其能够跨文本、图像和音频执行任务。
  • 实验性应用程序接口: 作为一个实验性框架,Transformers Agent 可能会发生变化,其功能也可能随着时间的推移而发展。


应用:

Transformers Agent 专为广泛的自然语言处理任务而设计,包括:


  • 问题解答
  • 文本摘要
  • 图像生成
  • 语音到文本的转换
  • 文本分类


对于希望实现复杂工作流程自动化并通过会话界面增强用户与人工智能交互的开发人员和研究人员来说,该工具尤为重要。


使用的技术栈:

  • Langchain:加载文档并对文档进行分块
  • 转换器:-构建代理系统
  • FAISS:矢量存储
  • Groq:用于函数调用和 LLM 引擎
  • Colab:代码执行(CPU)


代码执行

安装所需的依赖项


!pip install "git+https://github.com/huggingface/transformers.git#egg=transformers[agents]""git+https://github.com/huggingface/transformers.git#egg=transformers[agents]"
!pip install langchain 
!pip install langchain-community 
!pip install sentence-transformers 
!pip install faiss-cpu 
!pip install groq
!pip install -qU langchain-groq
!pip install unstructured
!pip install "unstructured[pdf]"
!pip install -U langchain-huggingface


导入所需的依赖项


import pandas as pd
import datasets
from transformers import AutoTokenizer
from langchain.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores.utils import DistanceStrategy
from tqdm import tqdm
from transformers.agents import Tool, HfEngine, ReactJsonAgent
from huggingface_hub import InferenceClient


加载文件


import os
os.mkdir("DATA")
#https://www.researchgate.net/publication/233850271_Fibromyalgia_Syndrome_Etiology_Pathogenesis_Diagnosis_and_Treatment
#
from langchain_community.document_loaders import DirectoryLoader
loader = DirectoryLoader('/content/DATA', glob="**/*.pdf", show_progress=True)
docs = loader.load()


初始化文本分割器


# Initialize the text splitterInitialize the text splitter
tokenizer = AutoTokenizer.from_pretrained("thenlper/gte-small")
text_splitter = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(
    tokenizer,
    chunk_size=200,
    chunk_overlap=20,
    add_start_index=True,
    strip_whitespace=True,
    separators=["\n\n", "\n", ".", " ", ""],
)


分割文档并删除重复内容


# Split documents and remove duplicatesSplit documents and remove duplicates
logger.info("Splitting documents...")
docs_processed = []
unique_texts = {}
for doc in tqdm(docs):
    new_docs = text_splitter.split_documents([doc])
    for new_doc in new_docs:
        if new_doc.page_content not in unique_texts:
            unique_texts[new_doc.page_content] = True
            docs_processed.append(new_doc)


初始化嵌入模型


from langchain_huggingface import HuggingFaceEmbeddings
model_name = "thenlper/gte-small"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
embedding_model = HuggingFaceEmbeddings(
    model_name=model_name,
    model_kwargs=model_kwargs,
    encode_kwargs=encode_kwargs
)


建立索引并加载文本向量


# Create the vector databaseCreate the vector database
logger.info("Creating vector database...")
vectordb = FAISS.from_documents(
    documents=docs_processed,
    embedding=embedding_model,
    distance_strategy=DistanceStrategy.COSINE,
)


创建检索工具


class RetrieverTool(Tool):
    name = "retriever"
    description = "Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query."
    inputs = {
        "query": {
            "type": "text",
            "description": "The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.",
        }
    }
    output_type = "text"
    def __init__(self, vectordb, **kwargs):
        super().__init__(**kwargs)
        self.vectordb = vectordb
    def forward(self, query: str) -> str:
        assert isinstance(query, str), "Your search query must be a string"
        docs = self.vectordb.similarity_search(
            query,
            k=7,
        )
        return "\nRetrieved documents:\n" + "".join(
            [f"===== Document {str(i)} =====\n" + doc.page_content for i, doc in enumerate(docs)]
        )
# Create an instance of the RetrieverTool
retriever_tool = RetrieverTool(vectordb)


为标准 RAG 初始化 LLM


from langchain_groq import ChatGroq
from google.colab import userdata
import os
os.environ["GROQ_API_KEY"] = userdata.get('GROQ_API_KEY')
#
llm = ChatGroq(
    model="llama3-70b-8192",
    temperature=0,
    max_tokens=2048,
  )


为 Agent 初始化 LLM 引擎


import os
from groq import Groq
from typing import List, Dict
from transformers.agents.llm_engine import MessageRole, get_clean_message_list
from huggingface_hub import InferenceClient
openai_role_conversions = {
    MessageRole.TOOL_RESPONSE: MessageRole.USER,
}

class OpenAIEngine:
    def __init__(self, model_name="llama3-groq-70b-8192-tool-use-preview"):
        self.model_name = model_name
        self.client = Groq(
            api_key=os.getenv("GROQ_API_KEY"),
        )
    def __call__(self, messages, stop_sequences=[]):
        messages = get_clean_message_list(messages, role_conversions=openai_role_conversions)
        response = self.client.chat.completions.create(
            model=self.model_name,
            messages=messages,
            stop=stop_sequences,
            temperature=0.5,
            max_tokens=2048
        )
        return response.choices[0].message.content


llm_engine = OpenAIEngine()OpenAIEngine()


创建代理


# Create the agentCreate the agent
agent = ReactJsonAgent(tools=[retriever_tool], llm_engine=llm_engine, max_iterations=4, verbose=2)


调用代理的辅助函数


# Function to run the agentFunction to run the agent
def run_agentic_rag(question: str) -> str:
    enhanced_question = f"""Using the information contained in your knowledge base, which you can access with the 'retriever' tool,
give a comprehensive answer to the question below.
Respond only to the question asked, response should be concise and relevant to the question.
If you cannot find information, do not give up and try calling your retriever again with different arguments!
Make sure to have covered the question completely by calling the retriever tool several times with semantically different queries.
Your queries should not be questions but affirmative form sentences: e.g. rather than "How do I load a model from the Hub in bf16?", query should be "load a model from the Hub bf16 weights".
Question:
{question}"""
    return agent.run(enhanced_question)


# Example usageExample usage
question = "Describe Fibromyalgia. "
answer = run_agentic_rag(question)
print(f"Question: {question}")
print(f"Answer: {answer}")
##### Response####################
======== New task ========
Using the information contained in your knowledge base, which you can access with the 'retriever' tool,
give a comprehensive answer to the question below.
Respond only to the question asked, response should be concise and relevant to the question.
If you cannot find information, do not give up and try calling your retriever again with different arguments!
Make sure to have covered the question completely by calling the retriever tool several times with semantically different queries.
Your queries should not be questions but affirmative form sentences: e.g. rather than "How do I load a model from the Hub in bf16?", query should be "load a model from the Hub bf16 weights".
Question:
Describe Fibromyalgia. 
System prompt is as follows:
You are an expert assistant who can solve any task using JSON tool calls. You will be given a task to solve as best you can.
To do so, you have been given access to the following tools: 'retriever', 'final_answer'
The way you use the tools is by specifying a json blob, ending with '<end_action>'.
Specifically, this json should have an `action` key (name of the tool to use) and an `action_input` key (input to the tool).
The $ACTION_JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. It should be formatted in json. Do not try to escape special characters. Here is the template of a valid $ACTION_JSON_BLOB:
{
  "action": $TOOL_NAME,
  "action_input": $INPUT
}<end_action>
Make sure to have the $INPUT as a dictionary in the right format for the tool you are using, and do not put variable names as input if you can find the right values.
You should ALWAYS use the following format:
Thought: you should always think about one action to take. Then use the action as follows:
Action:
$ACTION_JSON_BLOB
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $ACTION_JSON_BLOB must only use a SINGLE action at a time.)
You can use the result of the previous action as input for the next action.
The observation will always be a string: it can represent a file, like "image_1.jpg".
Then you can use it as input for the next action. You can do it for instance as follows:
Observation: "image_1.jpg"
Thought: I need to transform the image that I received in the previous observation to make it green.
Action:
{
  "action": "image_transformer",
  "action_input": {"image": "image_1.jpg"}
}<end_action>
To provide the final answer to the task, use an action blob with "action": "final_answer" tool. It is the only way to complete the task, else you will be stuck on a loop. So your final output should look like this:
Action:
{
  "action": "final_answer",
  "action_input": {"answer": "insert your final answer here"}
}<end_action>

Here are a few examples using notional tools:
---
Task: "Generate an image of the oldest person in this document."
Thought: I will proceed step by step and use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer.
Action:
{
  "action": "document_qa",
  "action_input": {"document": "document.pdf", "question": "Who is the oldest person mentioned?"}
}<end_action>
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."

Thought: I will now generate an image showcasing the oldest person.
Action:
{
  "action": "image_generator",
  "action_input": {"text": ""A portrait of John Doe, a 55-year-old man living in Canada.""}
}<end_action>
Observation: "image.png"
Thought: I will now return the generated image.
Action:
{
  "action": "final_answer",
  "action_input": "image.png"
}<end_action>
---
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
Thought: I will use python code evaluator to compute the result of the operation and then return the final answer using the `final_answer` tool
Action:
{
    "action": "python_interpreter",
    "action_input": {"code": "5 + 3 + 1294.678"}
}<end_action>
Observation: 1302.678
Thought: Now that I know the result, I will now return it.
Action:
{
  "action": "final_answer",
  "action_input": "1302.678"
}<end_action>
---
Task: "Which city has the highest population , Guangzhou or Shanghai?"
Thought: I need to get the populations for both cities and compare them: I will use the tool `search` to get the population of both cities.
Action:
{
    "action": "search",
    "action_input": "Population Guangzhou"
}<end_action>
Observation: ['Guangzhou has a population of 15 million inhabitants as of 2021.']

Thought: Now let's get the population of Shanghai using the tool 'search'.
Action:
{
    "action": "search",
    "action_input": "Population Shanghai"
}
Observation: '26 million (2019)'
Thought: Now I know that Shanghai has a larger population. Let's return the result.
Action:
{
  "action": "final_answer",
  "action_input": "Shanghai"
}<end_action>

Above example were using notional tools that might not exist for you. You only have acces to those tools:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
- final_answer: Provides a final answer to the given problem.
    Takes inputs: {'answer': {'type': 'text', 'description': 'The final answer to the problem'}}
Here are the rules you should always follow to solve your task:
1. ALWAYS provide a 'Thought:' sequence, and an 'Action:' sequence that ends with <end_action>, else you will fail.
2. Always use the right arguments for the tools. Never use variable names in the 'action_input' field, use the value instead.
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.
4. Never re-do a tool call that you previously did with the exact same parameters.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.USER: 'user'>, 'content': 'Task: Using the information contained in your knowledge base, which you can access with the \'retriever\' tool,\ngive a comprehensive answer to the question below.\nRespond only to the question asked, response should be concise and relevant to the question.\nIf you cannot find information, do not give up and try calling your retriever again with different arguments!\nMake sure to have covered the question completely by calling the retriever tool several times with semantically different queries.\nYour queries should not be questions but affirmative form sentences: e.g. rather than "How do I load a model from the Hub in bf16?", query should be "load a model from the Hub bf16 weights".\n\nQuestion:\nDescribe Fibromyalgia. '}
===== Output message of the LLM: =====
Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.
===== Extracting action =====
list index out of range
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 471, in extract_action
    split[-2],
IndexError: list index out of range
Error: No 'Action:' token provided in your output.
Your output:
Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.
. Be sure to include an action, prefaced with 'Action:'!
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 471, in extract_action
    split[-2],
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 756, in direct_run
    step_logs = self.step()
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 919, in step
    rationale, action = self.extract_action(llm_output=llm_output, split_token="Action:")
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 476, in extract_action
    raise AgentParsingError(
transformers.agents.agents.AgentParsingError: Error: No 'Action:' token provided in your output.
Your output:
Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.
. Be sure to include an action, prefaced with 'Action:'!
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.TOOL_RESPONSE: 'tool-response'>, 'content': "[OUTPUT OF STEP 0] Error: Error: No 'Action:' token provided in your output.\nYour output:\nFibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.\n. Be sure to include an action, prefaced with 'Action:'!\nNow let's retry: take care not to repeat previous errors! If you have retried several times, try a completely different approach.\n"}
===== Output message of the LLM: =====
Action: {
  "action": "retriever",
  "action_input": {
    "query": "Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies."
  }
}
===== Extracting action =====
Calling tool: 'retriever' with arguments: {'query': 'Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.'}
Retrieved documents:
===== Document 0 =====
Diagnosis is difficult and frequently missed because symptoms are vague and generalized. Despite this, three main symptoms are referred by almost every patient: pain, fatigue, and sleep disturbance [137, 138]. In particular the physician must investigate the features of the pain: it is typically diffuse, multifocal, deep, gnawing, or burning. It often waxes and wanes and is frequently migratory. If this is the case, fibromyalgia should be suspected since often this kind of pain is not the result of inflammation or damage in the area of the region(s) of interest. It is also important to evaluate additional symptoms, which may seem unrelated to fibromyalgia, such as weight fluctuations, morning stiffness, irritable bowel disease, cognitive disturbance, headaches, heat and cold intolerance, irritable bladder syndrome, rest- less legs, and Raynaud’s phenomenon [2].===== Document 1 =====
Fibromyalgia syndrome is mainly characterized by pain, fatigue, and sleep disruption. The etiology of fibromyalgia is still unclear: if central sensitization is considered to be the main mechanism involved, then many other factors, genetic, immunological, and hormonal, may play an important role. The diagnosis is typically clinical (there are no laboratory abnormalities) and the physician must concentrate on pain and on its features. Additional symptoms (e.g., Raynaud’s phenomenon, irritable bowel disease, and heat and cold intolerance) can be associated with this condition. A careful differential diagnosis is mandatory: fibromyalgia is not a diagnosis of exclusion. Since 1990, diagnosis has been principally based on the two major diagnostic criteria defined by the ACR===== Document 2 =====
See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/233850271
Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment
Article in Pain Research and Treatment · November 2012
DOI: 10.1155/2012/426130 · Source: PubMed
CITATIONS 358
READS 19,012
7 authors, including:
Enrico Bellato
Eleonora Marini
Università degli Studi di Torino
Azienda Ospedaliera Istituto Ortopedico Gaetano Pini
64 PUBLICATIONS 1,106 CITATIONS
18 PUBLICATIONS 511 CITATIONS
SEE PROFILE
SEE PROFILE===== Document 3 =====
1. Introduction
Fibromyalgia is a syndrome characterized by chronic wide- spread pain at multiple tender points, joint stiffness, and systemic symptoms (e.g., mood disorders, fatigue, cognitive dysfunction, and insomnia) [1–4] without a well-defined underlying organic disease. Nevertheless, it can be associated with specific diseases such as rheumatic pathologies, psychi- atric or neurological disorders, infections, and diabetes.
What today is defined as fibromyalgia had already been described in the nineteenth century. In 1904, Gowers [5] coined the term “fibrositis” which was used until the seventies and eighties of the last century when an etiology involving the central nervous system was discovered. But it was Graham [6] in 1950 who introduced the modern===== Document 4 =====
2.3. Sleep Disturbances. Patients with fibromyalgia often complain of sleep disorders [83] and these are probably involved in its pathogenesis [1]. As revealed by electroen- cephalographic examinations, the fourth phase of sleep is the most disturbed and a direct consequence should be a deficit of GH and insulin-like growth factor 1 (IGF-1) [84, 85]. Given that these hormones are involved in muscle microtrauma repair, the healing of this tissue could be affected by sleep disturbances [86].===== Document 5 =====
.03; P = 0.03) of the Fibromyalgia Impact Questionnaire (FIQ) and the 3 subscores relating to fatigue (F = 4.8; P = 0.003), stiffness (F = 11.7; P = 0.001), and morning tiredness (F = 7.47; P = 0.009).===== Document 6 =====
4. Treatment
The goals of fibromyalgia treatment are to alleviate pain, increase restorative sleep, and improve physical function through a reduction in associated symptoms [150]. The identification and treatment of all pain sources that may be present in addition to fibromyalgia such as peripheral inflammatory or neuropathic pain generators (e.g., comor- bid osteoarthritis or neuropathic pathologies) or visceral pain (e.g., comorbid irritable bowel syndrome) are central to the proper clinical management of fibromyalgia [151].
Because pain, depression, and other symptoms of fibro- myalgia are linked to inherited and environmental causes, a multifaceted treatment approach is often required including both nonpharmacological pain management strategies and medication [152].
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.TOOL_RESPONSE: 'tool-response'>, 'content': '[OUTPUT OF STEP 1] Observation:\nRetrieved documents:\n===== Document 0 =====\nDiagnosis is difficult and frequently missed because symptoms are vague and generalized. Despite this, three main symptoms are referred by almost every patient: pain, fatigue, and sleep disturbance [137, 138]. In particular the physician must investigate the features of the pain: it is typically diffuse, multifocal, deep, gnawing, or burning. It often waxes and wanes and is frequently migratory. If this is the case, fibromyalgia should be suspected since often this kind of pain is not the result of inflammation or damage in the area of the region(s) of interest. It is also important to evaluate additional symptoms, which may seem unrelated to fibromyalgia, such as weight fluctuations, morning stiffness, irritable bowel disease, cognitive disturbance, headaches, heat and cold intolerance, irritable bladder syndrome, rest- less legs, and Raynaud’s phenomenon [2].===== Document 1 =====\nFibromyalgia syndrome is mainly characterized by pain, fatigue, and sleep disruption. The etiology of fibromyalgia is still unclear: if central sensitization is considered to be the main mechanism involved, then many other factors, genetic, immunological, and hormonal, may play an important role. The diagnosis is typically clinical (there are no laboratory abnormalities) and the physician must concentrate on pain and on its features. Additional symptoms (e.g., Raynaud’s phenomenon, irritable bowel disease, and heat and cold intolerance) can be associated with this condition. A careful differential diagnosis is mandatory: fibromyalgia is not a diagnosis of exclusion. Since 1990, diagnosis has been principally based on the two major diagnostic criteria defined by the ACR===== Document 2 =====\nSee discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/233850271\n\nFibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment\n\nArticle in Pain Research and Treatment · November 2012\n\nDOI: 10.1155/2012/426130 · Source: PubMed\n\nCITATIONS 358\n\nREADS 19,012\n\n7 authors, including:\n\nEnrico Bellato\n\nEleonora Marini\n\nUniversità degli Studi di Torino\n\nAzienda Ospedaliera Istituto Ortopedico Gaetano Pini\n\n64 PUBLICATIONS 1,106 CITATIONS\n\n18 PUBLICATIONS 511 CITATIONS\n\nSEE PROFILE\n\nSEE PROFILE===== Document 3 =====\n1. Introduction\n\nFibromyalgia is a syndrome characterized by chronic wide- spread pain at multiple tender points, joint stiffness, and systemic symptoms (e.g., mood disorders, fatigue, cognitive dysfunction, and insomnia) [1–4] without a well-defined underlying organic disease. Nevertheless, it can be associated with specific diseases such as rheumatic pathologies, psychi- atric or neurological disorders, infections, and diabetes.\n\nWhat today is defined as fibromyalgia had already been described in the nineteenth century. In 1904, Gowers [5] coined the term “fibrositis” which was used until the seventies and eighties of the last century when an etiology involving the central nervous system was discovered. But it was Graham [6] in 1950 who introduced the modern===== Document 4 =====\n2.3. Sleep Disturbances. Patients with fibromyalgia often complain of sleep disorders [83] and these are probably involved in its pathogenesis [1]. As revealed by electroen- cephalographic examinations, the fourth phase of sleep is the most disturbed and a direct consequence should be a deficit of GH and insulin-like growth factor 1 (IGF-1) [84, 85]. Given that these hormones are involved in muscle microtrauma repair, the healing of this tissue could be affected by sleep disturbances [86].===== Document 5 =====\n.03; P = 0.03) of the Fibromyalgia Impact Questionnaire (FIQ) and the 3 subscores relating to fatigue (F = 4.8; P = 0.003), stiffness (F = 11.7; P = 0.001), and morning tiredness (F = 7.47; P = 0.009).===== Document 6 =====\n4. Treatment\n\nThe goals of fibromyalgia treatment are to alleviate pain, increase restorative sleep, and improve physical function through a reduction in associated symptoms [150]. The identification and treatment of all pain sources that may be present in addition to fibromyalgia such as peripheral inflammatory or neuropathic pain generators (e.g., comor- bid osteoarthritis or neuropathic pathologies) or visceral pain (e.g., comorbid irritable bowel syndrome) are central to the proper clinical management of fibromyalgia [151].\n\nBecause pain, depression, and other symptoms of fibro- myalgia are linked to inherited and environmental causes, a multifaceted treatment approach is often required including both nonpharmacological pain management strategies and medication [152].'}
===== Output message of the LLM: =====
Action: {
  "action": "final_answer",
  "action_input": {
    "answer": "Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies."
  }
}
===== Extracting action =====
Calling tool: 'final_answer' with arguments: {'answer': 'Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.'}
Question: Describe Fibromyalgia. 
Answer: Fibromyalgia is a chronic health condition characterized by widespread muscle pain, joint stiffness, and fatigue. The pain associated with fibromyalgia often feels like a dull ache or burning sensation. It is typically felt in multiple areas of the body, and can be continuous or it may come and go. Fibromyalgia can also cause sleep disturbances, memory problems, and mood changes. While the exact cause of fibromyalgia is not known, it is believed to involve a combination of genetic, environmental, and neurobiological factors that affect how the brain processes pain signals. There is no cure for fibromyalgia, but various treatments can help manage the symptoms. These may include medications, lifestyle changes, and alternative therapies.


由 Agentic RAG 生成的答案

纤维肌痛是一种慢性疾病,以广泛的肌肉骨骼疼痛、疲劳和疼痛敏感性增高为特征。它通常伴有睡眠障碍、记忆问题和情绪变化。确切病因尚不清楚,但据信与疼痛处理和应激反应异常有关。目前尚无根治方法,但治疗方案包括药物、改变生活方式和替代疗法,以控制症状和提高生活质量。


代理 RAG 与标准 RAG 的比较


# Standard RAG functionStandard RAG function
def run_standard_rag(question: str) -> str:
    context = retriever_tool(question)
    prompt = f"""Given the question and supporting documents below, give a comprehensive answer to the question.
Respond only to the question asked, response should be concise and relevant to the question.
Provide the number of the source document when relevant.
Question:
{question}
{context}
"""
    messages = [{"role": "human", "content": prompt}]
    response = llm.invoke(messages)
    return response.content


standard_answer = run_standard_rag(question)run_standard_rag(question)
print(standard_answer)
####### RESPONSE ##############
Fibromyalgia is a syndrome characterized by chronic widespread pain at multiple tender points, joint stiffness, and systemic symptoms such as mood disorders, fatigue, cognitive dysfunction, and insomnia, without a well-defined underlying organic disease (Document 0). It is a complex condition that is often difficult to diagnose, particularly for physicians who do not usually deal with this disease (Document 3). The etiology of fibromyalgia is still unclear, but it is thought to involve central sensitization, as well as genetic, immunological, and hormonal factors (Document 6). The diagnosis is typically clinical, with no laboratory abnormalities, and the physician must concentrate on pain and its features, as well as consider additional symptoms such as Raynaud's phenomenon, irritable bowel disease, and heat and cold intolerance (Document 6).


标准 RAG 生成的答案

纤维肌痛是一种综合征,其特征是多个触痛点的慢性广泛性疼痛、关节僵硬以及情绪障碍、疲劳、认知功能障碍和失眠等全身症状,但没有明确的潜在器质性疾病(文件 0)。纤维肌痛是一种复杂的疾病,通常很难诊断,尤其是对于通常不接触这种疾病的医生来说(文献 3)。纤维肌痛的病因尚不清楚,但认为涉及中枢过敏以及遗传、免疫和荷尔蒙因素(文献 6)。诊断通常是临床诊断,没有实验室异常,医生必须专注于疼痛及其特征,并考虑其他症状,如雷诺现象、肠易激综合征、冷热不耐受等(文献 6)。


比较问题 2 的代理 RAG 和标准 RAG


# Compare Agentic RAG and Standard RAGCompare Agentic RAG and Standard RAG
question = "Describe the Etiology and Pathogenesis of fibromyalgia"
agentic_answer = run_agentic_rag(question)
standard_answer = run_standard_rag(question)


print("Agentic RAG Answer:")
print(agentic_answer)


Agentic RAG Answer:
Etiology and Pathogenesis of Fibromyalgia:
Fibromyalgia is a chronic pain disorder characterized by widespread musculoskeletal pain, fatigue, and sleep disturbances. The exact etiology of fibromyalgia is not fully understood, but several factors are thought to contribute to its development.
1. Genetic predisposition: Fibromyalgia tends to run in families, suggesting a genetic component. Certain genetic variants may increase the risk of developing fibromyalgia.
2. Stress and trauma: Stressful life events, such as physical or emotional trauma, can trigger fibromyalgia in some individuals.
3. Infections: Some infections, like Epstein-Barr virus, Lyme disease, and parvovirus, have been linked to the onset of fibromyalgia in some cases.
4. Physical trauma: Physical injuries, especially those affecting the neck and back, can lead to fibromyalgia.
5. Environmental factors: Exposure to toxins, poor sleep quality, and lack of physical activity may also contribute to the development of fibromyalgia.
Pathogenesis:
The pathogenesis of fibromyalgia is complex and not fully understood. However, several mechanisms are thought to play a role:
1. Central sensitization: Fibromyalgia is characterized by increased sensitivity to pain, which is thought to be due to changes in the central nervous system's pain processing mechanisms.
2. Neurotransmitter abnormalities: Imbalances in certain neurotransmitters, such as serotonin and dopamine, which regulate pain and mood, may contribute to fibromyalgia.
3. Sleep disturbances: Chronic sleep deprivation can lead to increased pain sensitivity and contribute to fibromyalgia symptoms.
4. Muscle abnormalities: Some studies suggest that fibromyalgia may be associated with abnormalities in muscle metabolism and function.
5. Stress response: Chronic stress can activate the body's stress response system, leading to increased production of stress hormones like cortisol, which can exacerbate fibromyalgia symptoms.
Overall, fibromyalgia is a complex condition with multiple potential etiological factors and pathogenic mechanisms. Further research is needed to fully understand the underlying causes and develop effective treatments.


print("\nStandard RAG Answer:")
print(standard_answer)


Standard RAG Answer:
The etiology and pathogenesis of fibromyalgia are still not fully understood, but several factors are thought to be involved, including:
* Dysfunction of the central and autonomic nervous systems (Document 0)
* Neurotransmitters, hormones, and immune system dysregulation (Document 0)
* Genetic, immunological, and hormonal factors (Document 2)
* Central sensitization, which is considered the main mechanism involved (Document 2)
* External stressors, psychiatric aspects, and other factors (Document 0)
* Interactions between multiple factors that determine outcome (Document 3)
The exact etiology of fibromyalgia is still unclear, and it is likely that multiple factors contribute to the development of the condition. Further research is needed to explore these interactions and to better understand the etiology and pathogenesis of fibromyalgia.


因此,我们可以得出结论,Agentic RAG 制定的响应更好


直接使用 HuggingFace 变换器初始化 LLM 引擎


!pip install huggingface_hub


from huggingface_hub import notebook_login
notebook_login()


# Initialize the language model engineInitialize the language model engine
hf_llm_engine = HfEngine("meta-llama/Meta-Llama-3-8B-Instruct")
#
# Create the agent
agent = ReactJsonAgent(tools=[retriever_tool], llm_engine=hf_llm_engine, max_iterations=4, verbose=2)
#
agentic_answer_hf = run_agentic_rag(question)


响应


======== New task ========New task ========
Using the information contained in your knowledge base, which you can access with the 'retriever' tool,
give a comprehensive answer to the question below.
Respond only to the question asked, response should be concise and relevant to the question.
If you cannot find information, do not give up and try calling your retriever again with different arguments!
Make sure to have covered the question completely by calling the retriever tool several times with semantically different queries.
Your queries should not be questions but affirmative form sentences: e.g. rather than "How do I load a model from the Hub in bf16?", query should be "load a model from the Hub bf16 weights".
Question:
Describe the Etiology and Pathogenesis of fibromyalgia
System prompt is as follows:
You are an expert assistant who can solve any task using JSON tool calls. You will be given a task to solve as best you can.
To do so, you have been given access to the following tools: 'retriever', 'final_answer'
The way you use the tools is by specifying a json blob, ending with '<end_action>'.
Specifically, this json should have an `action` key (name of the tool to use) and an `action_input` key (input to the tool).
The $ACTION_JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. It should be formatted in json. Do not try to escape special characters. Here is the template of a valid $ACTION_JSON_BLOB:
{
  "action": $TOOL_NAME,
  "action_input": $INPUT
}<end_action>
Make sure to have the $INPUT as a dictionary in the right format for the tool you are using, and do not put variable names as input if you can find the right values.
You should ALWAYS use the following format:
Thought: you should always think about one action to take. Then use the action as follows:
Action:
$ACTION_JSON_BLOB
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $ACTION_JSON_BLOB must only use a SINGLE action at a time.)
You can use the result of the previous action as input for the next action.
The observation will always be a string: it can represent a file, like "image_1.jpg".
Then you can use it as input for the next action. You can do it for instance as follows:
Observation: "image_1.jpg"
Thought: I need to transform the image that I received in the previous observation to make it green.
Action:
{
  "action": "image_transformer",
  "action_input": {"image": "image_1.jpg"}
}<end_action>
To provide the final answer to the task, use an action blob with "action": "final_answer" tool. It is the only way to complete the task, else you will be stuck on a loop. So your final output should look like this:
Action:
{
  "action": "final_answer",
  "action_input": {"answer": "insert your final answer here"}
}<end_action>

Here are a few examples using notional tools:
---
Task: "Generate an image of the oldest person in this document."
Thought: I will proceed step by step and use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer.
Action:
{
  "action": "document_qa",
  "action_input": {"document": "document.pdf", "question": "Who is the oldest person mentioned?"}
}<end_action>
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."

Thought: I will now generate an image showcasing the oldest person.
Action:
{
  "action": "image_generator",
  "action_input": {"text": ""A portrait of John Doe, a 55-year-old man living in Canada.""}
}<end_action>
Observation: "image.png"
Thought: I will now return the generated image.
Action:
{
  "action": "final_answer",
  "action_input": "image.png"
}<end_action>
---
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
Thought: I will use python code evaluator to compute the result of the operation and then return the final answer using the `final_answer` tool
Action:
{
    "action": "python_interpreter",
    "action_input": {"code": "5 + 3 + 1294.678"}
}<end_action>
Observation: 1302.678
Thought: Now that I know the result, I will now return it.
Action:
{
  "action": "final_answer",
  "action_input": "1302.678"
}<end_action>
---
Task: "Which city has the highest population , Guangzhou or Shanghai?"
Thought: I need to get the populations for both cities and compare them: I will use the tool `search` to get the population of both cities.
Action:
{
    "action": "search",
    "action_input": "Population Guangzhou"
}<end_action>
Observation: ['Guangzhou has a population of 15 million inhabitants as of 2021.']

Thought: Now let's get the population of Shanghai using the tool 'search'.
Action:
{
    "action": "search",
    "action_input": "Population Shanghai"
}
Observation: '26 million (2019)'
Thought: Now I know that Shanghai has a larger population. Let's return the result.
Action:
{
  "action": "final_answer",
  "action_input": "Shanghai"
}<end_action>

Above example were using notional tools that might not exist for you. You only have acces to those tools:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
- final_answer: Provides a final answer to the given problem.
    Takes inputs: {'answer': {'type': 'text', 'description': 'The final answer to the problem'}}
Here are the rules you should always follow to solve your task:
1. ALWAYS provide a 'Thought:' sequence, and an 'Action:' sequence that ends with <end_action>, else you will fail.
2. Always use the right arguments for the tools. Never use variable names in the 'action_input' field, use the value instead.
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.
4. Never re-do a tool call that you previously did with the exact same parameters.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.USER: 'user'>, 'content': 'Task: Using the information contained in your knowledge base, which you can access with the \'retriever\' tool,\ngive a comprehensive answer to the question below.\nRespond only to the question asked, response should be concise and relevant to the question.\nIf you cannot find information, do not give up and try calling your retriever again with different arguments!\nMake sure to have covered the question completely by calling the retriever tool several times with semantically different queries.\nYour queries should not be questions but affirmative form sentences: e.g. rather than "How do I load a model from the Hub in bf16?", query should be "load a model from the Hub bf16 weights".\n\nQuestion:\nDescribe the Etiology and Pathogenesis of fibromyalgia'}
===== Output message of the LLM: =====
Thought: To answer the question, I will first use the retriever tool to get related documents from the knowledge base.
Action:
{
  "action": "retriever",
  "action_input": {"query": {"type": "text", "description": "Etiology and pathogenesis of fibromyalgia"}}
}<end_action>
===== Extracting action =====
Calling tool: 'retriever' with arguments: {'query': {'type': 'text', 'description': 'Etiology and pathogenesis of fibromyalgia'}}
Error in tool call execution: Your search query must be a string
You should only use this tool with a correct input.
As a reminder, this tool's description is the following:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 503, in execute_tool_call
    observation = self.toolbox.tools[tool_name](**arguments)
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/tools.py", line 134, in __call__
    outputs = self.forward(*args, **kwargs)
  File "<ipython-input-8-47e29b65c7a0>", line 17, in forward
    assert isinstance(query, str), "Your search query must be a string"
AssertionError: Your search query must be a string
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 756, in direct_run
    step_logs = self.step()
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 946, in step
    observation = self.execute_tool_call(tool_name, arguments)
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 506, in execute_tool_call
    raise AgentExecutionError(
transformers.agents.agents.AgentExecutionError: Error in tool call execution: Your search query must be a string
You should only use this tool with a correct input.
As a reminder, this tool's description is the following:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.TOOL_RESPONSE: 'tool-response'>, 'content': "[OUTPUT OF STEP 0] Error: Error in tool call execution: Your search query must be a string\nYou should only use this tool with a correct input.\nAs a reminder, this tool's description is the following:\n\n- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.\n    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}\nNow let's retry: take care not to repeat previous errors! If you have retried several times, try a completely different approach.\n"}
===== Output message of the LLM: =====
Thought: I will retry the previous action with a correct input format.
Action:
{
  "action": "retriever",
  "action_input": {"query": {"type": "text", "description": "fibromyalgia etiology and pathogenesis"}}
}<end_action>

===== Extracting action =====
Calling tool: 'retriever' with arguments: {'query': {'type': 'text', 'description': 'fibromyalgia etiology and pathogenesis'}}
Error in tool call execution: Your search query must be a string
You should only use this tool with a correct input.
As a reminder, this tool's description is the following:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 503, in execute_tool_call
    observation = self.toolbox.tools[tool_name](**arguments)
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/tools.py", line 134, in __call__
    outputs = self.forward(*args, **kwargs)
  File "<ipython-input-8-47e29b65c7a0>", line 17, in forward
    assert isinstance(query, str), "Your search query must be a string"
AssertionError: Your search query must be a string
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 756, in direct_run
    step_logs = self.step()
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 946, in step
    observation = self.execute_tool_call(tool_name, arguments)
  File "/usr/local/lib/python3.10/dist-packages/transformers/agents/agents.py", line 506, in execute_tool_call
    raise AgentExecutionError(
transformers.agents.agents.AgentExecutionError: Error in tool call execution: Your search query must be a string
You should only use this tool with a correct input.
As a reminder, this tool's description is the following:
- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.
    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.TOOL_RESPONSE: 'tool-response'>, 'content': "[OUTPUT OF STEP 1] Error: Error in tool call execution: Your search query must be a string\nYou should only use this tool with a correct input.\nAs a reminder, this tool's description is the following:\n\n- retriever: Using semantic similarity, retrieves some documents from the knowledge base that have the closest embeddings to the input query.\n    Takes inputs: {'query': {'type': 'text', 'description': 'The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.'}}\nNow let's retry: take care not to repeat previous errors! If you have retried several times, try a completely different approach.\n"}
===== Output message of the LLM: =====
Thought: I will retry the action again, this time making sure to provide a string input.
Action:
{
  "action": "retriever",
  "action_input": {"query": "fibromyalgia etiology and pathogenesis is a complex phenomenon"}
}<end_action>

===== Extracting action =====
Calling tool: 'retriever' with arguments: {'query': 'fibromyalgia etiology and pathogenesis is a complex phenomenon'}
Retrieved documents:
===== Document 0 =====
See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/233850271
Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment
Article in Pain Research and Treatment · November 2012
DOI: 10.1155/2012/426130 · Source: PubMed
CITATIONS 358
READS 19,012
7 authors, including:
Enrico Bellato
Eleonora Marini
Università degli Studi di Torino
Azienda Ospedaliera Istituto Ortopedico Gaetano Pini
64 PUBLICATIONS 1,106 CITATIONS
18 PUBLICATIONS 511 CITATIONS
SEE PROFILE
SEE PROFILE===== Document 1 =====
2. Etiology and Pathogenesis
The etiology and pathogenesis of fibromyalgia are still not fully understood. Several factors such as dysfunction of the central and autonomic nervous systems, neurotransmitters, hormones, immune system, external stressors, psychiatric aspects, and others seem to be involved.===== Document 2 =====
Fibromyalgia syndrome is mainly characterized by pain, fatigue, and sleep disruption. The etiology of fibromyalgia is still unclear: if central sensitization is considered to be the main mechanism involved, then many other factors, genetic, immunological, and hormonal, may play an important role. The diagnosis is typically clinical (there are no laboratory abnormalities) and the physician must concentrate on pain and on its features. Additional symptoms (e.g., Raynaud’s phenomenon, irritable bowel disease, and heat and cold intolerance) can be associated with this condition. A careful differential diagnosis is mandatory: fibromyalgia is not a diagnosis of exclusion. Since 1990, diagnosis has been principally based on the two major diagnostic criteria defined by the ACR===== Document 3 =====
1. Introduction
Fibromyalgia is a syndrome characterized by chronic wide- spread pain at multiple tender points, joint stiffness, and systemic symptoms (e.g., mood disorders, fatigue, cognitive dysfunction, and insomnia) [1–4] without a well-defined underlying organic disease. Nevertheless, it can be associated with specific diseases such as rheumatic pathologies, psychi- atric or neurological disorders, infections, and diabetes.
What today is defined as fibromyalgia had already been described in the nineteenth century. In 1904, Gowers [5] coined the term “fibrositis” which was used until the seventies and eighties of the last century when an etiology involving the central nervous system was discovered. But it was Graham [6] in 1950 who introduced the modern===== Document 4 =====
An implicit aim of the 2010 criteria is to facilitate more rigorous study of fibromyalgia etiology [10]. In common with all complex disorders, the onset of fibromyalgia will be attributable to multiple factors that interact in intricate ways to determine outcome. Studies of etiology need to explore these interactions, although often fail to do so. This problem is neither new nor unique to fibromyalgia research. In epidemiology research, the strongest study design is arguably
Pain Research and Treatment===== Document 5 =====
18 PUBLICATIONS 511 CITATIONS
SEE PROFILE
SEE PROFILE
Filippo Castoldi
Nicola Barbasetti
Università degli Studi di Torino
Università degli Studi di Torino
161 PUBLICATIONS 2,834 CITATIONS
4 PUBLICATIONS 393 CITATIONS
SEE PROFILE
SEE PROFILE
All content following this page was uploaded by Enrico Bellato on 01 June 2014.
The user has requested enhancement of the downloaded file.
Hindawi Publishing Corporation Pain Research and Treatment Volume 2012, Article ID 426130, 17 pages doi:10.1155/2012/426130
Review Article Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment===== Document 6 =====
5. Conclusions
Fibromyalgia is a complex syndrome that is often difficult to diagnose, particularly for physicians who do not usually deal with this disease. Pathogenesis is still not fully clear, but modern functional neuroimaging techniques are giving us important data about the CNS involvement. Fibromyalgia is not to be considered a diagnosis of exclusion: the recently published ACR 2010 criteria try to help us not to be confused by all the differential diagnoses for fibromyalgia. A multidisciplinary approach is optimal and the physician must take into consideration both drugs (in particular antidepressants and neuromodulating antiepileptics) and nonpharmacological treatment, such as aerobic exercise and strength training, aquatic exercises and balneotherapy, cognitive-behavioral therapy, and also the emerging brain stimulation techniques.
References
===== New step =====
===== Calling LLM with this last message: =====
{'role': <MessageRole.TOOL_RESPONSE: 'tool-response'>, 'content': '[OUTPUT OF STEP 2] Observation:\nRetrieved documents:\n===== Document 0 =====\nSee discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/233850271\n\nFibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment\n\nArticle in Pain Research and Treatment · November 2012\n\nDOI: 10.1155/2012/426130 · Source: PubMed\n\nCITATIONS 358\n\nREADS 19,012\n\n7 authors, including:\n\nEnrico Bellato\n\nEleonora Marini\n\nUniversità degli Studi di Torino\n\nAzienda Ospedaliera Istituto Ortopedico Gaetano Pini\n\n64 PUBLICATIONS 1,106 CITATIONS\n\n18 PUBLICATIONS 511 CITATIONS\n\nSEE PROFILE\n\nSEE PROFILE===== Document 1 =====\n2. Etiology and Pathogenesis\n\nThe etiology and pathogenesis of fibromyalgia are still not fully understood. Several factors such as dysfunction of the central and autonomic nervous systems, neurotransmitters, hormones, immune system, external stressors, psychiatric aspects, and others seem to be involved.===== Document 2 =====\nFibromyalgia syndrome is mainly characterized by pain, fatigue, and sleep disruption. The etiology of fibromyalgia is still unclear: if central sensitization is considered to be the main mechanism involved, then many other factors, genetic, immunological, and hormonal, may play an important role. The diagnosis is typically clinical (there are no laboratory abnormalities) and the physician must concentrate on pain and on its features. Additional symptoms (e.g., Raynaud’s phenomenon, irritable bowel disease, and heat and cold intolerance) can be associated with this condition. A careful differential diagnosis is mandatory: fibromyalgia is not a diagnosis of exclusion. Since 1990, diagnosis has been principally based on the two major diagnostic criteria defined by the ACR===== Document 3 =====\n1. Introduction\n\nFibromyalgia is a syndrome characterized by chronic wide- spread pain at multiple tender points, joint stiffness, and systemic symptoms (e.g., mood disorders, fatigue, cognitive dysfunction, and insomnia) [1–4] without a well-defined underlying organic disease. Nevertheless, it can be associated with specific diseases such as rheumatic pathologies, psychi- atric or neurological disorders, infections, and diabetes.\n\nWhat today is defined as fibromyalgia had already been described in the nineteenth century. In 1904, Gowers [5] coined the term “fibrositis” which was used until the seventies and eighties of the last century when an etiology involving the central nervous system was discovered. But it was Graham [6] in 1950 who introduced the modern===== Document 4 =====\nAn implicit aim of the 2010 criteria is to facilitate more rigorous study of fibromyalgia etiology [10]. In common with all complex disorders, the onset of fibromyalgia will be attributable to multiple factors that interact in intricate ways to determine outcome. Studies of etiology need to explore these interactions, although often fail to do so. This problem is neither new nor unique to fibromyalgia research. In epidemiology research, the strongest study design is arguably\n\nPain Research and Treatment===== Document 5 =====\n18 PUBLICATIONS 511 CITATIONS\n\nSEE PROFILE\n\nSEE PROFILE\n\nFilippo Castoldi\n\nNicola Barbasetti\n\nUniversità degli Studi di Torino\n\nUniversità degli Studi di Torino\n\n161 PUBLICATIONS 2,834 CITATIONS\n\n4 PUBLICATIONS 393 CITATIONS\n\nSEE PROFILE\n\nSEE PROFILE\n\nAll content following this page was uploaded by Enrico Bellato on 01 June 2014.\n\nThe user has requested enhancement of the downloaded file.\n\nHindawi Publishing Corporation Pain Research and Treatment Volume 2012, Article ID 426130, 17 pages doi:10.1155/2012/426130\n\nReview Article Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment===== Document 6 =====\n5. Conclusions\n\nFibromyalgia is a complex syndrome that is often difficult to diagnose, particularly for physicians who do not usually deal with this disease. Pathogenesis is still not fully clear, but modern functional neuroimaging techniques are giving us important data about the CNS involvement. Fibromyalgia is not to be considered a diagnosis of exclusion: the recently published ACR 2010 criteria try to help us not to be confused by all the differential diagnoses for fibromyalgia. A multidisciplinary approach is optimal and the physician must take into consideration both drugs (in particular antidepressants and neuromodulating antiepileptics) and nonpharmacological treatment, such as aerobic exercise and strength training, aquatic exercises and balneotherapy, cognitive-behavioral therapy, and also the emerging brain stimulation techniques.\n\nReferences'}
===== Output message of the LLM: =====
Thought: Now that I have retrieved the relevant documents, I will analyze the content to provide a comprehensive answer to the question about the etiology and pathogenesis of fibromyalgia.
Action:
{
  "action": "retriever",
  "action_input": {"query": "Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment"}
}<end_action>
Observation:
===== Extracting action =====
Calling tool: 'retriever' with arguments: {'query': 'Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment'}
Retrieved documents:
===== Document 0 =====
See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/233850271
Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment
Article in Pain Research and Treatment · November 2012
DOI: 10.1155/2012/426130 · Source: PubMed
CITATIONS 358
READS 19,012
7 authors, including:
Enrico Bellato
Eleonora Marini
Università degli Studi di Torino
Azienda Ospedaliera Istituto Ortopedico Gaetano Pini
64 PUBLICATIONS 1,106 CITATIONS
18 PUBLICATIONS 511 CITATIONS
SEE PROFILE
SEE PROFILE===== Document 1 =====
18 PUBLICATIONS 511 CITATIONS
SEE PROFILE
SEE PROFILE
Filippo Castoldi
Nicola Barbasetti
Università degli Studi di Torino
Università degli Studi di Torino
161 PUBLICATIONS 2,834 CITATIONS
4 PUBLICATIONS 393 CITATIONS
SEE PROFILE
SEE PROFILE
All content following this page was uploaded by Enrico Bellato on 01 June 2014.
The user has requested enhancement of the downloaded file.
Hindawi Publishing Corporation Pain Research and Treatment Volume 2012, Article ID 426130, 17 pages doi:10.1155/2012/426130
Review Article Fibromyalgia Syndrome: Etiology, Pathogenesis, Diagnosis, and Treatment===== Document 2 =====
Fibromyalgia syndrome is mainly characterized by pain, fatigue, and sleep disruption. The etiology of fibromyalgia is still unclear: if central sensitization is considered to be the main mechanism involved, then many other factors, genetic, immunological, and hormonal, may play an important role. The diagnosis is typically clinical (there are no laboratory abnormalities) and the physician must concentrate on pain and on its features. Additional symptoms (e.g., Raynaud’s phenomenon, irritable bowel disease, and heat and cold intolerance) can be associated with this condition. A careful differential diagnosis is mandatory: fibromyalgia is not a diagnosis of exclusion. Since 1990, diagnosis has been principally based on the two major diagnostic criteria defined by the ACR===== Document 3 =====
1. Introduction
Fibromyalgia is a syndrome characterized by chronic wide- spread pain at multiple tender points, joint stiffness, and systemic symptoms (e.g., mood disorders, fatigue, cognitive dysfunction, and insomnia) [1–4] without a well-defined underlying organic disease. Nevertheless, it can be associated with specific diseases such as rheumatic pathologies, psychi- atric or neurological disorders, infections, and diabetes.
What today is defined as fibromyalgia had already been described in the nineteenth century. In 1904, Gowers [5] coined the term “fibrositis” which was used until the seventies and eighties of the last century when an etiology involving the central nervous system was discovered. But it was Graham [6] in 1950 who introduced the modern===== Document 4 =====
5. Conclusions
Fibromyalgia is a complex syndrome that is often difficult to diagnose, particularly for physicians who do not usually deal with this disease. Pathogenesis is still not fully clear, but modern functional neuroimaging techniques are giving us important data about the CNS involvement. Fibromyalgia is not to be considered a diagnosis of exclusion: the recently published ACR 2010 criteria try to help us not to be confused by all the differential diagnoses for fibromyalgia. A multidisciplinary approach is optimal and the physician must take into consideration both drugs (in particular antidepressants and neuromodulating antiepileptics) and nonpharmacological treatment, such as aerobic exercise and strength training, aquatic exercises and balneotherapy, cognitive-behavioral therapy, and also the emerging brain stimulation techniques.
References===== Document 5 =====
[2] D. J. Clauw, “Fibromyalgia: an overview,” American Journal of Medicine, vol. 122, no. 12, supplement, pp. S3–S13, 2009. [3] T. Giesecke, D. A. Williams, R. E. Harris et al., “Subgrouping of fibromyalgia patients on the basis of pressure-pain thresh- olds and psychological factors,” Arthritis and Rheumatism, vol. 48, no. 10, pp. 2916–2922, 2003.
[4] P. Mease, “Fibromyalgia syndrome: review of clinical pre- sentation, pathogenesis, outcome measures, and treatment,” Journal of Rheumatology, vol. 75, no. 6, p. 21, 2005.===== Document 6 =====
[145] P. J. Mease, D. J. Clauw, L. M. Arnold et al., “Fibromyalgia syndrome,” Journal of Rheumatology, vol. 32, no. 11, pp. 2270–2277, 2005.
[146] P. J. Mease, D. J. Clauw, R. M. Gendreau et al., “The efficacy and safety of milnacipran for treatment of fibromyalgia. A randomized, double-blind, placebo-controlled trial,” Journal of Rheumatology, vol. 36, no. 2, pp. 398–409, 2009.
Reached max iterations.
NoneType: None
print(agentic_answer_hf)
The etiology and pathogenesis of fibromyalgia are still not fully understood, and it is considered a complex syndrome. Central sensitization is thought to be one of the main mechanisms involved, but other factors such as genetic, immunological, and hormonal factors may also play a role. The pathogenesis of fibromyalgia is characterized by chronic widespread pain, fatigue, and sleep disruption, and may be associated with symptoms such as Raynaud's phenomenon, irritable bowel disease, and heat and cold intolerance. The diagnosis is typically clinical, with no laboratory abnormalities, and a careful differential diagnosis is necessary. The pathogenesis of fibromyalgia is not fully clear, but modern functional neuroimaging techniques are providing important data on the central nervous system involvement. A multidisciplinary approach to diagnosis and treatment is optimal, and includes both pharmacological and non-pharmacological interventions, such as aerobic exercise and strength training, aquatic exercises and balneotherapy, cognitive-behavioral therapy, and brain stimulation techniques.


结论

代理检索-增强生成(RAG)的兴起标志着 RAG 技术的重大发展,超越了传统的问题解答系统。通过整合代理功能,这些智能系统可以对检索到的数据进行推理,执行多步骤操作,并综合各种来源的见解。这种创新方法为先进的研究助手和虚拟工具铺平了道路,它们可以自主地浏览复杂的信息环境。这些自适应系统可根据初步发现动态选择工具和定制响应,从而实现广泛的应用。从增强聊天机器人和虚拟助手到帮助用户进行深入研究,它们都具有巨大的潜力。随着该领域研究的不断深入,我们期待开发出更加复杂的代理,模糊人类和机器智能之间的界限,从而加深对知识的了解和理解。在这项技术的推动下,未来的信息检索和分析将大有可为。

文章来源:https://medium.com/the-ai-forum/build-an-agentic-rag-using-huggingface-transformer-agent-ec741f09ddcc
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
写评论取消
回复取消