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
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
18
+
## Make your .env file
21
19
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.
20
+
Copy [env.example](env.example)to `.env` and fill in values noted inside.
23
21
24
-
### Connect to Elasticsearch
22
+
##Installing and connecting to Elasticsearch
25
23
26
-
This app requires the following environment variables to be set to connect to Elasticsearch hosted on Elastic Cloud:
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.
27
26
28
-
```sh
29
-
export ELASTIC_CLOUD_ID=...
30
-
export ELASTIC_API_KEY=...
31
-
```
27
+
Once you decided your approach, edit your `.env` file accordingly.
32
28
33
-
You can add these to a `.env` file for convenience. See the `env.example` file for a .env file template.
29
+
### Running your own Elastic Stack with Docker
34
30
35
-
#### Self-Hosted Elasticsearch
31
+
If you'd like to start Elastic locally, you can use the provided
32
+
[docker-compose-elastic.yml](docker-compose-elastic.yml) file. This starts
33
+
Elasticsearch, Kibana, and APM Server and only requires Docker installed.
36
34
37
-
You can also connect to a self-hosted Elasticsearch instance. To do so, you will need to set the following environment variables:
35
+
Use docker compose to run Elastic stack in the background:
38
36
39
-
```sh
40
-
export ELASTICSEARCH_URL=...
37
+
```bash
38
+
docker compose -f docker-compose-elastic.yml up --force-recreate -d
41
39
```
42
40
43
-
### Change the Elasticsearch index and chat_history index
44
-
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).
94
+
If you want to run this example with Python and Node.js, you need to do a few
95
+
things listed in the [Dockerfile](Dockerfile). The below uses the same
96
+
production mode as used in Docker to avoid problems in debug mode.
112
97
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
**Double-check you have a `.env` file with all your variables set first!**
119
99
120
-
###Mistral AI
100
+
#### Build the frontend
121
101
122
-
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).
102
+
The web assets are in the [frontend](frontend) directory, and built with yarn.
123
103
104
+
```bash
105
+
# Install and use a recent node, if you don't have one.
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
132
-
133
-
To use Cohere you need to set the following environment variables:
134
111
135
-
```
136
-
export LLM_TYPE=cohere
137
-
export COHERE_API_KEY=...
138
-
export COHERE_MODEL=... # optional
139
-
```
112
+
#### Configure your python environment
140
113
141
-
## Running the App
114
+
Before we can run the app, we need a working Python environment with the
115
+
correct packages installed:
142
116
143
-
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 .
117
+
```bash
118
+
python3 -m venv .venv
119
+
source .venv/bin/activate
120
+
# Install dotenv which is a portable way to load environment variables.
121
+
pip install "python-dotenv[cli]"
122
+
pip install -r requirements.txt
151
123
```
152
124
153
-
#### Ingest data
154
-
155
-
Make sure you have a `.env` file with all your variables, then run:
125
+
#### Run the ingest command
156
126
157
-
```sh
158
-
docker run --rm --env-file .env chatbot-rag-app flask create-index
127
+
First, ingest the data into elasticsearch:
128
+
```bash
129
+
FLASK_APP=api/app.py dotenv run -- flask create-index
159
130
```
160
131
161
-
See "Ingest data" section under Running Locally for more details about the `flask create-index` command.
162
-
163
-
#### Run API and frontend
132
+
*Note*: First time creating the index can fail on timeout. Wait a few minutes
133
+
and retry.
164
134
165
-
You will need to set the appropriate environment variables in your `.env` file. See the `env.example` file for instructions.
135
+
#### Run the app
166
136
167
-
```sh
168
-
docker run --rm -p 4000:4000 --env-file .env -d chatbot-rag-app
137
+
Now, run the app, which listens on http://localhost:4000
138
+
```bash
139
+
dotenv run -- python api/app.py
169
140
```
170
141
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.
172
-
173
-
### Locally (for development)
174
-
175
-
With the environment variables set, you can run the following commands to start the server and frontend.
176
-
177
-
#### Pre-requisites
142
+
## Advanced
178
143
179
-
- Python 3.8+
180
-
- Node 14+
144
+
### Updating package versions
181
145
182
-
#### Install the dependencies
146
+
To update package versions, recreate [requirements.txt](requirements.txt) and
147
+
reinstall like this. Once checked in, any commands above will use updates.
183
148
184
-
For Python we recommend using a virtual environment.
185
-
186
-
_ℹ️ Here's a good [primer](https://realpython.com/python-virtual-environments-a-primer) on virtual environments from Real Python._
187
-
188
-
```sh
189
-
# Create a virtual environment
190
-
python -m venv .venv
191
-
192
-
# Activate the virtual environment
149
+
```bash
150
+
rm -rf .venv
151
+
python3 -m venv .venv
193
152
source .venv/bin/activate
194
-
195
-
# Install Python dependencies
153
+
# Install dev requirements for pip-compile
154
+
pip install pip-tools
155
+
# Recreate requirements.txt
156
+
pip-compile
157
+
# Install main dependencies
196
158
pip install -r requirements.txt
197
-
198
-
# Install Node dependencies
199
-
cd frontend && yarn &&cd ..
200
159
```
201
160
202
-
#### Ingest data
161
+
###Elasticsearch index and chat_history index
203
162
204
-
You can index the sample data from the provided .json files in the `data` folder:
163
+
By default, the app will use the `workplace-app-docs` index and the chat
164
+
history index will be `workplace-app-docs-chat-history`. If you want to change
165
+
these, edit `ES_INDEX` and `ES_INDEX_CHAT_HISTORY` entries in your `.env` file.
205
166
206
-
```sh
207
-
flask create-index
208
-
```
167
+
### Indexing your own data
209
168
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.
169
+
The ingesting logic is stored in [data/index_data.py](data/index_data.py). This
170
+
is a simple script that uses Langchain to index data into Elasticsearch, using
171
+
`RecursiveCharacterTextSplitter` to split the large JSON documents into
172
+
passages. Modify this script to index your own data.
211
173
212
-
##### Indexing your own data
174
+
See [Langchain documentation][loader-docs] for more ways to load documents.
213
175
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.
215
-
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)
217
-
218
-
Remember to keep the `ES_INDEX` environment variable set to the index you want to index into and to query from.
219
-
220
-
#### Run API and frontend
221
-
222
-
```sh
223
-
# Launch API app
224
-
flask run
225
-
226
-
# In a separate terminal launch frontend app
227
-
cd frontend && yarn start
228
-
```
229
176
230
-
You can now access the frontend at http://localhost:3000. Changes are automatically reloaded.
0 commit comments