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: make Anthropic compatible with new ChatMessage; fix prompt caching tests #1252

Merged
merged 2 commits into from
Dec 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dataclasses
import json
from typing import Any, Callable, ClassVar, Dict, List, Optional, Union

Expand Down Expand Up @@ -275,8 +274,16 @@ def _convert_to_anthropic_format(self, messages: List[ChatMessage]) -> List[Dict
"""
anthropic_formatted_messages = []
for m in messages:
message_dict = dataclasses.asdict(m)
formatted_message = {k: v for k, v in message_dict.items() if k in {"role", "content"} and v}
message_dict = m.to_dict()
formatted_message = {}

# legacy format
if "role" in message_dict and "content" in message_dict:
formatted_message = {k: v for k, v in message_dict.items() if k in {"role", "content"} and v}
# new format
elif "_role" in message_dict and "_content" in message_dict:
formatted_message = {"role": m.role.value, "content": m.text}

if m.is_from(ChatRole.SYSTEM):
# system messages are treated differently and MUST be in the format expected by the Anthropic API
# remove role and content from the message dict, add type and text
Expand Down
4 changes: 2 additions & 2 deletions integrations/anthropic/tests/test_chat_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,5 +428,5 @@ def test_prompt_caching(self, cache_enabled):
or token_usage.get("cache_read_input_tokens") > 1024
)
else:
assert "cache_creation_input_tokens" not in token_usage
assert "cache_read_input_tokens" not in token_usage
assert token_usage["cache_creation_input_tokens"] == 0
assert token_usage["cache_read_input_tokens"] == 0
Loading