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

AnthropicChatGenerator

此组件支持使用 Anthropic 大型语言模型 (LLM) 进行聊天补全。

pipeline 中的最常见位置使用 ChatPromptBuilder 后
必需的初始化变量"api_key": Anthropic API 密钥。可以通过ANTHROPIC_API_KEY 环境变量设置。
强制运行变量“messages” 一个包含 ChatMessage 对象的列表
输出变量"replies": 一个 ChatMessage 对象列表

”meta”:一个包含与每个回复相关的元数据的字典列表,例如 token 计数、结束原因等
API 参考Anthropic
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic

概述

此集成支持 Anthropic chat 模型,例如 claude-3-5-sonnet-20240620,claude-3-opus-20240229claude-3-haiku-20240307,以及类似的。请在 Anthropic 文档 中查看最新的完整列表。

参数

AnthropicChatGenerator 需要 Anthropic API 密钥才能工作。您可以在

  • 该 ANTHROPIC_API_KEY 环境变量 (推荐)
  • 该 api_key 初始化参数和 Haystack Secret API 中提供。Secret.from_token("your-api-key-here")

在初始化组件时,使用model 参数设置您偏好的 Anthropic 模型。

AnthropicChatGenerator 需要一个 prompt 来生成文本,但您可以使用generation_kwargs 参数,在初始化和运行组件时,直接将 Anthropic Messaging API 方法中提供的任何文本生成参数传递给此组件。有关 Anthropic API 支持的参数的更多详细信息,请参阅 Anthropic 文档

最后,组件需要一个 ChatMessage 对象列表才能运行。 ChatMessage 是一个数据类,包含消息、角色(谁生成的消息,例如 userassistantsystemfunction),以及可选的元数据。

目前仅支持文本输入模式。

流式传输

您可以随着输出的生成而流式传输。将回调函数传递给streaming_callback。使用内置的print_streaming_chunk 来打印文本 token 和工具事件(工具调用和工具结果)。

from haystack.components.generators.utils import print_streaming_chunk

# Configure any `Generator` or `ChatGenerator` with a streaming callback
component = SomeGeneratorOrChatGenerator(streaming_callback=print_streaming_chunk)

# If this is a `ChatGenerator`, pass a list of messages:
# from haystack.dataclasses import ChatMessage
# component.run([ChatMessage.from_user("Your question here")])

# If this is a (non-chat) `Generator`, pass a prompt:
# component.run({"prompt": "Your prompt here"})

📘

流式输出仅适用于单个响应。如果提供程序支持多个候选,请将n=1.

设置为 1。有关流式输出如何工作以及如何编写自定义回调函数,请参阅我们的流式输出支持文档。StreamingChunk 工作原理以及如何编写自定义回调。

默认首选print_streaming_chunk。仅当您需要特定的传输(例如 SSE/WebSocket)或自定义 UI 格式时,才编写自定义回调。

Prompt 缓存

Prompt 缓存是 Anthropic LLM 的一项功能,用于存储大型文本输入以便重复使用。它允许您发送一次大文本块,然后在后续请求中引用它,而无需重新发送整个文本。
此功能对于需要完整代码库上下文和处理大型文档的编码助手特别有用。它可以帮助降低成本并提高响应时间。

以下是一个初始化 AnthropicChatGenerator 并进行 prompt 缓存以及标记要缓存的消息的示例

from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage

generation_kwargs = {"extra_headers": {"anthropic-beta": "prompt-caching-2024-07-31"}}

claude_llm = AnthropicChatGenerator(
    api_key=Secret.from_env_var("ANTHROPIC_API_KEY"), generation_kwargs=generation_kwargs
)

system_message = ChatMessage.from_system("Replace with some long text documents, code or instructions")
system_message.meta["cache_control"] = {"type": "ephemeral"}

messages = [system_message, ChatMessage.from_user("A query about the long text for example")]
result = claude_llm.run(messages)

# and now invoke again with 

messages = [system_message, ChatMessage.from_user("Another query about the long text etc")]
result = claude_llm.run(messages)

# and so on, either invoking component directly or in the pipeline 

有关更多详细信息,请参阅 Anthropic 的 文档 和集成 示例

用法

安装使用 anthropic-haystack 包在您的 pipeline 中使用 AnthropicChatGenerator:

pip install anthropic-haystack

单独使用

from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage

generator = AnthropicChatGenerator()
message = ChatMessage.from_user("What's Natural Language Processing? Be brief.")
print(generator.run([message]))

在 pipeline 中

你也可以在你的管道中使用AnthropicChatGenerator 与 Anthropic 聊天模型。

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.utils import Secret

pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", AnthropicChatGenerator(Secret.from_env_var("ANTHROPIC_API_KEY")))
pipe.connect("prompt_builder", "llm")

country = "Germany"
system_message = ChatMessage.from_system("You are an assistant giving out valuable information to language learners.")
messages = [system_message, ChatMessage.from_user("What's the official language of {{ country }}?")]

res = pipe.run(data={"prompt_builder": {"template_variables": {"country": country}, "template": messages}})
print(res)

其他参考资料

🧑‍🍳 食谱: Anthropic 高级 Prompt 定制