'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" 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 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",{
singlePrompt: "Translate this into French: {title}"
}, {
limit: 5
})
// Inspect the response
for (let item of response.objects) {
console.log(item.properties.title)
console.log(item.generated)
}
client.close()
Explain the code
You must pass on one or more properties to the singlePrompt
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, let us know in the user forum.