Skip to content
Open
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
18 changes: 12 additions & 6 deletions apisix/plugins/ai-drivers/openai-base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ local function read_response(conf, ctx, res, response_filter)
core.log.info("got token usage from ai service: ",
core.json.delay_encode(data.usage))
ctx.llm_raw_usage = data.usage
local pt = data.usage.prompt_tokens or data.usage.input_tokens or 0
local ct = data.usage.completion_tokens or data.usage.output_tokens or 0
ctx.ai_token_usage = {
prompt_tokens = data.usage.prompt_tokens or 0,
completion_tokens = data.usage.completion_tokens or 0,
total_tokens = data.usage.total_tokens or 0,
prompt_tokens = pt,
completion_tokens = ct,
total_tokens = data.usage.total_tokens or (pt + ct),
}
ctx.var.llm_prompt_tokens = ctx.ai_token_usage.prompt_tokens
ctx.var.llm_completion_tokens = ctx.ai_token_usage.completion_tokens
Expand Down Expand Up @@ -188,9 +190,13 @@ local function read_response(conf, ctx, res, response_filter)
ctx.ai_token_usage = {}
if type(res_body.usage) == "table" then
ctx.llm_raw_usage = res_body.usage
ctx.ai_token_usage.prompt_tokens = res_body.usage.prompt_tokens or 0
ctx.ai_token_usage.completion_tokens = res_body.usage.completion_tokens or 0
ctx.ai_token_usage.total_tokens = res_body.usage.total_tokens or 0
ctx.ai_token_usage.prompt_tokens = res_body.usage.prompt_tokens
or res_body.usage.input_tokens or 0
ctx.ai_token_usage.completion_tokens = res_body.usage.completion_tokens
or res_body.usage.output_tokens or 0
ctx.ai_token_usage.total_tokens = res_body.usage.total_tokens
or (ctx.ai_token_usage.prompt_tokens
+ ctx.ai_token_usage.completion_tokens)
end
ctx.var.llm_prompt_tokens = ctx.ai_token_usage.prompt_tokens or 0
ctx.var.llm_completion_tokens = ctx.ai_token_usage.completion_tokens or 0
Expand Down
Loading