Agents for Question Answering¶
Overview¶
This example showcases a hierarchical multi-agent system for question-answering applications. The architecture diagram is shown below. The supervisor agent interfaces with the user and dispatch tasks to the worker agent and other tools to gather information and come up with answers. The worker agent uses the retrieval tool to generate answers to the queries posted by the supervisor agent. Other tools used by the supervisor agent may include APIs to interface knowledge graphs, SQL databases, external knowledge bases, etc.
The AgentQnA example is implemented using the component-level microservices defined in GenAIComps. The flow chart below shows the information flow between different microservices for this example.
Why Agent for question answering?¶
Improve relevancy of retrieved context. Agent can rephrase user queries, decompose user queries, and iterate to get the most relevant context for answering user’s questions. Compared to conventional RAG, RAG agent can significantly improve the correctness and relevancy of the answer.
Use tools to get additional knowledge. For example, knowledge graphs and SQL databases can be exposed as APIs for Agents to gather knowledge that may be missing in the retrieval vector database.
Hierarchical agent can further improve performance. Expert worker agents, such as retrieval agent, knowledge graph agent, SQL agent, etc., can provide high-quality output for different aspects of a complex query, and the supervisor agent can aggregate the information together to provide a comprehensive answer.
Deployment with docker¶
Build agent docker image [Optional]
[!NOTE] the step is optional. The docker images will be automatically pulled when running the docker compose commands. This step is only needed if pulling images failed.
First, clone the opea GenAIComps repo.
export WORKDIR=<your-work-directory>
cd $WORKDIR
git clone https://github.com/opea-project/GenAIComps.git
Then build the agent docker image. Both the supervisor agent and the worker agent will use the same docker image, but when we launch the two agents we will specify different strategies and register different tools.
cd GenAIComps
docker build -t opea/agent-langchain:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/agent/langchain/Dockerfile .
Set up environment for this example
First, clone this repo.
export WORKDIR=<your-work-directory> cd $WORKDIR git clone https://github.com/opea-project/GenAIExamples.git
Second, set up env vars.
# Example: host_ip="192.168.1.1" or export host_ip="External_Public_IP" export host_ip=$(hostname -I | awk '{print $1}') # if you are in a proxy environment, also set the proxy-related environment variables export http_proxy="Your_HTTP_Proxy" export https_proxy="Your_HTTPs_Proxy" # Example: no_proxy="localhost, 127.0.0.1, 192.168.1.1" export no_proxy="Your_No_Proxy" export TOOLSET_PATH=$WORKDIR/GenAIExamples/AgentQnA/tools/ # for using open-source llms export HUGGINGFACEHUB_API_TOKEN=<your-HF-token> export HF_CACHE_DIR=<directory-where-llms-are-downloaded> #so that no need to redownload every time # optional: OPANAI_API_KEY if you want to use OpenAI models export OPENAI_API_KEY=<your-openai-key>
Deploy the retrieval tool (i.e., DocIndexRetriever mega-service)
First, launch the mega-service.
cd $WORKDIR/GenAIExamples/AgentQnA/retrieval_tool bash launch_retrieval_tool.sh
Then, ingest data into the vector database. Here we provide an example. You can ingest your own data.
bash run_ingest_data.sh
Launch other tools. In this example, we will use some of the mock APIs provided in the Meta CRAG KDD Challenge to demonstrate the benefits of gaining additional context from mock knowledge graphs.
docker run -d -p=8080:8000 docker.io/aicrowd/kdd-cup-24-crag-mock-api:v0
Launch agent services We provide two options for
llm_engine
of the agents: 1. open-source LLMs, 2. OpenAI models via API calls.Deploy it on Gaudi or Xeon respectively
To use open-source LLMs on Gaudi2, run commands below.
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/hpu/gaudi bash launch_tgi_gaudi.sh bash launch_agent_service_tgi_gaudi.sh
To use OpenAI models, run commands below.
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/cpu/xeon bash launch_agent_service_openai.sh
Validate services¶
First look at logs of the agent docker containers:
# worker agent
docker logs rag-agent-endpoint
# supervisor agent
docker logs react-agent-endpoint
You should see something like “HTTP server setup successful” if the docker containers are started successfully.
Second, validate worker agent:
curl http://${host_ip}:9095/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
"query": "Most recent album by Taylor Swift"
}'
Third, validate supervisor agent:
curl http://${host_ip}:9090/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
"query": "Most recent album by Taylor Swift"
}'
How to register your own tools with agent¶
You can take a look at the tools yaml and python files in this example. For more details, please refer to the “Provide your own tools” section in the instructions here.