การ Query เวกเตอร์สำหรับ Semantic Search
ในแบบฝึกหัดนี้ คุณจะสร้าง query vector จากคำถาม 'What is in front of the Notre Dame Main Building?' จากนั้นใช้ query ที่ฝังค่าไว้แล้วนี้ไปค้นหาใน namespace 'squad_dataset' ของ index 'pinecone-datacamp' เพื่อดึงเวกเตอร์ที่คล้ายกันมากที่สุด 5 อันดับแรก
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Vector Databases สำหรับ Embeddings ด้วย Pinecone
คำแนะนำการฝึกหัด
- เริ่มต้น Pinecone client ด้วย API key ของคุณ (OpenAI client พร้อมใช้งานในชื่อตัวแปร
client) - สร้าง query vector โดยฝังค่า
queryที่กำหนดไว้ด้วย OpenAI embedding model เดียวกับที่ใช้ฝังเวกเตอร์อื่น ๆ - Query namespace
"squad_dataset"โดยใช้query_embและดึงผลลัพธ์ที่คล้ายกันมากที่สุด 5 อันดับแรก
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')
query = "What is in front of the Notre Dame Main Building?"
# Create the query vector
query_response = ____(
input=____,
model="text-embedding-3-small"
)
query_emb = query_response.data[0].embedding
# Query the index and retrieve the top five most similar vectors
retrieved_docs = ____
for result in retrieved_docs['matches']:
print(f"{result['id']}: {round(result['score'], 2)}")
print('\n')