Skip to main content

'Single prompt' generation

A 'single prompt' generation wil perform RAG queries on each retrieved object. This is useful when you want to transform each object separately, with the same prompt.

Code

This example finds entries in "Movie" whose vector best matches the query vector (for "dystopian future"). Then, instructs the large language model to translate the title of each movie into French.

Each of the results are then printed out to the console.

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_weaviate_cloud(..., headers=headers) or
# client = weaviate.connect_to_local(..., headers=headers)

# Get the collection
movies = client.collections.get("Movie")

# Perform query
response = movies.generate.near_text(
query="dystopian future",
limit=5,
single_prompt="Translate this into French: {title}"
)

# Inspect the response
for o in response.objects:
print(o.properties["title"]) # Print the title
print(o.generated) # Print the generated text (the title, in French)

client.close()

Explain the code

You must pass on one or more properties to the single_prompt parameter through braces, as we've done here with "... {title} ...". This will instruct Weaviate to pass on the title property from each retrieved object to the large language model.

Example results
In Time
À temps
Looper
Boucleur
I, Robot
Je, Robot
The Matrix
La Matrice
Children of Men
Les enfants des hommes

Response object

Each response object is similar to that from a regular search query, with an additional generated attribute. This attribute will contain the generated output for each object.

Questions and feedback

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