@@ -4,88 +4,32 @@ local cjson = require("cjson")
44local unpack = table.unpack or unpack
55local types
66types = require (" tableshape" ).types
7- local ChatSession , test_message
8- do
9- local _obj_0 = require (" openai.chat_session" )
10- ChatSession , test_message = _obj_0 .ChatSession , _obj_0 .test_message
11- end
127local parse_url = require (" socket.url" ).parse
13- local parse_completion_chunk = types .partial ({
14- object = " chat.completion.chunk" ,
15- choices = types .shape ({
16- types .partial ({
17- delta = types .partial ({
18- [" content" ] = types .string :tag (" content" )
19- }),
20- index = types .number :tag (" index" )
21- })
22- })
23- })
24- local consume_json_head
25- do
26- local C , S , P
27- do
28- local _obj_0 = require (" lpeg" )
29- C , S , P = _obj_0 .C , _obj_0 .S , _obj_0 .P
30- end
31- local consume_json = P (function (str , pos )
32- local str_len = # str
33- for k = pos + 1 , str_len do
34- local candidate = str :sub (pos , k )
35- local parsed = false
36- pcall (function ()
37- parsed = cjson .decode (candidate )
38- end )
39- if parsed then
40- return k + 1
41- end
42- end
43- return nil
44- end )
45- consume_json_head = S (" \t\n\r " ) ^ 0 * P (" data: " ) * C (consume_json ) * C (P (1 ) ^ 0 )
46- end
478local OpenAI
489do
4910 local _class_0
5011 local _base_0 = {
5112 api_base = " https://api.openai.com/v1" ,
5213 default_model = " gpt-4.1" ,
5314 new_chat_session = function (self , ...)
15+ local ChatSession
16+ ChatSession = require (" openai.chat_completions" ).ChatSession
5417 return ChatSession (self , ... )
5518 end ,
5619 new_response_chat_session = function (self , ...)
5720 local ResponsesChatSession
5821 ResponsesChatSession = require (" openai.responses" ).ResponsesChatSession
5922 return ResponsesChatSession (self , ... )
6023 end ,
61- create_stream_filter = function (self , chunk_callback )
62- assert (types [" function" ](chunk_callback ), " Must provide chunk_callback function when streaming response" )
63- local accumulation_buffer = " "
64- return function (...)
65- local chunk = ...
66- if type (chunk ) == " string" then
67- accumulation_buffer = accumulation_buffer .. chunk
68- while true do
69- local json_blob , rest = consume_json_head :match (accumulation_buffer )
70- if not (json_blob ) then
71- break
72- end
73- accumulation_buffer = rest
74- do
75- chunk = parse_completion_chunk (cjson .decode (json_blob ))
76- if chunk then
77- chunk_callback (chunk )
78- end
79- end
80- end
81- end
82- return ...
83- end
84- end ,
8524 chat = function (self , messages , opts , chunk_callback )
8625 if chunk_callback == nil then
8726 chunk_callback = nil
8827 end
28+ local test_message , create_chat_stream_filter
29+ do
30+ local _obj_0 = require (" openai.chat_completions" )
31+ test_message , create_chat_stream_filter = _obj_0 .test_message , _obj_0 .create_chat_stream_filter
32+ end
8933 local test_messages = types .array_of (test_message )
9034 assert (test_messages (messages ))
9135 local payload = {
9943 end
10044 local stream_filter
10145 if payload .stream then
102- stream_filter = self : create_stream_filter (chunk_callback )
46+ stream_filter = create_chat_stream_filter (chunk_callback )
10347 end
10448 return self :_request (" POST" , " /chat/completions" , payload , nil , stream_filter )
10549 end ,
294238end
295239return {
296240 OpenAI = OpenAI ,
297- ChatSession = ChatSession ,
298241 VERSION = VERSION ,
299242 new = OpenAI
300243}
0 commit comments