-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathresponses.lua
More file actions
269 lines (269 loc) · 7.68 KB
/
responses.lua
File metadata and controls
269 lines (269 loc) · 7.68 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local cjson = require("cjson")
local types
types = require("tableshape").types
local response_mt = {
__index = {
get_output_text = function(self)
local parts = { }
if self.output then
local _list_0 = self.output
for _index_0 = 1, #_list_0 do
local block = _list_0[_index_0]
if block.content then
local _list_1 = block.content
for _index_1 = 1, #_list_1 do
local item = _list_1[_index_1]
if item.type == "output_text" and item.text then
table.insert(parts, item.text)
end
end
end
end
elseif self.content then
local _list_0 = self.content
for _index_0 = 1, #_list_0 do
local item = _list_0[_index_0]
if item.type == "output_text" and item.text then
table.insert(parts, item.text)
end
end
end
return table.concat(parts)
end,
get_images = function(self)
local images = { }
if self.output then
local _list_0 = self.output
for _index_0 = 1, #_list_0 do
local block = _list_0[_index_0]
if block.type == "image_generation_call" and block.result then
table.insert(images, {
b64_json = block.result
})
end
end
end
return images
end
},
__tostring = function(self)
return self:get_output_text()
end
}
local response_chunk_mt = {
__tostring = function(self)
return self.content or ""
end
}
local add_response_helpers
add_response_helpers = function(response)
if response then
setmetatable(response, response_mt)
end
return response
end
local empty = (types["nil"] + types.literal(cjson.null)):describe("nullable")
local input_content_item = types.one_of({
types.partial({
type = "input_text",
text = types.string
}),
types.partial({
type = "input_image",
image_url = types.string
}),
types.partial({
type = "input_file",
file_id = types.string
}),
types.partial({
type = "input_file",
file_url = types.string
}),
types.partial({
type = "input_file",
file_data = types.string,
filename = types.string
})
})
local function_call_output_item = types.partial({
type = types.literal("function_call_output"),
call_id = types.string,
output = types.string
})
local input_message = types.partial({
role = types.one_of({
"system",
"user",
"assistant"
}),
content = types.string + types.array_of(input_content_item)
})
local input_format = types.string + types.array_of(input_message + function_call_output_item)
local content_item = types.one_of({
types.partial({
type = "output_text",
text = types.string,
annotations = empty + types.array_of(types.table),
logprobs = empty + types.table
}),
types.partial({
type = "input_text",
text = types.string,
annotations = empty + types.array_of(types.table),
logprobs = empty + types.table
}),
types.partial({
type = "tool_use",
id = types.string,
name = types.string,
input = types.table
})
})
local response_message = types.partial({
id = empty + types.string,
type = types.literal("message"),
role = types.literal("assistant") + types.string,
content = types.array_of(content_item),
status = empty + types.string
})
local function_call_item = types.partial({
id = empty + types.string,
type = types.literal("function_call"),
name = types.string,
arguments = types.string,
call_id = types.string,
status = empty + types.string
})
local output_item = response_message + function_call_item
local parse_responses_response = types.partial({
id = types.string:tag("id"),
object = empty + types.literal("response"):tag("object"),
output = types.array_of(output_item):tag("output"),
model = empty + types.string:tag("model"),
usage = empty + types.table:tag("usage"),
status = empty + types.string:tag("status")
})
local ResponsesChatSession
do
local _class_0
local _base_0 = {
send = function(self, input, opts)
if opts == nil then
opts = { }
end
if type(opts) == "function" then
opts = {
stream_callback = opts
}
end
local stream_callback = opts.stream_callback
local request_opts = {
previous_response_id = self.current_response_id,
stream = stream_callback and true or nil
}
for k, v in pairs(opts) do
if k ~= "stream_callback" then
request_opts[k] = v
end
end
return self:create_response(input, request_opts, stream_callback)
end,
create_response = function(self, input, opts, stream_callback)
if opts == nil then
opts = { }
end
if stream_callback == nil then
stream_callback = nil
end
assert(input, "input must be provided")
assert(input_format(input))
local merged_opts = {
model = self.opts.model,
previous_response_id = self.current_response_id
}
if self.opts.instructions then
merged_opts.instructions = self.opts.instructions
end
if self.opts.tools then
merged_opts.tools = self.opts.tools
end
if opts then
for k, v in pairs(opts) do
merged_opts[k] = v
end
end
if stream_callback then
merged_opts.stream = merged_opts.stream or true
end
local accumulated_text = { }
local final_response = nil
local wrapped_callback
if merged_opts.stream then
wrapped_callback = function(chunk)
local _exp_0 = chunk.type
if "response.output_text.delta" == _exp_0 then
table.insert(accumulated_text, chunk.delta)
if stream_callback then
return stream_callback(setmetatable({
content = chunk.delta
}, response_chunk_mt), chunk)
end
elseif "response.completed" == _exp_0 then
if type(chunk.response) == "table" then
local parsed = parse_responses_response(chunk.response)
if parsed then
add_response_helpers(parsed)
final_response = parsed
end
end
end
end
end
local status, response = self.client:create_response(input, merged_opts, wrapped_callback)
if status ~= 200 then
return nil, "Request failed with status: " .. tostring(status), response
end
if merged_opts.stream then
if final_response then
self.current_response_id = final_response.id
table.insert(self.response_history, final_response)
end
return table.concat(accumulated_text)
end
local parsed_response, err = parse_responses_response(response)
if not (parsed_response) then
return nil, "Failed to parse response: " .. tostring(err), response
end
add_response_helpers(parsed_response)
self.current_response_id = parsed_response.id
table.insert(self.response_history, parsed_response)
return parsed_response
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, client, opts)
if opts == nil then
opts = { }
end
self.client, self.opts = client, opts
self.response_history = { }
self.current_response_id = self.opts.previous_response_id
end,
__base = _base_0,
__name = "ResponsesChatSession"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
ResponsesChatSession = _class_0
end
return {
ResponsesChatSession = ResponsesChatSession
}