c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
690 lines
27 KiB
Markdown
690 lines
27 KiB
Markdown
---
|
||
title: "Anthropic"
|
||
id: integrations-anthropic
|
||
description: "Anthropic integration for Haystack"
|
||
slug: "/integrations-anthropic"
|
||
---
|
||
|
||
|
||
## haystack_integrations.components.generators.anthropic.chat.chat_generator
|
||
|
||
### AnthropicChatGenerator
|
||
|
||
Completes chats using Anthropic's large language models (LLMs).
|
||
|
||
It uses [ChatMessage](https://docs.haystack.deepset.ai/docs/data-classes#chatmessage)
|
||
format in input and output. Supports multimodal inputs including text and images.
|
||
|
||
You can customize how the text is generated by passing parameters to the
|
||
Anthropic API. Use the `**generation_kwargs` argument when you initialize
|
||
the component or when you run it. Any parameter that works with
|
||
`anthropic.Message.create` will work here too.
|
||
|
||
For details on Anthropic API parameters, see
|
||
[Anthropic documentation](https://docs.anthropic.com/en/api/messages).
|
||
|
||
Usage example:
|
||
|
||
```python
|
||
from haystack_integrations.components.generators.anthropic import (
|
||
AnthropicChatGenerator,
|
||
)
|
||
from haystack.dataclasses import ChatMessage
|
||
|
||
generator = AnthropicChatGenerator(
|
||
generation_kwargs={
|
||
"max_tokens": 1000,
|
||
"temperature": 0.7,
|
||
},
|
||
)
|
||
|
||
messages = [
|
||
ChatMessage.from_system(
|
||
"You are a helpful, respectful and honest assistant"
|
||
),
|
||
ChatMessage.from_user("What's Natural Language Processing?"),
|
||
]
|
||
print(generator.run(messages=messages))
|
||
```
|
||
|
||
Usage example with images:
|
||
|
||
```python
|
||
from haystack.dataclasses import ChatMessage, ImageContent
|
||
|
||
image_content = ImageContent.from_file_path("path/to/image.jpg")
|
||
messages = [
|
||
ChatMessage.from_user(
|
||
content_parts=["What's in this image?", image_content]
|
||
)
|
||
]
|
||
generator = AnthropicChatGenerator()
|
||
result = generator.run(messages)
|
||
```
|
||
|
||
#### SUPPORTED_MODELS
|
||
|
||
```python
|
||
SUPPORTED_MODELS: list[str] = [
|
||
"claude-opus-4-6",
|
||
"claude-sonnet-4-6",
|
||
"claude-haiku-4-5-20251001",
|
||
"claude-sonnet-4-5-20250929",
|
||
"claude-opus-4-5-20251101",
|
||
"claude-opus-4-1-20250805",
|
||
"claude-sonnet-4-20250514",
|
||
"claude-opus-4-20250514",
|
||
"claude-3-haiku-20240307",
|
||
]
|
||
|
||
```
|
||
|
||
A non-exhaustive list of chat models supported by this component. See
|
||
https://platform.claude.com/docs/en/about-claude/models/overview for the full list.
|
||
|
||
#### __init__
|
||
|
||
```python
|
||
__init__(
|
||
api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"),
|
||
model: str = "claude-sonnet-4-5",
|
||
streaming_callback: StreamingCallbackT | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
ignore_tools_thinking_messages: bool = True,
|
||
tools: ToolsType | None = None,
|
||
*,
|
||
timeout: float | None = None,
|
||
max_retries: int | None = None
|
||
) -> None
|
||
```
|
||
|
||
Creates an instance of AnthropicChatGenerator.
|
||
|
||
**Parameters:**
|
||
|
||
- **api_key** (<code>Secret</code>) – The Anthropic API key
|
||
- **model** (<code>str</code>) – The name of the model to use.
|
||
- **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
The callback function accepts StreamingChunk as an argument.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Other parameters to use for the model. These parameters are all sent directly to
|
||
the Anthropic endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post)
|
||
for more details.
|
||
|
||
Supported generation_kwargs parameters are:
|
||
|
||
- `system`: The system message to be passed to the model.
|
||
- `max_tokens`: The maximum number of tokens to generate.
|
||
- `metadata`: A dictionary of metadata to be passed to the model.
|
||
- `stop_sequences`: A list of strings that the model should stop generating at.
|
||
- `temperature`: The temperature to use for sampling.
|
||
- `top_p`: The top_p value to use for nucleus sampling.
|
||
- `top_k`: The top_k value to use for top-k sampling.
|
||
- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features).
|
||
- `thinking`: A dictionary of thinking parameters to be passed to the model.
|
||
The `budget_tokens` passed for thinking should be less than `max_tokens`.
|
||
For more details and supported models, see: [Anthropic Extended Thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
|
||
- `output_config`: A dictionary of output configuration options to be passed to the model.
|
||
- **ignore_tools_thinking_messages** (<code>bool</code>) – Anthropic's approach to tools (function calling) resolution involves a
|
||
"chain of thought" messages before returning the actual function names and parameters in a message. If
|
||
`ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool
|
||
use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use)
|
||
for more details.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name.
|
||
- **timeout** (<code>float | None</code>) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client.
|
||
- **max_retries** (<code>int | None</code>) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by
|
||
the Anthropic client.
|
||
|
||
#### to_dict
|
||
|
||
```python
|
||
to_dict() -> dict[str, Any]
|
||
```
|
||
|
||
Serialize this component to a dictionary.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, Any\]</code> – The serialized component as a dictionary.
|
||
|
||
#### from_dict
|
||
|
||
```python
|
||
from_dict(data: dict[str, Any]) -> AnthropicChatGenerator
|
||
```
|
||
|
||
Deserialize this component from a dictionary.
|
||
|
||
**Parameters:**
|
||
|
||
- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component.
|
||
|
||
**Returns:**
|
||
|
||
- <code>AnthropicChatGenerator</code> – The deserialized component instance.
|
||
|
||
#### run
|
||
|
||
```python
|
||
run(
|
||
messages: list[ChatMessage] | str,
|
||
streaming_callback: StreamingCallbackT | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
tools: ToolsType | None = None,
|
||
) -> dict[str, list[ChatMessage]]
|
||
```
|
||
|
||
Invokes the Anthropic API with the given messages and generation kwargs.
|
||
|
||
**Parameters:**
|
||
|
||
- **messages** (<code>list\[ChatMessage\] | str</code>) – A list of ChatMessage instances representing the input messages.
|
||
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
||
- **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name. If set, it will override the `tools` parameter set during component
|
||
initialization.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys:
|
||
- `replies`: The responses from the model
|
||
|
||
#### run_async
|
||
|
||
```python
|
||
run_async(
|
||
messages: list[ChatMessage] | str,
|
||
streaming_callback: StreamingCallbackT | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
tools: ToolsType | None = None,
|
||
) -> dict[str, list[ChatMessage]]
|
||
```
|
||
|
||
Async version of the run method. Invokes the Anthropic API with the given messages and generation kwargs.
|
||
|
||
**Parameters:**
|
||
|
||
- **messages** (<code>list\[ChatMessage\] | str</code>) – A list of ChatMessage instances representing the input messages.
|
||
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
||
- **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name. If set, it will override the `tools` parameter set during component
|
||
initialization.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys:
|
||
- `replies`: The responses from the model
|
||
|
||
## haystack_integrations.components.generators.anthropic.chat.foundry_chat_generator
|
||
|
||
### AnthropicFoundryChatGenerator
|
||
|
||
Bases: <code>AnthropicChatGenerator</code>
|
||
|
||
Enables text generation using Anthropic's Claude models via Azure Foundry.
|
||
|
||
A variety of Claude models (Opus, Sonnet, Haiku, and others) are available through Azure Foundry.
|
||
|
||
To use AnthropicFoundryChatGenerator, you must have an Azure subscription with Foundry enabled
|
||
and the desired Anthropic model deployed in your Foundry resource.
|
||
|
||
For more details, refer to the [Anthropic Foundry documentation](https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/foundry.md).
|
||
|
||
Any valid text generation parameters for the Anthropic messaging API can be passed to
|
||
the AnthropicFoundry API. Users can provide these parameters directly to the component via
|
||
the `generation_kwargs` parameter in `__init__` or the `run` method.
|
||
|
||
For more details on the parameters supported by the Anthropic API, refer to the
|
||
Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages).
|
||
|
||
```python
|
||
from haystack_integrations.components.generators.anthropic import AnthropicFoundryChatGenerator
|
||
from haystack.dataclasses import ChatMessage
|
||
from haystack.utils import Secret
|
||
|
||
messages = [ChatMessage.from_user("What's Natural Language Processing?")]
|
||
|
||
client = AnthropicFoundryChatGenerator(
|
||
model="claude-sonnet-4-5",
|
||
api_key=Secret.from_env_var("ANTHROPIC_FOUNDRY_API_KEY"),
|
||
resource="my-resource",
|
||
)
|
||
|
||
response = client.run(messages)
|
||
print(response)
|
||
>> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
|
||
>> "Natural Language Processing (NLP) is a field of artificial intelligence that
|
||
>> focuses on enabling computers to understand, interpret, and generate human language. It involves developing
|
||
>> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and
|
||
>> communicate in natural languages like English, Spanish, or Chinese.")],
|
||
>> _name=None, _meta={'model': 'claude-sonnet-4-5', 'index': 0, 'finish_reason': 'end_turn',
|
||
>> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]}
|
||
```
|
||
|
||
For more details on supported models and their capabilities, refer to the Anthropic
|
||
[documentation](https://docs.anthropic.com/claude/docs/intro-to-claude).
|
||
|
||
#### SUPPORTED_MODELS
|
||
|
||
```python
|
||
SUPPORTED_MODELS: list[str] = [
|
||
"claude-opus-4-6",
|
||
"claude-sonnet-4-6",
|
||
"claude-sonnet-4-5",
|
||
"claude-opus-4-5",
|
||
"claude-opus-4-1",
|
||
"claude-haiku-4-5",
|
||
]
|
||
|
||
```
|
||
|
||
A non-exhaustive list of chat models supported by this component.
|
||
The actual availability depends on your Azure Foundry resource configuration.
|
||
|
||
#### __init__
|
||
|
||
```python
|
||
__init__(
|
||
*,
|
||
api_key: Secret | None = Secret.from_env_var(
|
||
"ANTHROPIC_FOUNDRY_API_KEY", strict=True
|
||
),
|
||
resource: str | None = None,
|
||
endpoint: str | None = None,
|
||
model: str = "claude-sonnet-4-5",
|
||
streaming_callback: Callable[[StreamingChunk], None] | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
ignore_tools_thinking_messages: bool = True,
|
||
tools: ToolsType | None = None,
|
||
timeout: float | None = None,
|
||
max_retries: int | None = None,
|
||
azure_ad_token_provider: Callable[[], str] | None = None
|
||
) -> None
|
||
```
|
||
|
||
Creates an instance of AnthropicFoundryChatGenerator.
|
||
|
||
**Parameters:**
|
||
|
||
- **api_key** (<code>Secret | None</code>) – The API key to use for authentication.
|
||
Defaults to the `ANTHROPIC_FOUNDRY_API_KEY` environment variable.
|
||
Can be `None` when using `azure_ad_token_provider` instead.
|
||
- **resource** (<code>str | None</code>) – The Foundry resource name. Can also be set via the `ANTHROPIC_FOUNDRY_RESOURCE`
|
||
environment variable. Either `resource` or `endpoint` must be provided.
|
||
- **endpoint** (<code>str | None</code>) – The full Foundry endpoint URL (e.g.,
|
||
"https://your-resource.openai.azure.com/anthropic").
|
||
Either `resource` or `endpoint` must be provided.
|
||
- **model** (<code>str</code>) – The name of the model to use (deployment name in Foundry).
|
||
- **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
The callback function accepts StreamingChunk as an argument.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Other parameters to use for the model. These parameters are all sent directly to
|
||
the AnthropicFoundry endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post)
|
||
for more details.
|
||
Supported generation_kwargs parameters are:
|
||
- `system`: The system message to be passed to the model.
|
||
- `max_tokens`: The maximum number of tokens to generate.
|
||
- `metadata`: A dictionary of metadata to be passed to the model.
|
||
- `stop_sequences`: A list of strings that the model should stop generating at.
|
||
- `temperature`: The temperature to use for sampling.
|
||
- `top_p`: The top_p value to use for nucleus sampling.
|
||
- `top_k`: The top_k value to use for top-k sampling.
|
||
- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features).
|
||
- **ignore_tools_thinking_messages** (<code>bool</code>) – Anthropic's approach to tools (function calling) resolution involves a
|
||
"chain of thought" messages before returning the actual function names and parameters in a message. If
|
||
`ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool
|
||
use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use)
|
||
for more details.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name.
|
||
- **timeout** (<code>float | None</code>) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client.
|
||
- **max_retries** (<code>int | None</code>) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by
|
||
the Anthropic client.
|
||
- **azure_ad_token_provider** (<code>Callable\[[], str\] | None</code>) – A function that returns an Azure AD token for authentication.
|
||
Can be used instead of `api_key` for enhanced security.
|
||
See [Azure Identity documentation](https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication/overview)
|
||
for more details.
|
||
|
||
#### warm_up
|
||
|
||
```python
|
||
warm_up() -> None
|
||
```
|
||
|
||
Create the AnthropicFoundry clients.
|
||
|
||
This method is idempotent — it only creates clients once.
|
||
|
||
#### run
|
||
|
||
```python
|
||
run(
|
||
messages: list[ChatMessage] | str,
|
||
streaming_callback: StreamingCallbackT | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
tools: ToolsType | None = None,
|
||
) -> dict[str, list[ChatMessage]]
|
||
```
|
||
|
||
Invokes the AnthropicFoundry API with the given messages and generation kwargs.
|
||
|
||
**Parameters:**
|
||
|
||
- **messages** (<code>list\[ChatMessage\] | str</code>) – A list of ChatMessage instances representing the input messages.
|
||
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
||
- **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name. If set, it will override the `tools` parameter set during component
|
||
initialization.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys:
|
||
- `replies`: The responses from the model
|
||
|
||
#### run_async
|
||
|
||
```python
|
||
run_async(
|
||
messages: list[ChatMessage] | str,
|
||
streaming_callback: StreamingCallbackT | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
tools: ToolsType | None = None,
|
||
) -> dict[str, list[ChatMessage]]
|
||
```
|
||
|
||
Async version of the run method. Invokes the AnthropicFoundry API with the given messages and generation kwargs.
|
||
|
||
**Parameters:**
|
||
|
||
- **messages** (<code>list\[ChatMessage\] | str</code>) – A list of ChatMessage instances representing the input messages.
|
||
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
||
- **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name. If set, it will override the `tools` parameter set during component
|
||
initialization.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys:
|
||
- `replies`: The responses from the model
|
||
|
||
#### to_dict
|
||
|
||
```python
|
||
to_dict() -> dict[str, Any]
|
||
```
|
||
|
||
Serialize this component to a dictionary.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, Any\]</code> – The serialized component as a dictionary.
|
||
|
||
#### from_dict
|
||
|
||
```python
|
||
from_dict(data: dict[str, Any]) -> AnthropicFoundryChatGenerator
|
||
```
|
||
|
||
Deserialize this component from a dictionary.
|
||
|
||
**Parameters:**
|
||
|
||
- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component.
|
||
|
||
**Returns:**
|
||
|
||
- <code>AnthropicFoundryChatGenerator</code> – The deserialized component instance.
|
||
|
||
## haystack_integrations.components.generators.anthropic.chat.vertex_chat_generator
|
||
|
||
### AnthropicVertexChatGenerator
|
||
|
||
Bases: <code>AnthropicChatGenerator</code>
|
||
|
||
Enables text generation using Anthropic's Claude models via the Anthropic Vertex AI API.
|
||
|
||
A variety of Claude models (Opus, Sonnet, Haiku, and others) are available through the Vertex AI API endpoint.
|
||
|
||
To use AnthropicVertexChatGenerator, you must have a GCP project with Vertex AI enabled.
|
||
Additionally, ensure that the desired Anthropic model is activated in the Vertex AI Model Garden.
|
||
Before making requests, you may need to authenticate with GCP using `gcloud auth login`.
|
||
For more details, refer to the [guide] (https://docs.anthropic.com/en/api/claude-on-vertex-ai).
|
||
|
||
Any valid text generation parameters for the Anthropic messaging API can be passed to
|
||
the AnthropicVertex API. Users can provide these parameters directly to the component via
|
||
the `generation_kwargs` parameter in `__init__` or the `run` method.
|
||
|
||
For more details on the parameters supported by the Anthropic API, refer to the
|
||
Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages).
|
||
|
||
```python
|
||
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-sonnet-4@20250514",
|
||
project_id="your-project-id", region="your-region"
|
||
)
|
||
response = client.run(messages)
|
||
print(response)
|
||
|
||
>> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
|
||
>> "Natural Language Processing (NLP) is a field of artificial intelligence that
|
||
>> focuses on enabling computers to understand, interpret, and generate human language. It involves developing
|
||
>> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and
|
||
>> communicate in natural languages like English, Spanish, or Chinese.")],
|
||
>> _name=None, _meta={'model': 'claude-sonnet-4@20250514', 'index': 0, 'finish_reason': 'end_turn',
|
||
>> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]}
|
||
```
|
||
|
||
For more details on supported models and their capabilities, refer to the Anthropic
|
||
[documentation](https://docs.anthropic.com/claude/docs/intro-to-claude).
|
||
|
||
For a list of available model IDs when using Claude on Vertex AI, see
|
||
[Claude on Vertex AI - model availability](https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability).
|
||
|
||
#### SUPPORTED_MODELS
|
||
|
||
```python
|
||
SUPPORTED_MODELS: list[str] = [
|
||
"claude-opus-4-6",
|
||
"claude-sonnet-4-6",
|
||
"claude-sonnet-4-5@20250929",
|
||
"claude-sonnet-4@20250514",
|
||
"claude-opus-4-5@20251101",
|
||
"claude-opus-4-1@20250805",
|
||
"claude-opus-4@20250514",
|
||
"claude-haiku-4-5@20251001",
|
||
]
|
||
|
||
```
|
||
|
||
A non-exhaustive list of chat models supported by this component. See
|
||
https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability for the full list.
|
||
|
||
#### __init__
|
||
|
||
```python
|
||
__init__(
|
||
region: str,
|
||
project_id: str,
|
||
model: str = "claude-sonnet-4@20250514",
|
||
streaming_callback: Callable[[StreamingChunk], None] | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
ignore_tools_thinking_messages: bool = True,
|
||
tools: ToolsType | None = None,
|
||
*,
|
||
timeout: float | None = None,
|
||
max_retries: int | None = None
|
||
) -> None
|
||
```
|
||
|
||
Creates an instance of AnthropicVertexChatGenerator.
|
||
|
||
**Parameters:**
|
||
|
||
- **region** (<code>str</code>) – The region where the Anthropic model is deployed. Defaults to "us-central1".
|
||
- **project_id** (<code>str</code>) – The GCP project ID where the Anthropic model is deployed.
|
||
- **model** (<code>str</code>) – The name of the model to use.
|
||
- **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – A callback function that is called when a new token is received from the stream.
|
||
The callback function accepts StreamingChunk as an argument.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Other parameters to use for the model. These parameters are all sent directly to
|
||
the AnthropicVertex endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post)
|
||
for more details.
|
||
|
||
Supported generation_kwargs parameters are:
|
||
|
||
- `system`: The system message to be passed to the model.
|
||
- `max_tokens`: The maximum number of tokens to generate.
|
||
- `metadata`: A dictionary of metadata to be passed to the model.
|
||
- `stop_sequences`: A list of strings that the model should stop generating at.
|
||
- `temperature`: The temperature to use for sampling.
|
||
- `top_p`: The top_p value to use for nucleus sampling.
|
||
- `top_k`: The top_k value to use for top-k sampling.
|
||
- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features).
|
||
- **ignore_tools_thinking_messages** (<code>bool</code>) – Anthropic's approach to tools (function calling) resolution involves a
|
||
"chain of thought" messages before returning the actual function names and parameters in a message. If
|
||
`ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool
|
||
use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use)
|
||
for more details.
|
||
- **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use.
|
||
Each tool should have a unique name.
|
||
- **timeout** (<code>float | None</code>) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client.
|
||
- **max_retries** (<code>int | None</code>) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by
|
||
the Anthropic client.
|
||
|
||
#### to_dict
|
||
|
||
```python
|
||
to_dict() -> dict[str, Any]
|
||
```
|
||
|
||
Serialize this component to a dictionary.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, Any\]</code> – The serialized component as a dictionary.
|
||
|
||
#### from_dict
|
||
|
||
```python
|
||
from_dict(data: dict[str, Any]) -> AnthropicVertexChatGenerator
|
||
```
|
||
|
||
Deserialize this component from a dictionary.
|
||
|
||
**Parameters:**
|
||
|
||
- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component.
|
||
|
||
**Returns:**
|
||
|
||
- <code>AnthropicVertexChatGenerator</code> – The deserialized component instance.
|
||
|
||
## haystack_integrations.components.generators.anthropic.generator
|
||
|
||
### AnthropicGenerator
|
||
|
||
Enables text generation using Anthropic large language models (LLMs). It supports the Claude family of models.
|
||
|
||
Although Anthropic natively supports a much richer messaging API, we have intentionally simplified it in this
|
||
component so that the main input/output interface is string-based.
|
||
For more complete support, consider using the AnthropicChatGenerator.
|
||
|
||
```python
|
||
from haystack_integrations.components.generators.anthropic import AnthropicGenerator
|
||
|
||
client = AnthropicGenerator(model="claude-sonnet-4-20250514")
|
||
response = client.run("What's Natural Language Processing? Be brief.")
|
||
print(response)
|
||
>>{'replies': ['Natural language processing (NLP) is a branch of artificial intelligence focused on enabling
|
||
>>computers to understand, interpret, and manipulate human language. The goal of NLP is to read, decipher,
|
||
>> understand, and make sense of the human languages in a manner that is valuable.'], 'meta': {'model':
|
||
>> 'claude-2.1', 'index': 0, 'finish_reason': 'end_turn', 'usage': {'input_tokens': 18, 'output_tokens': 58}}}
|
||
```
|
||
|
||
#### __init__
|
||
|
||
```python
|
||
__init__(
|
||
api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"),
|
||
model: str = "claude-sonnet-4-5",
|
||
streaming_callback: Callable[[StreamingChunk], None] | None = None,
|
||
system_prompt: str | None = None,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
*,
|
||
timeout: float | None = None,
|
||
max_retries: int | None = None
|
||
) -> None
|
||
```
|
||
|
||
Initialize the AnthropicGenerator.
|
||
|
||
**Parameters:**
|
||
|
||
- **api_key** (<code>Secret</code>) – The Anthropic API key.
|
||
- **model** (<code>str</code>) – The name of the Anthropic model to use.
|
||
- **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – An optional callback function to handle streaming chunks.
|
||
- **system_prompt** (<code>str | None</code>) – An optional system prompt to use for generation.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for generation.
|
||
|
||
#### to_dict
|
||
|
||
```python
|
||
to_dict() -> dict[str, Any]
|
||
```
|
||
|
||
Serialize this component to a dictionary.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, Any\]</code> – The serialized component as a dictionary.
|
||
|
||
#### from_dict
|
||
|
||
```python
|
||
from_dict(data: dict[str, Any]) -> AnthropicGenerator
|
||
```
|
||
|
||
Deserialize this component from a dictionary.
|
||
|
||
**Parameters:**
|
||
|
||
- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component.
|
||
|
||
**Returns:**
|
||
|
||
- <code>AnthropicGenerator</code> – The deserialized component instance.
|
||
|
||
#### run
|
||
|
||
```python
|
||
run(
|
||
prompt: str,
|
||
generation_kwargs: dict[str, Any] | None = None,
|
||
streaming_callback: Callable[[StreamingChunk], None] | None = None,
|
||
) -> dict[str, list[str] | list[dict[str, Any]]]
|
||
```
|
||
|
||
Generate replies using the Anthropic API.
|
||
|
||
**Parameters:**
|
||
|
||
- **prompt** (<code>str</code>) – The input prompt for generation.
|
||
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for generation.
|
||
- **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – An optional callback function to handle streaming chunks.
|
||
|
||
**Returns:**
|
||
|
||
- <code>dict\[str, list\[str\] | list\[dict\[str, Any\]\]\]</code> – A dictionary containing:
|
||
- `replies`: A list of generated replies.
|
||
- `meta`: A list of metadata dictionaries for each reply.
|