Skip to content

Commit 287f03a

Browse files
minmin-intelpre-commit-ci[bot]chensuyue
authored
Add SQL agent to AgentQnA (#1370)
Signed-off-by: minmin-intel <minmin.hou@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: chen, suyue <suyue.chen@intel.com>
1 parent a65a1e5 commit 287f03a

18 files changed

+478
-77
lines changed

AgentQnA/README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
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.
5+
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 two worker agents to gather information and come up with answers. The worker RAG agent uses the retrieval tool to retrieve relevant documents from the knowledge base (a vector database). The worker SQL agent retrieve relevant data from the SQL database. Although not included in this example, but other tools such as a web search tool or a knowledge graph query tool can be used by the supervisor agent to gather information from additional sources.
66
![Architecture Overview](assets/agent_qna_arch.png)
77

88
The AgentQnA example is implemented using the component-level microservices defined in [GenAIComps](https://github.com/opea-project/GenAIComps). The flow chart below shows the information flow between different microservices for this example.
@@ -38,6 +38,7 @@ flowchart LR
3838
end
3939
AG_REACT([Agent MicroService - react]):::blue
4040
AG_RAG([Agent MicroService - rag]):::blue
41+
AG_SQL([Agent MicroService - sql]):::blue
4142
LLM_gen{{LLM Service <br>}}
4243
DP([Data Preparation MicroService]):::blue
4344
TEI_RER{{Reranking service<br>}}
@@ -51,6 +52,7 @@ flowchart LR
5152
direction LR
5253
a[User Input Query] --> AG_REACT
5354
AG_REACT --> AG_RAG
55+
AG_REACT --> AG_SQL
5456
AG_RAG --> DocIndexRetriever-MegaService
5557
EM ==> RET
5658
RET ==> RER
@@ -59,6 +61,7 @@ flowchart LR
5961
%% Embedding service flow
6062
direction LR
6163
AG_RAG <-.-> LLM_gen
64+
AG_SQL <-.-> LLM_gen
6265
AG_REACT <-.-> LLM_gen
6366
EM <-.-> TEI_EM
6467
RET <-.-> R_RET
@@ -75,11 +78,11 @@ flowchart LR
7578
### Why Agent for question answering?
7679

7780
1. Improve relevancy of retrieved context.
78-
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.
79-
2. Use tools to get additional knowledge.
80-
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.
81-
3. Hierarchical agent can further improve performance.
82-
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.
81+
RAG 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.
82+
2. Expand scope of the agent.
83+
The supervisor agent can interact with multiple worker agents that specialize in different domains with different skills (e.g., retrieve documents, write SQL queries, etc.), and thus can answer questions in multiple domains.
84+
3. Hierarchical multi-agents can improve performance.
85+
Expert worker agents, such as RAG agent and SQL agent, 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. If we only use one agent and provide all the tools to this single agent, it may get overwhelmed and not able to provide accurate answers.
8386

8487
## Deployment with docker
8588

@@ -148,28 +151,55 @@ docker build -t opea/agent:latest --build-arg https_proxy=$https_proxy --build-a
148151
bash run_ingest_data.sh
149152
```
150153

151-
4. Launch other tools. </br>
154+
4. Prepare SQL database
155+
In this example, we will use the Chinook SQLite database. Run the commands below.
156+
157+
```
158+
# Download data
159+
cd $WORKDIR
160+
git clone https://github.com/lerocha/chinook-database.git
161+
cp chinook-database/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite $WORKDIR/GenAIExamples/AgentQnA/tests/
162+
```
163+
164+
5. Launch other tools. </br>
152165
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.
153166

154167
```
155168
docker run -d -p=8080:8000 docker.io/aicrowd/kdd-cup-24-crag-mock-api:v0
156169
```
157170

158-
5. Launch agent services</br>
159-
We provide two options for `llm_engine` of the agents: 1. open-source LLMs, 2. OpenAI models via API calls.
160-
161-
Deploy it on Gaudi or Xeon respectively
171+
6. Launch multi-agent system. </br>
172+
We provide two options for `llm_engine` of the agents: 1. open-source LLMs on Intel Gaudi2, 2. OpenAI models via API calls.
162173

163174
::::{tab-set}
164175
:::{tab-item} Gaudi
165176
:sync: Gaudi
166177

167-
To use open-source LLMs on Gaudi2, run commands below.
178+
On Gaudi2 we will serve `meta-llama/Meta-Llama-3.1-70B-Instruct` using vllm.
179+
180+
First build vllm-gaudi docker image.
181+
182+
```bash
183+
cd $WORKDIR
184+
git clone https://github.com/vllm-project/vllm.git
185+
cd ./vllm
186+
git checkout v0.6.6
187+
docker build --no-cache -f Dockerfile.hpu -t opea/vllm-gaudi:latest --shm-size=128g . --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy
188+
```
189+
190+
Then launch vllm on Gaudi2 with the command below.
168191

192+
```bash
193+
vllm_port=8086
194+
model="meta-llama/Meta-Llama-3.1-70B-Instruct"
195+
docker run -d --runtime=habana --rm --name "vllm-gaudi-server" -e HABANA_VISIBLE_DEVICES=0,1,2,3 -p $vllm_port:8000 -v $vllm_volume:/data -e HF_TOKEN=$HF_TOKEN -e HUGGING_FACE_HUB_TOKEN=$HF_TOKEN -e HF_HOME=/data -e OMPI_MCA_btl_vader_single_copy_mechanism=none -e PT_HPU_ENABLE_LAZY_COLLECTIVES=true -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy -e VLLM_SKIP_WARMUP=true --cap-add=sys_nice --ipc=host opea/vllm-gaudi:latest --model ${model} --max-seq-len-to-capture 16384 --tensor-parallel-size 4
169196
```
170-
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/hpu/gaudi
171-
bash launch_tgi_gaudi.sh
172-
bash launch_agent_service_tgi_gaudi.sh
197+
198+
Then launch Agent microservices.
199+
200+
```bash
201+
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/hpu/gaudi/
202+
bash launch_agent_service_gaudi.sh
173203
```
174204

175205
:::
@@ -179,6 +209,7 @@ docker build -t opea/agent:latest --build-arg https_proxy=$https_proxy --build-a
179209
To use OpenAI models, run commands below.
180210

181211
```
212+
export OPENAI_API_KEY=<your-openai-key>
182213
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/cpu/xeon
183214
bash launch_agent_service_openai.sh
184215
```
@@ -195,8 +226,11 @@ Refer to the [AgentQnA helm chart](./kubernetes/helm/README.md) for instructions
195226
First look at logs of the agent docker containers:
196227

197228
```
198-
# worker agent
229+
# worker RAG agent
199230
docker logs rag-agent-endpoint
231+
232+
# worker SQL agent
233+
docker logs sql-agent-endpoint
200234
```
201235

202236
```
@@ -206,19 +240,27 @@ docker logs react-agent-endpoint
206240

207241
You should see something like "HTTP server setup successful" if the docker containers are started successfully.</p>
208242

209-
Second, validate worker agent:
243+
Second, validate worker RAG agent:
210244

211245
```
212246
curl http://${host_ip}:9095/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
213-
"query": "Most recent album by Taylor Swift"
247+
"messages": "Michael Jackson song Thriller"
248+
}'
249+
```
250+
251+
Third, validate worker SQL agent:
252+
253+
```
254+
curl http://${host_ip}:9096/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
255+
"messages": "How many employees are in the company"
214256
}'
215257
```
216258

217-
Third, validate supervisor agent:
259+
Finally, validate supervisor agent:
218260

219261
```
220262
curl http://${host_ip}:9090/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
221-
"query": "Most recent album by Taylor Swift"
263+
"messages": "How many albums does Iron Maiden have?"
222264
}'
223265
```
224266

AgentQnA/assets/agent_qna_arch.png

138 KB
Loading

AgentQnA/docker_compose/intel/cpu/xeon/README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,33 @@ This example showcases a hierarchical multi-agent system for question-answering
4141
bash run_ingest_data.sh
4242
```
4343

44-
4. Launch Tool service
44+
4. Prepare SQL database
45+
In this example, we will use the SQLite database provided in the [TAG-Bench](https://github.com/TAG-Research/TAG-Bench/tree/main). Run the commands below.
46+
47+
```
48+
# Download data
49+
cd $WORKDIR
50+
git clone https://github.com/TAG-Research/TAG-Bench.git
51+
cd TAG-Bench/setup
52+
chmod +x get_dbs.sh
53+
./get_dbs.sh
54+
```
55+
56+
5. Launch Tool service
4557
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.
4658
```
4759
docker run -d -p=8080:8000 docker.io/aicrowd/kdd-cup-24-crag-mock-api:v0
4860
```
49-
5. Launch `Agent` service
61+
6. Launch multi-agent system
5062

51-
The configurations of the supervisor agent and the worker agent are defined in the docker-compose yaml file. We currently use openAI GPT-4o-mini as LLM, and llama3.1-70B-instruct (served by TGI-Gaudi) in Gaudi example. To use openai llm, run command below.
63+
The configurations of the supervisor agent and the worker agents are defined in the docker-compose yaml file. We currently use openAI GPT-4o-mini as LLM.
5264

5365
```
5466
cd $WORKDIR/GenAIExamples/AgentQnA/docker_compose/intel/cpu/xeon
5567
bash launch_agent_service_openai.sh
5668
```
5769

58-
6. [Optional] Build `Agent` docker image if pulling images failed.
70+
7. [Optional] Build `Agent` docker image if pulling images failed.
5971

6072
```
6173
git clone https://github.com/opea-project/GenAIComps.git
@@ -68,8 +80,11 @@ This example showcases a hierarchical multi-agent system for question-answering
6880
First look at logs of the agent docker containers:
6981

7082
```
71-
# worker agent
83+
# worker RAG agent
7284
docker logs rag-agent-endpoint
85+
86+
# worker SQL agent
87+
docker logs sql-agent-endpoint
7388
```
7489

7590
```
@@ -79,19 +94,27 @@ docker logs react-agent-endpoint
7994

8095
You should see something like "HTTP server setup successful" if the docker containers are started successfully.</p>
8196

82-
Second, validate worker agent:
97+
Second, validate worker RAG agent:
98+
99+
```
100+
curl http://${host_ip}:9095/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
101+
"messages": "Michael Jackson song Thriller"
102+
}'
103+
```
104+
105+
Third, validate worker SQL agent:
83106

84107
```
85108
curl http://${host_ip}:9095/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
86-
"query": "Most recent album by Taylor Swift"
109+
"messages": "How many employees are in the company?"
87110
}'
88111
```
89112

90-
Third, validate supervisor agent:
113+
Finally, validate supervisor agent:
91114

92115
```
93116
curl http://${host_ip}:9090/v1/chat/completions -X POST -H "Content-Type: application/json" -d '{
94-
"query": "Most recent album by Taylor Swift"
117+
"messages": "How many albums does Iron Maiden have?"
95118
}'
96119
```
97120

AgentQnA/docker_compose/intel/cpu/xeon/compose_openai.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ services:
3131
LANGCHAIN_PROJECT: "opea-worker-agent-service"
3232
port: 9095
3333

34+
worker-sql-agent:
35+
image: opea/agent:latest
36+
container_name: sql-agent-endpoint
37+
volumes:
38+
- ${WORKDIR}/TAG-Bench/:/home/user/TAG-Bench # SQL database
39+
ports:
40+
- "9096:9096"
41+
ipc: host
42+
environment:
43+
ip_address: ${ip_address}
44+
strategy: sql_agent
45+
db_name: ${db_name}
46+
db_path: ${db_path}
47+
use_hints: false
48+
hints_file: /home/user/TAG-Bench/${db_name}_hints.csv
49+
recursion_limit: ${recursion_limit_worker}
50+
llm_engine: openai
51+
OPENAI_API_KEY: ${OPENAI_API_KEY}
52+
model: ${model}
53+
temperature: 0
54+
max_new_tokens: ${max_new_tokens}
55+
stream: false
56+
require_human_feedback: false
57+
no_proxy: ${no_proxy}
58+
http_proxy: ${http_proxy}
59+
https_proxy: ${https_proxy}
60+
port: 9096
3461

3562
supervisor-react-agent:
3663
image: opea/agent:latest

AgentQnA/docker_compose/intel/cpu/xeon/launch_agent_service_openai.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export temperature=0
1313
export max_new_tokens=4096
1414
export OPENAI_API_KEY=${OPENAI_API_KEY}
1515
export WORKER_AGENT_URL="http://${ip_address}:9095/v1/chat/completions"
16+
export SQL_AGENT_URL="http://${ip_address}:9096/v1/chat/completions"
1617
export RETRIEVAL_TOOL_URL="http://${ip_address}:8889/v1/retrievaltool"
1718
export CRAG_SERVER=http://${ip_address}:8080
19+
export db_name=california_schools
20+
export db_path="sqlite:////home/user/TAG-Bench/dev_folder/dev_databases/${db_name}/${db_name}.sqlite"
1821

1922
docker compose -f compose_openai.yaml up -d

0 commit comments

Comments
 (0)