OpenAI ChatGPT Embedding Search Example

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

OpenAI ChatGPT Embedding Search
Example

import pandas as pd
import numpy as np
from ast import literal_eval
import openai

def get_embedding(text: str, model="text-similarity-davinci-001", **kwargs):
text = text.replace("\n", " ")
response = openai.embeddings.create(input=[text], model=model, **kwargs)
return response.data[0].embedding

def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

Load the dataset and convert embeddings

datafile_path = "fine_food_reviews_with_embeddings.csv"
df = pd.read_csv(datafile_path)
df["embedding"] = df.embedding.apply(literal_eval).apply(np.array)

Function to search through the reviews

def search_reviews(df, product_description, n=3, pprint=True):
product_embedding = get_embedding(
product_description,
model="text-embedding-ada-002"
)
df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, product_embedding))

results = (
df.sort_values("similarity", ascending=False)
.head(n)
.combined.str.replace("Title: ", "")
.str.replace("; Content:", ": ")
)
if pprint:
for r in results:
print(r[:200])
print()
return results
Enter fullscreen mode Exit fullscreen mode




Example searches

results = search_reviews(df, "tomato", n=1)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .