開始使用免費開始

語意搜尋的向量查詢

在這個練習中,你會從問題 「'What is in front of the Notre Dame Main Building?'」 建立一個查詢向量。使用這個嵌入式查詢,你將在 'pinecone-datacamp' 索引中的 'squad_dataset' 命名空間進行查詢,並回傳最相似的前五個向量。

本練習屬於課程

使用 Pinecone 構建 AI 應用

檢視課程

練習說明

  • 使用你的 API 金鑰初始化 Pinecone 用戶端(OpenAI 用戶端已可透過 client 使用)。
  • 使用與先前嵌入其他向量相同的 OpenAI 嵌入模型,對提供的 query 進行嵌入以建立查詢向量。
  • 使用 query_emb 查詢 "squad_dataset" 命名空間,並回傳最相似的前五筆結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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')
編輯並執行程式碼