Skip to content

Commit 72f8079

Browse files
authored
Refactor text2sql. (#1304)
Signed-off-by: Yao, Qing <qing.yao@intel.com> Signed-off-by: chensuyue <suyue.chen@intel.com>
1 parent 6169ea4 commit 72f8079

File tree

7 files changed

+56
-66
lines changed

7 files changed

+56
-66
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ First of all, you need to build Docker Images locally. This step can be ignored
1111
```bash
1212
git clone https://github.com/opea-project/GenAIComps.git
1313
cd GenAIComps
14-
docker build --no-cache -t opea/texttosql:comps -f comps/texttosql/langchain/Dockerfile .
14+
docker build --no-cache -t opea/text2sql:comps -f comps/text2sql/src/Dockerfile .
1515

1616
```
1717

@@ -21,13 +21,13 @@ Build the frontend Docker image based on react framework via below command:
2121

2222
```bash
2323
cd GenAIExamples/DBQnA/ui
24-
docker build --no-cache -t opea/texttosql-react-ui:latest -f docker/Dockerfile.react .
24+
docker build --no-cache -t opea/text2sql-react-ui:latest -f docker/Dockerfile.react .
2525

2626
```
2727

2828
Then run the command `docker images`, you will have the following Docker Images:
2929

30-
1. `opea/texttosql:latest`
30+
1. `opea/text2sql:latest`
3131
2. `opea/dbqna-react-ui:latest`
3232

3333
## 🚀 Start Microservices
@@ -62,7 +62,7 @@ export LLM_MODEL_ID="mistralai/Mistral-7B-Instruct-v0.3"
6262
export POSTGRES_USER=postgres
6363
export POSTGRES_PASSWORD=testpwd
6464
export POSTGRES_DB=chinook
65-
export texttosql_port=9090
65+
export text2sql_port=9090
6666
```
6767

6868
Note: Please replace with `your_ip` with your external IP address, do not use localhost.
@@ -90,22 +90,22 @@ We will use [Chinook](https://github.com/lerocha/chinook-database) sample databa
9090

9191
```bash
9292

93-
docker run --name test-texttosql-postgres --ipc=host -e POSTGRES_USER=${POSTGRES_USER} -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} -p 5442:5432 -d -v $WORKPATH/comps/texttosql/langchain/chinook.sql:/docker-entrypoint-initdb.d/chinook.sql postgres:latest
93+
docker run --name test-text2sql-postgres --ipc=host -e POSTGRES_USER=${POSTGRES_USER} -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} -p 5442:5432 -d -v $WORKPATH/comps/text2sql/langchain/chinook.sql:/docker-entrypoint-initdb.d/chinook.sql postgres:latest
9494
```
9595

9696
- Start TGI Service
9797

9898
```bash
9999

100-
docker run -d --name="test-texttosql-tgi-endpoint" --ipc=host -p $TGI_PORT:80 -v ./data:/data --shm-size 1g -e HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN} -e HF_TOKEN=${HF_TOKEN} -e model=${model} ghcr.io/huggingface/text-generation-inference:2.1.0 --model-id $model
100+
docker run -d --name="test-text2sql-tgi-endpoint" --ipc=host -p $TGI_PORT:80 -v ./data:/data --shm-size 1g -e HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN} -e HF_TOKEN=${HF_TOKEN} -e model=${model} ghcr.io/huggingface/text-generation-inference:2.1.0 --model-id $model
101101
```
102102

103103
- Start Text-to-SQL Service
104104

105105
```bash
106106
unset http_proxy
107107

