跳到主要内容

使用嵌入进行语义文本搜索

nbviewer

通过将搜索查询嵌入并找到最相似的评论,我们可以以非常高效且成本很低的方式在所有评论中进行语义搜索。数据集是在Get_embeddings_from_dataset Notebook中创建的。

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

datafile_path = "data/fine_food_reviews_with_embeddings_1k.csv"

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


在这里,我们比较查询和文档嵌入的余弦相似度,并展示前n个最佳匹配项。

from utils.embeddings_utils import get_embedding, cosine_similarity

# 在评论中搜索特定产品
def search_reviews(df, product_description, n=3, pprint=True):
product_embedding = get_embedding(
product_description,
model="text-embedding-3-small"
)
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


results = search_reviews(df, "delicious beans", n=3)


Delicious!:  I enjoy this white beans seasoning, it gives a rich flavor to the beans I just love it, my mother in law didn't know about this Zatarain's brand and now she is traying different seasoning

Fantastic Instant Refried beans: Fantastic Instant Refried Beans have been a staple for my family now for nearly 20 years. All 7 of us love it and my grown kids are passing on the tradition.

Delicious: While there may be better coffee beans available, this is my first purchase and my first time grinding my own beans. I read several reviews before purchasing this brand, and am extremely
results = search_reviews(df, "whole wheat pasta", n=3)


Tasty and Quick Pasta:  Barilla Whole Grain Fusilli with Vegetable Marinara is tasty and has an excellent chunky vegetable marinara.  I just wish there was more of it.  If you aren't starving or on a 

sooo good: tastes so good. Worth the money. My boyfriend hates wheat pasta and LOVES this. cooks fast tastes great.I love this brand and started buying more of their pastas. Bulk is best.

Bland and vaguely gamy tasting, skip this one: As far as prepared dinner kits go, "Barilla Whole Grain Mezze Penne with Tomato and Basil Sauce" just did not do it for me...and this is coming from a p

我们可以轻松地搜索这些评论。为了加快计算速度,我们可以使用一种特殊的算法,旨在通过嵌入进行更快速的搜索。

results = search_reviews(df, "bad delivery", n=1)


great product, poor delivery:  The coffee is excellent and I am a repeat buyer.  Problem this time was with the UPS delivery.  They left the box in front of my garage door in the middle of the drivewa

正如我们所看到的,这可以立即提供很大的价值。在这个例子中,我们展示了能够快速找到交付失败示例的能力。

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


Disappointed:  The metal cover has severely disformed. And most of the cookies inside have been crushed into small pieces. Shopping experience is awful. I'll never buy it online again.
results = search_reviews(df, "pet food", n=2)


Great food!:  I wanted a food for a a dog with skin problems. His skin greatly improved with the switch, though he still itches some.  He loves the food. No recalls, American made with American ingred

Great food!: I wanted a food for a a dog with skin problems. His skin greatly improved with the switch, though he still itches some. He loves the food. No recalls, American made with American ingred