开始使用免费开始使用

用于语义搜索的向量查询

在本练习中,您将从问题 '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')
编辑并运行代码