GoogleGenAIDocumentEmbedder
GoogleGenAIDocumentEmbedder
이 컴포넌트가 계산한 벡터는 문서 컬렉션에 대해 임베딩 검색(embedding retrieval)을 수행하는 데 필요해요. 검색 시점에 쿼리를 나타내는 벡터를 문서의 벡터와 비교해 가장 유사하거나 관련 있는 문서를 찾아요.
출처: 문서
본문
GoogleGenAIDocumentEmbedder는 문서 메타데이터를 콘텐츠의 임베딩으로 풍부하게 만들어요. 문자열을 임베딩하려면 GoogleGenAITextEmbedder를 사용해야 해요. 컴포넌트는 Google AI Embedding 모델을 지원해요. gemini-embedding-001이 기본 모델이에요.
pip install google-genai-haystack
Authentication
Google Gen AI는 Gemini Developer API와 Vertex AI API 모두와 호환돼요. Gemini Developer API와 함께 사용하고 API 키를 얻으려면 Google AI Studio를, Vertex AI API와 함께 사용하려면 Google Cloud > Vertex AI를 방문하세요. 컴포넌트는 기본적으로 GOOGLE_API_KEY 또는 GEMINI_API_KEY 환경 변수를 사용해요. 그렇지 않으면 초기화 시점에 Secret과 Secret.from_token 정적 메서드로 API 키를 전달할 수 있어요:
embedder = GoogleGenAIDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
Gemini Developer API (API Key Authentication)
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
embedder = GoogleGenAIDocumentEmbedder()
Vertex AI (Application Default Credentials)
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
# Using Application Default Credentials (requires gcloud auth setup)
embedder = GoogleGenAIDocumentEmbedder(
api="vertex",
vertex_ai_project="my-project",
vertex_ai_location="us-central1",
)
Vertex AI (API Key Authentication)
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
embedder = GoogleGenAIDocumentEmbedder(api="vertex")
- 대표적인 파이프라인 위치: 인덱싱 파이프라인에서
DocumentWriter앞 - 필수 init 변수:
api_key— Google API 키.GOOGLE_API_KEY또는GEMINI_API_KEYenv var로 설정 가능. - 필수 run 변수:
documents— 임베딩할 문서 리스트 - 출력 변수:
documents— 임베딩으로 풍부해진 문서 리스트 /meta— 메타데이터 딕셔너리 - API reference: Google GenAI
- 패키지명:
google-genai-haystack
Usage
Embedding Metadata
텍스트 문서는 종종 메타데이터 집합과 함께 제공돼요. 그것이 독특하고 의미론적으로 의미가 있다면 문서 텍스트와 함께 임베딩해 검색을 개선할 수 있어요. Document Embedder로 이렇게 해요:
from haystack import Document
from haystack.utils import Secret
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
embedder = GoogleGenAIDocumentEmbedder(
api_key=Secret.from_token("<your-api-key>"),
meta_fields_to_embed=["title"],
)
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
On its own
컴포넌트를 단독으로 사용하는 방법이에요. Google API 키를 Secret으로 전달하거나 GOOGLE_API_KEY·GEMINI_API_KEY 환경 변수로 설정해야 해요. 아래 예시는 환경 변수를 설정했다고 전제해요.
from haystack import Document
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
doc = Document(content="I love pizza!")
document_embedder = GoogleGenAIDocumentEmbedder()
result = document_embedder.run([doc])
print(result["documents"][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]
In a pipeline
from haystack import Document
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAITextEmbedder,
)
from haystack_integrations.components.embedders.google_genai import (
GoogleGenAIDocumentEmbedder,
)
from haystack.components.writers import DocumentWriter
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
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", GoogleGenAIDocumentEmbedder())
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", GoogleGenAITextEmbedder())
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')