Skip to content

⚡️ Speed up function _map_usage by 47% #18

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

Open
wants to merge 1 commit into
base: try-refinement
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions pydantic_ai_slim/pydantic_ai/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
check_allow_model_requests,
get_user_agent,
)
from mistralai import CompletionChunk as MistralCompletionChunk
from mistralai.models import ChatCompletionResponse as MistralChatCompletionResponse

try:
from mistralai import (
Expand Down Expand Up @@ -679,13 +681,9 @@ def _validate_required_json_schema(json_dict: dict[str, Any], json_schema: dict[

def _map_usage(response: MistralChatCompletionResponse | MistralCompletionChunk) -> Usage:
"""Maps a Mistral Completion Chunk or Chat Completion Response to a Usage."""
if response.usage:
return Usage(
request_tokens=response.usage.prompt_tokens,
response_tokens=response.usage.completion_tokens,
total_tokens=response.usage.total_tokens,
details=None,
)
usage = response.usage
if usage:
return Usage(usage.prompt_tokens, usage.completion_tokens, usage.total_tokens, None)
else:
return Usage() # pragma: no cover

Expand Down
Loading