Skip to content
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

Utility method for getting usage details from Response #644

Closed
simonw opened this issue Nov 20, 2024 · 2 comments
Closed

Utility method for getting usage details from Response #644

simonw opened this issue Nov 20, 2024 · 2 comments
Labels
enhancement New feature or request python-api

Comments

@simonw
Copy link
Owner

simonw commented Nov 20, 2024

Currently you can read usage numbers (from #610) like this:

response.input_tokens
response.output_tokens
response.token_details

But.. this only works if the response has been completed. It should work more like these methods:

llm/llm/models.py

Lines 350 to 355 in 02852fe

def duration_ms(self) -> int:
self._force()
return int(((self._end or 0) - (self._start or 0)) * 1000)
def datetime_utc(self) -> str:
self._force()

And in the AsyncResponse:

llm/llm/models.py

Lines 442 to 448 in 02852fe

async def duration_ms(self) -> int:
await self._force()
return int(((self._end or 0) - (self._start or 0)) * 1000)
async def datetime_utc(self) -> str:
await self._force()
return self._start_utcnow.isoformat() if self._start_utcnow else ""

@simonw simonw added enhancement New feature or request python-api labels Nov 20, 2024
@simonw
Copy link
Owner Author

simonw commented Nov 20, 2024

I think it's response.usage() and await response.usage() which call self._force() and then return the usage information as a dataclass with properties .input and .output and .details to match this set_usage() method:

llm/llm/models.py

Lines 215 to 224 in 02852fe

def set_usage(
self,
*,
input: Optional[int] = None,
output: Optional[int] = None,
details: Optional[dict] = None,
):
self.input_tokens = input
self.output_tokens = output
self.token_details = details

@simonw
Copy link
Owner Author

simonw commented Nov 20, 2024

This will be documented in:

@simonw simonw closed this as completed in 8a7b0c4 Nov 20, 2024
simonw added a commit that referenced this issue Nov 20, 2024
simonw added a commit that referenced this issue Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python-api
Projects
None yet
Development

No branches or pull requests

1 participant