跳至主要内容
前往文档
⌘U
Weaviate 数据库

使用 Weaviate 的 APIs 和工具开发 AI 应用

部署

部署、配置和维护 Weaviate 数据库

Weaviate Agents

使用 Weaviate 构建和部署智能代理

Weaviate Cloud

在云端管理和扩展 Weaviate

更多资源

集成
贡献者指南
活动 & 工作坊
Weaviate Academy

需要帮助?

Weaviate Logo询问 AI 助手⌘K
社区论坛

检索增强生成 (RAG)

检索增强生成 (RAG) 将信息检索与生成式 AI 模型相结合。

在 Weaviate 中,RAG 查询包含两部分:搜索查询模型的提示语。Weaviate 首先执行搜索,然后将搜索结果和您的提示语传递给生成式 AI 模型,最后返回生成的响应。

您更喜欢自然语言查询吗?

查询代理将简单的英文问题自动转换为优化的 Weaviate 查询 - 无需手动构建查询。

仅云端

配置生成模型提供程序

新增于 v1.30

要使用具有 生成模型集成 的 RAG

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
from weaviate.classes.generate import GenerativeConfig
from weaviate.classes.query import MetadataQuery

reviews = client.collections.use("WineReviewNV")
response = reviews.generate.near_text(
query="a sweet German white wine",
limit=2,
target_vector="title_country",
single_prompt="Translate this into German: {review_body}",
grouped_task="Summarize these review",
generative_provider=GenerativeConfig.openai(),
)

for o in response.objects:
print(f"Properties: {o.properties}")
print(f"Single prompt result: {o.generative.text}")
print(f"Grouped task result: {response.generative.text}")
示例响应
Properties: {'country': 'Austria', 'title': 'Gebeshuber 2013 Frizzante Rosé Pinot Noir (Österreichischer Perlwein)', 'review_body': "With notions of cherry and cinnamon on the nose and just slight fizz, this is a refreshing, fruit-driven sparkling rosé that's full of strawberry and cherry notes—it might just be the very definition of easy summer wine. It ends dry, yet refreshing.", 'points': 85, 'price': 21.0}

Single prompt result: Mit Noten von Kirsche und Zimt in der Nase und nur leicht prickelnd, ist dies ein erfrischender, fruchtiger sprudelnder Rosé, der voller Erdbeer- und Kirschnoten steckt - es könnte genau die Definition von leichtem Sommerwein sein. Er endet trocken, aber erfrischend.

Properties: {'price': 27.0, 'points': 89, 'review_body': 'Beautifully perfumed, with acidity, white fruits and a mineral context. The wine is layered with citrus and lime, hints of fresh pineapple acidity. Screw cap.', 'title': 'Stadt Krems 2009 Steinterrassen Riesling (Kremstal)', 'country': 'Austria'}

Single prompt result: Wunderschön parfümiert, mit Säure, weißen Früchten und einem mineralischen Kontext. Der Wein ist mit Zitrus- und Limettennoten durchzogen, mit Anklängen von frischer Ananas-Säure. Schraubverschluss.

Grouped task result: The first review is for the Gebeshuber 2013 Frizzante Rosé Pinot Noir from Austria, describing it as a refreshing and fruit-driven sparkling rosé with cherry and cinnamon notes. It is said to be the perfect easy summer wine, ending dry yet refreshing.

The second review is for the Stadt Krems 2009 Steinterrassen Riesling from Austria, noting its beautiful perfume, acidity, white fruits, and mineral context. The wine is described as layered with citrus and lime flavors, with hints of fresh pineapple acidity. It is sealed with a screw cap.
提示

有关可用模型及其附加选项的更多信息,请参阅 模型提供程序部分

命名向量

新增于 v1.24

配置了 命名向量 的集合上的任何基于向量的搜索都必须在查询中包含一个 target 向量名称。这允许 Weaviate 找到要与查询向量进行比较的正确向量。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
from weaviate.classes.query import MetadataQuery

reviews = client.collections.use("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),
)

for o in response.objects:
print(f"Properties: {o.properties}")
print(f"Single prompt result: {o.generative.text}")
print(f"Grouped task result: {response.generative.text}")
示例响应
Properties: {'country': 'Austria', 'title': 'Gebeshuber 2013 Frizzante Rosé Pinot Noir (Österreichischer Perlwein)', 'review_body': "With notions of cherry and cinnamon on the nose and just slight fizz, this is a refreshing, fruit-driven sparkling rosé that's full of strawberry and cherry notes—it might just be the very definition of easy summer wine. It ends dry, yet refreshing.", 'points': 85, 'price': 21.0}

