| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | from collections.abc import Iterable | 
					
						
							|  |  |  | from typing import Any, Literal, Union | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | import anthropic | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from _pytest.monkeypatch import MonkeyPatch | 
					
						
							| 
									
										
										
										
											2024-10-08 11:13:11 +08:00
										 |  |  | from anthropic import Stream | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | from anthropic.resources import Messages | 
					
						
							|  |  |  | from anthropic.types import ( | 
					
						
							|  |  |  |     ContentBlock, | 
					
						
							|  |  |  |     ContentBlockDeltaEvent, | 
					
						
							|  |  |  |     Message, | 
					
						
							|  |  |  |     MessageDeltaEvent, | 
					
						
							|  |  |  |     MessageDeltaUsage, | 
					
						
							|  |  |  |     MessageParam, | 
					
						
							|  |  |  |     MessageStartEvent, | 
					
						
							|  |  |  |     MessageStopEvent, | 
					
						
							|  |  |  |     MessageStreamEvent, | 
					
						
							|  |  |  |     TextDelta, | 
					
						
							|  |  |  |     Usage, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | from anthropic.types.message_delta_event import Delta | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | MOCK = os.getenv("MOCK_SWITCH", "false") == "true" | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | class MockAnthropicClass: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |     def mocked_anthropic_chat_create_sync(model: str) -> Message: | 
					
						
							|  |  |  |         return Message( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             id="msg-123", | 
					
						
							|  |  |  |             type="message", | 
					
						
							|  |  |  |             role="assistant", | 
					
						
							|  |  |  |             content=[ContentBlock(text="hello, I'm a chatbot from anthropic", type="text")], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             model=model, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             stop_reason="stop_sequence", | 
					
						
							|  |  |  |             usage=Usage(input_tokens=1, output_tokens=1), | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |     def mocked_anthropic_chat_create_stream(model: str) -> Stream[MessageStreamEvent]: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         full_response_text = "hello, I'm a chatbot from anthropic" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |         yield MessageStartEvent( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             type="message_start", | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |             message=Message( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 id="msg-123", | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |                 content=[], | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 role="assistant", | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |                 model=model, | 
					
						
							|  |  |  |                 stop_reason=None, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 type="message", | 
					
						
							|  |  |  |                 usage=Usage(input_tokens=1, output_tokens=1), | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         index = 0 | 
					
						
							|  |  |  |         for i in range(0, len(full_response_text)): | 
					
						
							|  |  |  |             yield ContentBlockDeltaEvent( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 type="content_block_delta", delta=TextDelta(text=full_response_text[i], type="text_delta"), index=index | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             index += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield MessageDeltaEvent( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             type="message_delta", delta=Delta(stop_reason="stop_sequence"), usage=MessageDeltaUsage(output_tokens=1) | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         yield MessageStopEvent(type="message_stop") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def mocked_anthropic( | 
					
						
							|  |  |  |         self: Messages, | 
					
						
							|  |  |  |         *, | 
					
						
							|  |  |  |         max_tokens: int, | 
					
						
							|  |  |  |         messages: Iterable[MessageParam], | 
					
						
							|  |  |  |         model: str, | 
					
						
							|  |  |  |         stream: Literal[True], | 
					
						
							|  |  |  |         **kwargs: Any, | 
					
						
							|  |  |  |     ) -> Union[Message, Stream[MessageStreamEvent]]: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         if len(self._client.api_key) < 18: | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             raise anthropic.AuthenticationError("Invalid API key") | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if stream: | 
					
						
							|  |  |  |             return MockAnthropicClass.mocked_anthropic_chat_create_stream(model=model) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return MockAnthropicClass.mocked_anthropic_chat_create_sync(model=model) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | @pytest.fixture | 
					
						
							|  |  |  | def setup_anthropic_mock(request, monkeypatch: MonkeyPatch): | 
					
						
							|  |  |  |     if MOCK: | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         monkeypatch.setattr(Messages, "create", MockAnthropicClass.mocked_anthropic) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     yield | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if MOCK: | 
					
						
							| 
									
										
										
										
											2024-03-05 01:37:42 +08:00
										 |  |  |         monkeypatch.undo() |