Skip to content

Commit 5763770

Browse files
authored
Merge branch 'elastic:main' into main
2 parents 7c243f8 + dc9dcfe commit 5763770

File tree

18 files changed

+6359
-4041
lines changed

18 files changed

+6359
-4041
lines changed

example-apps/chatbot-rag-app/.flaskenv

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
frontend/build
22
frontend/node_modules
33
api/__pycache__
4-
api/.env
54
.venv
65
venv
7-
.DS_Store
6+
.DS_Store
7+
.env
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# app/Dockerfile
2-
3-
FROM node:16-alpine as build-step
1+
FROM node:22-alpine AS build-step
42
WORKDIR /app
5-
ENV PATH /node_modules/.bin:$PATH
3+
ENV PATH=/node_modules/.bin:$PATH
64
COPY frontend ./frontend
75
RUN rm -rf /app/frontend/node_modules
86
RUN cd frontend && yarn install
97
RUN cd frontend && REACT_APP_API_HOST=/api yarn build
108

11-
FROM python:3.9-slim
9+
FROM python:3.12-slim
1210

1311
WORKDIR /app
1412
RUN mkdir -p ./frontend/build
15-
COPY --from=build-step ./app/frontend/build ./frontend/build
13+
COPY --from=build-step ./app/frontend/build ./frontend/build
1614
RUN mkdir ./api
1715
RUN mkdir ./data
1816