Single prompt result: Mit Noten von Kirsche und Zimt in der Nase und nur leicht prickelnd, ist dies ein erfrischender, fruchtiger sprudelnder Rosé, der voller Erdbeer- und Kirschnoten steckt - es könnte genau die Definition von leichtem Sommerwein sein. Er endet trocken, aber erfrischend.

Properties: {'price': 27.0, 'points': 89, 'review_body': 'Beautifully perfumed, with acidity, white fruits and a mineral context. The wine is layered with citrus and lime, hints of fresh pineapple acidity. Screw cap.', 'title': 'Stadt Krems 2009 Steinterrassen Riesling (Kremstal)', 'country': 'Austria'}

Single prompt result: Wunderschön parfümiert, mit Säure, weißen Früchten und einem mineralischen Kontext. Der Wein ist mit Zitrus- und Limettennoten durchzogen, mit Anklängen von frischer Ananas-Säure. Schraubverschluss.

Grouped task result: The first review is for the Gebeshuber 2013 Frizzante Rosé Pinot Noir from Austria, describing it as a refreshing and fruit-driven sparkling rosé with cherry and cinnamon notes. It is said to be the perfect easy summer wine, ending dry yet refreshing.

The second review is for the Stadt Krems 2009 Steinterrassen Riesling from Austria, noting its beautiful perfume, acidity, white fruits, and mineral context. The wine is described as layered with citrus and lime flavors, with hints of fresh pineapple acidity. It is sealed with a screw cap.

单提示搜索为查询结果中的每个对象返回生成的响应。
定义对象 属性 – 使用 {prop-name} 语法 – 以在提示语中插入检索到的内容。
在提示语中使用的属性不必是您在查询中检索的属性之一。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
prompt = (
"Convert this quiz question: {question} and answer: {answer} into a trivia tweet."
)

jeopardy = client.collections.use("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(f"Properties: {o.properties}")
print(f"Single prompt result: {o.generative.text}")
示例响应
Property 'question': Including, in 19th century, one quarter of world's land & people, the sun never set on it
Single prompt result: 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? ☀️🌍 #historybuffs #funfact

Property 'question': From Menes to the Ptolemys, this country had more kings than any other in ancient history
Single prompt result: Which country in ancient history had more kings than any other, from Menes to the Ptolemys? 👑🏛️ #historybuffs #ancientkings

附加参数

新增于 v1.30

您可以使用生成参数来指定执行单提示搜索时的其他选项

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
from weaviate.classes.generate import GenerativeConfig, GenerativeParameters

prompt = GenerativeParameters.single_prompt(
prompt="Convert this quiz question: {question} and answer: {answer} into a trivia tweet.",
metadata=True,
debug=True,
)

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

# print source properties and generated responses
for o in response.objects:
print(f"Properties: {o.properties}")
print(f"Single prompt result: {o.generative.text}")
print(f"Debug: {o.generative.debug}")
print(f"Metadata: {o.generative.metadata}")
示例响应
Properties: {'points': 400, 'answer': 'the British Empire', 'air_date': datetime.datetime(1984, 12, 10, 0, 0, tzinfo=datetime.timezone.utc), 'question': "Including, in 19th century, one quarter of world's land & people, the sun never set on it", 'round': 'Double Jeopardy!'}

Single prompt result: Did you know that in the 19th century, the sun never set on the British Empire, which included one quarter of the world's land and people? #triviatuesday #britishempire

Debug: full_prompt: "Convert this quiz question: Including, in 19th century, one quarter of world\'s land & people, the sun never set on it and answer: the British Empire into a trivia tweet."

Metadata: usage {
prompt_tokens: 46
completion_tokens: 43
total_tokens: 89
}

Properties: {'points': 400, 'answer': 'Egypt', 'air_date': datetime.datetime(1989, 9, 5, 0, 0, tzinfo=datetime.timezone.utc), 'question': 'From Menes to the Ptolemys, this country had more kings than any other in ancient history', 'round': 'Double Jeopardy!'}

Single prompt result: Did you know that Egypt had more kings than any other country in ancient history, from Menes to the Ptolemys? #triviathursday #ancienthistory

Debug: full_prompt: "Convert this quiz question: From Menes to the Ptolemys, this country had more kings than any other in ancient history and answer: Egypt into a trivia tweet."

Metadata: usage {
prompt_tokens: 42
completion_tokens: 36
total_tokens: 78
}

分组任务搜索返回一个响应,其中包含所有查询结果。默认情况下,分组任务搜索在提示语中使用所有对象 属性

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
task = "What do these animals have in common, if anything?"

jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Cute animals",
limit=3,
grouped_task=task,
)

