Skip to main content

'Grouped task' generation

A 'grouped task' generation wil perform RAG queries on the set of retrieved objects. This is useful when you want to transform the set of objects as a whole, with one 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 find commonalities between them.

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,
grouped_task="What do these movies have in common?",
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()

Explain the code

For grouped_task queries, you simply pass on the prompt to the grouped_task parameter. This will instruct Weaviate to pass on the:

  • text properties from all retrieved objects, and
  • the prompt

to the large language model.

Example results
Interstellar
Gravity
Arrival
Armageddon
Godzilla
These movies all involve space exploration, extraterrestrial beings, or catastrophic events threatening Earth. They all deal with themes of survival, human ingenuity, and the unknown mysteries of the universe.

Optional parameters

You can also pass on a list of properties to be used, as the grouped_properties parameter. This can be useful to reduce the amount of data passed on to the large language model and omit irrelevant properties.

Response object

A RAG query with the grouped_task parameter will return a response with an additional generated attribute. This attribute will contain the generated output for the set of objects.

Questions and feedback

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