108-
docker run -d --name="test-texttosql-server" --ipc=host -p ${texttosql_port}:8090 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e TGI_LLM_ENDPOINT=$TGI_LLM_ENDPOINT opea/texttosql:latest
108+
docker run -d --name="test-text2sql-server" --ipc=host -p ${text2sql_port}:8090 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e TGI_LLM_ENDPOINT=$TGI_LLM_ENDPOINT opea/text2sql:latest
109109
```
110110

111111
- Start React UI service
@@ -141,7 +141,7 @@ curl --location http://${your_ip}:9090/v1/postgres/health \
141141
#### 3.2.2 Invoke the microservice.
142142

143143
```bash
144-
curl http://${your_ip}:9090/v1/texttosql\
144+
curl http://${your_ip}:9090/v1/text2sql\
145145
-X POST \
146146
-d '{"input_text": "Find the total number of Albums.","conn_str": {"user": "'${POSTGRES_USER}'","password": "'${POSTGRES_PASSWORD}'","host": "'${your_ip}'", "port": "5442", "database": "'${POSTGRES_DB}'"}}' \
147147
-H 'Content-Type: application/json'

DBQnA/docker_compose/intel/cpu/xeon/compose.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ services:
1515
no_proxy: ${no_proxy}
1616
http_proxy: ${http_proxy}
1717
https_proxy: ${https_proxy}
18-
HF_TOKEN: ${HF_TOKEN}
18+
HF_TOKEN: ${HUGGINGFACEHUB_API_TOKEN}
19+
HUGGINGFACEHUB_API_TOKEN: ${HUGGINGFACEHUB_API_TOKEN}
1920
shm_size: 1g
2021
command: --model-id ${LLM_MODEL_ID}
2122

@@ -32,19 +33,19 @@ services:
3233
volumes:
3334
- ./chinook.sql:/docker-entrypoint-initdb.d/chinook.sql
3435

35-
texttosql-service:
36-
image: opea/texttosql:latest
37-
container_name: texttosql-service
36+
text2sql-service:
37+
image: ${REGISTRY:-opea}/text2sql:${TAG:-latest}
38+
container_name: text2sql-service
3839
ports:
39-
- "9090:8090"
40+
- "9090:8080"
4041
environment:
4142
- TGI_LLM_ENDPOINT=${TGI_LLM_ENDPOINT}
4243

4344
dbqna-xeon-react-ui-server:
44-
image: opea/dbqna-react-ui:latest
45+
image: ${REGISTRY:-opea}/text2sql-react-ui:${TAG:-latest}
4546
container_name: dbqna-xeon-react-ui-server
4647
depends_on:
47-
- texttosql-service
48+
- text2sql-service
4849
ports:
4950
- "5174:80"
5051
environment:

DBQnA/docker_image_build/build.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
services:
5-
texttosql:
5+
text2sql:
66
build:
77
context: GenAIComps
8-
dockerfile: comps/texttosql/langchain/Dockerfile
8+
dockerfile: comps/text2sql/src/Dockerfile
99
args:
1010
http_proxy: ${http_proxy}
1111
https_proxy: ${https_proxy}
1212
no_proxy: ${no_proxy}
13-
image: ${REGISTRY:-opea}/texttosql:${TAG:-latest}
14-
texttosql-react-ui:
13+
image: ${REGISTRY:-opea}/text2sql:${TAG:-latest}
14+
text2sql-react-ui:
1515
build:
1616
context: ../ui
1717
dockerfile: ./docker/Dockerfile.react
1818
args:
1919
http_proxy: ${http_proxy}
2020
https_proxy: ${https_proxy}
2121
no_proxy: ${no_proxy}
22-
image: ${REGISTRY:-opea}/texttosql-react-ui:${TAG:-latest}
22+
image: ${REGISTRY:-opea}/text2sql-react-ui:${TAG:-latest}

DBQnA/tests/test_compose_on_xeon.sh

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,54 @@
44

55
set -xe
66

