|
1 |
| -from llama_index.core.base.llms.base import BaseLLM |
| 1 | +import pytest |
2 | 2 | from llama_index.llms.bedrock_converse import BedrockConverse
|
| 3 | +from llama_index.core.base.llms.types import ( |
| 4 | + ChatMessage, |
| 5 | + ChatResponse, |
| 6 | + MessageRole, |
| 7 | + CompletionResponse, |
| 8 | +) |
| 9 | +from llama_index.core.callbacks import CallbackManager |
3 | 10 |
|
| 11 | +# Expected values |
| 12 | +EXP_RESPONSE = "Test" |
| 13 | +EXP_STREAM_RESPONSE = ["Test ", "value"] |
| 14 | +EXP_MAX_TOKENS = 100 |
| 15 | +EXP_TEMPERATURE = 0.7 |
| 16 | +EXP_MODEL = "anthropic.claude-v2" |
4 | 17 |
|
5 |
| -def test_text_inference_embedding_class(): |
6 |
| - names_of_base_classes = [b.__name__ for b in BedrockConverse.__mro__] |
7 |
| - assert BaseLLM.__name__ in names_of_base_classes |
| 18 | +# Reused chat message and prompt |
| 19 | +messages = [ChatMessage(role=MessageRole.USER, content="Test")] |
| 20 | +prompt = "Test" |
| 21 | + |
| 22 | + |
| 23 | +class MockExceptions: |
| 24 | + class ThrottlingException(Exception): |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class AsyncMockClient: |
| 29 | + def __init__(self) -> "AsyncMockClient": |
| 30 | + self.exceptions = MockExceptions() |
| 31 | + |
| 32 | + async def __aenter__(self) -> "AsyncMockClient": |
| 33 | + return self |
| 34 | + |
| 35 | + async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: |
| 36 | + pass |
| 37 | + |
| 38 | + async def converse(self, *args, **kwargs): |
| 39 | + return {"output": {"message": {"content": [{"text": EXP_RESPONSE}]}}} |
| 40 | + |
| 41 | + async def converse_stream(self, *args, **kwargs): |
| 42 | + async def stream_generator(): |
| 43 | + for element in EXP_STREAM_RESPONSE: |
| 44 | + yield {"contentBlockDelta": {"delta": {"text": element}}} |
| 45 | + |
| 46 | + return {"stream": stream_generator()} |
| 47 | + |
| 48 | + |
| 49 | +class MockClient: |
| 50 | + def __init__(self) -> "MockClient": |
| 51 | + self.exceptions = MockExceptions() |
| 52 | + |
| 53 | + def converse(self, *args, **kwargs): |
| 54 | + return {"output": {"message": {"content": [{"text": EXP_RESPONSE}]}}} |
| 55 | + |
| 56 | + def converse_stream(self, *args, **kwargs): |
| 57 | + def stream_generator(): |
| 58 | + for element in EXP_STREAM_RESPONSE: |
| 59 | + yield {"contentBlockDelta": {"delta": {"text": element}}} |
| 60 | + |
| 61 | + return {"stream": stream_generator()} |
| 62 | + |
| 63 | + |
| 64 | +class MockAsyncSession: |
| 65 | + def __init__(self, *args, **kwargs) -> "MockAsyncSession": |
| 66 | + pass |
| 67 | + |
| 68 | + def client(self, *args, **kwargs): |
| 69 | + return AsyncMockClient() |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture() |
| 73 | +def mock_boto3_session(monkeypatch): |
| 74 | + def mock_client(*args, **kwargs): |
| 75 | + return MockClient() |
| 76 | + |
| 77 | + monkeypatch.setattr("boto3.Session.client", mock_client) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.fixture() |
| 81 | +def mock_aioboto3_session(monkeypatch): |
| 82 | + monkeypatch.setattr("aioboto3.Session", MockAsyncSession) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture() |
| 86 | +def bedrock_converse(mock_boto3_session, mock_aioboto3_session): |
| 87 | + return BedrockConverse( |
| 88 | + model=EXP_MODEL, |
| 89 | + max_tokens=EXP_MAX_TOKENS, |
| 90 | + temperature=EXP_TEMPERATURE, |
| 91 | + callback_manager=CallbackManager(), |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_init(bedrock_converse): |
| 96 | + assert bedrock_converse.model == EXP_MODEL |
| 97 | + assert bedrock_converse.max_tokens == EXP_MAX_TOKENS |
| 98 | + assert bedrock_converse.temperature == EXP_TEMPERATURE |
| 99 | + assert bedrock_converse._client is not None |
| 100 | + |
| 101 | + |
| 102 | +def test_chat(bedrock_converse): |
| 103 | + response = bedrock_converse.chat(messages) |
| 104 | + |
| 105 | + assert response.message.role == MessageRole.ASSISTANT |
| 106 | + assert response.message.content == EXP_RESPONSE |
| 107 | + |
| 108 | + |
| 109 | +def test_complete(bedrock_converse): |
| 110 | + response = bedrock_converse.complete(prompt) |
| 111 | + |
| 112 | + assert isinstance(response, CompletionResponse) |
| 113 | + assert response.text == EXP_RESPONSE |
| 114 | + assert response.additional_kwargs["status"] == [] |
| 115 | + assert response.additional_kwargs["tool_call_id"] == [] |
| 116 | + assert response.additional_kwargs["tool_calls"] == [] |
| 117 | + |
| 118 | + |
| 119 | +def test_stream_chat(bedrock_converse): |
| 120 | + response_stream = bedrock_converse.stream_chat(messages) |
| 121 | + |
| 122 | + for response in response_stream: |
| 123 | + assert response.message.role == MessageRole.ASSISTANT |
| 124 | + assert response.delta in EXP_STREAM_RESPONSE |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.asyncio() |
| 128 | +async def test_achat(bedrock_converse): |
| 129 | + response = await bedrock_converse.achat(messages) |
| 130 | + |
| 131 | + assert isinstance(response, ChatResponse) |
| 132 | + assert response.message.role == MessageRole.ASSISTANT |
| 133 | + assert response.message.content == EXP_RESPONSE |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio() |
| 137 | +async def test_astream_chat(bedrock_converse): |
| 138 | + response_stream = await bedrock_converse.astream_chat(messages) |
| 139 | + |
| 140 | + responses = [] |
| 141 | + async for response in response_stream: |
| 142 | + assert response.message.role == MessageRole.ASSISTANT |
| 143 | + assert response.delta in EXP_STREAM_RESPONSE |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.asyncio() |
| 147 | +async def test_acomplete(bedrock_converse): |
| 148 | + response = await bedrock_converse.acomplete(prompt) |
| 149 | + |
| 150 | + assert isinstance(response, CompletionResponse) |
| 151 | + assert response.text == EXP_RESPONSE |
| 152 | + assert response.additional_kwargs["status"] == [] |
| 153 | + assert response.additional_kwargs["tool_call_id"] == [] |
| 154 | + assert response.additional_kwargs["tool_calls"] == [] |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.asyncio() |
| 158 | +async def test_astream_complete(bedrock_converse): |
| 159 | + response_stream = await bedrock_converse.astream_complete(prompt) |
| 160 | + |
| 161 | + async for response in response_stream: |
| 162 | + assert response.delta in EXP_STREAM_RESPONSE |
0 commit comments