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 weaviate, { GenerativeReturn, WeaviateClient } from "weaviate-client";
let client: WeaviateClient;
let response: GenerativeReturn<undefined>

// Instantiate your client (not shown). e.g.:
// const requestHeaders = {'X-VoyageAI-Api-Key': process.env.VOYAGEAI_API_KEY as string,}
// client = weaviate.connectToWeaviateCloud(..., headers: requestHeaders) or
// client = weaviate.connectToLocal(..., headers: requestHeaders)

async function urlToBase64(imageUrl: string) {
const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
const content = Buffer.from(arrayBuffer);
return content.toString('base64');
}

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

// Perform query
const srcImgPath = "https://github.com/weaviate-tutorials/edu-datasets/blob/main/img/International_Space_Station_after_undocking_of_STS-132.jpg?raw=true"
const queryB64 = await urlToBase64(srcImgPath)

response = await movies.generate.nearMedia(queryB64, "image",{
groupedTask: "What do these movies have in common?",
groupedProperties: ["title", "overview"] // Optional parameter; for reducing prompt length
},{
limit: 5
}
)

// Inspect the response
for (let item of response.objects) {
console.log('Title: ', item.properties.title) // Print the title
}

console.log(response.generated) // Print the generated text (the commonalities between them)

client.close()

Explain the code

For groupedTask queries, you simply pass on the prompt to the groupedTask 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 groupedProperties 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 groupedTask 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.