Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Llama Index
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
Llama Index
Commits
2087401a
Unverified
Commit
2087401a
authored
1 year ago
by
vldvasi
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Handling the case where the response does not have a "usage" in the token counts function (#9298)
parent
d11c12d3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
llama_index/callbacks/token_counting.py
+15
-14
15 additions, 14 deletions
llama_index/callbacks/token_counting.py
with
15 additions
and
14 deletions
llama_index/callbacks/token_counting.py
+
15
−
14
View file @
2087401a
...
...
@@ -46,25 +46,26 @@ def get_llm_token_counts(
# try getting attached token counts first
try
:
usage
=
response
.
raw
[
"
usage
"
]
# type: ignore
messages_tokens
=
0
response_tokens
=
0
if
usage
is
not
None
:
messages_tokens
=
usage
.
prompt_tokens
response_tokens
=
usage
.
completion_tokens
if
response
is
not
None
and
response
.
raw
is
not
None
:
usage
=
response
.
raw
.
get
(
"
usage
"
)
# type: ignore
if
messages_tokens
==
0
or
response_tokens
==
0
:
raise
ValueError
(
"
Invalid token counts!
"
)
if
usage
is
not
None
:
messages_tokens
=
usage
.
prompt_tokens
response_tokens
=
usage
.
completion_tokens
return
TokenCountingEvent
(
event_id
=
event_id
,
prompt
=
messages_str
,
prompt_token_count
=
messages_tokens
,
completion
=
response_str
,
completion_token_count
=
response_tokens
,
)
if
messages_tokens
==
0
or
response_tokens
==
0
:
raise
ValueError
(
"
Invalid token counts!
"
)
return
TokenCountingEvent
(
event_id
=
event_id
,
prompt
=
messages_str
,
prompt_token_count
=
messages_tokens
,
completion
=
response_str
,
completion_token_count
=
response_tokens
,
)
except
(
ValueError
,
KeyError
):
# Invalid token counts, or no token counts attached
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment