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

CohereDocumentEmbedder

此组件计算文档列表的嵌入,并将获得的向量存储在每个文档的嵌入字段中。它使用 Cohere 嵌入模型。

此组件计算的向量对于对文档集合执行 embedding 检索至关重要。在检索时,表示查询的向量会与文档的向量进行比较,以找到最相似或最相关的文档。

pipeline 中的最常见位置在索引管道中的 DocumentWriter 之前
必需的初始化变量"api_key": Cohere API 密钥。可以设置为COHERE_API_KEYCO_API_KEY 环境变量。
强制运行变量“documents”:要计算 embedding 的文档列表
输出变量“documents”:文档列表(已添加嵌入信息)

“meta”:字符串元数据字典
API 参考Cohere
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere

概述

CohereDocumentEmbedder 会用内容的嵌入来丰富文档的元数据。要嵌入一个字符串,你应该使用 CohereTextEmbedder

该组件支持以下 Cohere 模型
"embed-english-v3.0", "embed-english-light-v3.0", "embed-multilingual-v3.0",
"embed-multilingual-light-v3.0", "embed-english-v2.0", "embed-english-light-v2.0",
"embed-multilingual-v2.0"。默认模型是embed-english-v2.0。所有支持的模型列表可以在 Cohere 的 模型文档 中找到。

要开始将此集成与 Haystack 一起使用,请使用以下命令安装它:

pip install cohere-haystack

该组件默认使用COHERE_API_KEYCO_API_KEY 环境变量,默认情况下。否则,您可以在初始化时使用以下方式传递 API 密钥:api_key:

embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))

要获取 Cohere API 密钥,请访问 https://cohere.com/

Embedding 元数据

文本文档通常附带一组元数据。如果它们具有辨识度和语义意义,您可以将它们与文档文本一起 embedding,以提高检索效果。

您可以通过使用 Document Embedder 来做到这一点

from haystack import Document
from cohere_haystack.embedders.document_embedder import CohereDocumentEmbedder

doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})

embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>", meta_fields_to_embed=["title"])

docs_w_embeddings = embedder.run(documents=[doc])["documents"]

用法

单独使用

请记住设置COHERE_API_KEY 作为环境变量,或者直接传递它。

以下是独立使用该组件的方法:

from haystack import Document
from haystack_integrations.components.embedders.cohere.document_embedder import CohereDocumentEmbedder

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

embedder = CohereDocumentEmbedder()

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.cohere.document_embedder import CohereDocumentEmbedder
from haystack_integrations.components.embedders.cohere.text_embedder import CohereTextEmbedder

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", CohereDocumentEmbedder())
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", CohereTextEmbedder())
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')

相关链接

请查看 GitHub 仓库或我们的文档中的 API 参考