Skip to content

Commit

Permalink
feat: Update Anthropic default models, pydocs (#839)
Browse files Browse the repository at this point in the history
* Update default models, pydocs

* Update unit test
  • Loading branch information
vblagoje authored Jun 21, 2024
1 parent 1f7f75a commit 7cd71f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"llm",
AnthropicChatGenerator(
api_key=Secret.from_env_var("ANTHROPIC_API_KEY"),
model="claude-3-sonnet-20240229",
streaming_callback=print_streaming_chunk,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,38 @@ class AnthropicChatGenerator:
Enables text generation using Anthropic state-of-the-art Claude 3 family of large language models (LLMs) through
the Anthropic messaging API.
It supports models like `claude-3-opus`, `claude-3-sonnet`, and `claude-3-haiku`, accessed through the
`/v1/messages` API endpoint using the Claude v2.1 messaging version.
It supports models like `claude-3-5-sonnet`, `claude-3-opus`, `claude-3-sonnet`, and `claude-3-haiku`,
accessed through the [`/v1/messages`](https://docs.anthropic.com/en/api/messages) API endpoint.
Users can pass any text generation parameters valid for the Anthropic messaging API directly to this component
via the `generation_kwargs` parameter in `__init__` or the `generation_kwargs` parameter in 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/claude/reference/messages_post).
Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages).
```python
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage
messages = [ChatMessage.from_user("What's Natural Language Processing?")]
client = AnthropicChatGenerator(model="claude-3-sonnet-20240229")
client = AnthropicChatGenerator(model="claude-3-5-sonnet-20240620")
response = client.run(messages)
print(response)
>> {'replies': [ChatMessage(content='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.', role=<ChatRole.ASSISTANT: 'assistant'>,
>> name=None, meta={'model': 'claude-3-sonnet-20240229', 'index': 0, 'finish_reason': 'end_turn',
>> name=None, meta={'model': 'claude-3-5-sonnet-20240620', '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).
Note: We don't yet support vision [capabilities](https://docs.anthropic.com/claude/docs/vision) in the current
implementation.
Note: We only support text input/output modalities, and
image [modality](https://docs.anthropic.com/en/docs/build-with-claude/vision) is not supported in
this version of AnthropicChatGenerator.
"""

# The parameters that can be passed to the Anthropic API https://docs.anthropic.com/claude/reference/messages_post
Expand All @@ -76,7 +77,7 @@ class AnthropicChatGenerator:
def __init__(
self,
api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), # noqa: B008
model: str = "claude-3-sonnet-20240229",
model: str = "claude-3-5-sonnet-20240620",
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
generation_kwargs: Optional[Dict[str, Any]] = None,
ignore_tools_thinking_messages: bool = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AnthropicGenerator:
```python
from haystack_integrations.components.generators.anthropic import AnthropicGenerator
client = AnthropicGenerator(model="claude-2.1")
client = AnthropicGenerator(model="claude-3-sonnet-20240229")
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
Expand Down
20 changes: 10 additions & 10 deletions integrations/anthropic/tests/test_chat_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_init_default(self, monkeypatch):
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key")
component = AnthropicChatGenerator()
assert component.client.api_key == "test-api-key"
assert component.model == "claude-3-sonnet-20240229"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.streaming_callback is None
assert not component.generation_kwargs
assert component.ignore_tools_thinking_messages
Expand All @@ -36,13 +36,13 @@ def test_init_fail_wo_api_key(self, monkeypatch):
def test_init_with_parameters(self):
component = AnthropicChatGenerator(
api_key=Secret.from_token("test-api-key"),
model="claude-3-sonnet-20240229",
model="claude-3-5-sonnet-20240620",
streaming_callback=print_streaming_chunk,
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
ignore_tools_thinking_messages=False,
)
assert component.client.api_key == "test-api-key"
assert component.model == "claude-3-sonnet-20240229"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.streaming_callback is print_streaming_chunk
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
assert component.ignore_tools_thinking_messages is False
Expand All @@ -55,7 +55,7 @@ def test_to_dict_default(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"},
"model": "claude-3-sonnet-20240229",
"model": "claude-3-5-sonnet-20240620",
"streaming_callback": None,
"generation_kwargs": {},
"ignore_tools_thinking_messages": True,
Expand All @@ -74,7 +74,7 @@ def test_to_dict_with_parameters(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
"model": "claude-3-sonnet-20240229",
"model": "claude-3-5-sonnet-20240620",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
"ignore_tools_thinking_messages": True,
Expand All @@ -84,7 +84,7 @@ def test_to_dict_with_parameters(self, monkeypatch):
def test_to_dict_with_lambda_streaming_callback(self, monkeypatch):
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key")
component = AnthropicChatGenerator(
model="claude-3-sonnet-20240229",
model="claude-3-5-sonnet-20240620",
streaming_callback=lambda x: x,
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
)
Expand All @@ -93,7 +93,7 @@ def test_to_dict_with_lambda_streaming_callback(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"},
"model": "claude-3-sonnet-20240229",
"model": "claude-3-5-sonnet-20240620",
"streaming_callback": "tests.test_chat_generator.<lambda>",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
"ignore_tools_thinking_messages": True,
Expand All @@ -106,14 +106,14 @@ def test_from_dict(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"},
"model": "claude-3-sonnet-20240229",
"model": "claude-3-5-sonnet-20240620",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
"ignore_tools_thinking_messages": True,
},
}
component = AnthropicChatGenerator.from_dict(data)
assert component.model == "claude-3-sonnet-20240229"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.streaming_callback is print_streaming_chunk
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
assert component.api_key == Secret.from_env_var("ANTHROPIC_API_KEY")
Expand All @@ -124,7 +124,7 @@ def test_from_dict_fail_wo_env_var(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"},
"model": "claude-3-sonnet-20240229",
"model": "claude-3-5-sonnet-20240620",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
"ignore_tools_thinking_messages": True,
Expand Down

0 comments on commit 7cd71f1

Please sign in to comment.