|
| 1 | +import os |
| 2 | + |
| 3 | +import anthropic |
| 4 | +import pytest |
| 5 | +from haystack.components.generators.utils import print_streaming_chunk |
| 6 | +from haystack.dataclasses import ChatMessage, ChatRole |
| 7 | + |
| 8 | +from haystack_integrations.components.generators.anthropic import AnthropicVertexChatGenerator |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def chat_messages(): |
| 13 | + return [ |
| 14 | + ChatMessage.from_system("\\nYou are a helpful assistant, be super brief in your responses."), |
| 15 | + ChatMessage.from_user("What's the capital of France?"), |
| 16 | + ] |
| 17 | + |
| 18 | + |
| 19 | +class TestAnthropicVertexChatGenerator: |
| 20 | + def test_init_default(self): |
| 21 | + component = AnthropicVertexChatGenerator(region="us-central1", project_id="test-project-id") |
| 22 | + assert component.region == "us-central1" |
| 23 | + assert component.project_id == "test-project-id" |
| 24 | + assert component.model == "claude-3-5-sonnet@20240620" |
| 25 | + assert component.streaming_callback is None |
| 26 | + assert not component.generation_kwargs |
| 27 | + assert component.ignore_tools_thinking_messages |
| 28 | + |
| 29 | + def test_init_with_parameters(self): |
| 30 | + component = AnthropicVertexChatGenerator( |
| 31 | + region="us-central1", |
| 32 | + project_id="test-project-id", |
| 33 | + model="claude-3-5-sonnet@20240620", |
| 34 | + streaming_callback=print_streaming_chunk, |
| 35 | + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, |
| 36 | + ignore_tools_thinking_messages=False, |
| 37 | + ) |
| 38 | + assert component.region == "us-central1" |
| 39 | + assert component.project_id == "test-project-id" |
| 40 | + assert component.model == "claude-3-5-sonnet@20240620" |
| 41 | + assert component.streaming_callback is print_streaming_chunk |
| 42 | + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} |
| 43 | + assert component.ignore_tools_thinking_messages is False |
| 44 | + |
| 45 | + def test_to_dict_default(self): |
| 46 | + component = AnthropicVertexChatGenerator(region="us-central1", project_id="test-project-id") |
| 47 | + data = component.to_dict() |
| 48 | + assert data == { |
| 49 | + "type": ( |
| 50 | + "haystack_integrations.components.generators." |
| 51 | + "anthropic.chat.vertex_chat_generator.AnthropicVertexChatGenerator" |
| 52 | + ), |
| 53 | + "init_parameters": { |
| 54 | + "region": "us-central1", |
| 55 | + "project_id": "test-project-id", |
| 56 | + "model": "claude-3-5-sonnet@20240620", |
| 57 | + "streaming_callback": None, |
| 58 | + "generation_kwargs": {}, |
| 59 | + "ignore_tools_thinking_messages": True, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + def test_to_dict_with_parameters(self): |
| 64 | + component = AnthropicVertexChatGenerator( |
| 65 | + region="us-central1", |
| 66 | + project_id="test-project-id", |
| 67 | + streaming_callback=print_streaming_chunk, |
| 68 | + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, |
| 69 | + ) |
| 70 | + data = component.to_dict() |
| 71 | + assert data == { |
| 72 | + "type": ( |
| 73 | + "haystack_integrations.components.generators." |
| 74 | + "anthropic.chat.vertex_chat_generator.AnthropicVertexChatGenerator" |
| 75 | + ), |
| 76 | + "init_parameters": { |
| 77 | + "region": "us-central1", |
| 78 | + "project_id": "test-project-id", |
| 79 | + "model": "claude-3-5-sonnet@20240620", |
| 80 | + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", |
| 81 | + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, |
| 82 | + "ignore_tools_thinking_messages": True, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + def test_to_dict_with_lambda_streaming_callback(self): |
| 87 | + component = AnthropicVertexChatGenerator( |
| 88 | + region="us-central1", |
| 89 | + project_id="test-project-id", |
| 90 | + model="claude-3-5-sonnet@20240620", |
| 91 | + streaming_callback=lambda x: x, |
| 92 | + generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, |
| 93 | + ) |
| 94 | + data = component.to_dict() |
| 95 | + assert data == { |
| 96 | + "type": ( |
| 97 | + "haystack_integrations.components.generators." |
| 98 | + "anthropic.chat.vertex_chat_generator.AnthropicVertexChatGenerator" |
| 99 | + ), |
| 100 | + "init_parameters": { |
| 101 | + "region": "us-central1", |
| 102 | + "project_id": "test-project-id", |
| 103 | + "model": "claude-3-5-sonnet@20240620", |
| 104 | + "streaming_callback": "tests.test_vertex_chat_generator.<lambda>", |
| 105 | + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, |
| 106 | + "ignore_tools_thinking_messages": True, |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + def test_from_dict(self): |
| 111 | + data = { |
| 112 | + "type": ( |
| 113 | + "haystack_integrations.components.generators." |
| 114 | + "anthropic.chat.vertex_chat_generator.AnthropicVertexChatGenerator" |
| 115 | + ), |
| 116 | + "init_parameters": { |
| 117 | + "region": "us-central1", |
| 118 | + "project_id": "test-project-id", |
| 119 | + "model": "claude-3-5-sonnet@20240620", |
| 120 | + "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk", |
| 121 | + "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, |
| 122 | + "ignore_tools_thinking_messages": True, |
| 123 | + }, |
| 124 | + } |
| 125 | + component = AnthropicVertexChatGenerator.from_dict(data) |
| 126 | + assert component.model == "claude-3-5-sonnet@20240620" |
| 127 | + assert component.region == "us-central1" |
| 128 | + assert component.project_id == "test-project-id" |
| 129 | + assert component.streaming_callback is print_streaming_chunk |
| 130 | + assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"} |
| 131 | + |
| 132 | + def test_run(self, chat_messages, mock_chat_completion): |
| 133 | + component = AnthropicVertexChatGenerator(region="us-central1", project_id="test-project-id") |
| 134 | + response = component.run(chat_messages) |
| 135 | + |
| 136 | + # check that the component returns the correct ChatMessage response |
| 137 | + assert isinstance(response, dict) |
| 138 | + assert "replies" in response |
| 139 | + assert isinstance(response["replies"], list) |
| 140 | + assert len(response["replies"]) == 1 |
| 141 | + assert [isinstance(reply, ChatMessage) for reply in response["replies"]] |
| 142 | + |
| 143 | + def test_run_with_params(self, chat_messages, mock_chat_completion): |
| 144 | + component = AnthropicVertexChatGenerator( |
| 145 | + region="us-central1", project_id="test-project-id", generation_kwargs={"max_tokens": 10, "temperature": 0.5} |
| 146 | + ) |
| 147 | + response = component.run(chat_messages) |
| 148 | + |
| 149 | + # check that the component calls the Anthropic API with the correct parameters |
| 150 | + _, kwargs = mock_chat_completion.call_args |
| 151 | + assert kwargs["max_tokens"] == 10 |
| 152 | + assert kwargs["temperature"] == 0.5 |
| 153 | + |
| 154 | + # check that the component returns the correct response |
| 155 | + assert isinstance(response, dict) |
| 156 | + assert "replies" in response |
| 157 | + assert isinstance(response["replies"], list) |
| 158 | + assert len(response["replies"]) == 1 |
| 159 | + assert [isinstance(reply, ChatMessage) for reply in response["replies"]] |
| 160 | + |
| 161 | + @pytest.mark.skipif( |
| 162 | + not (os.environ.get("REGION", None) or os.environ.get("PROJECT_ID", None)), |
| 163 | + reason="Authenticate with GCP and set env variables REGION and PROJECT_ID to run this test.", |
| 164 | + ) |
| 165 | + @pytest.mark.integration |
| 166 | + def test_live_run_wrong_model(self, chat_messages): |
| 167 | + component = AnthropicVertexChatGenerator( |
| 168 | + model="something-obviously-wrong", region=os.environ.get("REGION"), project_id=os.environ.get("PROJECT_ID") |
| 169 | + ) |
| 170 | + with pytest.raises(anthropic.NotFoundError): |
| 171 | + component.run(chat_messages) |
| 172 | + |
| 173 | + @pytest.mark.skipif( |
| 174 | + not (os.environ.get("REGION", None) or os.environ.get("PROJECT_ID", None)), |
| 175 | + reason="Authenticate with GCP and set env variables REGION and PROJECT_ID to run this test.", |
| 176 | + ) |
| 177 | + @pytest.mark.integration |
| 178 | + def test_default_inference_params(self, chat_messages): |
| 179 | + client = AnthropicVertexChatGenerator( |
| 180 | + region=os.environ.get("REGION"), project_id=os.environ.get("PROJECT_ID"), model="claude-3-sonnet@20240229" |
| 181 | + ) |
| 182 | + response = client.run(chat_messages) |
| 183 | + |
| 184 | + assert "replies" in response, "Response does not contain 'replies' key" |
| 185 | + replies = response["replies"] |
| 186 | + assert isinstance(replies, list), "Replies is not a list" |
| 187 | + assert len(replies) > 0, "No replies received" |
| 188 | + |
| 189 | + first_reply = replies[0] |
| 190 | + assert isinstance(first_reply, ChatMessage), "First reply is not a ChatMessage instance" |
| 191 | + assert first_reply.content, "First reply has no content" |
| 192 | + assert ChatMessage.is_from(first_reply, ChatRole.ASSISTANT), "First reply is not from the assistant" |
| 193 | + assert "paris" in first_reply.content.lower(), "First reply does not contain 'paris'" |
| 194 | + assert first_reply.meta, "First reply has no metadata" |
| 195 | + |
| 196 | + # Anthropic messages API is similar for AnthropicVertex and Anthropic endpoint, |
| 197 | + # remaining tests are skipped for AnthropicVertexChatGenerator as they are already tested in AnthropicChatGenerator. |
0 commit comments