|
| 1 | +from base_llm_unit_tests import BaseLLMChatTest |
| 2 | +import pytest |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +sys.path.insert( |
| 8 | + 0, os.path.abspath("../..") |
| 9 | +) # Adds the parent directory to the system path |
| 10 | +import litellm |
| 11 | +from litellm.types.llms.bedrock import BedrockInvokeNovaRequest |
| 12 | + |
| 13 | + |
| 14 | +class TestBedrockInvokeClaudeJson(BaseLLMChatTest): |
| 15 | + def get_base_completion_call_args(self) -> dict: |
| 16 | + litellm._turn_on_debug() |
| 17 | + return { |
| 18 | + "model": "bedrock/invoke/anthropic.claude-3-5-sonnet-20240620-v1:0", |
| 19 | + } |
| 20 | + |
| 21 | + def test_tool_call_no_arguments(self, tool_call_no_arguments): |
| 22 | + """Test that tool calls with no arguments is translated correctly. Relevant issue: https://github.com/BerriAI/litellm/issues/6833""" |
| 23 | + pass |
| 24 | + |
| 25 | + @pytest.fixture(autouse=True) |
| 26 | + def skip_non_json_tests(self, request): |
| 27 | + if not "json" in request.function.__name__.lower(): |
| 28 | + pytest.skip( |
| 29 | + f"Skipping non-JSON test: {request.function.__name__} does not contain 'json'" |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class TestBedrockInvokeNovaJson(BaseLLMChatTest): |
| 34 | + def get_base_completion_call_args(self) -> dict: |
| 35 | + litellm._turn_on_debug() |
| 36 | + return { |
| 37 | + "model": "bedrock/invoke/us.amazon.nova-micro-v1:0", |
| 38 | + } |
| 39 | + |
| 40 | + def test_tool_call_no_arguments(self, tool_call_no_arguments): |
| 41 | + """Test that tool calls with no arguments is translated correctly. Relevant issue: https://github.com/BerriAI/litellm/issues/6833""" |
| 42 | + pass |
| 43 | + |
| 44 | + @pytest.fixture(autouse=True) |
| 45 | + def skip_non_json_tests(self, request): |
| 46 | + if not "json" in request.function.__name__.lower(): |
| 47 | + pytest.skip( |
| 48 | + f"Skipping non-JSON test: {request.function.__name__} does not contain 'json'" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_nova_invoke_remove_empty_system_messages(): |
| 53 | + """Test that _remove_empty_system_messages removes empty system list.""" |
| 54 | + input_request = BedrockInvokeNovaRequest( |
| 55 | + messages=[{"content": [{"text": "Hello"}], "role": "user"}], |
| 56 | + system=[], |
| 57 | + inferenceConfig={"temperature": 0.7}, |
| 58 | + ) |
| 59 | + |
| 60 | + litellm.AmazonInvokeNovaConfig()._remove_empty_system_messages(input_request) |
| 61 | + |
| 62 | + assert "system" not in input_request |
| 63 | + assert "messages" in input_request |
| 64 | + assert "inferenceConfig" in input_request |
| 65 | + |
| 66 | + |
| 67 | +def test_nova_invoke_filter_allowed_fields(): |
| 68 | + """ |
| 69 | + Test that _filter_allowed_fields only keeps fields defined in BedrockInvokeNovaRequest. |
| 70 | +
|
| 71 | + Nova Invoke does not allow `additionalModelRequestFields` and `additionalModelResponseFieldPaths` in the request body. |
| 72 | + This test ensures that these fields are not included in the request body. |
| 73 | + """ |
| 74 | + _input_request = { |
| 75 | + "messages": [{"content": [{"text": "Hello"}], "role": "user"}], |
| 76 | + "system": [{"text": "System prompt"}], |
| 77 | + "inferenceConfig": {"temperature": 0.7}, |
| 78 | + "additionalModelRequestFields": {"this": "should be removed"}, |
| 79 | + "additionalModelResponseFieldPaths": ["this", "should", "be", "removed"], |
| 80 | + } |
| 81 | + |
| 82 | + input_request = BedrockInvokeNovaRequest(**_input_request) |
| 83 | + |
| 84 | + result = litellm.AmazonInvokeNovaConfig()._filter_allowed_fields(input_request) |
| 85 | + |
| 86 | + assert "additionalModelRequestFields" not in result |
| 87 | + assert "additionalModelResponseFieldPaths" not in result |
| 88 | + assert "messages" in result |
| 89 | + assert "system" in result |
| 90 | + assert "inferenceConfig" in result |
| 91 | + |
| 92 | + |
| 93 | +def test_nova_invoke_streaming_chunk_parsing(): |
| 94 | + """ |
| 95 | + Test that the AWSEventStreamDecoder correctly handles Nova's /bedrock/invoke/ streaming format |
| 96 | + where content is nested under 'contentBlockDelta'. |
| 97 | + """ |
| 98 | + from litellm.llms.bedrock.chat.invoke_handler import AWSEventStreamDecoder |
| 99 | + |
| 100 | + # Initialize the decoder with a Nova model |
| 101 | + decoder = AWSEventStreamDecoder(model="bedrock/invoke/us.amazon.nova-micro-v1:0") |
| 102 | + |
| 103 | + # Test case 1: Text content in contentBlockDelta |
| 104 | + nova_text_chunk = { |
| 105 | + "contentBlockDelta": { |
| 106 | + "delta": {"text": "Hello, how can I help?"}, |
| 107 | + "contentBlockIndex": 0, |
| 108 | + } |
| 109 | + } |
| 110 | + result = decoder._chunk_parser(nova_text_chunk) |
| 111 | + assert result["text"] == "Hello, how can I help?" |
| 112 | + assert result["index"] == 0 |
| 113 | + assert not result["is_finished"] |
| 114 | + assert result["tool_use"] is None |
| 115 | + |
| 116 | + # Test case 2: Tool use start in contentBlockDelta |
| 117 | + nova_tool_start_chunk = { |
| 118 | + "contentBlockDelta": { |
| 119 | + "start": {"toolUse": {"name": "get_weather", "toolUseId": "tool_1"}}, |
| 120 | + "contentBlockIndex": 1, |
| 121 | + } |
| 122 | + } |
| 123 | + result = decoder._chunk_parser(nova_tool_start_chunk) |
| 124 | + assert result["text"] == "" |
| 125 | + assert result["index"] == 1 |
| 126 | + assert result["tool_use"] is not None |
| 127 | + assert result["tool_use"]["type"] == "function" |
| 128 | + assert result["tool_use"]["function"]["name"] == "get_weather" |
| 129 | + assert result["tool_use"]["id"] == "tool_1" |
| 130 | + |
| 131 | + # Test case 3: Tool use arguments in contentBlockDelta |
| 132 | + nova_tool_args_chunk = { |
| 133 | + "contentBlockDelta": { |
| 134 | + "delta": {"toolUse": {"input": '{"location": "New York"}'}}, |
| 135 | + "contentBlockIndex": 2, |
| 136 | + } |
| 137 | + } |
| 138 | + result = decoder._chunk_parser(nova_tool_args_chunk) |
| 139 | + assert result["text"] == "" |
| 140 | + assert result["index"] == 2 |
| 141 | + assert result["tool_use"] is not None |
| 142 | + assert result["tool_use"]["function"]["arguments"] == '{"location": "New York"}' |
| 143 | + |
| 144 | + # Test case 4: Stop reason in contentBlockDelta |
| 145 | + nova_stop_chunk = { |
| 146 | + "contentBlockDelta": { |
| 147 | + "stopReason": "tool_use", |
| 148 | + } |
| 149 | + } |
| 150 | + result = decoder._chunk_parser(nova_stop_chunk) |
| 151 | + print(result) |
| 152 | + assert result["is_finished"] is True |
| 153 | + assert result["finish_reason"] == "tool_calls" |
0 commit comments