7+
IMAGE_REPO=${IMAGE_REPO:-"opea"}
8+
IMAGE_TAG=${IMAGE_TAG:-"latest"}
9+
echo "REGISTRY=IMAGE_REPO=${IMAGE_REPO}"
10+
echo "TAG=IMAGE_TAG=${IMAGE_TAG}"
11+
export REGISTRY=${IMAGE_REPO}
12+
export TAG=${IMAGE_TAG}
13+
714
WORKPATH=$(dirname "$PWD")
815
LOG_PATH="$WORKPATH/tests"
916
ip_address=$(hostname -I | awk '{print $1}')
1017
tgi_port=8008
11-
tgi_volume=$WORKPATH/data
12-
13-
export model="meta-llama/Meta-Llama-3-8B-Instruct"
14-
export HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN}
15-
export POSTGRES_USER=postgres
16-
export POSTGRES_PASSWORD=testpwd
17-
export POSTGRES_DB=chinook
18-
export TEXTTOSQL_PORT=9090
19-
export TGI_LLM_ENDPOINT="http://${ip_address}:${tgi_port}"
20-
2118

2219
function build_docker_images() {
23-
echo $WORKPATH
24-
OPEAPATH=$(realpath "$WORKPATH/../..")
25-
26-
echo "Building Text to Sql service..."
27-
cd $OPEAPATH
28-
rm -rf GenAIComps/
29-
git clone https://github.com/opea-project/GenAIComps.git
30-
cd $OPEAPATH/GenAIComps
31-
docker build --no-cache -t opea/texttosql:latest -f comps/texttosql/langchain/Dockerfile .
32-
33-
echo "Building React UI service..."
34-
cd $OPEAPATH/GenAIExamples/DBQnA/ui
35-
docker build --no-cache -t opea/dbqna-react-ui:latest -f docker/Dockerfile.react .
20+
cd $WORKPATH/docker_image_build
21+
git clone https://github.com/opea-project/GenAIComps.git && cd GenAIComps && git checkout "${opea_branch:-"main"}" && cd ../
3622

23+
echo "Build all the images with --no-cache, check docker_image_build.log for details..."
24+
docker compose -f build.yaml build --no-cache > ${LOG_PATH}/docker_image_build.log
3725
}
3826

3927
function start_service() {
40-
41-
docker run --name test-texttosql-postgres --ipc=host -e POSTGRES_USER=${POSTGRES_USER} -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} -p 5442:5432 -d -v $WORKPATH/docker_compose/intel/cpu/xeon/chinook.sql:/docker-entrypoint-initdb.d/chinook.sql postgres:latest
42-
43-
docker run -d --name="test-texttosql-tgi-endpoint" --ipc=host -p $tgi_port:80 -v ./data:/data --shm-size 1g -e HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN} -e HF_TOKEN=${HUGGINGFACEHUB_API_TOKEN} -e model=${model} ghcr.io/huggingface/text-generation-inference:2.1.0 --model-id $model
44-
45-
46-
docker run -d --name="test-texttosql-server" --ipc=host -p $TEXTTOSQL_PORT:8090 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e TGI_LLM_ENDPOINT=$TGI_LLM_ENDPOINT opea/texttosql:latest
28+
cd $WORKPATH/docker_compose/intel/cpu/xeon
29+
export model="mistralai/Mistral-7B-Instruct-v0.3"
30+
export LLM_MODEL_ID=${model}
31+
export HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN}
32+
export POSTGRES_USER=postgres
33+
export POSTGRES_PASSWORD=testpwd
34+
export POSTGRES_DB=chinook
35+
export TEXT2SQL_PORT=9090
36+
export TGI_LLM_ENDPOINT="http://${ip_address}:${tgi_port}"
37+
38+
# Start Docker Containers
39+
docker compose -f compose.yaml up -d > ${LOG_PATH}/start_services_with_compose.log
4740

4841
# check whether tgi is fully ready.
4942
n=0
5043
until [[ "$n" -ge 100 ]] || [[ $ready == true ]]; do
51-
docker logs test-texttosql-tgi-endpoint > ${LOG_PATH}/tgi.log
44+
docker logs tgi-service > ${LOG_PATH}/tgi.log
5245
n=$((n+1))
5346
if grep -q Connected ${LOG_PATH}/tgi.log; then
5447
break
5548
fi
5649
sleep 5s
5750
done
58-
sleep 5s
59-
60-
# Run the UI container
61-
docker run -d --name="test-dbqna-react-ui-server" --ipc=host -p 5174:80 -e no_proxy=$no_proxy -e https_proxy=$https_proxy -e http_proxy=$http_proxy opea/dbqna-react-ui:latest
62-
6351
}
6452

