WeaviateDocumentStore
Weaviate 是一个多用途的向量数据库,可以存储嵌入和数据对象,非常适合多模态应用。
该WeaviateDocumentStore 可以连接到任何 Weaviate 实例,无论是运行在 Weaviate Cloud Services、Kubernetes 还是本地 Docker 容器中。
安装
您可以通过以下方式轻松安装 Weaviate Haystack 集成:
pip install weaviate-haystack
初始化
嵌入式 Weaviate
要使用将 WeaviateDocumentStore 作为临时实例,并将其初始化为 “嵌入式”
from haystack_integrations.document_stores.weaviate import WeaviateDocumentStore
from weaviate.embedded import EmbeddedOptions
document_store = WeaviateDocumentStore(embedded_options=EmbeddedOptions())
Docker
您可以使用在本地 Docker 容器中初始化 WeaviateDocumentStore。以下是一个最小化的docker-compose.yml 示例:
---
version: '3.4'
services:
weaviate:
command:
- --host
- 0.0.0.0
- --port
- '8080'
- --scheme
- http
image: semitechnologies/weaviate:1.30.17
ports:
- 8080:8080
- 50051:50051
volumes:
- weaviate_data:/var/lib/weaviate
restart: 'no'
environment:
QUERY_DEFAULTS_LIMIT: 25
AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true'
PERSISTENCE_DATA_PATH: '/var/lib/weaviate'
DEFAULT_VECTORIZER_MODULE: 'none'
ENABLE_MODULES: ''
CLUSTER_HOSTNAME: 'node1'
volumes:
weaviate_data:
...
通过此示例,我们明确启用了无需身份验证的访问,因此您无需设置用户名、密码或 API 密钥即可连接到我们的本地实例。强烈建议不要在生产环境中使用此设置。有关详细信息,请参阅 授权 部分。
使用以下命令启动您的容器:docker compose up -d,然后使用以下命令初始化 Document Store:
from haystack_integrations.document_stores.weaviate.document_store import WeaviateDocumentStore
from haystack import Document
document_store = WeaviateDocumentStore(url="https://:8080")
document_store.write_documents([
Document(content="This is first"),
Document(content="This is second")
])
print(document_store.count_documents())
Weaviate 云服务
要使用 Weaviate 托管云服务,请先创建您的 Weaviate 集群。
然后,使用您在 Weaviate 账户 中找到的 API 密钥和 URL 初始化WeaviateDocumentStore。
from haystack_integrations.document_stores.weaviate import WeaviateDocumentStore, AuthApiKey
from haystack import Document
import os
os.environ["WEAVIATE_API_KEY"] = "YOUR-API-KEY"
auth_client_secret = AuthApiKey()
document_store = WeaviateDocumentStore(url="YOUR-WEAVIATE-URL",
auth_client_secret=auth_client_secret)
授权
我们在auth 包中提供了一些实用程序类来处理使用不同凭据的授权。每个类都存储不同的 密钥,并在需要时从环境变量中检索它们。
这些类的默认环境变量是:
AuthApiKeyWEAVIATE_API_KEY
AuthBearerTokenWEAVIATE_ACCESS_TOKENWEAVIATE_REFRESH_TOKEN
AuthClientCredentialsWEAVIATE_CLIENT_SECRETWEAVIATE_SCOPE
AuthClientPasswordWEAVIATE_USERNAMEWEAVIATE_PASSWORDWEAVIATE_SCOPE
您可以根据需要轻松更改环境变量。在下面的代码片段中,我们指示AuthApiKey 查找MY_ENV_VAR.
from haystack_integrations.document_stores.weaviate.auth import AuthApiKey
from haystack.utils.auth import Secret
AuthApiKey(api_key=Secret.from_env_var("MY_ENV_VAR"))
支持的 Retrievers
WeaviateBM25Retriever:一种基于关键词的检索器,用于从 Document Store 中检索与查询匹配的文档。
WeaviateEmbeddingRetriever:比较查询和文档的嵌入,并检索与查询最相关的文档。
更新于 20 天前
