Use cases
RAG, or retrieval augmented generation, is a powerful feature that combines the strengths of both vector search and language generation. Named vectors can be used in RAG queries to improve workflow and results.
In this section, we'll explore a few examples of how named vectors allow different users to search and generate results based on their specific needs, using the same collection.
Design agency: evaluating a poster design
Imagine a design agency (Aesthetico) that is contracted to work on the poster design for a new movie.
Aesthetico's designers have arrived at this film poster design. They would now like to see how their poster compares with other movie posters in existence, and what types of movies those posters are for.
![Metropolis poster](https://upload.wikimedia.org/wikipedia/commons/7/78/1927_Boris_Bilinski_%281900-1948%29_Plakat_f%C3%BCr_den_Film_Metropolis%2C_Staatliche_Museen_zu_Berlin.jpg)
Luckily for them, the MovieNVDemo
collection has poster_title
named vectors which is primarily based on the poster design. So Aesthetico's designers can search against the poster_title
named vector and find movies that are similar to their poster design. And, they can then perform RAG to summarize the movies that are found.
Code
This query will find similar movies to the input image, and then provide insights using RAG.
import os
import weaviate
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_local(headers=headers)
def url_to_base64(url):
import requests
import base64
image_response = requests.get(url)
content = image_response.content
return base64.b64encode(content).decode("utf-8")
# Get the collection
movies = client.collections.get("MovieNVDemo")
# Perform query
src_img_path = "https://raw.githubusercontent.com/weaviate-tutorials/edu-datasets/main/img/1927_Boris_Bilinski_(1900-1948)_Plakat_f%C3%BCr_den_Film_Metropolis%2C_Staatliche_Museen_zu_Berlin.jpg"
query_b64 = url_to_base64(src_img_path)
# response = movies.generate.near_text(
# query="Science fiction film set in space",
response = movies.generate.near_image(
near_image=query_b64,
limit=10,
target_vector="poster_title", # The target vector to search against
grouped_task="What types of movies are these, and what kinds of audience might this set of movies be aimed at overall?",
grouped_properties=["title", "overview"] # Optional parameter; for reducing prompt length
)
# Inspect the response
for o in response.objects:
print(o.properties["title"]) # Print the title
print(response.generated) # Print the generated text (the commonalities between them)
client.close()
Output
This is an example output of the RAG query:
These movies can be categorized as action, science fiction, thriller, and drama. The audience for these movies would likely be fans of action-packed films with elements of suspense, mystery, and fantastical creatures or scenarios. These movies may appeal to a wide range of viewers, including fans of superhero movies, science fiction enthusiasts, and those who enjoy intense and thrilling storylines.
The designers at Aesthetico could use this to understand the types of movies that are similar to their poster design, and further inform their own design choices.
Search results
Predator 2
Inception
Mission: Impossible
The Dark Knight
Lost in Translation
Independence Day
Godzilla vs. Kong
Fargo
The Amazing Spider-Man
Godzilla
Film writers: evaluating ideas
Now, in another project, a set of writers at ScriptManiacs are working on a movie script for a science fiction film. They are working a few ideas for the movie title, and they want to see what kinds of imagery and themes are associated with each title.
They could also use the same collection to do what they want to do. In fact, they could run multiple queries against the same collection, each with a different target_vector
parameter.
The ScriptManiacs writers can:
- Search against the
title
named vector to find movies with similar titles; - Search against the
overview
named vector to find movies whose plots are similar to their title idea; and
Let's see how they could do it for a title - "Chrono Tides: The Anomaly Rift".
Code
This example finds entries in "MovieNVDemo" based on their similarity to "Chrono Tides: The Anomaly Rift", then instructs the large language model to find commonalities between them.
Note the for tgt_vector
loop, which allows the writers to run the same query against different named vectors.
import os
import weaviate
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_local(headers=headers)
# Get the collection
movies = client.collections.get("MovieNVDemo")
# Perform query
# Loop through the target vectors
for tgt_vector in ["title", "overview"]:
response = movies.generate.near_text(
query="Chrono Tides: The Anomaly Rift",
limit=5,
target_vector=tgt_vector, # The target vector to search against
grouped_task="What types of movies are these, and what kinds of audience might this set of movies be aimed at overall?",
grouped_properties=["title", "overview"] # Optional parameter; for reducing prompt length
)
# Inspect the response
for o in response.objects:
print(o.properties["title"]) # Print the title
print(response.generated) # Print the generated text (the commonalities between them)
client.close()
Output
The two queries produced quite different outputs to each other. When we search for titles most similar to "Chrono Tides: The Anomaly Rift", the results skew towards action/adventure films, while searching for an overview most similar to "Chrono Tides: The Anomaly Rift" include science fiction and adventure films.
Given the different results, the writers at ScriptManiacs could use this to understand the different themes and genres that are associated with their title idea, and further inform their own writing choices.
For example, if "Chrono Tides: The Anomaly Rift" is intended to be a science fiction film with an action/adventure skew, the title may be a good one. On the other hand, if the writers are looking for a more dramatic or romantic theme, they may need to reconsider the title.
The results of the overview search include multiple science-fiction and adventure films, indicating that the writes are on the right path of naming a science fiction movie as such.
Similar titles
According to our RAG query, movies with similar titles to "Chrono Tides: The Anomaly Rift" have the following commonalities:
These movies are action/adventure films that are likely aimed at a wide audience, including fans of fantasy, adventure, and romance genres. The Pirates of the Caribbean and Lara Croft movies are targeted towards fans of swashbuckling adventures and treasure hunting, while The Croods appeals to families and fans of animated films. The Twilight Saga targets fans of supernatural romance, and Meg 2: The Trench is aimed at fans of underwater thrillers and action movies. Overall, these movies cater to audiences who enjoy high-stakes adventures, fantastical elements, and dramatic storylines.
Search results
Pirates of the Caribbean: On Stranger Tides
Lara Croft: Tomb Raider
The Croods: A New Age
The Twilight Saga: Breaking Dawn - Part 1
Meg 2: The Trench
Similar overviews
While movies with overviews that are most similar to the search "Chrono Tides: The Anomaly Rift" have the following commonalities:
These movies can be categorized as science fiction and adventure films. They are aimed at audiences who enjoy stories about space exploration, ancient civilizations, dinosaurs, natural disasters, and mythical adventures. The target audience may include fans of action-packed and visually stunning movies with elements of fantasy and suspense.
Search results
Stargate
Interstellar
Jurassic Park III
2012
Moana
Questions and feedback
If you have any questions or feedback, let us know in the user forum.