Python Back End

In this section you will setup and configure the back end portion of the project.

Install Python Dependencies

To follow Python best practices, you are now going to create a virtual environment, which is a private Python installation dedicated to this project, where all the dependencies can be installed. Do this with the following command:

python3 -m venv .venv

This command creates a Python virtual environment in a .venv (dot-venv) directory. You can replace .venv in this command with any other name that you like. Note that in some installations of Python, you may need to use python instead of python3 to invoke the Python interpreter.

The next step is to activate the virtual environment, which is a way to make this virtual environment the active Python environment for the terminal session you are in. If you are working on a UNIX-based operating system such as Linux or macOS, activate the virtual environment as follows:

source .venv/bin/activate

The above activation command would also work if you are working inside a WSL environment on a Microsoft Windows computer. But if you are using the Windows command prompt or PowerShell, the activation command is different:


When the virtual environment is activated, the command-line prompt changes to show the name of the environment:

(.venv) $ _

The last step to configure the Python environment is to install a few packages that are needed by the starter application. Make sure that the virtual environment was activated in the previous step, and then run the following command to install these dependencies:

pip install -r requirements.txt

Write a Configuration File

In the main directory of the code you downloaded in the previous section there is a file named env.example. This file contains all the configuration variables supported by the application.

Make a copy of this file, and name it .env:

cp env.example .env

If you are following the tutorial on Windows, use copy instead of cp in the command above.

Open .env in your favorite text editor to review the application configuration and review the following sub-sections for guidance on how to configure the application.

Elasticsearch Setup


If you are using an Elastic Cloud account, then you must set the ELASTIC_CLOUD_ID and ELASTIC_API_KEY variables, and ensure that ELASTICSEARCH_URL is commented out:

ELASTIC_CLOUD_ID="paste your Cloud ID here"
ELASTIC_API_KEY="paste your API Key here"

For help obtaining your Elastic Cloud ID and API Key, see the instructions on how to create an Elastic Cloud deployment.

If you are working with a self-hosted Elasticsearch instance, then you should comment out ELASTIC_CLOUD_ID and ELASTIC_API_KEY, and set ELASTICSEARCH_URL to your Elasticsearch endpoint:



The application uses two Elasticsearch indexes. The ES_INDEX and ES_INDEX_CHAT_HISTORY configuration variables allow you to provide names for these indexes. The defaults included in the configuration file should be fine in most cases.


LLM Setup

The application also needs access to your LLM. If you are using OpenAI, then use the following configuration variables:

OPENAI_API_KEY="paste your OpenAI Key here"

This application supports any LLMs that have a Langchain integration. The comments in the configuration file will guide you to configure a few popular LLMs in addition to OpenAI. You will also be able to use any other LLM that does not come preconfigured with minimal changes to the application.

Load Sample Dataset

The application comes with an example dataset, stored in the data/data.json file. Feel free to open this file in your text editor to familiarize yourself with the documents it contains.

Import the dataset into the application using the following command:

flask create-index

Start the Back End

After following all the steps above, you should be able to start the Python back end with the following command:

flask run

Leave the back end running and open a new terminal session to continue with the rest of this tutorial.

Share this article