HuggingFaceLocalChatGenerator
提供一个使用本地运行的 Hugging Face 模型进行聊天补全的接口。
| pipeline 中的最常见位置 | 在 ChatPromptBuilder 之后 |
| 必需的初始化变量 | "token": Hugging Face API 令牌。可以通过HF_API_TOKEN 或HF_TOKEN 环境变量设置。 |
| 强制运行变量 | “messages”:一个 ChatMessage 对象列表,代表聊天记录 |
| 输出变量 | “replies”:一个包含 LLM 生成的所有回复的字符串列表 |
| API 参考 | Generators (生成器) |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/chat/hugging_face_local.py |
概述
请注意,如果 LLM 在本地运行,您可能需要一台强大的机器来运行它们。这在很大程度上取决于您选择的模型及其参数数量。
此组件专为聊天补全而设计,而非文本生成。如果您想将 Hugging Face LLM 用于文本生成,请改用
HuggingFaceLocalGenerator。
对于远程文件授权,此组件使用HF_API_TOKEN 环境变量。否则,您可以在初始化时使用token:
local_generator = HuggingFaceLocalChatGenerator(token=Secret.from_token("<your-api-key>"))
流式传输
此 Generator 支持将 LLM 的 token直接流式传输到输出中。要做到这一点,请将一个函数传递给streaming_callback 初始化参数。
用法
单独使用
from haystack.components.generators.chat import HuggingFaceLocalChatGenerator
from haystack.dataclasses import ChatMessage
generator = HuggingFaceLocalChatGenerator(model="HuggingFaceH4/zephyr-7b-beta")
generator.warm_up()
messages = [ChatMessage.from_user("What's Natural Language Processing? Be brief.")]
print(generator.run(messages))
在 Pipeline 中
from haystack import Pipeline
from haystack.components.builders.prompt_builder import ChatPromptBuilder
from haystack.components.generators.chat import HuggingFaceLocalChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret
prompt_builder = ChatPromptBuilder()
llm = HuggingFaceLocalChatGenerator(model="HuggingFaceH4/zephyr-7b-beta", token=Secret.from_env_var("HF_API_TOKEN"))
pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)
pipe.connect("prompt_builder.prompt", "llm.messages")
location = "Berlin"
messages = [ChatMessage.from_system("Always respond in German even if some input data is in other languages."),
ChatMessage.from_user("Tell me about {{location}}")]
pipe.run(data={"prompt_builder": {"template_variables":{"location": location}, "template": messages}})
更新于 11 个月前
相关链接
