Skip to content

Commit

Permalink
Fill raw of TokenUsage in chat models
Browse files Browse the repository at this point in the history
  • Loading branch information
nallwhy committed Jan 16, 2025
1 parent c9081ff commit 0784df4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/chat_models/chat_anthropic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ defmodule LangChain.ChatModels.ChatAnthropic do
# https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage
TokenUsage.new!(%{
input: Map.get(usage, "input_tokens"),
output: Map.get(usage, "output_tokens")
output: Map.get(usage, "output_tokens"),
raw: usage
})
end

Expand Down
4 changes: 2 additions & 2 deletions lib/chat_models/chat_bumblebee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ defmodule LangChain.ChatModels.ChatBumblebee do
[result]
end

defp fire_token_usage_callback(model, %{input: input, output: output} = _token_summary) do
defp fire_token_usage_callback(model, %{input: input, output: output} = token_summary) do
Callbacks.fire(model.callbacks, :on_llm_token_usage, [
TokenUsage.new!(%{input: input, output: output})
TokenUsage.new!(%{input: input, output: output, raw: token_summary})
])
end

Expand Down
3 changes: 2 additions & 1 deletion lib/chat_models/chat_google_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
# extract out the reported response token usage
TokenUsage.new!(%{
input: Map.get(usage, "promptTokenCount", 0),
output: Map.get(usage, "candidatesTokenCount", 0)
output: Map.get(usage, "candidatesTokenCount", 0),
raw: usage
})
end

Expand Down
3 changes: 2 additions & 1 deletion lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,8 @@ defmodule LangChain.ChatModels.ChatOpenAI do
# https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage
TokenUsage.new!(%{
input: Map.get(usage, "prompt_tokens"),
output: Map.get(usage, "completion_tokens")
output: Map.get(usage, "completion_tokens"),
raw: usage
})
end

Expand Down

0 comments on commit 0784df4

Please sign in to comment.