Skip to content

Commit 183c6b8

Browse files
committed
initial attempt to capture perf experiment stats
1 parent 752b279 commit 183c6b8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

backend/tenantfirstaid/chat.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from flask import request, stream_with_context, Response, session, after_this_request
77
from flask.views import View
88
import os
9+
from typing import List
910

1011
from .shared import DEFAULT_INSTRUCTIONS, DATA_DIR
1112

@@ -87,9 +88,13 @@ def generate():
8788
tools=self.openai_tools,
8889
)
8990

91+
chunk_times: List[datetime.datetime] = [
92+
datetime.datetime.now(datetime.timezone.utc)
93+
]
9094
assistant_chunks = []
9195
for chunk in response_stream:
9296
if hasattr(chunk, "delta"):
97+
chunk_times.append(datetime.datetime.now(datetime.timezone.utc))
9398
token = chunk.delta or ""
9499
assistant_chunks.append(token)
95100
yield token
@@ -99,7 +104,9 @@ def generate():
99104
# print("assistant_msg", assistant_msg)
100105

101106
# Add this as a training example
102-
self._append_training_example(session_id, user_msg, assistant_msg)
107+
self._append_training_example(
108+
session_id, user_msg, assistant_msg, chunk_times
109+
)
103110
current_session.append({"role": "assistant", "content": assistant_msg})
104111

105112
except Exception as e:
@@ -116,11 +123,14 @@ def generate():
116123
mimetype="text/plain",
117124
)
118125

119-
def _append_training_example(self, session_id, user_msg, assistant_msg):
126+
def _append_training_example(
127+
self, session_id, user_msg, assistant_msg, chunk_times: List[datetime.datetime]
128+
):
120129
# Ensure the parent directory exists
121130
self.DATA_FILE.parent.mkdir(exist_ok=True)
122131

123132
with jsonlines.open(self.DATA_FILE, mode="a") as f:
133+
now: datetime.datetime = datetime.datetime.now(datetime.timezone.utc)
124134
f.write(
125135
{
126136
"messages": [
@@ -129,7 +139,20 @@ def _append_training_example(self, session_id, user_msg, assistant_msg):
129139
],
130140
"metadata": {
131141
"session_id": session_id,
132-
"ts": datetime.datetime.utcnow().isoformat(),
142+
"model": MODEL,
143+
"model_reasoning_effort": MODEL_REASONING_EFFORT,
144+
"chunk_times": [
145+
t.isoformat(timespec="milliseconds") for t in chunk_times
146+
],
147+
"ts": now.isoformat(timespec="milliseconds"),
133148
},
134149
}
135150
)
151+
152+
# print interesting stats in the console
153+
print(
154+
f"{(len(chunk_times) - 1):1} chunks\n",
155+
f"{(chunk_times[1] - chunk_times[0]).total_seconds():6.3f} first chunk time (seconds)\n",
156+
f"{((now - chunk_times[1]).total_seconds()) / len(chunk_times[1:]):6.3f} average chunk time after first chunk (seconds)\n",
157+
f"{(now - chunk_times[0]).total_seconds():6.3f} total seconds\n",
158+
)

0 commit comments

Comments
 (0)