6
6
from flask import request , stream_with_context , Response , session , after_this_request
7
7
from flask .views import View
8
8
import os
9
+ from typing import List
9
10
10
11
from .shared import DEFAULT_INSTRUCTIONS , DATA_DIR
11
12
@@ -87,9 +88,13 @@ def generate():
87
88
tools = self .openai_tools ,
88
89
)
89
90
91
+ chunk_times : List [datetime .datetime ] = [
92
+ datetime .datetime .now (datetime .timezone .utc )
93
+ ]
90
94
assistant_chunks = []
91
95
for chunk in response_stream :
92
96
if hasattr (chunk , "delta" ):
97
+ chunk_times .append (datetime .datetime .now (datetime .timezone .utc ))
93
98
token = chunk .delta or ""
94
99
assistant_chunks .append (token )
95
100
yield token
@@ -99,7 +104,9 @@ def generate():
99
104
# print("assistant_msg", assistant_msg)
100
105
101
106
# 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
+ )
103
110
current_session .append ({"role" : "assistant" , "content" : assistant_msg })
104
111
105
112
except Exception as e :
@@ -116,11 +123,14 @@ def generate():
116
123
mimetype = "text/plain" ,
117
124
)
118
125
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
+ ):
120
129
# Ensure the parent directory exists
121
130
self .DATA_FILE .parent .mkdir (exist_ok = True )
122
131
123
132
with jsonlines .open (self .DATA_FILE , mode = "a" ) as f :
133
+ now : datetime .datetime = datetime .datetime .now (datetime .timezone .utc )
124
134
f .write (
125
135
{
126
136
"messages" : [
@@ -129,7 +139,20 @@ def _append_training_example(self, session_id, user_msg, assistant_msg):
129
139
],
130
140
"metadata" : {
131
141
"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" ),
133
148
},
134
149
}
135
150
)
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