# print the generated response
print(f"Grouped task result: {response.generative.text}")
示例响应
Grouped task result: All of these animals are mammals.

设置分组任务提示属性

定义要在提示语中使用的对象 属性。这限制了提示语中的信息并缩短了提示语的长度。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
task = "What do these animals have in common, if anything?"

jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Australian animals",
limit=3,
grouped_task=task,
grouped_properties=["answer", "question"],
)

# print the generated response
for o in response.objects:
print(f"Properties: {o.properties}")
print(f"Grouped task result: {response.generative.text}")
示例响应
Grouped task result: The commonality among these animals is that they are all native to Australia.

附加参数

新增于 v1.30

您可以使用生成参数来指定执行分组任务时的其他选项

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
from weaviate.classes.generate import GenerativeConfig, GenerativeParameters

grouped_task = GenerativeParameters.grouped_task(
prompt="What do these animals have in common, if anything?",
metadata=True,
)

jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Cute animals",
limit=3,
grouped_task=grouped_task,
generative_provider=GenerativeConfig.openai()
)

# print the generated response
print(f"Grouped task result: {response.generative.text}")
print(f"Metadata: {o.generative.metadata}")
示例响应
Grouped task result: They are all animals.
Metadata: usage {
prompt_tokens: 42
completion_tokens: 36
total_tokens: 78
}

处理图像

您还可以将图像作为输入的一部分,在单提示和分组任务中执行检索增强生成。以下字段可用于使用图像进行生成搜索

  • images:图像字节的 base64 编码字符串。
  • image_properties:存储图像以提供附加上下文的 Weaviate 中的属性名称。
py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue
import base64
import requests
from weaviate.classes.generate import GenerativeConfig, GenerativeParameters

src_img_path = "https://images.unsplash.com/photo-1459262838948-3e2de6c1ec80?w=500&h=500&fit=crop"
base64_image = base64.b64encode(requests.get(src_img_path).content).decode('utf-8')

prompt = GenerativeParameters.grouped_task(
prompt="Formulate a Jeopardy!-style question about this image",
images=[base64_image], # A list of base64 encoded strings of the image bytes
# image_properties=["img"], # Properties containing images in Weaviate
)

jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.generate.near_text(
query="Australian animals",
limit=3,
grouped_task=prompt,
grouped_properties=["answer", "question"],
generative_provider=GenerativeConfig.anthropic(
max_tokens=1000
),
)

# Print the source property and the generated response
for o in response.objects:
print(f"Properties: {o.properties}")
print(f"Grouped task result: {response.generative.text}")
示例响应
Properties: {'points': 800, 'answer': 'sheep', 'air_date': datetime.datetime(2007, 12, 13, 0, 0, tzinfo=datetime.timezone.utc), 'question': 'Australians call this animal a jumbuck or a monkey', 'round': 'Jeopardy!'}
Properties: {'points': 100, 'answer': 'Australia', 'air_date': datetime.datetime(2000, 3, 10, 0, 0, tzinfo=datetime.timezone.utc), '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]', 'round': 'Jeopardy!'}
Properties: {'points': 300, 'air_date': datetime.datetime(1996, 7, 18, 0, 0, tzinfo=datetime.timezone.utc), 'answer': 'Kangaroo', 'question': 'Found chiefly in Australia, the wallaby is a smaller type of this marsupial', 'round': 'Jeopardy!'}

Grouped task result: I'll formulate a Jeopardy!-style question based on the image of the koala:

Answer: This Australian marsupial, often mistakenly called a bear, spends most of its time in eucalyptus trees.

Question: What is a koala?

问题和反馈

如果您有任何问题或反馈,请在 用户论坛 中告诉我们。