Skip to main content

Vector search

In this scenario, we've added data objects and our own vectors. Accordingly, any similarity searches will also require a vector input. This can be done with a near vector query.

Code

This example finds entries in "Movie" based on their similarity to the input vector and prints out the title and release year of the top 5 matches.

import weaviate
import weaviate.classes.query as wq
import os


# Instantiate your client (not shown). e.g.:
# client = weaviate.connect_to_weaviate_cloud(...) or
# client = weaviate.connect_to_local(...)

# Define a function to call the endpoint and obtain embeddings
from typing import List
import os
import cohere
from cohere import Client as CohereClient

co_token = os.getenv("COHERE_APIKEY")
co = cohere.Client(co_token)


# Define a function to call the endpoint and obtain embeddings
def vectorize(cohere_client: CohereClient, texts: List[str]) -> List[List[float]]:

response = cohere_client.embed(
texts=texts, model="embed-multilingual-v3.0", input_type="search_document"
)

return response.embeddings


query_text = "dystopian future"
query_vector = vectorize(co, [query_text])[0]
# Get the collection
movies = client.collections.get("MovieCustomVector")

# Perform query
response = movies.query.near_vector(
near_vector=query_vector, # A list of floating point numbers
limit=5,
return_metadata=wq.MetadataQuery(distance=True),
)

# Inspect the response
for o in response.objects:
print(
o.properties["title"], o.properties["release_date"].year
) # Print the title and release year (note the release date is a datetime object)
print(
f"Distance to query: {o.metadata.distance:.3f}\n"
) # Print the distance of the object from the query

client.close()

Explain the code

The results are based on similarity of the vector embeddings between the query and the database object text. In this case, the embeddings are input manually in the query.

The limit parameter here sets the maximum number of results to return.

The return_metadata parameter takes an instance of the MetadataQuery class to set metadata to return in the search results. The current query returns the vector distance to the query.

Example results
In Time 2011
Distance to query: 0.179

Gattaca 1997
Distance to query: 0.180

I, Robot 2004
Distance to query: 0.182

Mad Max: Fury Road 2015
Distance to query: 0.190

The Maze Runner 2014
Distance to query: 0.193

Response object

The returned object is an instance of a custom class. Its objects attribute is a list of search results, each object being an instance of another custom class.

Each returned object will:

  • Include all properties and its UUID by default except those with blob data types.
  • Not include any other information (e.g. references, metadata, vectors.) by default.

Where did the query vector come from?

The query vector in this example is obtained similarly to how it was in the data ingestion. The only difference is that the vector is not stored in the database, but is used directly in the query.

# Define a function to call the endpoint and obtain embeddings
from typing import List
import os
import cohere
from cohere import Client as CohereClient

co_token = os.getenv("COHERE_APIKEY")
co = cohere.Client(co_token)


# Define a function to call the endpoint and obtain embeddings
def vectorize(cohere_client: CohereClient, texts: List[str]) -> List[List[float]]:

response = cohere_client.embed(
texts=texts, model="embed-multilingual-v3.0", input_type="search_document"
)

return response.embeddings


query_text = "dystopian future"
query_vector = vectorize(co, [query_text])[0]

Questions and feedback

If you have any questions or feedback, let us know in the user forum.