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

STACKITDocumentEmbedder

此组件允许使用 STACKIT API 进行文档嵌入。

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

概述

STACKITDocumentEmbedder 允许嵌入由 STACKIT 通过其 API 提供的文档嵌入模型。

参数

要使用STACKITDocumentEmbedder,请确保你已设置一个STACKIT_API_KEY 作为环境变量。或者,通过设置api_key 并使用 Haystack 的 秘密管理,将 API 密钥作为不同名称的环境变量或令牌提供。

在初始化组件时,使用 model 参数设置你首选的支持模型。请参阅 STACKIT 网站上所有支持模型的完整列表:STACKIT 网站

可选地,你可以更改默认的api_base_url,它是"https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1".

你可以使用 init 或 run 方法中的generation_kwargs 参数,将 STACKIT Chat Completion API 有效的任何文本生成参数直接传递给此组件。

该组件需要文档列表作为输入才能运行。

用法

安装stackit-haystack 软件包来使用 STACKITDocumentEmbedder,并将名为STACKIT_API_KEY 的环境变量设置为你的 API 密钥。

pip install stackit-haystack

单独使用

from haystack_integrations.components.embedders.stackit import STACKITDocumentEmbedder

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

document_embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")

result = document_embedder.run([doc])
print(result["documents"][0].embedding)

# [0.0215301513671875, 0.01499176025390625, ...]

在 pipeline 中

你也可以在你的管道中使用STACKITDocumentEmbedder,如下所示。

from haystack import Document
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.embedders.stackit import STACKITTextEmbedder, STACKITDocumentEmbedder
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever

document_store = InMemoryDocumentStore()

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 = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
documents_with_embeddings = document_embedder.run(documents)['documents']
document_store.write_documents(documents_with_embeddings)

text_embedder = STACKITTextEmbedder(model="intfloat/e5-mistral-7b-instruct")

query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", text_embedder)
query_pipeline.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store))
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")

query = "Where does Wolfgang live?"

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: ...)

你可以在 STACKIT 集成 存储库 及其 集成页面 中找到更多使用示例。