Skip to content

Instantly share code, notes, and snippets.

@keskival
Created September 10, 2023 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keskival/e47e3e72e177c02c009ad140b3a5d86a to your computer and use it in GitHub Desktop.
Save keskival/e47e3e72e177c02c009ad140b3a5d86a to your computer and use it in GitHub Desktop.
get_query_embeddings.py
#!/usr/bin/python
import openai
import json
import os
from datetime import datetime
from PIL import Image
import time
import pandas as pd
# Using the first review text as-is, plus 9 rephrasing by GPT-4:
queries = [
"I have bought several of the Vitality canned dog food products and have found them all to be of good quality. The product looks more like a stew than a processed meat and it smells better. My Labrador is finicky and she appreciates this product better than most.",
"I've purchased numerous Vitality canned dog food items and all seem to be of high quality. The product has more of a stew appearance rather than that of processed meat, and it has a more pleasant aroma. My picky Labrador prefers this over many other brands.",
"Over time, I've acquired several Vitality dog food cans and they all maintain good standards. The content resembles a stew more than it does processed meat, and the scent is much nicer. It's a hit with my choosy Labrador.",
"I've tried multiple Vitality dog food cans and I'm always impressed with the quality. It looks and smells more natural, resembling a stew instead of typical processed meat. Even my selective Labrador likes it more than most others.",
"Many Vitality canned dog food products have found their way into my pantry, and I'm happy with their consistent quality. They look more like a homemade stew and have a fresh smell. My Labrador, who's quite particular, seems to enjoy this more than other brands.",
"From the several Vitality dog food cans I've purchased, the quality has always been commendable. The contents look more stew-like than processed, with a much friendlier aroma. Notably, my finicky Labrador gives it a thumbs up more often than not.",
"I've repeatedly chosen Vitality's canned dog food and haven't been disappointed. The appearance leans more towards a stew than industrial meat, and the smell is much more inviting. My discerning Labrador takes a particular liking to it.",
"I’ve invested in a variety of Vitality’s canned dog food and I can vouch for its quality. With its stew-like appearance and a better scent compared to typical dog food, my fussy Labrador seems to favor it.",
"Every time I've bought Vitality’s canned dog food, I've noted its superior quality. The food looks closer to a rich stew rather than the usual processed consistency, and it’s aromatic in a good way. My choosy Labrador clearly prefers it.",
"Having tried several cans of Vitality dog food, I'm consistently pleased with its quality. The food not only looks wholesome, resembling a stew, but also smells appetizing. It's evidently a favorite for my particular Labrador."
]
# The hypothesis is that these should produce the same ranking of best and worst ranked documents.
with open('apikey.json', 'r') as apikey_file:
config = json.load(apikey_file)
openai.api_key = config['apikey']
openai.organization = config['org']
index = 0
def get_embedding(text, model="text-embedding-ada-002"):
global index
text = text.replace("\n", " ")
embedding = openai.Embedding.create(input = [text], model=model)['data'][0]['embedding']
index = index + 1
print(f"{index}/10")
print(text[0:100])
print(embedding[0:100])
return embedding
df = pd.DataFrame(data={'queryText': queries})
df.head(2)
df['ada_embedding'] = df.queryText.apply(lambda x: get_embedding(x, model='text-embedding-ada-002'))
df.to_csv('data/queries.csv', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment