Small sample Python/Flask application for demonstrating integration between Elastic and OpenAI.
Includes a tool for generating text embeddings with OpenAI on documents during indexing, and a tiny webapp for running semantic search. These both use the Elasticsearch and OpenAI Python clients.
This section will walk you through the steps for setting up and using the application from scratch.
- Go to https://platform.openai.com/ and sign up
- Generate an API key and make note of it
- Go to https://cloud.elastic.co/ and sign up
- Make note of the master username/password shown to you during creation of the deployment
- Make note of the Elastic Cloud ID after the deployment
pip install -r requirements.txt
export ELASTIC_CLOUD_ID=<your Elastic cloud ID>
export ELASTIC_USERNAME=<your Elastic username>
export ELASTIC_PASSWORD=<your Elastic password>
export OPENAI_API_KEY=<your OpenAI API key>
python generate_embeddings.py
Connecting to Elastic Cloud: my-openai-integration-test:dXMt(...)
Reading from file sample_data/medicare.json
Processing 12 documents...
Processing batch of 10 documents...
Calling OpenAI API for 10 embeddings with model text-embedding-ada-002
Indexing 10 documents to index openai-integration...
Processing batch of 2 documents...
Calling OpenAI API for 2 embeddings with model text-embedding-ada-002
Indexing 2 documents to index openai-integration...
Processing complete
Note: the sample application uses the text-embedding-ada-002
OpenAI model for generating the embeddings, which provides a 1536-dimensional vector output. See this section if you want to use a different model.
python search_app.py
Connecting to Elastic Cloud: my-openai-integration-test:dXMt(...)
Starting Flask app
* Serving Flask app 'search_app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8080
Press CTRL+C to quit
- Open http://localhost:8080 in your browser
- Enter a search query and press Search
Here are some tips for modifying the code in case your use case differs from the sample app.
- Modify the initialization of
MODEL
in both.py
files - Ensure that
embedding.dims
in your index mapping is the same number as the dimensions of the model's output
- Ensure your file contains the documents in JSON format
- Modify the document mappings and fields in the
.py
files and intemplates/search.html
- Modify the initialization of
FILE
ingenerate_embeddings.py
- Modify the initialization of
INDEX
in both.py
files.
- Modify the initialization of
es_client
in both.py
files - Refer to this document about authentication schemes
- Modify the initialization of
es_client
in both.py
files - Refer to this document about connecting to a self-managed cluster