NVIDIA 生成式 AI 与 Weaviate
v1.28.5 和 v1.29.0 中添加Weaviate 与 NVIDIA NIM API 的集成使您可以直接从 Weaviate 访问其模型的功能。
配置 Weaviate 集合以使用 NVIDIA 的生成式 AI 模型。Weaviate 将使用您提供的 NVIDIA NIM API 密钥,使用指定的模型执行检索增强生成 (RAG)。
更具体地说,Weaviate 将执行搜索,检索最相关的对象,然后将它们传递给 NVIDIA 上的生成式模型以生成输出。

要求
Weaviate 配置
您的 Weaviate 实例必须配置为使用 NVIDIA 生成式 (generative-nvidia) 模块。
对于 Weaviate Cloud (WCD) 用户
此集成在 Weaviate Cloud (WCD) 实例上默认启用。
API 凭证
您必须向 Weaviate 提供有效的 API 密钥才能进行此集成。请访问 NVIDIA 注册并获取 API 密钥。
使用以下方法之一向 Weaviate 提供 API 密钥
- 设置可供 Weaviate 使用的
NVIDIA_APIKEY环境变量。 - 在运行时提供 token,如下例所示。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
import weaviate
from weaviate.classes.init import Auth
import os
# Recommended: save sensitive data as environment variables
nvidia_key = os.getenv("NVIDIA_APIKEY")
headers = {
"X-NVIDIA-Api-Key": nvidia_key,
}
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url, # `weaviate_url`: your Weaviate URL
auth_credentials=Auth.api_key(weaviate_key), # `weaviate_key`: your Weaviate API key
headers=headers
)
# Work with Weaviate
client.close()
配置集合
集合的 generative 模型集成配置从 v1.25.23、v1.26.8 和 v1.27.1 开始是可变的。有关如何更新集合配置的详细信息,请参阅 此部分。
配置 Weaviate 索引,如下所示,以使用 NVIDIA 生成式 AI 模型
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
client.collections.create(
"DemoCollection",
generative_config=Configure.Generative.nvidia()
# Additional parameters not shown
)
选择一个模型
您可以指定 Weaviate 要使用的 可用模型 之一,如以下配置示例所示
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
client.collections.create(
"DemoCollection",
generative_config=Configure.Generative.nvidia(
model="nvidia/llama-3.1-nemotron-70b-instruct"
)
# Additional parameters not shown
)
您可以 指定 Weaviate 要使用的 可用模型 之一。如果未指定模型,则使用 默认模型。
生成参数
配置以下生成参数以自定义模型行为。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
client.collections.create(
"DemoCollection",
generative_config=Configure.Generative.nvidia(
# # These parameters are optional
# base_url="https://integrate.api.nvidia.com/v1",
# model="meta/llama-3.3-70b-instruct",
# temperature=0.7,
# max_tokens=1024
)
# Additional parameters not shown
)
有关模型参数的更多详细信息,请参阅 NVIDIA API 文档。
在运行时选择模型
除了在创建集合时设置默认模型提供程序之外,您还可以在查询时覆盖它。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
from weaviate.classes.generate import GenerativeConfig
collection = client.collections.use("DemoCollection")
response = collection.generate.near_text(
query="A holiday film",
limit=2,
grouped_task="Write a tweet promoting these two movies",
generative_provider=GenerativeConfig.nvidia(
# # These parameters are optional
# base_url="https://integrate.api.nvidia.com/v1",
# model="meta/llama-3.3-70b-instruct",
# temperature=0.7,
# max_tokens=1024
),
# Additional parameters not shown
)
检索增强生成
配置生成式 AI 集成后,执行 RAG 操作,使用 单提示 或 分组任务 方法。
单提示

要为搜索结果中的每个对象生成文本,请使用单提示方法。
以下示例为 n 个搜索结果中的每一个生成输出,其中 n 由 limit 参数指定。
在创建单提示查询时,使用大括号 {} 来插值您希望 Weaviate 传递给语言模型的对象属性。例如,要传递对象的 title 属性,请在查询中包含 {title}。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
collection = client.collections.use("DemoCollection")
response = collection.generate.near_text(
query="A holiday film", # The model provider integration will automatically vectorize the query
single_prompt="Translate this into French: {title}",
limit=2
)
for obj in response.objects:
print(obj.properties["title"])
print(f"Generated output: {obj.generated}") # Note that the generated output is per object
分组任务

要为整个搜索结果集生成一个文本,请使用分组任务方法。
换句话说,当您有 n 个搜索结果时,生成模型为整个组生成一个输出。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
collection = client.collections.use("DemoCollection")
response = collection.generate.near_text(
query="A holiday film", # The model provider integration will automatically vectorize the query
grouped_task="Write a fun tweet to promote readers to check out these films.",
limit=2
)
print(f"Generated output: {response.generative.text}") # Note that the generated output is per query
for obj in response.objects:
print(obj.properties["title"])
参考
可用模型
您可以使用任何生成式模型 在 NVIDIA NIM API 上 与 Weaviate 一起使用。
默认模型为 nvidia/llama-3.1-nemotron-51b-instruct。
更多资源
其他集成
代码示例
配置完集成后,Weaviate 中的数据管理和搜索操作与任何其他集合的工作方式相同。请参阅以下非模型特定的示例
参考文献
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
