|
1 | 1 | local cjson = require("cjson") |
2 | 2 | local types |
3 | 3 | types = require("tableshape").types |
| 4 | +local DEFAULT_RESPONSES_MODEL = "gpt-4.1-mini" |
4 | 5 | local empty = (types["nil"] + types.literal(cjson.null)):describe("nullable") |
5 | 6 | local input_format = types.string + types.array_of(types.partial({ |
6 | 7 | role = types.one_of({ |
@@ -43,8 +44,7 @@ local parse_responses_response = types.partial({ |
43 | 44 | output = types.array_of(response_message):tag("output"), |
44 | 45 | model = empty + types.string:tag("model"), |
45 | 46 | usage = empty + types.table:tag("usage"), |
46 | | - stop_reason = empty + types.string:tag("stop_reason"), |
47 | | - stop_sequence = empty + (types.string + empty):tag("stop_sequence") |
| 47 | + status = empty + types.string:tag("status") |
48 | 48 | }) |
49 | 49 | local parse_response_stream_chunk |
50 | 50 | parse_response_stream_chunk = function(chunk) |
@@ -160,7 +160,7 @@ create_response_stream_filter = function(chunk_callback) |
160 | 160 | return ... |
161 | 161 | end |
162 | 162 | end |
163 | | -local ResponseSession |
| 163 | +local ResponsesChatSession |
164 | 164 | do |
165 | 165 | local _class_0 |
166 | 166 | local _base_0 = { |
|
182 | 182 | end |
183 | 183 | assert(input, "input must be provided") |
184 | 184 | assert(input_format(input)) |
185 | | - local payload = { |
186 | | - model = self.opts.model or "gpt-4.1-mini", |
187 | | - input = input |
| 185 | + local merged_opts = { |
| 186 | + model = self.opts.model, |
| 187 | + previous_response_id = self.current_response_id |
188 | 188 | } |
189 | 189 | if self.opts.instructions then |
190 | | - payload.instructions = self.opts.instructions |
| 190 | + merged_opts.instructions = self.opts.instructions |
191 | 191 | end |
192 | 192 | if opts then |
193 | 193 | for k, v in pairs(opts) do |
194 | | - payload[k] = v |
| 194 | + merged_opts[k] = v |
195 | 195 | end |
196 | 196 | end |
| 197 | + if stream_callback then |
| 198 | + merged_opts.stream = merged_opts.stream or true |
| 199 | + end |
197 | 200 | local accumulated_text = { } |
198 | 201 | local final_response = nil |
199 | | - local stream_filter |
200 | | - if payload.stream then |
201 | | - stream_filter = create_response_stream_filter(function(chunk) |
| 202 | + local wrapped_callback |
| 203 | + if merged_opts.stream then |
| 204 | + wrapped_callback = function(chunk) |
202 | 205 | if chunk.text_delta then |
203 | 206 | table.insert(accumulated_text, chunk.text_delta) |
204 | 207 | end |
|
209 | 212 | if stream_callback then |
210 | 213 | return stream_callback(chunk) |
211 | 214 | end |
212 | | - end) |
| 215 | + end |
213 | 216 | end |
214 | | - local status, response = self.client:_request("POST", "/responses", payload, nil, stream_filter) |
| 217 | + local status, response = self.client:create_response(input, merged_opts, wrapped_callback) |
215 | 218 | if status ~= 200 then |
216 | 219 | return nil, "Request failed with status: " .. tostring(status), response |
217 | 220 | end |
218 | | - if payload.stream then |
219 | | - local text_out = table.concat(accumulated_text) |
| 221 | + if merged_opts.stream then |
220 | 222 | if final_response then |
221 | 223 | self.current_response_id = final_response.id |
222 | 224 | table.insert(self.response_history, final_response) |
223 | | - elseif text_out ~= "" then |
224 | | - self.current_response_id = "stream_" .. tostring(os.time()) |
225 | 225 | end |
226 | | - return text_out |
| 226 | + return table.concat(accumulated_text) |
227 | 227 | end |
228 | 228 | local parsed_response, err = parse_responses_response(response) |
229 | 229 | if not (parsed_response) then |
|
246 | 246 | self.current_response_id = self.opts.previous_response_id |
247 | 247 | end, |
248 | 248 | __base = _base_0, |
249 | | - __name = "ResponseSession" |
| 249 | + __name = "ResponsesChatSession" |
250 | 250 | }, { |
251 | 251 | __index = _base_0, |
252 | 252 | __call = function(cls, ...) |
|
256 | 256 | end |
257 | 257 | }) |
258 | 258 | _base_0.__class = _class_0 |
259 | | - ResponseSession = _class_0 |
| 259 | + ResponsesChatSession = _class_0 |
260 | 260 | end |
261 | | -local responses_methods = { |
262 | | - new_response_session = function(self, ...) |
263 | | - return ResponseSession(self, ...) |
264 | | - end, |
265 | | - create_response = function(self, input, opts, stream_callback) |
266 | | - if opts == nil then |
267 | | - opts = { } |
268 | | - end |
269 | | - if stream_callback == nil then |
270 | | - stream_callback = nil |
271 | | - end |
272 | | - assert(input, "input must be provided") |
273 | | - assert(input_format(input)) |
274 | | - local payload = { |
275 | | - model = "gpt-4.1-mini", |
276 | | - input = input |
277 | | - } |
278 | | - if opts then |
279 | | - for k, v in pairs(opts) do |
280 | | - payload[k] = v |
281 | | - end |
282 | | - end |
283 | | - local accumulated_text = { } |
284 | | - local final_response = nil |
285 | | - local stream_filter |
286 | | - if payload.stream then |
287 | | - stream_filter = create_response_stream_filter(function(chunk) |
288 | | - if chunk.text_delta then |
289 | | - table.insert(accumulated_text, chunk.text_delta) |
290 | | - end |
291 | | - if chunk.response then |
292 | | - final_response = add_response_helpers(chunk.response) |
293 | | - chunk.response = final_response |
294 | | - end |
295 | | - if stream_callback then |
296 | | - return stream_callback(chunk) |
297 | | - end |
298 | | - end) |
299 | | - end |
300 | | - local status, response = self:_request("POST", "/responses", payload, nil, stream_filter) |
301 | | - if status ~= 200 then |
302 | | - return nil, "Request failed with status: " .. tostring(status), response |
303 | | - end |
304 | | - if payload.stream then |
305 | | - if final_response then |
306 | | - add_response_helpers(final_response) |
307 | | - end |
308 | | - return table.concat(accumulated_text) |
309 | | - end |
310 | | - local parsed_response, err = parse_responses_response(response) |
311 | | - if not (parsed_response) then |
312 | | - return nil, "Failed to parse response: " .. tostring(err), response |
313 | | - end |
314 | | - add_response_helpers(parsed_response) |
315 | | - return parsed_response |
316 | | - end |
317 | | -} |
318 | 261 | return { |
319 | | - ResponseSession = ResponseSession, |
320 | | - responses_methods = responses_methods, |
| 262 | + ResponsesChatSession = ResponsesChatSession, |
321 | 263 | parse_responses_response = parse_responses_response, |
322 | 264 | parse_response_stream_chunk = parse_response_stream_chunk, |
| 265 | + create_response_stream_filter = create_response_stream_filter, |
323 | 266 | add_response_helpers = add_response_helpers, |
324 | 267 | extract_output_text = extract_output_text |
325 | 268 | } |
0 commit comments