Skip to content

Commit

Permalink
fix(anthropic/chat/transformation.py): return citations as a provider…
Browse files Browse the repository at this point in the history
…-specific-field

Resolves #7970
  • Loading branch information
krrishdholakia committed Feb 8, 2025
1 parent cda73c9 commit fa68e3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
5 changes: 5 additions & 0 deletions litellm/llms/anthropic/chat/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ def transform_response(
)
else:
text_content = ""
citations: List[Any] = []
tool_calls: List[ChatCompletionToolCallChunk] = []
for idx, content in enumerate(completion_response["content"]):
if content["type"] == "text":
Expand All @@ -645,10 +646,14 @@ def transform_response(
index=idx,
)
)
## CITATIONS
if content.get("citations", None) is not None:
citations.append(content["citations"])

_message = litellm.Message(
tool_calls=tool_calls,
content=text_content or None,
provider_specific_fields={"citations": citations},
)

## HANDLE JSON MODE - anthropic returns single function call
Expand Down
63 changes: 25 additions & 38 deletions tests/llm_translation/test_anthropic_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,45 +1092,32 @@ def test_anthropic_citations_api():
from litellm.llms.custom_httpx.http_handler import HTTPHandler
import json

client = HTTPHandler()

with patch.object(client, "post") as mock_post:
try:
resp = completion(
model="claude-3-5-sonnet-20241022",
messages=[
resp = completion(
model="claude-3-5-sonnet-20241022",
messages=[
{
"role": "user",
"content": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{
"type": "text",
"text": "What color is the grass and sky?",
},
],
}
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{
"type": "text",
"text": "What color is the grass and sky?",
},
],
client=client,
)

print(resp)
except Exception as e:
print(e)

mock_post.assert_called_once()

print(mock_post.call_args.kwargs)
}
],
)

json_data = json.loads(mock_post.call_args.kwargs["data"])
citations = resp.choices[0].message.provider_specific_fields["citations"]

assert json_data["messages"][0]["content"][0]["citations"]["enabled"]
assert citations is not None

0 comments on commit fa68e3b

Please sign in to comment.