查詢向量

使用 Pinecone 構建 AI 應用

James Chapman

Curriculum Manager, DataCamp

查詢的威力

 

  • 查詢:取得與輸入向量在語意上最相似的向量

使用者輸入被轉成 embedding,並用來查詢 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 應用

查詢的讀取單位(RU)

 

  • 查詢時,RU 較難計算
  • 取決於:
    • 命名空間中的「記錄數量」
    • 「記錄大小」
      • 向量維度數
      • 中繼資料量

查詢時消耗的 RU 表格。

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...