Skip to main content

Generative search

Overview

This page shows you how to perform generative searches.

A generative search uses a large language model (LLM) to generate text based on the search results and a user-provided prompt. This technique is also called retrieval augmented generation, or RAG.

Requirements

To use the generative search feature, you must:

  1. Configure Weaviate to use a generator module (generative-openai, generative-cohere, generative-palm),
  2. Configure the parameters for the generative-* module in the target class,
  3. Specify a query to retrieve one or more objects, and
  4. Provide a single prompt or a grouped task to generate text from.
How do I configure Weaviate with a generator module?

You must enable the desired generative search module and (optionally) specify the corresponding inference service (OpenAI, Cohere, PaLM) API key in the relevant Docker Compose file (e.g. docker-compose.yml), or (recommended) request that client code provide it with every request. You can generate this file using the Weaviate configuration tool.

Here are the relevant settings from the Docker Compose file. Ensure the corresponding environment variable is set (i.e. $OPENAI_APIKEY, $COHERE_APIKEY, or $PALM_APIKEY), unless you want the client to supply the API key (recommended).

services:
weaviate:
environment:
OPENAI_APIKEY: $OPENAI_APIKEY
ENABLE_MODULES: '...,generative-openai,...'
How do I set the generative module in the target class?

Where multiple generative modules are enabled, you must specify the generative module to be used in the moduleConfig section of the schema. For example, this configures the Article class to use the generative-openai module:

{
"classes": [
{
"class": "Article",
...,
"moduleConfig": {
"generative-openai": {}, // This will configure the 'Article' class to use the 'generative-openai' module
}
}
]
}

You can configure additional module parameters here also. Please refer to the "Schema configuration" section in the relevant module page.

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 this 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.
prompt = "Convert the following into a question for twitter. Include emojis for fun, but do not include the answer: {question}."

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="World history",
limit=2,
single_prompt=prompt
)

# print source questions and generated responses
for o in response.objects:
print(o.properties["question"])
print(o.generated)
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 this 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.

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)
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 this 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.

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

Added in v1.18.3

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 this example, the prompt only includes the question and answer properties. Note that the answer property is not explicitly retrieved in the query, but it is used by the prompt.

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 source properties
for o in response.objects:
print(o.properties)

# print the generated response
print(response.generated)
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"
}
]
}
}
}