View Source LangChain.TokenUsage (LangChain v0.4.0-rc.2)
Contains token usage information returned from an LLM.
Example
%TokenUsage{
input: 30,
output: 15,
raw: %{
"total_tokens" => 29
}
}
Input is the tokens from the prompt. Output is the completion or generated tokens returned.
Refer to the raw
token usage information for access to LLM-specific information that may be available.
Summary
Functions
Combines two TokenUsage structs by adding their respective input and output values. The raw maps are merged, with values being added if they are numeric.
Extracts token usage information from a LangChain.Message
or
LangChain.MessageDelta
struct's metadata. Returns nil if no token usage
information is found.
Build a new TokenUsage and return an :ok
/:error
tuple with the result.
Build a new TokenUsage and return it or raise an error if invalid.
Sets the token usage information on a LangChain.Message
or
LangChain.MessageDelta
struct in the metadata
under the :usage
key.
Sets the token usage information on a LangChain.Message
or
LangChain.MessageDelta
struct when wrapped in an :ok,:error tuple in the metadata
under the :usage
key.
Return the total token usage amount. The total is the sum of input and output.
Types
Functions
Combines two TokenUsage structs by adding their respective input and output values. The raw maps are merged, with values being added if they are numeric.
If both arguments are nil, returns nil. If one argument is nil, returns the non-nil argument.
Example
iex> usage1 = LangChain.TokenUsage.new!(%{input: 10, output: 20, raw: %{"total_tokens" => 30}})
iex> usage2 = LangChain.TokenUsage.new!(%{input: 5, output: 15, raw: %{"total_tokens" => 20}})
iex> combined = LangChain.TokenUsage.add(usage1, usage2)
iex> combined.input
15
iex> combined.output
35
iex> combined.raw["total_tokens"]
50
Extracts token usage information from a LangChain.Message
or
LangChain.MessageDelta
struct's metadata. Returns nil if no token usage
information is found.
Example
iex> message = %LangChain.Message{metadata: %{usage: %LangChain.TokenUsage{input: 10, output: 20}}}
iex> LangChain.TokenUsage.get(message)
%LangChain.TokenUsage{input: 10, output: 20}
iex> message = %LangChain.Message{metadata: %{}}
iex> LangChain.TokenUsage.get(message)
nil
@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
Build a new TokenUsage and return an :ok
/:error
tuple with the result.
Build a new TokenUsage and return it or raise an error if invalid.
Sets the token usage information on a LangChain.Message
or
LangChain.MessageDelta
struct in the metadata
under the :usage
key.
Example
iex> message = %LangChain.Message{metadata: %{}}
iex> token_usage = %LangChain.TokenUsage{input: 10, output: 20}
iex> LangChain.TokenUsage.set(message, token_usage)
%LangChain.Message{metadata: %{usage: %LangChain.TokenUsage{input: 10, output: 20}}}
@spec set_wrapped( {:ok, %{metadata: nil | map()}}, nil | t() ) :: {:ok, %{metadata: %{usage: t()}} | {:error, any()}}
Sets the token usage information on a LangChain.Message
or
LangChain.MessageDelta
struct when wrapped in an :ok,:error tuple in the metadata
under the :usage
key.
Return the total token usage amount. The total is the sum of input and output.