Skip to main content

Generative search

Overviewโ€‹

This page shows you how to perform generative searches using Weaviate.

To use the generative search feature, you must:

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:

  1. Retrieves two JeopardyQuestion objects related to World history,
  2. 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
  3. Retrieves a generated text for each object (2 total), and
  4. Returns the generated text as a part of each object, along with the question property.
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))
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:

  1. Retrieves two JeopardyQuestion objects related to World history,
  2. 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
  3. Retrieves a generated text for each object (2 total), and
  4. 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.

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))
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:

  1. Retrieves three JeopardyQuestion objects related to cute animals,
  2. Combines the user prompt with the set of retrieved objects to build the grouped task,
  3. Retrieves one generated text using the grouped task, and
  4. 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.

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))
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โ€‹

Requires Weaviate v1.18.3 or higher

You 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.

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))
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:

  1. Frequently Asked Questions. Or,
  2. Knowledge base of old issues. Or,
  3. For questions: Stackoverflow. Or,
  4. For more involved discussion: Weaviate Community Forum. Or,
  5. We also have a Slack channel.