|
| 1 | +# Elastic Chatbot RAG App |
| 2 | + |
| 3 | +This is a sample app that combines Elasticsearch, Langchain and a number of different LLMs to create a chatbot experience with ELSER with your own private data. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## 1. Download the Project |
| 8 | + |
| 9 | +Download the project from Github and extract the `chatbot-rag-app` folder. |
| 10 | + |
| 11 | +```bash |
| 12 | +curl https://codeload.github.com/elastic/elasticsearch-labs/tar.gz/main | \ |
| 13 | +tar -xz --strip=2 elasticsearch-labs-main/example-apps/chatbot-rag-app |
| 14 | +``` |
| 15 | + |
| 16 | +## 2. Installing and connecting to Elasticsearch |
| 17 | + |
| 18 | +### Install Elasticsearch |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +### Connect to Elasticsearch |
| 23 | + |
| 24 | +This app requires the following environment variables to be set to connect to Elasticsearch |
| 25 | + |
| 26 | +```sh |
| 27 | +export ELASTIC_CLOUD_ID=... |
| 28 | +export ELASTIC_USERNAME=... |
| 29 | +export ELASTIC_PASSWORD=... |
| 30 | +``` |
| 31 | + |
| 32 | +### Change the Elasticsearch index and chat_history index |
| 33 | + |
| 34 | +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: |
| 35 | + |
| 36 | +```sh |
| 37 | +ES_INDEX=workplace-app-docs |
| 38 | +ES_INDEX_CHAT_HISTORY=workplace-app-docs-chat-history |
| 39 | +``` |
| 40 | + |
| 41 | +## 3. Connecting to LLM |
| 42 | + |
| 43 | +We support three LLM providers: Azure, OpenAI and Bedrock. |
| 44 | + |
| 45 | +To use one of them, you need to set the `LLM_TYPE` environment variable: |
| 46 | + |
| 47 | +```sh |
| 48 | +export LLM_TYPE=azure |
| 49 | +``` |
| 50 | + |
| 51 | +### OpenAI |
| 52 | + |
| 53 | +To use OpenAI LLM, you will need to provide the OpenAI key via `OPENAI_API_KEY` environment variable: |
| 54 | + |
| 55 | +```sh |
| 56 | +export LLM_TYPE=openai |
| 57 | +export OPENAI_API_KEY=... |
| 58 | +``` |
| 59 | + |
| 60 | +You can get your OpenAI key from the [OpenAI dashboard](https://platform.openai.com/account/api-keys). |
| 61 | + |
| 62 | +### Azure OpenAI |
| 63 | + |
| 64 | +If you are using Azure LLM, you will need to set the following environment variables: |
| 65 | + |
| 66 | +```sh |
| 67 | +export LLM_TYPE=azure |
| 68 | +export OPENAI_VERSION=... # e.g. 2023-05-15 |
| 69 | +export OPENAI_BASE_URL=... |
| 70 | +export OPENAI_API_KEY=... |
| 71 | +export OPENAI_ENGINE=... # deployment name in Azure |
| 72 | +``` |
| 73 | + |
| 74 | +### Bedrock LLM |
| 75 | + |
| 76 | +To use Bedrock LLM you need to set the following environment variables in order to AWS. |
| 77 | + |
| 78 | +```sh |
| 79 | +export LLM_TYPE=bedrock |
| 80 | +export AWS_ACCESS_KEY=... |
| 81 | +export AWS_SECRET_KEY=... |
| 82 | +export AWS_REGION=... # e.g. us-east-1 |
| 83 | +export AWS_MODEL_ID=... # Default is anthropic.claude-v2 |
| 84 | +``` |
| 85 | + |
| 86 | +#### AWS Config |
| 87 | + |
| 88 | +Optionally, you can connect to AWS via the config file in `~/.aws/config` described here: |
| 89 | +https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#configuring-credentials |
| 90 | + |
| 91 | +``` |
| 92 | +[default] |
| 93 | +aws_access_key_id=... |
| 94 | +aws_secret_access_key=... |
| 95 | +region=... |
| 96 | +``` |
| 97 | + |
| 98 | +### Vertex AI |
| 99 | + |
| 100 | +To use Vertex AI you need to set the following environment variables. More infos [here](https://python.langchain.com/docs/integrations/llms/google_vertex_ai_palm). |
| 101 | + |
| 102 | +```sh |
| 103 | + export LLM_TYPE=vertex |
| 104 | + export VERTEX_PROJECT_ID=<gcp-project-id> |
| 105 | + export VERTEX_REGION=<gcp-region> # Default is us-central1 |
| 106 | + export GOOGLE_APPLICATION_CREDENTIALS=<path-json-service-account> |
| 107 | +``` |
| 108 | + |
| 109 | +## 3. Ingest Data |
| 110 | + |
| 111 | +You can index the sample data from the provided .json files in the `data` folder: |
| 112 | + |
| 113 | +```sh |
| 114 | +python data/index-data.py |
| 115 | +``` |
| 116 | + |
| 117 | +by default, this will index the data into the `workplace-app-docs` index. You can change this by setting the `ES_INDEX` environment variable. |
| 118 | + |
| 119 | +### Indexing your own data |
| 120 | + |
| 121 | +`index-data.py` 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 | + |
| 123 | +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) |
| 124 | + |
| 125 | +Remember to keep the `ES_INDEX` environment variable set to the index you want to index into and to query from. |
| 126 | + |
| 127 | +## Running the App |
| 128 | + |
| 129 | +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. |
| 130 | + |
| 131 | +### Through Docker |
| 132 | + |
| 133 | +Build the Docker image and run it with the following environment variables. |
| 134 | + |
| 135 | +```sh |
| 136 | +docker build -f Dockerfile -t chatbot-rag-app . |
| 137 | +``` |
| 138 | + |
| 139 | +Then run it with the following environment variables. In the example below, we are using OpenAI LLM. |
| 140 | + |
| 141 | +If you're using one of the other LLMs, you will need to set the appropriate environment variables via `-e` flag. |
| 142 | + |
| 143 | +```sh |
| 144 | +docker run -p 4000:4000 \ |
| 145 | + -e "ELASTIC_CLOUD_ID=<cloud_id>" \ |
| 146 | + -e "ELASTIC_USERNAME=elastic" \ |
| 147 | + -e "ELASTIC_PASSWORD=<password>" \ |
| 148 | + -e "LLM_TYPE=openai" \ |
| 149 | + -e "OPENAI_API_KEY=<openai_key>" \ |
| 150 | + -d chatbot-rag-app |
| 151 | +``` |
| 152 | + |
| 153 | +### Locally (for development) |
| 154 | + |
| 155 | +With the environment variables set, you can run the following commands to start the server and frontend. |
| 156 | + |
| 157 | +#### Pre-requisites |
| 158 | + |
| 159 | +- Python 3.8+ |
| 160 | +- Node 14+ |
| 161 | + |
| 162 | +#### Install the dependencies |
| 163 | + |
| 164 | +For Python we recommend using a virtual environment. |
| 165 | + |
| 166 | +_ℹ️ Here's a good [primer](https://realpython.com/python-virtual-environments-a-primer) on virtual environments from Real Python._ |
| 167 | + |
| 168 | +```sh |
| 169 | +# Create a virtual environment |
| 170 | +python -m venv .venv |
| 171 | + |
| 172 | +# Activate the virtual environment |
| 173 | +source .venv/bin/activate |
| 174 | +``` |
| 175 | + |
| 176 | +```sh |
| 177 | +# Install Python dependencies |
| 178 | +pip install -r requirements.txt |
| 179 | + |
| 180 | +# Install Node dependencies |
| 181 | +cd frontend && yarn |
| 182 | +``` |
| 183 | + |
| 184 | +#### Run API and frontend |
| 185 | + |
| 186 | +```sh |
| 187 | +# Launch API app |
| 188 | +python api/app.py |
| 189 | + |
| 190 | +# In a separate terminal launch frontend app |
| 191 | +cd frontend && yarn start |
| 192 | +``` |
| 193 | + |
| 194 | +You can now access the frontend at http://localhost:3000. Changes are automatically reloaded. |
0 commit comments