Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bedrock.py): enable claude-3 streaming #2425

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions litellm/llms/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def map_openai_params(self, non_default_params: dict, optional_params: dict):
optional_params["max_tokens"] = value
if param == "tools":
optional_params["tools"] = value
if param == "stream":
optional_params["stream"] = value
return optional_params


Expand Down
25 changes: 25 additions & 0 deletions litellm/tests/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,31 @@ def test_completion_claude_stream_bad_key():
# pytest.fail(f"Error occurred: {e}")


def test_bedrock_claude_3_streaming():
try:
litellm.set_verbose = True
response: ModelResponse = completion(
model="bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
messages=messages,
max_tokens=10,
stream=True,
)
complete_response = ""
# Add any assertions here to check the response
for idx, chunk in enumerate(response):
chunk, finished = streaming_format_tests(idx, chunk)
if finished:
break
complete_response += chunk
if complete_response.strip() == "":
raise Exception("Empty response received")
print(f"completion_response: {complete_response}")
except RateLimitError:
pass
except Exception as e:
pytest.fail(f"Error occurred: {e}")


@pytest.mark.skip(reason="Replicate changed exceptions")
def test_completion_replicate_stream_bad_key():
try:
Expand Down
11 changes: 9 additions & 2 deletions litellm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8778,13 +8778,20 @@ def handle_bedrock_stream(self, chunk):
text = chunk_data.get("completions")[0].get("data").get("text")
is_finished = True
finish_reason = "stop"
# anthropic mapping
elif "completion" in chunk_data:
######## bedrock.anthropic mappings ###############
elif "completion" in chunk_data: # not claude-3
text = chunk_data["completion"] # bedrock.anthropic
stop_reason = chunk_data.get("stop_reason", None)
if stop_reason != None:
is_finished = True
finish_reason = stop_reason
elif "delta" in chunk_data:
if chunk_data["delta"].get("text", None) is not None:
text = chunk_data["delta"]["text"]
stop_reason = chunk_data["delta"].get("stop_reason", None)
if stop_reason != None:
is_finished = True
finish_reason = stop_reason
######## bedrock.cohere mappings ###############
# meta mapping
elif "generation" in chunk_data:
Expand Down