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

AnthropicVertexChatGenerator

此组件支持使用 AnthropicVertex API 进行聊天补全。

pipeline 中的最常见位置ChatPromptBuilder 之后
必需的初始化变量"region": Anthropic 模型部署的区域

“project_id”: Anthropic 模型部署的 GCP 项目 ID
强制运行变量“messages”: 一系列 ChatMessage   对象
输出变量“replies”:一个包含 LLM 生成的所有回复的字符串列表

”meta”: 一系列字典,包含与每次回复关联的元数据,例如 token 数量、完成原因等
API 参考Anthropic
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic

概述

AnthropicVertexChatGenerator支持使用 Anthropic Vertex AI API 来生成文本,利用最先进的 Claude 3 LLM。
它支持Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Sonnet,以及Claude 3 Haiku 模型,这些模型都可以通过 Vertex AI API 终端访问。有关模型的更多详细信息,请参阅 Anthropic Vertex AI 文档

参数

要使用AnthropicVertexChatGenerator,请确保您拥有启用了 Vertex AI 的 GCP 项目。您需要指定您的 GCPproject_idregion.

您可以通过以下方式提供这些键:

  • 该 REGIONPROJECT_ID 环境变量(推荐)
  • 该 regionproject_id 初始化参数

在发出请求之前,您可能需要使用以下命令进行 GCP 身份验证:gcloud auth login.

使用 初始化组件时,设置您首选的支持的 Anthropic 模型model 参数。此外,请确保所需的 Anthropic 模型已在 Vertex AI Model Garden 中激活。

AnthropicVertexChatGenerator 需要提示来生成文本,但您可以通过 初始化和运行组件时,直接使用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 格式时,才编写自定义回调。

提示缓存

提示缓存是 Anthropic LLM 的一项功能,用于存储大型文本输入以便重用。它允许您发送一次大型文本块,然后在后续请求中引用它,而无需重新发送整个文本。

此功能对于需要完整代码库上下文的编码助手和处理大型文档特别有用。它可以帮助降低成本并提高响应时间。

以下是一个初始化 AnthropicVertexChatGenerator 并带有提示缓存以及标记要缓存的消息的示例

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

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

claude_llm = AnthropicVertexChatGenerator(
    region="your_region", project_id="test_id", 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 中使用 AnthropicVertexChatGenerator:

pip install anthropic-haystack

单独使用

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

messages = [ChatMessage.from_user("What's Natural Language Processing?")]
client = AnthropicVertexChatGenerator(
  model="claude-3-sonnet@20240229",
  project_id="your-project-id", region="us-central1"
)

response = client.run(messages)
print(response)

在 pipeline 中

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

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

pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", AnthropicVertexChatGenerator(project_id="test_id", region="us-central1"))
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)