WatsonxDocumentEmbedder
此组件计算的向量对于对文档集合执行 embedding 检索至关重要。在检索时,表示查询的向量会与文档的向量进行比较,以找到最相似或最相关的文档。
| pipeline 中的最常见位置 | 在索引管道中的 DocumentWriter 之前 |
| 必需的初始化变量 | "api_key": IBM Cloud API 密钥。可以通过以下方式设置环境变量 WATSONX_API_KEY。"project_id": IBM Cloud 项目 ID。可以通过以下方式设置环境变量 WATSONX_PROJECT_ID。 |
| 强制运行变量 | "documents": 要嵌入的文档列表 |
| 输出变量 | "documents": 文档列表(已添加 embedding) "meta": 元数据字符串字典 |
| API 参考 | Watsonx |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx |
概述
WatsonxDocumentEmbedder 会用文档内容的嵌入来丰富文档的元数据。要嵌入一个字符串,你应该使用 WatsonxTextEmbedder。
该组件支持 IBM watsonx.ai 嵌入模型,例如ibm/slate-30m-english-rtrvr 等。默认模型是ibm/slate-30m-english-rtrvr。所有支持的模型列表可以在 IBM 的 模型文档 中找到。
要开始将此集成与 Haystack 一起使用,请使用以下命令安装它:
pip install watsonx-haystack
该组件使用默认情况下,使用 WATSONX_API_KEY 和WATSONX_PROJECT_ID 环境变量。否则,你可以在初始化时通过以下方式传递 API 凭证:api_key 和project_id:
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_token("<your-api-key>"),
project_id=Secret.from_token("<your-project-id>")
)
要获取 IBM Cloud 凭证,请访问 https://cloud.ibm.com/。
Embedding 元数据
文本文档通常附带一组元数据。如果它们具有辨识度和语义意义,您可以将它们与文档文本一起 embedding,以提高检索效果。
您可以通过使用 Document Embedder 来做到这一点
from haystack import Document
from haystack_integrations.components.embedders.watsonx.document_embedder import WatsonxDocumentEmbedder
from haystack.utils import Secret
doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
meta_fields_to_embed=["title"]
)
docs_w_embeddings = embedder.run(documents=[doc])["documents"]
用法
安装使用 watsonx-haystack 包来使用WatsonxDocumentEmbedder:
pip install watsonx-haystack
单独使用
请记住先设置默认情况下,使用 WATSONX_API_KEY 和WATSONX_PROJECT_ID 作为环境变量,或者直接传递它们。
以下是独立使用该组件的方法:
from haystack import Document
from haystack_integrations.components.embedders.watsonx.document_embedder import WatsonxDocumentEmbedder
doc = Document(content="I love pizza!")
embedder = WatsonxDocumentEmbedder()
result = embedder.run([doc])
print(result['documents'][0].embedding)
# [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
在 pipeline 中
from haystack import 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.document_embedder import WatsonxDocumentEmbedder
from haystack_integrations.components.embedders.watsonx.text_embedder import WatsonxTextEmbedder
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=..., text: 'My name is Wolfgang and I live in Berlin')
更新于 3 个月前
