WatsonxTextEmbedder
WatsonxTextEmbedder
IBM watsonx.ai의 임베딩 모델로 문자열을 벡터로 임베딩해 주는 컴포넌트예요. 쿼리를 임베딩 검색에 넘기기 전에 임베딩할 때 사용해요.
파이프라인에서 가장 흔한 위치: 쿼리/RAG 파이프라인의 임베딩 Retriever 앞
필수 init 변수: api_key — IBM Cloud API 키. WATSONX_API_KEY 환경 변수로도 설정할 수 있어요. / project_id — IBM Cloud 프로젝트 ID. WATSONX_PROJECT_ID 환경 변수로도 설정할 수 있어요.
필수 run 변수: text — 임베딩할 문자열
출력 변수: embedding — float 리스트 / meta — 메타데이터 문자열 딕셔너리
API 레퍼런스: Watsonx
GitHub 링크: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx
패키지 이름: watsonx-haystack
출처: 문서
본문
개요 (Overview)
WatsonxTextEmbedder는 단순한 문자열(예: 쿼리)을 벡터로 임베딩해요. 문서 리스트를 임베딩할 때는 WatsonxDocumentEmbedder를 사용해요.
이 컴포넌트는 ibm/slate-30m-english-rtrvr-v2 같은 IBM watsonx.ai 임베딩 모델을 지원해요. 기본 모델은 ibm/slate-30m-english-rtrvr-v2이에요. 지원되는 전체 모델 목록은 IBM의 모델 문서에서 확인할 수 있어요.
Haystack에서 이 통합을 쓰려면 설치해요:
pip install watsonx-haystack
이 컴포넌트는 기본적으로 WATSONX_API_KEY와 WATSONX_PROJECT_ID 환경 변수를 사용해요. 그렇지 않으면 초기화할 때 api_key와 project_id로 API 자격 증명을 넘길 수 있어요:
embedder = WatsonxTextEmbedder(
api_key=Secret.from_token("<your-api-key>"),
project_id=Secret.from_token("<your-project-id>"),
)
IBM Cloud 자격 증명을 얻으려면 https://cloud.ibm.com/로 가면 돼요.
사용법 (Usage)
WatsonxTextEmbedder를 쓰려면 watsonx-haystack 패키지를 설치해요:
pip install watsonx-haystack
단독으로 사용하기 (On its own)
먼저 WATSONX_API_KEY와 WATSONX_PROJECT_ID를 환경 변수로 설정하거나 직접 전달하는 걸 기억하세요.
컴포넌트를 단독으로 쓰는 방법은 이래요:
from haystack_integrations.components.embedders.watsonx.text_embedder import (
WatsonxTextEmbedder,
)
text_embedder = WatsonxTextEmbedder()
result = text_embedder.run(text="I love pizza!")
print(result["embedding"])
# [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
파이프라인 안에서 사용하기 (In a pipeline)
from haystack import Document, Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.writers import DocumentWriter
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack_integrations.components.embedders.watsonx.text_embedder import (
WatsonxTextEmbedder,
)
from haystack_integrations.components.embedders.watsonx.document_embedder import (
WatsonxDocumentEmbedder,
)
document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
documents = [
Document(content="My name is Wolfgang and I live in Berlin"),
Document(content="I saw a black horse running"),
Document(content="Germany has many big cities"),
]
indexing_pipeline = Pipeline()
indexing_pipeline.add_component("embedder", WatsonxDocumentEmbedder())
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
indexing_pipeline.connect("embedder", "writer")
indexing_pipeline.run({"embedder": {"documents": documents}})
query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", WatsonxTextEmbedder())
query_pipeline.add_component(
"retriever",
InMemoryEmbeddingRetriever(document_store=document_store),
)
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
query = "Who lives in Berlin?"
result = query_pipeline.run({"text_embedder": {"text": query}})
print(result["retriever"]["documents"][0])
# Document(id=..., content: 'My name is Wolfgang and I live in Berlin', score: ...)