Querying vectors

Vector Databases for Embeddings with Pinecone

James Chapman

Curriculum Manager, DataCamp

The power of querying

 

  • Querying: receive the most semantically similar vectors to an input vector

A user input being embedded and used to query a Pinecone vector database.

Vector Databases for Embeddings with Pinecone

The .query() method

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}}
Vector Databases for Embeddings with Pinecone

The .query() method

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}}
Vector Databases for Embeddings with Pinecone

Read units (RUs) for querying

 

  • For querying, RUs is harder to calculate
  • Dependent on:
    • No. of records in the namespace
    • Size of records
      • Vector dimensionality
      • Amount of metadata

Table of RUs consumed when querying.

1 https://docs.pinecone.io/guides/organizations/manage-cost/understanding-cost#query
Vector Databases for Embeddings with Pinecone

Distance metrics

 

The cosine similarity: showing an angle between two vectors.

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
Vector Databases for Embeddings with Pinecone

Distance metrics

 

The euclidean distance: showing the straight-line distance between two vectors.

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
Vector Databases for Embeddings with Pinecone

Distance metrics

 

The dot product: how the value is calculated from the angle between the vectors.

1 https://docs.pinecone.io/guides/indexes/understanding-indexes#distance-metrics
Vector Databases for Embeddings with Pinecone

Setting the distance metric

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

metric='dotproduct',
spec=ServerlessSpec( cloud='aws', region='us-east-1' ) )
  • metric'cosine', 'euclidean', 'dotproduct'
Vector Databases for Embeddings with Pinecone

Your turn to query!

Vector Databases for Embeddings with Pinecone

Preparing Video For Download...