Skip to main content

'Grouped task' generation

A 'grouped task' generation performs 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

Run this to find entries in "Movies" most similar to "dystopian future", find commonalities between them, and print out the results.

import os
import weaviate

# Instantiate your client (not shown). e.g.:
# client = weaviate.connect_to_weaviate_cloud(...) or
# client = weaviate.connect_to_local(...)

# Configure collection object
movies = client.collections.use("Movies")

# Perform query
response = movies.generate.near_text(
query="dystopian future",
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

RAG queries are accessed in Weaviate through the generate submodule of the collection object. Each generative query performs RAG in addition to the regular search query.

For grouped_task queries, provide the prompt to the grouped_task parameter. This instructs Weaviate to pass to the generative AI model:

  • text properties from all retrieved objects, and
  • the prompt
Example results
In Time
Looper
I, Robot
The Matrix
Children of Men
These movies all involve futuristic settings and explore themes related to the manipulation of time, technology, and the potential consequences of advancements in society. They also touch on issues such as inequality, control, and the impact of human actions on the future of humanity.

Optional parameters

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

Response object

A RAG query with the grouped_task parameter returns a response with an additional generative.text attribute. This attribute contains the generated output for the set of objects.

What's next?

In this course, you've learned how to work with Weaviate from collection creation, data import, searching, and retrieval-augmented generation (RAG). You can now apply these skills to build your own applications with Weaviate.

Login to track your progress