6553
function validate_microservice() {
66-
result=$(http_proxy="" curl --connect-timeout 5 --max-time 120000 http://${ip_address}:$TEXTTOSQL_PORT/v1/texttosql\
54+
result=$(http_proxy="" curl --connect-timeout 5 --max-time 120000 http://${ip_address}:$TEXT2SQL_PORT/v1/text2sql\
6755
-X POST \
6856
-d '{"input_text": "Find the total number of Albums.","conn_str": {"user": "'${POSTGRES_USER}'","password": "'${POSTGRES_PASSWORD}'","host": "'${ip_address}'", "port": "5442", "database": "'${POSTGRES_DB}'" }}' \
6957
-H 'Content-Type: application/json')
@@ -73,8 +61,8 @@ function validate_microservice() {
7361
echo "Result correct."
7462
else
7563
echo "Result wrong. Received was $result"
76-
docker logs test-texttosql-server > ${LOG_PATH}/texttosql.log
77-
docker logs test-texttosql-tgi-endpoint > ${LOG_PATH}/tgi.log
64+
docker logs text2sql-service > ${LOG_PATH}/text2sql.log
65+
docker logs tgi-service > ${LOG_PATH}/tgi.log
7866
exit 1
7967
fi
8068

@@ -110,8 +98,8 @@ function validate_frontend() {
11098
}
11199

112100
function stop_docker() {
113-
cid=$(docker ps -aq --filter "name=test-*")
114-
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
101+
cd $WORKPATH/docker_compose/intel/cpu/xeon
102+
docker compose stop && docker compose rm -f
115103
}
116104

117105
function main() {

DBQnA/ui/react/src/App.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const getHostIP = () => {
2121
test('testing api with dynamic host', async () => {
2222
// Get the dynamic host IP
2323
const host = await getHostIP();
24-
const endpointUrl = `http://${host}:9090/v1/texttosql`;
24+
const endpointUrl = `http://${host}:9090/v1/text2sql`;
2525

2626
const formData = {
2727
user: 'postgres',
@@ -43,9 +43,10 @@ test('testing api with dynamic host', async () => {
4343
expect(response.status).toBe(200);
4444

4545
const result = response.data.result;
46+
console.log(result);
4647
expect(result.hasOwnProperty('sql')).toBe(true);
4748
expect(result.hasOwnProperty('output')).toBe(true);
4849
expect(result.hasOwnProperty('input')).toBe(true);
49-
expect(result.input).toBe(question);
50+
expect(result.input.input_text).toBe(question);
5051

5152
}, apiTimeOutInSeconds * 1000);

DBQnA/ui/react/src/components/DbConnect/DBConnect.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const DBConnect: React.FC = () => {
7979
};
8080

8181
let api_response: Record<string, any>;
82-
api_response = await axios.post(`${TEXT_TO_SQL_URL}/texttosql`, payload);
82+
api_response = await axios.post(`${TEXT_TO_SQL_URL}/text2sql`, payload);
8383

8484
setSqlQuery(api_response.data.result.sql); // Assuming the API returns an SQL query
8585
setQueryOutput(api_response.data.result.output);
@@ -163,7 +163,7 @@ const DBConnect: React.FC = () => {
163163
</div>
164164

165165
{/* DBQnA Section */}
166-
<div className={styleClasses.textToSQLSection}>
166+
<div className={styleClasses.text2SQLSection}>
167167
<Title order={1}>DBQnA</Title>
168168
{isConnected && (
169169
<form className={styleClasses.form} onSubmit={handleGenerateSQL}>

DBQnA/ui/react/src/components/DbConnect/dbconnect.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020

2121
.dbConnectSection,
22-
.textToSQLSection {
22+
.text2SQLSection {
2323
flex: 1; /* Allow each section to take up equal space */
2424
padding: 20px;
2525
background-color: white;

0 commit comments

Comments
 (0)