AnthropicGenerator
此组件支持使用 Anthropic 大型语言模型(LLM)进行文本补全。
| pipeline 中的最常见位置 | 在 PromptBuilder 之后 |
| 必需的初始化变量 | "api_key":Anthropic API 密钥。可以通过ANTHROPIC_API_KEY 环境变量设置。 |
| 强制运行变量 | “prompt”:一个包含 LLM 提示的字符串 |
| 输出变量 | “replies”:一个包含 LLM 生成的所有回复的字符串列表 ”meta”:一个包含与每个回复相关的元数据的字典列表,例如 token 计数、结束原因等 |
| API 参考 | Anthropic |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic |
概述
此集成支持 Anthropic 模型,例如claude-3-5-sonnet-20240620,claude-3-opus-20240229, claude-3-haiku-20240307 等。尽管这些 LLM 被称为聊天模型,但主要提示接口可以处理字符串提示。请参阅最新的完整列表,请访问Anthropic 文档。
参数
AnthropicGenerator 需要 Anthropic API 密钥才能工作。您可以在
- 该
ANTHROPIC_API_KEY环境变量(推荐) - 该
api_key初始化参数以及 Haystack Secret API 中提供Secret.from_token("your-api-key-here")
在初始化组件时,在model 参数中设置您偏好的 Anthropic 模型。
AnthropicGenerator 需要提示来生成文本,但您可以使用generation_kwargs 参数,无论是初始化时还是run() 方法直接将 Anthropic Messaging API 方法中可用的任何文本生成参数传递给此组件。有关 Anthropic API 支持的参数的更多详细信息,请参阅 Anthropic 文档。
最后,组件的 run 方法需要一个单一的字符串提示来生成文本。
流式传输
此 Generator 支持将 LLM 的 token直接流式传输到输出中。要做到这一点,请将一个函数传递给streaming_callback 初始化参数。
用法
安装使用 anthropic-haystack 包来使用AnthropicGenerator:
pip install anthropic-haystack
单独使用
from haystack_integrations.components.generators.anthropic import AnthropicGenerator
generator = AnthropicGenerator()
print(generator.run("What's Natural Language Processing? Be brief."))
在 pipeline 中
你也可以在你的管道中使用AnthropicGenerator 在您的管道中与 Anthropic 模型进行集成。
from haystack import Pipeline
from haystack.components.builders import PromptBuilder
from haystack_integrations.components.generators.anthropic import AnthropicGenerator
from haystack.utils import Secret
template = """
You are an assistant giving out valuable information to language learners.
Answer this question, be brief.
Question: {{ query }}?
"""
pipe = Pipeline()
pipe.add_component("prompt_builder", PromptBuilder(template))
pipe.add_component("llm", AnthropicGenerator(Secret.from_env_var("ANTHROPIC_API_KEY")))
pipe.connect("prompt_builder", "llm")
query = "What language is spoke in Germany?"
res = pipe.run(data={"prompt_builder": {"query": {query}}})
print(res)
更新于 大约 1 年前
