Truy vấn vector cho tìm kiếm ngữ nghĩa
Trong bài tập này, bạn sẽ tạo một vector truy vấn từ câu hỏi 'What is in front of the Notre Dame Main Building?'. Dùng truy vấn đã nhúng này, bạn sẽ truy vấn namespace 'squad_dataset' từ chỉ mục 'pinecone-datacamp' và trả về năm vector tương tự nhất.
Bài tập này là một phần của khóa học
Cơ sở dữ liệu vector cho Embeddings với Pinecone
Hướng dẫn bài tập
- Khởi tạo client Pinecone với API key của bạn (client OpenAI có sẵn là
client). - Tạo một vector truy vấn bằng cách nhúng
queryđã cung cấp với cùng mô hình OpenAI embedding mà bạn đã dùng để nhúng các vector khác. - Truy vấn namespace
"squad_dataset"bằngquery_emb, trả về năm kết quả tương tự nhất.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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')