|
| 1 | +local cjson = require("cjson") |
| 2 | +local types |
| 3 | +types = require("tableshape").types |
| 4 | +local empty = (types["nil"] + types.literal(cjson.null)):describe("nullable") |
| 5 | +local content_format = types.string + types.array_of(types.one_of({ |
| 6 | + types.shape({ |
| 7 | + type = "text", |
| 8 | + text = types.string |
| 9 | + }), |
| 10 | + types.shape({ |
| 11 | + type = "image_url", |
| 12 | + image_url = types.string + types.partial({ |
| 13 | + url = types.string |
| 14 | + }) |
| 15 | + }) |
| 16 | +})) |
| 17 | +local test_message = types.one_of({ |
| 18 | + types.partial({ |
| 19 | + role = types.one_of({ |
| 20 | + "system", |
| 21 | + "user", |
| 22 | + "assistant" |
| 23 | + }), |
| 24 | + content = empty + content_format, |
| 25 | + name = empty + types.string, |
| 26 | + function_call = empty + types.table |
| 27 | + }), |
| 28 | + types.partial({ |
| 29 | + role = types.one_of({ |
| 30 | + "function" |
| 31 | + }), |
| 32 | + name = types.string, |
| 33 | + content = empty + types.string |
| 34 | + }) |
| 35 | +}) |
| 36 | +local test_function = types.shape({ |
| 37 | + name = types.string, |
| 38 | + description = types["nil"] + types.string, |
| 39 | + parameters = types["nil"] + types.table |
| 40 | +}) |
| 41 | +local parse_chat_response = types.partial({ |
| 42 | + usage = types.table:tag("usage"), |
| 43 | + choices = types.partial({ |
| 44 | + types.partial({ |
| 45 | + message = types.one_of({ |
| 46 | + types.partial({ |
| 47 | + role = "assistant", |
| 48 | + content = types.string + empty, |
| 49 | + function_call = types.partial({ |
| 50 | + name = types.string, |
| 51 | + arguments = types.string |
| 52 | + }) |
| 53 | + }), |
| 54 | + types.partial({ |
| 55 | + role = "assistant", |
| 56 | + content = types.string:tag("response") |
| 57 | + }) |
| 58 | + }):tag("message") |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
| 62 | +local parse_error_message = types.partial({ |
| 63 | + error = types.partial({ |
| 64 | + message = types.string:tag("message"), |
| 65 | + code = empty + types.string:tag("code") |
| 66 | + }) |
| 67 | +}) |
| 68 | +local ChatSession |
| 69 | +do |
| 70 | + local _class_0 |
| 71 | + local _base_0 = { |
| 72 | + append_message = function(self, m, ...) |
| 73 | + assert(test_message(m)) |
| 74 | + table.insert(self.messages, m) |
| 75 | + if select("#", ...) > 0 then |
| 76 | + return self:append_message(...) |
| 77 | + end |
| 78 | + end, |
| 79 | + last_message = function(self) |
| 80 | + return self.messages[#self.messages] |
| 81 | + end, |
| 82 | + send = function(self, message, stream_callback) |
| 83 | + if stream_callback == nil then |
| 84 | + stream_callback = nil |
| 85 | + end |
| 86 | + if type(message) == "string" then |
| 87 | + message = { |
| 88 | + role = "user", |
| 89 | + content = message |
| 90 | + } |
| 91 | + end |
| 92 | + self:append_message(message) |
| 93 | + return self:generate_response(true, stream_callback) |
| 94 | + end, |
| 95 | + generate_response = function(self, append_response, stream_callback) |
| 96 | + if append_response == nil then |
| 97 | + append_response = true |
| 98 | + end |
| 99 | + if stream_callback == nil then |
| 100 | + stream_callback = nil |
| 101 | + end |
| 102 | + local status, response = self.client:chat(self.messages, { |
| 103 | + function_call = self.opts.function_call, |
| 104 | + functions = self.functions, |
| 105 | + model = self.opts.model, |
| 106 | + temperature = self.opts.temperature, |
| 107 | + stream = stream_callback and true or nil, |
| 108 | + response_format = self.opts.response_format |
| 109 | + }, stream_callback) |
| 110 | + if status ~= 200 then |
| 111 | + local err_msg = "Bad status: " .. tostring(status) |
| 112 | + do |
| 113 | + local err = parse_error_message(response) |
| 114 | + if err then |
| 115 | + if err.message then |
| 116 | + err_msg = err_msg .. ": " .. tostring(err.message) |
| 117 | + end |
| 118 | + if err.code then |
| 119 | + err_msg = err_msg .. " (" .. tostring(err.code) .. ")" |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + return nil, err_msg, response |
| 124 | + end |
| 125 | + if stream_callback then |
| 126 | + assert(type(response) == "string", "Expected string response from streaming output") |
| 127 | + local parts = { } |
| 128 | + local f = self.client:create_stream_filter(function(c) |
| 129 | + return table.insert(parts, c.content) |
| 130 | + end) |
| 131 | + f(response) |
| 132 | + local message = { |
| 133 | + role = "assistant", |
| 134 | + content = table.concat(parts) |
| 135 | + } |
| 136 | + if append_response then |
| 137 | + self:append_message(message) |
| 138 | + end |
| 139 | + return message.content |
| 140 | + end |
| 141 | + local out, err = parse_chat_response(response) |
| 142 | + if not (out) then |
| 143 | + err = "Failed to parse response from server: " .. tostring(err) |
| 144 | + return nil, err, response |
| 145 | + end |
| 146 | + if append_response then |
| 147 | + self:append_message(out.message) |
| 148 | + end |
| 149 | + return out.response or out.message |
| 150 | + end |
| 151 | + } |
| 152 | + _base_0.__index = _base_0 |
| 153 | + _class_0 = setmetatable({ |
| 154 | + __init = function(self, client, opts) |
| 155 | + if opts == nil then |
| 156 | + opts = { } |
| 157 | + end |
| 158 | + self.client, self.opts = client, opts |
| 159 | + self.messages = { } |
| 160 | + if type(self.opts.messages) == "table" then |
| 161 | + self:append_message(unpack(self.opts.messages)) |
| 162 | + end |
| 163 | + if type(self.opts.functions) == "table" then |
| 164 | + self.functions = { } |
| 165 | + local _list_0 = self.opts.functions |
| 166 | + for _index_0 = 1, #_list_0 do |
| 167 | + local func = _list_0[_index_0] |
| 168 | + assert(test_function(func)) |
| 169 | + table.insert(self.functions, func) |
| 170 | + end |
| 171 | + end |
| 172 | + end, |
| 173 | + __base = _base_0, |
| 174 | + __name = "ChatSession" |
| 175 | + }, { |
| 176 | + __index = _base_0, |
| 177 | + __call = function(cls, ...) |
| 178 | + local _self_0 = setmetatable({}, _base_0) |
| 179 | + cls.__init(_self_0, ...) |
| 180 | + return _self_0 |
| 181 | + end |
| 182 | + }) |
| 183 | + _base_0.__class = _class_0 |
| 184 | + ChatSession = _class_0 |
| 185 | +end |
| 186 | +return { |
| 187 | + ChatSession = ChatSession, |
| 188 | + test_message = test_message |
| 189 | +} |
0 commit comments