You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Updates chatbot-rag-app and simplifies instructions (#362)
* Updates chatbot-rag-app and simplifies instructions
This updates chatbot-rag-app to the latest libraries and simplifies
instructions by delegating details of configuration to the .env file.
This also works around problems we found in DEBUG mode, by using
production mode regardless of task. Doing so notably will allow
tracing to work in the future.
Efforts here were thanks to many, despite me raising it, notably
@joemcelroy@ezimuel@xrmx and @anuraaga
Signed-off-by: Adrian Cole <[email protected]>
* precommit
Signed-off-by: Adrian Cole <[email protected]>
* feedback
Signed-off-by: Adrian Cole <[email protected]>
* addresses feedback and cleans up azure
Signed-off-by: Adrian Cole <[email protected]>
* RIP rip
Signed-off-by: Adrian Cole <[email protected]>
---------
Signed-off-by: Adrian Cole <[email protected]>
tar -xz --strip=2 elasticsearch-labs-main/example-apps/chatbot-rag-app
16
16
```
17
17
18
-
## Installing and connecting to Elasticsearch
19
-
20
-
### Install Elasticsearch
21
-
22
-
There are a number of ways to install Elasticsearch. Cloud is best for most use-cases. Visit the [Install Elasticsearch](https://www.elastic.co/search-labs/tutorials/install-elasticsearch) for more information.
23
-
24
-
### Connect to Elasticsearch
25
-
26
-
This app requires the following environment variables to be set to connect to Elasticsearch hosted on Elastic Cloud:
27
-
28
-
```sh
29
-
export ELASTIC_CLOUD_ID=...
30
-
export ELASTIC_API_KEY=...
31
-
```
32
-
33
-
You can add these to a `.env` file for convenience. See the `env.example` file for a .env file template.
18
+
## Make your .env file
34
19
35
-
#### Self-Hosted Elasticsearch
20
+
Copy [env.example](env.example) to `.env` and fill in values noted inside.
36
21
37
-
You can also connect to a self-hosted Elasticsearch instance. To do so, you will need to set the following environment variables:
22
+
## Installing and connecting to Elasticsearch
38
23
39
-
```sh
40
-
export ELASTICSEARCH_URL=...
41
-
```
24
+
There are a number of ways to install Elasticsearch. Cloud is best for most
25
+
use-cases. Visit the [Install Elasticsearch](https://www.elastic.co/search-labs/tutorials/install-elasticsearch) for more information.
42
26
43
-
### Change the Elasticsearch index and chat_history index
27
+
Once you decided your approach, edit your `.env` file accordingly.
44
28
45
-
By default, the app will use the `workplace-app-docs`index and the chat history index will be `workplace-app-docs-chat-history`. If you want to change these, you can set the following environment variables:
To use Vertex AI you need to set the following environment variables. More information [here](https://python.langchain.com/docs/integrations/llms/google_vertex_ai_palm).
112
-
113
-
```sh
114
-
export LLM_TYPE=vertex
115
-
export VERTEX_PROJECT_ID=<gcp-project-id>
116
-
export VERTEX_REGION=<gcp-region># Default is us-central1
To use Mistral AI you need to set the following environment variables. The app has been tested with Mistral Large Model deployed through Microsoft Azure. More information [here](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/deploy-models-mistral).
123
-
124
-
```
125
-
export LLM_TYPE=mistral
126
-
export MISTRAL_API_KEY=...
127
-
export MISTRAL_API_ENDPOINT=... # should be of the form https://<endpoint>.<region>.inference.ai.azure.com
128
-
export MISTRAL_MODEL=... # optional
129
-
```
130
-
131
-
### Cohere
37
+
We support several LLM providers, but only one is used at runtime, and selected
38
+
by the `LLM_TYPE` entry in your `.env` file. Edit that file to choose an LLM,
39
+
and configure its templated connection settings:
132
40
133
-
To use Cohere you need to set the following environment variables:
Once you have indexed data into the Elasticsearch index, there are two ways to run the app: via Docker or locally. Docker is advised for testing & production use. Locally is advised for development.
144
-
145
-
### Through Docker
146
-
147
-
Build the Docker image and run it with the following environment variables.
148
-
149
-
```sh
150
-
docker build -f Dockerfile -t chatbot-rag-app .
151
-
```
152
-
153
-
#### Ingest data
154
-
155
-
Make sure you have a `.env` file with all your variables, then run:
156
-
157
-
```sh
158
-
docker run --rm --env-file .env chatbot-rag-app flask create-index
159
-
```
51
+
There are two ways to run the app: via Docker or locally. Docker is advised for
52
+
ease while locally is advised if you are making changes to the application.
160
53
161
-
See "Ingest data" section under Running Locally for more details about the `flask create-index` command.
54
+
### Run with docker
162
55
163
-
#### Run API and frontend
56
+
Docker compose is the easiest way, as you get one-step to:
57
+
* build the [frontend](frontend)
58
+
* ingest data into elasticsearch
59
+
* run the app, which listens on http://localhost:4000
164
60
165
-
You will need to set the appropriate environment variables in your `.env` file. See the `env.example` file for instructions.
61
+
**Double-check you have a `.env` file with all your variables set first!**
166
62
167
-
```sh
168
-
docker run --rm -p 4000:4000 --env-file .env -d chatbot-rag-app
63
+
```bash
64
+
docker compose up --build --force-recreate
169
65
```
170
66
171
-
Note that if you are using an LLM that requires an external credentials file (such as Vertex AI), you will need to make this file accessible to the container in the `run` command above. For this you can use a bind mount, or you can also edit the Dockerfile to copy the credentials file to the container image at build time.
67
+
*Note*: First time creating the index can fail on timeout. Wait a few minutes
68
+
and retry.
172
69
173
-
### Locally (for development)
70
+
### Run locally
174
71
175
-
With the environment variables set, you can run the following commands to start the server and frontend.
72
+
If you want to run this example with Python and Node.js, you need to do a few
73
+
things listed in the [Dockerfile](Dockerfile). The below uses the same
74
+
production mode as used in Docker to avoid problems in debug mode.
176
75
177
-
#### Pre-requisites
76
+
**Double-check you have a `.env` file with all your variables set first!**
178
77
179
-
- Python 3.8+
180
-
- Node 14+
78
+
#### Build the frontend
181
79
182
-
#### Install the dependencies
80
+
The web assets are in the [frontend](frontend) directory, and built with yarn.
183
81
184
-
For Python we recommend using a virtual environment.
82
+
```bash
83
+
# Install and use a recent node, if you don't have one.
_ℹ️ Here's a good [primer](https://realpython.com/python-virtual-environments-a-primer) on virtual environments from Real Python._
90
+
#### Configure your python environment
187
91
188
-
```sh
189
-
# Create a virtual environment
190
-
python -m venv .venv
92
+
Before we can run the app, we need a working Python environment with the
93
+
correct packages installed:
191
94
192
-
# Activate the virtual environment
95
+
```bash
96
+
python3 -m venv .venv
193
97
source .venv/bin/activate
194
-
195
-
# Install Python dependencies
98
+
# install dev requirements for pip-compile and dotenv
99
+
pip install pip-tools "python-dotenv[cli]"
100
+
pip-compile
196
101
pip install -r requirements.txt
197
-
198
-
# Install Node dependencies
199
-
cd frontend && yarn &&cd ..
200
102
```
201
103
202
-
#### Ingest data
203
-
204
-
You can index the sample data from the provided .json files in the `data` folder:
104
+
#### Run the ingest command
205
105
206
-
```sh
207
-
flask create-index
106
+
First, ingest the data into elasticsearch:
107
+
```bash
108
+
$ dotenv run -- flask create-index
109
+
".elser_model_2" model not available, downloading it now
110
+
Model downloaded, starting deployment
111
+
Loading data from ./data/data.json
112
+
Loaded 15 documents
113
+
Split 15 documents into 26 chunks
114
+
Creating Elasticsearch sparse vector store in http://localhost:9200
208
115
```
209
116
210
-
By default, this will index the data into the `workplace-app-docs` index. You can change this by setting the `ES_INDEX` environment variable.
117
+
*Note*: First time creating the index can fail on timeout. Wait a few minutes
118
+
and retry.
211
119
212
-
##### Indexing your own data
120
+
####Run the app
213
121
214
-
The ingesting logic is stored in `data/index-data.py`. This is a simple script that uses Langchain to index data into Elasticsearch, using the `JSONLoader` and `CharacterTextSplitter` to split the large documents into passages. Modify this script to index your own data.
122
+
Now, run the app, which listens on http://localhost:4000
123
+
```bash
124
+
$ dotenv run -- flask run
125
+
* Serving Flask app 'api/app.py'
126
+
* Debug mode: off
127
+
```
215
128
216
-
Langchain offers many different ways to index data, if you cant just load it via JSONLoader. See the [Langchain documentation](https://python.langchain.com/docs/modules/data_connection/document_loaders)
129
+
## Customizing the app
217
130
218
-
Remember to keep the `ES_INDEX` environment variable set to the index you want to index into and to query from.
131
+
### Indexing your own data
219
132
220
-
#### Run API and frontend
133
+
The ingesting logic is stored in [data/index_data.py](data/index_data.py). This
134
+
is a simple script that uses Langchain to index data into Elasticsearch, using
135
+
`RecursiveCharacterTextSplitter` to split the large JSON documents into
136
+
passages. Modify this script to index your own data.
221
137
222
-
```sh
223
-
# Launch API app
224
-
flask run
138
+
See [Langchain documentation][loader-docs] for more ways to load documents.
225
139
226
-
# In a separate terminal launch frontend app
227
-
cd frontend && yarn start
228
-
```
229
140
230
-
You can now access the frontend at http://localhost:3000. Changes are automatically reloaded.
0 commit comments