Embedding more detailed descriptions
One of the last predicted labels didn't seem representative of the review; this was probably down to the lack of information being captured when we're only embedding the class labels. This time, descriptions of each class will be embedded instead, so the model better "understands" that you're classifying restaurant reviews.
The following objects are available for you to use:
sentiments = [{'label': 'Positive',
'description': 'A positive restaurant review'},
{'label': 'Neutral',
'description':'A neutral restaurant review'},
{'label': 'Negative',
'description': 'A negative restaurant review'}]
reviews = ["The food was delicious!",
"The service was a bit slow but the food was good",
"The food was cold, really disappointing!"]
This exercise is part of the course
Introduction to Embeddings with the OpenAI API
Exercise instructions
- Extract a list containing the sentiment descriptions and embed them.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Extract and embed the descriptions from sentiments
class_descriptions = ____
class_embeddings = ____
review_embeddings = create_embeddings(reviews)
def find_closest(query_vector, embeddings):
distances = []
for index, embedding in enumerate(embeddings):
dist = distance.cosine(query_vector, embedding)
distances.append({"distance": dist, "index": index})
return min(distances, key=lambda x: x["distance"])
for index, review in enumerate(reviews):
closest = find_closest(review_embeddings[index], class_embeddings)
label = sentiments[closest['index']]['label']
print(f'"{review}" was classified as {label}')