Skip to content

Commit

Permalink
fix(llm): add a second pass usage metrics collect if the service
Browse files Browse the repository at this point in the history
response is not openai format
  • Loading branch information
fffonion committed Dec 2, 2024
1 parent fb99349 commit d4ae174
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions kong/llm/plugin/shared-filters/normalize-json-response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ local function transform_body(conf)
end

set_global_ctx("response_body", response_body) -- to be sent out later or consumed by other plugins

-- update the usage metrics, if the original request is not openai compatible format
-- TODO: to de-duplicate that from parse-json-response
local t, err
if response_body then
t, err = cjson.decode(response_body)
if err then
kong.log.warn("failed to decode response body for usage introspection: ", err)
end

if t and t.usage and t.usage.prompt_tokens then
ai_plugin_o11y.metrics_set("llm_prompt_tokens_count", t.usage.prompt_tokens)
end

if t and t.usage and t.usage.completion_tokens then
ai_plugin_o11y.metrics_set("llm_completion_tokens_count", t.usage.completion_tokens)
end
end
end

function _M:run(conf)
Expand Down

0 comments on commit d4ae174

Please sign in to comment.