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(AI-Proxy): improve the robustness of anthropic's statistics #12854

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Changes from 1 commit
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
22 changes: 15 additions & 7 deletions kong/llm/drivers/anthropic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ local transformers_from = {

if response_table.content then
local usage = response_table.usage

if usage then
usage = {
prompt_tokens = usage.input_tokens or nil,
completion_tokens = usage.output_tokens or nil,
total_tokens = usage.input_tokens and usage.output_tokens and
usage.input_tokens + usage.output_tokens or nil,
}

else
usage = "no usage data returned from upstream"
end

local res = {
choices = {
{
Expand All @@ -149,16 +162,11 @@ local transformers_from = {
finish_reason = response_table.stop_reason,
},
},
usage = usage and {
prompt_tokens = usage and usage.input_tokens or nil,
completion_tokens = usage and usage.output_tokens or nil,
total_tokens = usage and usage.input_tokens and usage.output_tokens and
usage.input_tokens + usage.output_tokens or nil,
} or "no usage data returned from upstream",
usage = usage,
model = response_table.model,
object = "chat.content",
}

return cjson.encode(res)
else
-- it's probably an error block, return generic error
Expand Down
Loading