Skip to content

Commit

Permalink
fix: make Anthropic compatible with new ChatMessage; fix prompt cac…
Browse files Browse the repository at this point in the history
…hing tests (#1252)

* make Anthropic compatible with new chatmessage; fix prompt caching tests

* rm print
  • Loading branch information
anakin87 authored Dec 18, 2024
1 parent be3789f commit b12461d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit b12461d

Please sign in to comment.