Retrieval Augmented Generation (RAG)
Retrieval Augmented Generation (RAG) combines information retrieval with generative AI models.
In Weaviate, a RAG query consists of two parts: a search query, and a prompt for the model. Weaviate first performs the search, then passes both the search results and your prompt to a generative AI model before returning the generated response.
How to configure RAG
To use RAG, a collection must be configured to use a generative model integration.
See the relevant model provider documentation for more details.
Named vectors
v1.24
Any vector-based search on collections with named vectors configured must include a target
vector name in the query. This allows Weaviate to find the correct vector to compare with the query vector.
- Python Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- GraphQL
from weaviate.classes.query import MetadataQuery
reviews = client.collections.get("WineReviewNV")
response = reviews.generate.near_text(
query="a sweet German white wine",
limit=2,
target_vector="title_country", # Specify the target vector for named vector collections
single_prompt="Translate this into German: {review_body}",
grouped_task="Summarize these review",
return_metadata=MetadataQuery(distance=True)
)
print(response.generated)
for o in response.objects:
print(o.properties)
print(o.generated)
# Unfortunately, named vectors are not suppored in the v3 API / Python client.
# Please upgrade to the v4 API / Python client to use named vectors.
const myNVCollection = client.collections.get('WineReviewNV');
const result = await myNVCollection.generate.nearText(
'a sweet German white wine',
{
singlePrompt: 'Translate this into German: {review_body}',
groupedTask: 'Summarize these review',
},
{
limit: 2,
targetVector: 'title_country',
}
);
console.log(result.generated); // print groupedTask result
for (let object of result.objects) {
console.log(JSON.stringify(object.properties, null, 2));
console.log(object.generated); // print singlePrompt result
}
result = await client.graphql
.get()
.withClassName('WineReviewNV')
.withNearText({
concepts: ['a sweet German white wine'],
targetVectors: ['title_country'],
})
.withGenerate({
singlePrompt: 'Translate this into German: {review_body}',
groupedTask: 'Summarize these reviews',
})
.withLimit(2)
.withFields('title review_body country')
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion(
limit: 2
nearText: {
concepts: ["animals in movies"]
}
where: {
path: ["round"]
operator: Equal
valueText: "Double Jeopardy!"
}
) {
question
answer
_additional {
generate(
singleResult: {
prompt: """
Translate this into German: {review_body}
"""
}
groupedResult: {
task: """
Summarize these reviews
"""
}
) {
singleResult
error
}
}
}
}
}
Single prompt search
Single prompt search returns a generated response for each object in the query results.
Define object properties
– using {prop-name}
syntax – to interpolate retrieved content in the prompt.
The properties you use in the prompt do not have to be among the properties you retrieve in the query.
- Python Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- Go
- GraphQL
prompt = "Convert this quiz question: {question} and answer: {answer} into a trivia tweet."
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="World history",
limit=2,
single_prompt=prompt
)
# print source properties and generated responses
for o in response.objects:
print(o.properties)
print(o.generated)
generate_prompt = "Convert this quiz question: {question} and answer: {answer} into a trivia tweet."
response = (
client.query
.get("JeopardyQuestion")
.with_generate(single_prompt=generate_prompt)
.with_near_text({
"concepts": ["World history"]
})
.with_limit(2)
).do()
print(json.dumps(response, indent=2))
const jeopardy = client.collections.get('JeopardyQuestion');
const prompt = `Convert this quiz question: {question} and answer: {answer} into a trivia tweet.`
const result = await jeopardy.generate.nearText('World history',
{ singlePrompt: prompt },
{ limit: 2 }
)
for (let object of result.objects) {
console.log(JSON.stringify(object.properties, null, 2));
console.log(object.generated); // print singlePrompt result
}
generatePrompt = 'Convert this quiz question: {question} and answer: {answer} into a trivia tweet.';
result = await client.graphql
.get()
.withClassName('JeopardyQuestion')
.withGenerate({
singlePrompt: generatePrompt,
})
.withNearText({
concepts: ['World history'],
})
.withFields('round')
.withLimit(2)
.do();
console.log(JSON.stringify(result, null, 2));
generatePrompt := "Convert the following into a question for twitter. Include emojis for fun, but do not include the answer: {question}."
gs := graphql.NewGenerativeSearch().SingleResult(generatePrompt)
response, err := client.GraphQL().Get().
WithClassName("JeopardyQuestion").
WithFields(
graphql.Field{Name: "question"},
).
WithGenerativeSearch(gs).
WithNearText((&graphql.NearTextArgumentBuilder{}).
WithConcepts([]string{"World history"})).
WithLimit(2).
Do(ctx)
generatePrompt := "Convert this quiz question: {question} and answer: {answer} into a trivia tweet."
gs := graphql.NewGenerativeSearch().SingleResult(generatePrompt)
response, err := client.GraphQL().Get().
WithClassName("JeopardyQuestion").
WithFields(
graphql.Field{Name: "question"},
graphql.Field{Name: "answer"},
).
WithGenerativeSearch(gs).
WithNearText((&graphql.NearTextArgumentBuilder{}).
WithConcepts([]string{"World history"})).
WithLimit(2).
Do(ctx)
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["World history"]
}
limit: 2
) {
_additional {
generate(
singleResult: {
prompt: """
Convert this quiz question: {question} and answer: {answer} into a trivia tweet.
"""
}
) {
singleResult
error
}
}
}
}
}
Example response
The output is like this:
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"generate": {
"error": null,
"singleResult": "Did you know that in the 19th century, the British Empire included one quarter of the world's land and people? The sun never set on it! #BritishEmpire #TriviaTuesday"
}
}
},
{
"_additional": {
"generate": {
"error": null,
"singleResult": "Did you know that Egypt had more kings than any other country in ancient history? From Menes to the Ptolemys, they ruled the land of the Nile. #Egypt #AncientHistory #Kings"
}
}
}
]
}
}
}
Grouped task search
Grouped task search returns one response that includes all of the query results. By default grouped task search uses all object properties
in the prompt.
- Python Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- Go
- GraphQL
task = "What do these animals have in common, if anything?"
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Cute animals",
limit=3,
grouped_task=task
)
# print the generated response
print(response.generated)
generate_prompt = "What do these animals have in common, if anything?"
response = (
client.query
.get("JeopardyQuestion", ["points"])
.with_generate(grouped_task=generate_prompt)
.with_near_text({
"concepts": ["Cute animals"]
})
.with_limit(3)
).do()
print(json.dumps(response, indent=2))
const jeopardy = client.collections.get('JeopardyQuestion');
const generatePrompt = `What do these animals have in common, if anything?`;
const result = await jeopardy.generate.nearText('Cute animals',
{ groupedTask: generatePrompt },
{ limit: 3 }
)
console.log(result.generated);
generatePrompt = 'What do these animals have in common, if anything?';
result = await client.graphql
.get()
.withClassName('JeopardyQuestion')
.withGenerate({
groupedTask: generatePrompt,
})
.withNearText({
concepts: ['Cute animals'],
})
.withFields('points')
.withLimit(3)
.do();
console.log(JSON.stringify(result, null, 2));
generatePrompt := "What do these animals have in common, if anything?"
gs := graphql.NewGenerativeSearch().GroupedResult(generatePrompt)
response, err := client.GraphQL().Get().
WithClassName("JeopardyQuestion").
WithFields(
graphql.Field{Name: "points"},
).
WithGenerativeSearch(gs).
WithNearText((&graphql.NearTextArgumentBuilder{}).
WithConcepts([]string{"Cute animals"})).
WithLimit(3).
Do(ctx)
generatePrompt := "What do these animals have in common, if anything?"
gs := graphql.NewGenerativeSearch().GroupedResult(generatePrompt, "answer", "question")
response, err := client.GraphQL().Get().
WithClassName("JeopardyQuestion").
WithFields(
graphql.Field{Name: "question"},
graphql.Field{Name: "points"},
).
WithGenerativeSearch(gs).
WithNearText((&graphql.NearTextArgumentBuilder{}).
WithConcepts([]string{"Australian animals"})).
WithLimit(3).
Do(ctx)
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["Cute animals"]
}
limit: 3
) {
points
_additional {
generate(
groupedResult: {
task: """
What do these animals have in common, if anything?
"""
}
) {
groupedResult
error
}
}
}
}
}
Example response
The output is like this:
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"generate": {
"error": null,
"groupedResult": "All of these animals are mammals."
}
},
"points": 400
},
{
"_additional": {
"generate": null
},
"points": 300
},
{
"_additional": {
"generate": null
},
"points": 400
}
]
}
}
}
Set grouped task prompt properties
v1.18.3
Define object properties
to use in the prompt. This limits the information in the prompt and reduces prompt length.
- Python Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- Go
- GraphQL
task = "What do these animals have in common, if anything?"
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Australian animals",
limit=3,
grouped_task=task,
grouped_properties=["answer", "question"]
)
# print the generated response
print(response.generated)
generate_prompt = "What do these animals have in common, if anything?"
response = (
client.query
.get("JeopardyQuestion", ["question points"])
.with_generate(
grouped_task=generate_prompt,
grouped_properties=["answer", "question"] # available since client version 3.19.2
)
.with_near_text({
"concepts": ["Australian animals"]
})
.with_limit(3)
).do()
print(json.dumps(response, indent=2))
const jeopardy = client.collections.get('JeopardyQuestion');
const generatePrompt = `What do these animals have in common, if anything?`;
const result = await jeopardy.generate.nearText('Australian animals',
{
groupedTask: generatePrompt,
groupedProperties: ['answer', 'question'],
},
{ limit: 3 }
)
console.log(result.generated);
generatePrompt = 'What do these animals have in common, if anything?';
result = await client.graphql
.get()
.withClassName('JeopardyQuestion')
.withGenerate({
groupedTask: generatePrompt,
groupedProperties: ['answer', 'question'], // available since client version 1.3.2
})
.withNearText({
concepts: ['Australian animals'],
})
.withFields('question points')
.withLimit(3)
.do();
console.log(JSON.stringify(result, null, 2));
generatePrompt := "What do these animals have in common, if anything?"
gs := graphql.NewGenerativeSearch().GroupedResult(generatePrompt, "answer", "question")
response, err := client.GraphQL().Get().
WithClassName("JeopardyQuestion").
WithFields(
graphql.Field{Name: "question"},
graphql.Field{Name: "points"},
).
WithGenerativeSearch(gs).
WithNearText((&graphql.NearTextArgumentBuilder{}).
WithConcepts([]string{"Australian animals"})).
WithLimit(3).
Do(ctx)
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["Australian animals"]
}
limit: 3
) {
question
points
_additional {
generate(
groupedResult: {
task: """
What do these animals have in common, if anything?
"""
properties: ["answer", "question"]
}
) {
groupedResult
error
}
}
}
}
}
Example response
The output is like this:
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"generate": {
"error": null,
"groupedResult": "All of the animals mentioned are native to Australia."
}
},
"points": 800,
"question": "Australians call this animal a jumbuck or a monkey"
},
{
"_additional": {
"generate": null
},
"points": 100,
"question": "An island named for the animal seen <a href=\"http://www.j-archive.com/media/2000-03-10_J_01.jpg\" target=\"_blank\">here</a> belongs to this country [kangaroo]"
},
{
"_additional": {
"generate": null
},
"points": 300,
"question": "Found chiefly in Australia, the wallaby is a smaller type of this marsupial"
}
]
}
}
}
Related pages
Questions and feedback
If you have any questions or feedback, let us know in the user forum.