查询向量

使用 Pinecone 构建 AI 应用

James Chapman

Curriculum Manager, DataCamp

查询的力量

 

  • 查询:返回与输入向量在语义上最相似的向量

将用户输入嵌入并用于查询 Pinecone 向量数据库。

使用 Pinecone 构建 AI 应用

.query() 方法

index.query(

vector=[-0.250919762305275, ...],
top_k=3
)
{'matches': [{'id': '1', 'score': 0.0478537641, 'values': []},
             {'id': '2', 'score': 0.046000585, 'values': []},
             {'id': '3', 'score': 0.0458319113, 'values': []}],
 'namespace': '',
 'usage': {'read_units': 5}}
使用 Pinecone 构建 AI 应用

.query() 方法

index.query(
    vector=[-0.250919762305275, ...],
    top_k=3,

include_values=True
)
{'matches': [{'id': '1', 'score': 0.0478537641, 'values': [-0.0131468913, ...]},
             {'id': '2', 'score': 0.046000585, 'values': [-0.0120476764, ...]},
             {'id': '3', 'score': 0.0458319113, 'values': [0.00285418332, ...]}],
 'namespace': '',
 'usage': {'read_units': 5}}
使用 Pinecone 构建 AI 应用

查询的读取单位(RUs)

 

  • 对查询而言,RUs 更难精确计算
  • 取决于:
    • 命名空间中的记录数
    • 记录大小
      • 向量维度
      • 元数据量

查询时消耗的 RUs 表格。

1 https://docs.pinecone.io/guides/organizations/manage-cost/understanding-cost#query
使用 Pinecone 构建 AI 应用

距离度量

 

余弦相似度:显示两个向量间的角度。

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
使用 Pinecone 构建 AI 应用

距离度量

 

欧氏距离:显示两个向量的直线距离。

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
使用 Pinecone 构建 AI 应用

距离度量

 

点积:展示如何由向量间角度计算取值。

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
使用 Pinecone 构建 AI 应用

设置距离度量

pc.create_index(
    name="datacamp-index", 
    dimension=1536,

metric='dotproduct',
spec=ServerlessSpec( cloud='aws', region='us-east-1' ) )
  • metric → 'cosine'、'euclidean'、'dotproduct'
使用 Pinecone 构建 AI 应用

轮到您来查询!

使用 Pinecone 构建 AI 应用

Preparing Video For Download...