文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

VertexAIDocumentEmbedder

此组件通过 VertexAI Embeddings API 使用模型计算文档的嵌入。

🚧

弃用通知

此集成使用了已弃用的 google-generativeai SDK,该 SDK 将于 2025 年 8 月后停止支持。

我们建议改用新的 GoogleGenAIDocumentEmbedder 集成。

pipeline 中的最常见位置在索引管道中的 DocumentWriter 之前
必需的初始化变量"model": 通过 VertexAI Embeddings API 使用的模型
强制运行变量“documents”:要计算 embedding 的文档列表
输出变量“documents”: 一份包含嵌入的文档列表
API 参考Google Vertex
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex

VertexAIDocumentEmbedder 使用其内容的嵌入来丰富文档的元数据。要嵌入字符串,请使用 VertexAITextEmbedder

要使用VertexAIDocumentEmbedder,使用以下方式初始化它

  • model:支持的模型有
    • "text-embedding-004"
    • "text-embedding-005"
    • "textembedding-gecko-multilingual@001"
    • "text-multilingual-embedding-002"
    • "text-embedding-large-exp-03-07"
  • task_type:默认值为 "RETRIEVAL_DOCUMENT”。您可以在官方 Google 文档中找到所有任务类型。

身份验证

VertexAIDocumentEmbedder 使用 Google Cloud Application Default Credentials (ADC) 进行身份验证。有关如何设置 ADC 的更多信息,请参阅 官方文档

请记住,使用有权访问已授权使用 Google Vertex AI 端点的项目的帐户至关重要。

您可以在GCP 资源管理器中找到您的项目 ID,或者通过在终端中运行gcloud projects list 来在本地找到。有关 gcloud CLI 的更多信息,请参阅其官方文档

用法

安装使用 google-vertex-haystack 包来使用此 Embedder

pip install google-vertex-haystack

单独使用

from haystack import Document
from haystack_integrations.components.embedders.google_vertex import VertexAIDocumentEmbedder

doc = Document(content="I love pizza!")

document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005")

result = document_embedder.run([doc])
print(result['documents'][0].embedding)
# [-0.044606007635593414, 0.02857724390923977, -0.03549133986234665,

在 pipeline 中

from haystack import Document
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.google_vertex import VertexAITextEmbedder
from haystack_integrations.components.embedders.google_vertex import VertexAIDocumentEmbedder
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")]

document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005")
documents_with_embeddings = document_embedder.run(documents)['documents']
document_store.write_documents(documents_with_embeddings)

query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", VertexAITextEmbedder(model="text-embedding-005"))
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')