ベクトルのクエリ

Pineconeを使ったAIアプリケーション開発

James Chapman

Curriculum Manager, DataCamp

クエリの力

 

  • クエリ: 入力ベクトルに最も「意味的に類似」したベクトルを取得

ユーザー入力を埋め込み、Pinecone のベクトルDBをクエリする図。

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アプリケーション開発

クエリの Read Units (RUs)

 

  • クエリの RUs は算出がやや複雑
  • 依存要因:
    • 名前空間内のレコード数
    • レコードのサイズ
      • ベクトル次元数
      • メタデータ量

クエリ時に消費される RUs の表。

1 https://docs.pinecone.io/guides/organizations/manage-cost/understanding-cost#query
Pineconeを使ったAIアプリケーション開発

距離指標

 

コサイン類似度:2つのベクトル間の角度を示す図。

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
Pineconeを使ったAIアプリケーション開発

距離指標

 

ユークリッド距離:2つのベクトル間の直線距離を示す図。

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