@@ -24,12 +22,10 @@ RUN apt-get update && apt-get install -y \
2422
&& rm -rf /var/lib/apt/lists/*
2523

2624

27-
COPY api ./api
28-
COPY data ./data
2925
COPY requirements.txt ./requirements.txt
3026
RUN pip3 install -r ./requirements.txt
31-
ENV FLASK_ENV production
27+
COPY api ./api
28+
COPY data ./data
3229

3330
EXPOSE 4000
34-
WORKDIR /app/api
35-
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=4000" ]
31+
CMD [ "python", "api/app.py"]

example-apps/chatbot-rag-app/README.md

Lines changed: 101 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -15,216 +15,164 @@ curl https://codeload.github.com/elastic/elasticsearch-labs/tar.gz/main | \
1515
tar -xz --strip=2 elasticsearch-labs-main/example-apps/chatbot-rag-app
1616
```
1717

18-
## Installing and connecting to Elasticsearch
19-
20-
### Install Elasticsearch
18+
## Make your .env file
2119

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.
2321

24-
### Connect to Elasticsearch
22+
## Installing and connecting to Elasticsearch
2523

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.
2726

28-
```sh
29-
export ELASTIC_CLOUD_ID=...
30-
export ELASTIC_API_KEY=...
31-
```
27+
Once you decided your approach, edit your `.env` file accordingly.
3228

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
3430

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.
3634

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:
3836

39-
```sh
40-
export ELASTICSEARCH_URL=...
37+
```bash
38+
docker compose -f docker-compose-elastic.yml up --force-recreate -d
4139
```
4240

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:
46-
47-
```sh
48-
ES_INDEX=workplace-app-docs
49-
ES_INDEX_CHAT_HISTORY=workplace-app-docs-chat-history
50-
```
41+
Then, you can view Kibana at http://localhost:5601/app/home#/
5142

52-
## Connecting to LLM
43+
If asked for a username and password, use username: elastic and password: elastic.
5344

54-
We support several LLM providers. To use one of them, you need to set the `LLM_TYPE` environment variable. For example:
45+
Clean up when finished, like this:
5546

56-
```sh
57-
export LLM_TYPE=azure
47+
```bash
48+
docker compose -f docker-compose-elastic.yml down
5849
```
5950

60-
The following sub-sections define the configuration requirements of each supported LLM.
61-
62-
### OpenAI
63-
64-
To use OpenAI LLM, you will need to provide the OpenAI key via `OPENAI_API_KEY` environment variable:
51+
## Connecting to LLM
6552

66-
```sh
67-
export LLM_TYPE=openai
68-
export OPENAI_API_KEY=...
69-
```
53+
We support several LLM providers, but only one is used at runtime, and selected
54+
by the `LLM_TYPE` entry in your `.env` file. Edit that file to choose an LLM,
55+
and configure its templated connection settings:
7056

71-
You can get your OpenAI key from the [OpenAI dashboard](https://platform.openai.com/account/api-keys).
57+
* azure: [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/)
58+
* bedrock: [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/)
59+
* openai: [OpenAI Platform](https://platform.openai.com/docs/overview) and
60+
services compatible with its API.
61+
* vertex: [Google Vertex AI](https://cloud.google.com/vertex-ai/docs)
62+
* mistral: [Mistral AI](https://docs.mistral.ai/)
63+
* cohere: [Cohere](https://docs.cohere.com/)
7264

73-
### Azure OpenAI
65+
## Running the App
7466

75-
If you want to use Azure LLM, you will need to set the following environment variables:
67+
There are two ways to run the app: via Docker or locally. Docker is advised for
68+
ease while locally is advised if you are making changes to the application.
7669

77-
```sh
78-
export LLM_TYPE=azure
79-
export OPENAI_VERSION=... # e.g. 2023-05-15
80-
export OPENAI_BASE_URL=...
81-
export OPENAI_API_KEY=...
82-
export OPENAI_ENGINE=... # deployment name in Azure
83-
```
70+
### Run with docker
8471

85-
### Bedrock LLM
72+
Docker compose is the easiest way, as you get one-step to:
73+
* build the [frontend](frontend)
74+
* ingest data into elasticsearch
75+
* run the app, which listens on http://localhost:4000
8676

87-
To use Bedrock LLM you need to set the following environment variables in order to authenticate to AWS.
77+
**Double-check you have a `.env` file with all your variables set first!**
8878

89-
```sh
90-
export LLM_TYPE=bedrock
91-
export AWS_ACCESS_KEY=...
92-
export AWS_SECRET_KEY=...
93-
export AWS_REGION=... # e.g. us-east-1
94-
export AWS_MODEL_ID=... # Default is anthropic.claude-v2
79+
```bash
80+
docker compose up --build --force-recreate
9581
```
9682

97-
#### AWS Config
83+
*Note*: First time creating the index can fail on timeout. Wait a few minutes
84+
and retry.
9885

99-
Optionally, you can connect to AWS via the config file in `~/.aws/config` described here:
100-
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#configuring-credentials
86+
Clean up when finished, like this:
10187

102-
```
103-
[default]
104-
aws_access_key_id=...
105-
aws_secret_access_key=...
106-
region=...
88+
```bash
89+
docker compose down
10790
```
10891

109-
### Vertex AI
92+
### Run locally
11093

111-
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.
11297

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
117-
export GOOGLE_APPLICATION_CREDENTIALS=<path-json-service-account>
118-
```
98+
**Double-check you have a `.env` file with all your variables set first!**
11999

120-
### Mistral AI
100+
#### Build the frontend
121101

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.
123103

104+
```bash
105+
# Install and use a recent node, if you don't have one.
106+
nvm install --lts
107+
nvm use --lts
108+
# Build the frontend web assets
109+
(cd frontend; yarn install; REACT_APP_API_HOST=/api yarn build)
124110
```
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
132-
133-
To use Cohere you need to set the following environment variables:
134111

135-
```
136-
export LLM_TYPE=cohere
137-
export COHERE_API_KEY=...
138-
export COHERE_MODEL=... # optional
139-
```
112+
#### Configure your python environment
140113

141-
## Running the App
114+
Before we can run the app, we need a working Python environment with the
115+
correct packages installed:
142116

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
151123
```
152124

153-
#### Ingest data
154-
155-
Make sure you have a `.env` file with all your variables, then run:
125+
#### Run the ingest command
156126

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
159130
```
160131

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.
164134

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
166136

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
169140
```
170141

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
178143

179-
- Python 3.8+
180-
- Node 14+
144+
### Updating package versions
181145

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.
183148

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
193152
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
196158
pip install -r requirements.txt
197-
198-
# Install Node dependencies
199-
cd frontend && yarn && cd ..
200159
```
201160

202-
#### Ingest data
161+
### Elasticsearch index and chat_history index
203162

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.
205166

206-
```sh
207-
flask create-index
208-
```
167+
### Indexing your own data
209168

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.
211173

212-
##### Indexing your own data
174+
See [Langchain documentation][loader-docs] for more ways to load documents.
213175

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-
```
229176

230-
You can now access the frontend at http://localhost:3000. Changes are automatically reloaded.
177+
---
178+
[loader-docs]: https://python.langchain.com/docs/how_to/#document-loaders

example-apps/chatbot-rag-app/api/app.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from flask import Flask, jsonify, request, Response
2-
from flask_cors import CORS
3-
from uuid import uuid4
4-
from chat import ask_question
51
import os
62
import sys
3+
from uuid import uuid4
4+
5+
from chat import ask_question
6+
from flask import Flask, Response, jsonify, request
7+
from flask_cors import CORS
78

89
app = Flask(__name__, static_folder="../frontend/build", static_url_path="/")
910
CORS(app)
@@ -37,4 +38,4 @@ def create_index():
3738

3839

3940
if __name__ == "__main__":
40-
app.run(port=3001, debug=True)
41+
app.run(host="0.0.0.0", port=4000, debug=False)

0 commit comments

Comments
 (0)