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

STACKITChatGenerator

此组件使用 STACKIT API 实现聊天补全。

pipeline 中的最常见位置ChatPromptBuilder 之后
必需的初始化变量"model": 通过 STACKIT API 使用的模型
强制运行变量“messages”: 一个对象列表 ChatMessage  对象
输出变量"replies": 一个对象列表 ChatMessage 对象

”meta”: 一个包含与每个回复关联的元数据的字典列表(例如 token 计数、完成原因等)
API 参考STACKIT
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/stackit

概述

STACKITChatGenerator 通过其 API 实现 STACKIT 提供的文本生成模型。

参数

要使用STACKITChatGenerator,请确保您已设置STACKIT_API_KEY 作为环境变量。或者,通过设置提供 API 密钥作为另一个环境变量或 token:
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 有效的任何文本生成参数直接传递给此组件。

该组件需要一个列表ChatMessage 对象来运行。ChatMessage 是一个数据类,包含消息、角色(谁生成的消息,例如 userassistantsystemfunction)、以及可选的元数据。在此处了解更多关于它的信息:ChatMessage 文档

流式传输

此 ChatGenerator 支持 流式传输 LLM 的 token 到输出。为此,请将一个函数传递给 streaming_callback 初始化参数。

用法

安装stackit-haystack 软件包来使用 STACKITChatGenerator:

pip install stackit-haystack

单独使用

from haystack_integrations.components.generators.stackit import STACKITChatGenerator
from haystack.dataclasses import ChatMessage

generator = STACKITChatGenerator(model="neuralmagic/Meta-Llama-3.1-70B-Instruct-FP8")

result = generator.run([ChatMessage.from_user("Tell me a joke.")])
print(result)

在 pipeline 中

你也可以在你的管道中使用STACKITChatGenerator 在您的管道中。

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage

from haystack_integrations.components.generators.stackit import STACKITChatGenerator

prompt_builder = ChatPromptBuilder()
llm = STACKITChatGenerator(model="neuralmagic/Meta-Llama-3.1-70B-Instruct-FP8")

messages = [ChatMessage.from_user("Question: {{question}} \\n")]

pipeline = Pipeline()
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("llm", llm)

pipeline.connect("prompt_builder.prompt", "llm.messages")

result = pipeline.run({"prompt_builder": {"template_variables": {"question": "Tell me a joke."}, "template": messages}})

print(result)

有关管道中流式传输的示例,请参阅 STACKIT 集成 存储库 中的示例以及其专用的 集成页面