Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

elastic/openai-integration-example-python

Repository files navigation

Elastic OpenAI integration example - Python

Overview

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.

Setup

This section will walk you through the steps for setting up and using the application from scratch.

1. Create OpenAI account and API key

OpenAI API key

2. Create Elastic Cloud account and credentials

  • 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

Elastic Cloud credentials

Elastic Cloud ID

3. Install Python dependencies

pip install -r requirements.txt

4. Set environment variables

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>

5. Generate embeddings and index documents

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.

6. Launch web app

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

7. Run semantic search in the web app

Search example

Configuration

Here are some tips for modifying the code in case your use case differs from the sample app.

Using a different OpenAI model

  • 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

Using a different source file or document mapping

  • Ensure your file contains the documents in JSON format
  • Modify the document mappings and fields in the .py files and in templates/search.html
  • Modify the initialization of FILE in generate_embeddings.py

Using a different Elastic index

  • Modify the initialization of INDEX in both .py files.

Using a different method for authenticating with Elastic

  • Modify the initialization of es_client in both .py files
  • Refer to this document about authentication schemes

Running on self-managed Elastic cluster

  • Modify the initialization of es_client in both .py files
  • Refer to this document about connecting to a self-managed cluster

About

Elastic OpenAI integration example in Python

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published