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 "MovieMM" based on their similarity to this image of the International Space Station. 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_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("MovieMM")

# Perform query
src_img_path = "https://github.com/weaviate-tutorials/edu-datasets/blob/main/img/International_Space_Station_after_undocking_of_STS-132.jpg?raw=true"
query_b64 = url_to_base64(src_img_path)

response = movies.generate.near_image(
near_image=query_b64,
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
Interstellar
Interstellaire
Gravity
Gravité
Arrival
Arrivée
Armageddon
Armageddon
Godzilla
Godzilla

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, please let us know on our forum. For example, you can: