anthropic.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. from collections.abc import Iterable
  3. from typing import Any, Literal, Union
  4. import anthropic
  5. import pytest
  6. from _pytest.monkeypatch import MonkeyPatch
  7. from anthropic import Anthropic, Stream
  8. from anthropic.resources import Messages
  9. from anthropic.types import (
  10. ContentBlock,
  11. ContentBlockDeltaEvent,
  12. Message,
  13. MessageDeltaEvent,
  14. MessageDeltaUsage,
  15. MessageParam,
  16. MessageStartEvent,
  17. MessageStopEvent,
  18. MessageStreamEvent,
  19. TextDelta,
  20. Usage,
  21. )
  22. from anthropic.types.message_delta_event import Delta
  23. MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
  24. class MockAnthropicClass:
  25. @staticmethod
  26. def mocked_anthropic_chat_create_sync(model: str) -> Message:
  27. return Message(
  28. id='msg-123',
  29. type='message',
  30. role='assistant',
  31. content=[ContentBlock(text='hello, I\'m a chatbot from anthropic', type='text')],
  32. model=model,
  33. stop_reason='stop_sequence',
  34. usage=Usage(
  35. input_tokens=1,
  36. output_tokens=1
  37. )
  38. )
  39. @staticmethod
  40. def mocked_anthropic_chat_create_stream(model: str) -> Stream[MessageStreamEvent]:
  41. full_response_text = "hello, I'm a chatbot from anthropic"
  42. yield MessageStartEvent(
  43. type='message_start',
  44. message=Message(
  45. id='msg-123',
  46. content=[],
  47. role='assistant',
  48. model=model,
  49. stop_reason=None,
  50. type='message',
  51. usage=Usage(
  52. input_tokens=1,
  53. output_tokens=1
  54. )
  55. )
  56. )
  57. index = 0
  58. for i in range(0, len(full_response_text)):
  59. yield ContentBlockDeltaEvent(
  60. type='content_block_delta',
  61. delta=TextDelta(text=full_response_text[i], type='text_delta'),
  62. index=index
  63. )
  64. index += 1
  65. yield MessageDeltaEvent(
  66. type='message_delta',
  67. delta=Delta(
  68. stop_reason='stop_sequence'
  69. ),
  70. usage=MessageDeltaUsage(
  71. output_tokens=1
  72. )
  73. )
  74. yield MessageStopEvent(type='message_stop')
  75. def mocked_anthropic(self: Messages, *,
  76. max_tokens: int,
  77. messages: Iterable[MessageParam],
  78. model: str,
  79. stream: Literal[True],
  80. **kwargs: Any
  81. ) -> Union[Message, Stream[MessageStreamEvent]]:
  82. if len(self._client.api_key) < 18:
  83. raise anthropic.AuthenticationError('Invalid API key')
  84. if stream:
  85. return MockAnthropicClass.mocked_anthropic_chat_create_stream(model=model)
  86. else:
  87. return MockAnthropicClass.mocked_anthropic_chat_create_sync(model=model)
  88. @pytest.fixture
  89. def setup_anthropic_mock(request, monkeypatch: MonkeyPatch):
  90. if MOCK:
  91. monkeypatch.setattr(Messages, 'create', MockAnthropicClass.mocked_anthropic)
  92. yield
  93. if MOCK:
  94. monkeypatch.undo()