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.:
# headers = {"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY")} # Replace with your OpenAI API key
# client = weaviate.connect_to_wcs(..., headers=headers) or
# client = weaviate.connect_to_local(..., headers=headers)

# Define a function to call the endpoint and obtain embeddings
def query(texts):
import requests
import os

model_id = "sentence-transformers/all-MiniLM-L6-v2"
hf_token = os.getenv("HUGGINGFACE_APIKEY")

api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
headers = {"Authorization": f"Bearer {hf_token}"}

response = requests.post(
api_url,
headers=headers,
json={"inputs": texts, "options": {"wait_for_model": True}},
)
return response.json()


query_text = "dystopian future"
query_vector = query(query_text)
# Get the collection
movies = client.collections.get("Movie")

# 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
def query(texts):
import requests
import os

model_id = "sentence-transformers/all-MiniLM-L6-v2"
hf_token = os.getenv("HUGGINGFACE_APIKEY")

api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
headers = {"Authorization": f"Bearer {hf_token}"}

response = requests.post(
api_url,
headers=headers,
json={"inputs": texts, "options": {"wait_for_model": True}},
)
return response.json()


query_text = "dystopian future"
query_vector = query(query_text)

Questions and feedback

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