Generative search
Overviewโ
This page shows you how to perform generative
searches using Weaviate.
To use the generative search feature, you must:
- Specify a query to retrieve one or more objects, and
- Provide a
single prompt
or agrouped task
to generate text from.
Single promptโ
A single prompt generative search returns a generated response for each object in the query results. For single prompt generative searches, you must specify which object properties to use in the prompt.
In the below example, the query:
- Retrieves two
JeopardyQuestion
objects related toWorld history
, - Prepares a prompt for each object, based on the prompt
"Convert the following into a question for twitter. Include emojis for fun, but do not include the answer: {question}."
, where{question}
is an object property, and - Retrieves a generated text for each object (2 total), and
- Returns the generated text as a part of each object, along with the
question
property.
- Python
- JavaScript/TypeScript
- GraphQL
generate_prompt = "Convert the following into a question for twitter. Include emojis for fun, but do not include the answer: {question}."
response = (
client.query
.get("JeopardyQuestion", ["question"])
.with_generate(single_prompt=generate_prompt)
.with_near_text({
"concepts": ["World history"]
})
.with_limit(2)
).do()
print(json.dumps(response, indent=2))
generatePrompt = `Convert the following into a question for twitter.
Include emojis for fun, but do not include the answer: {question}.`;
result = await client.graphql
.get()
.withClassName('JeopardyQuestion')
.withGenerate({
singlePrompt: generatePrompt,
})
.withNearText({
concepts: ['World history'],
})
.withLimit(2)
.withFields('question')
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["World history"]
}
limit: 2
) {
question
_additional {
generate(
singleResult: {
prompt: """
Convert the following into a question for twitter. Include emojis for fun, but do not include the answer: {question}.
"""
}
) {
singleResult
error
}
}
}
}
}
Example response
It should produce a response like the one below:
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"generate": {
"error": null,
"singleResult": "\ud83c\udf0e\ud83c\udf1e Did you know that in the 19th century, one quarter of the world's land and people were part of an empire where the sun never set? #history #funfact"
}
},
"question": "Including, in 19th century, one quarter of world's land & people, the sun never set on it"
},
{
"_additional": {
"generate": {
"error": null,
"singleResult": "\ud83e\udd14 Which country had more kings than any other in ancient history, from Menes to the Ptolemys? \ud83d\udc51\ud83c\udfdb\ufe0f #history #ancientworld"
}
},
"question": "From Menes to the Ptolemys, this country had more kings than any other in ancient history"
}
]
}
}
}
Single prompt property selectionโ
When using generative search with single prompts, you must specify which object properties to use in the prompt.
The properties to use as a part of the prompt do not need to be among the properties retrieved in the query.
In the below example, the query:
- Retrieves two
JeopardyQuestion
objects related toWorld history
, - Prepares a prompt for each object, based on the prompt
"Convert this quiz question: {question} and answer: {answer} into a trivia tweet.
where{question}
and{answer}
are object properties, and - Retrieves a generated text for each object (2 total), and
- Returns the generated text as a part of each object.
Note that the question
and answer
properties are not retrieved in the query, but are used in the prompt.
- Python
- JavaScript/TypeScript
- GraphQL
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))
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));
{
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
It should produce a response like the one below:
{
"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โ
A grouped task works by generating a response for the entire query results set.
When using generative search with a grouped task, the required parameter is the user prompt. By default, the entire set of properties are included in the combined prompt unless specified otherwise.
Exampleโ
In the below example, the query:
- Retrieves three
JeopardyQuestion
objects related tocute animals
, - Combines the user prompt with the set of retrieved objects to build the grouped task,
- Retrieves one generated text using the grouped task, and
- Returns the generated text as a part of the first object returned, as well as the requested
points
property.
Note that the prompt includes information about the type of the animal (from the answer
property), even though the answer
property is not explicitly retrieved.
- Python
- JavaScript/TypeScript
- GraphQL
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))
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));
{
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
It should produce a response like the one below:
{
"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
}
]
}
}
}
Grouped task property selectionโ
v1.18.3
or higherYou can specify which properties will be included in the grouped task
prompt. Use this to limit the information provided in the prompt, and to reduce the prompt length.
In the below example, the prompt will only include the question
and answer
properties. Note that the answer
property is not explicitly retrieved in the query, but is used by the prompt.
- Python
- JavaScript/TypeScript
- GraphQL
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))
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));
{
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
It should produce a response like the one below:
{
"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"
}
]
}
}
}
More Resourcesโ
If you can't find the answer to your question here, please look at the:
- Frequently Asked Questions. Or,
- Knowledge base of old issues. Or,
- For questions: Stackoverflow. Or,
- For more involved discussion: Weaviate Community Forum. Or,
- We also have a Slack channel.