WeaviateDocumentStore

WeaviateDocumentStore

Haystack에서 Weaviate를 문서 저장소로 쓰는 방법을 알아봐요. WeaviateDocumentStore는 임베딩과 데이터 객체를 모두 저장할 수 있는 다목적 벡터 DB인 Weaviate에 연결해요. 멀티모달(multi-modality) 작업에 좋은 선택이에요.

WeaviateDocumentStore는 Weaviate Cloud Services, Kubernetes, 로컬 Docker 컨테이너 등 어디서 실행 중인 Weaviate 인스턴스든 연결할 수 있어요.

설치 (Installation)

Weaviate Haystack 통합은 아래처럼 간단히 설치할 수 있어요.

pip install weaviate-haystack

초기화 (Initialization)

Weaviate Embedded

WeaviateDocumentStore를 임시 인스턴스로 쓰려면 "Embedded" 방식으로 초기화해요.

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은 대략 이렇게 생겼어요.

---
services:
  weaviate:
    command:
    - --host
    - 0.0.0.0
    - --port
    - '8080'
    - --scheme
    - http
    image: semitechnologies/weaviate:1.36.2
    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:
...

:::warning 이 예시에서는 인증 없이 접근을 명시적으로 허용해요. 그래서 로컬 인스턴스에 연결할 때 사용자 이름, 비밀번호, API 키를 설정하지 않아도 돼요. 다만 운영 환경에서는 이렇게 하는 걸 강력히 권장하지 않아요. 자세한 내용은 authorization 섹션을 참고하세요. :::

docker compose up -d로 컨테이너를 시작하고 나서 Document Store를 아래처럼 초기화해요.

from haystack_integrations.document_stores.weaviate.document_store import (
    WeaviateDocumentStore,
)
from haystack import Document

document_store = WeaviateDocumentStore(url="http://localhost:8080")
document_store.write_documents(
    [Document(content="This is first"), Document(content="This is second")],
)
print(document_store.count_documents())

Weaviate Cloud Service

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

인증 (Authorization)

auth 패키지에 서로 다른 자격 증명으로 인증을 처리하는 유틸리티 클래스를 제공해요. 각 클래스는 각기 다른 secret을 저장하고, 필요할 때 환경 변수에서 가져와요.

클래스별 기본 환경 변수는 다음과 같아요.

  • AuthApiKey
    • WEAVIATE_API_KEY
  • AuthBearerToken
    • WEAVIATE_ACCESS_TOKEN
    • WEAVIATE_REFRESH_TOKEN
  • AuthClientCredentials
    • WEAVIATE_CLIENT_SECRET
    • WEAVIATE_SCOPE
  • AuthClientPassword
    • WEAVIATE_USERNAME
    • WEAVIATE_PASSWORD
    • WEAVIATE_SCOPE

필요하면 환경 변수를 쉽게 바꿀 수 있어요. 아래 스니펫에서는 AuthApiKeyMY_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"))

지원하는 Retriever

  • WeaviateBM25Retriever: 키워드 기반 Retriever로, Document Store에서 쿼리와 매칭되는 문서를 가져와요.
  • WeaviateEmbeddingRetriever: 쿼리와 문서 임베딩을 비교해 쿼리와 가장 관련 있는 문서를 가져와요.

출처: 공식문서