-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathstream_usage.py
More file actions
40 lines (33 loc) · 1.16 KB
/
stream_usage.py
File metadata and controls
40 lines (33 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from dataclasses import dataclass
from typing import Any
@dataclass
class StreamUsageTracker:
response_id: str | None = None
model: str | None = None
created: int | None = None
usage: dict[str, Any] | None = None
def ingest_chunk(self, chunk_data: dict[str, Any]) -> None:
if self.response_id is None:
response_id = chunk_data.get("id")
if isinstance(response_id, str):
self.response_id = response_id
if self.model is None:
model = chunk_data.get("model")
if isinstance(model, str):
self.model = model
if self.created is None:
created = chunk_data.get("created")
if isinstance(created, int):
self.created = created
usage = chunk_data.get("usage")
if isinstance(usage, dict) and usage:
self.usage = usage
def build_logging_payload(self) -> dict[str, Any]:
return {
"id": self.response_id,
"object": "chat.completion",
"created": self.created,
"model": self.model,
"choices": [],
"usage": self.usage,
}