-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
Log input tokens, output tokens and token details #642
Conversation
I'm going to omit the token information from |
End output of
|
This diff to diff --git a/llm_claude_3.py b/llm_claude_3.py
index a05b01b..281084e 100644
--- a/llm_claude_3.py
+++ b/llm_claude_3.py
@@ -240,16 +240,23 @@ class ClaudeMessages(_Shared, llm.Model):
def execute(self, prompt, stream, response, conversation):
client = Anthropic(api_key=self.get_key())
kwargs = self.build_kwargs(prompt, conversation)
+ usage = None
if stream:
with client.messages.stream(**kwargs) as stream:
for text in stream.text_stream:
yield text
# This records usage and other data:
response.response_json = stream.get_final_message().model_dump()
+ usage = response.response_json.pop("usage")
else:
completion = client.messages.create(**kwargs)
yield completion.content[0].text
response.response_json = completion.model_dump()
+ usage = response.response_json.pop("usage")
+ if usage:
+ response.set_usage(
+ input=usage.get("input_tokens"), output=usage.get("output_tokens")
+ )
class ClaudeMessagesLong(ClaudeMessages): |
Better Claude diff: diff --git a/llm_claude_3.py b/llm_claude_3.py
index a05b01b..0a6e236 100644
--- a/llm_claude_3.py
+++ b/llm_claude_3.py
@@ -231,6 +231,13 @@ class _Shared:
kwargs["extra_headers"] = self.extra_headers
return kwargs
+ def set_usage(self, response):
+ usage = response.response_json.pop("usage")
+ if usage:
+ response.set_usage(
+ input=usage.get("input_tokens"), output=usage.get("output_tokens")
+ )
+
def __str__(self):
return "Anthropic Messages: {}".format(self.model_id)
@@ -250,6 +257,7 @@ class ClaudeMessages(_Shared, llm.Model):
completion = client.messages.create(**kwargs)
yield completion.content[0].text
response.response_json = completion.model_dump()
+ self.set_usage(response)
class ClaudeMessagesLong(ClaudeMessages):
@@ -270,6 +278,7 @@ class AsyncClaudeMessages(_Shared, llm.AsyncModel):
completion = await client.messages.create(**kwargs)
yield completion.content[0].text
response.response_json = completion.model_dump()
+ self.set_usage(response)
class AsyncClaudeMessagesLong(AsyncClaudeMessages): |
Refs:
TODO:
responses
table.llm prompt -u/--usage
optionllm logs
outputresponse.set_usage()
Document how to use this in the Python API (I'll need this myself for theI need to documentdatasette-llm
package)Response
generally, will do this in a new issue.