Store provider and model details

This commit is contained in:
Kovid Goyal 2025-09-03 17:01:18 +05:30
parent 53c4bbb570
commit 29d4d3fc09
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 6 deletions

View File

@ -37,11 +37,18 @@ class ChatResponse(NamedTuple):
content: str = ''
reasoning: str = ''
type: ChatMessageType = ChatMessageType.assistant
cost: float = 0
currency: str = 'USD'
exception: Exception | None = None
error_details: str = '' # can be traceback or error message from HTTP response
# This metadata will typically be present in the last response from a
# streaming chat session.
has_metadata: bool = False
cost: float = 0
currency: str = 'USD'
provider: str = ''
model: str = ''
class NoFreeModels(Exception):
pass

View File

@ -310,7 +310,10 @@ def text_chat_implementation(messages: Iterable[ChatMessage]) -> Iterator[ChatRe
if c or r:
yield ChatResponse(content=c, reasoning=r, type=ChatMessageType(role))
if u := data.get('usage'):
yield ChatResponse(cost=float(u['cost'] or 0))
yield ChatResponse(
cost=float(u['cost'] or 0), currency=_('credits'), provider=data.get('provider') or '',
model=data.get('model') or '', has_metadata=True,
)
with opener().open(rq) as response:
if response.status != http.HTTPStatus.OK:
@ -350,9 +353,8 @@ def develop():
raise SystemExit(str(x.exception))
if x.content:
print(end=x.content, flush=True)
if x.cost:
print(f'Cost: {x.cost}')
print()
if x.has_metadata:
print(f'\nCost: {x.cost} {x.currency} Provider: {x.provider} Model: {x.model}')
if __name__ == '__main__':