OpenAI

Search the cosine similarity using OpenAI API in Python

Learn how to find text most similar to a user query using OpenAI's API in Python. This recipe covers creating embeddings for the user query, calculating cosine similarity with precomputed embeddings, and retrieving the most similar text chunk. Boost your text analysis and search by integrating these steps into your Python apps.

Required packages

You need below packages to use the code generated by recipe. All packages are automatically installed in MLJAR Studio.

openai>=1.35.14

numpy>=1.26.4

scikit-learn>=1.5.1

Interactive recipe

You can use below interactive recipe to generate code. This recipe is available in MLJAR Studio.

In the below recipe, we assume that you have following variables available in your notebook:

  • client (type OpenAI)

Python code

# Python code will be here

Code explanation

  1. Define the user query.
  2. Generate embedding for the user query.
  3. Find the most similar ID by calculating cosine similarity.
  4. Print the best matching text chunk.

Example Python notebooks

Please find inspiration in example notebooks

« Previous
Chat completions
Next »
Embeddings