16
16
--
17
17
local _M = {}
18
18
19
+ local mt = {
20
+ __index = _M
21
+ }
22
+
19
23
local core = require (" apisix.core" )
20
24
local http = require (" resty.http" )
21
25
local url = require (" socket.url" )
22
26
23
27
local pairs = pairs
24
28
local type = type
29
+ local setmetatable = setmetatable
30
+
31
+
32
+ function _M .new (opts )
25
33
26
- -- globals
27
- local DEFAULT_HOST = " api.openai.com"
28
- local DEFAULT_PORT = 443
29
- local DEFAULT_PATH = " /v1/chat/completions"
34
+ local self = {
35
+ host = opts .host ,
36
+ port = opts .port ,
37
+ path = opts .path ,
38
+ }
39
+ return setmetatable (self , mt )
40
+ end
30
41
31
42
32
- function _M .request (conf , request_table , ctx )
43
+ function _M .request (self , conf , request_table , extra_opts )
33
44
local httpc , err = http .new ()
34
45
if not httpc then
35
46
return nil , " failed to create http client to send request to LLM server: " .. err
36
47
end
37
48
httpc :set_timeout (conf .timeout )
38
49
39
- local endpoint = core . table . try_read_attr ( conf , " override " , " endpoint" )
50
+ local endpoint = extra_opts . endpoint
40
51
local parsed_url
41
52
if endpoint then
42
53
parsed_url = url .parse (endpoint )
43
54
end
44
55
45
56
local ok , err = httpc :connect ({
46
57
scheme = endpoint and parsed_url .scheme or " https" ,
47
- host = endpoint and parsed_url .host or DEFAULT_HOST ,
48
- port = endpoint and parsed_url .port or DEFAULT_PORT ,
58
+ host = endpoint and parsed_url .host or self . host ,
59
+ port = endpoint and parsed_url .port or self . port ,
49
60
ssl_verify = conf .ssl_verify ,
50
- ssl_server_name = endpoint and parsed_url .host or DEFAULT_HOST ,
61
+ ssl_server_name = endpoint and parsed_url .host or self . host ,
51
62
pool_size = conf .keepalive and conf .keepalive_pool ,
52
63
})
53
64
54
65
if not ok then
55
66
return nil , " failed to connect to LLM server: " .. err
56
67
end
57
68
58
- local query_params = conf . auth . query or {}
69
+ local query_params = extra_opts . query_params
59
70
60
71
if type (parsed_url ) == " table" and parsed_url .query and # parsed_url .query > 0 then
61
72
local args_tab = core .string .decode_args (parsed_url .query )
@@ -64,9 +75,9 @@ function _M.request(conf, request_table, ctx)
64
75
end
65
76
end
66
77
67
- local path = (endpoint and parsed_url .path or DEFAULT_PATH )
78
+ local path = (endpoint and parsed_url .path or self . path )
68
79
69
- local headers = ( conf . auth . header or {})
80
+ local headers = extra_opts . headers
70
81
headers [" Content-Type" ] = " application/json"
71
82
local params = {
72
83
method = " POST" ,
@@ -77,13 +88,14 @@ function _M.request(conf, request_table, ctx)
77
88
query = query_params
78
89
}
79
90
80
- if conf . model . options then
81
- for opt , val in pairs (conf . model . options ) do
91
+ if extra_opts . model_options then
92
+ for opt , val in pairs (extra_opts . model_options ) do
82
93
request_table [opt ] = val
83
94
end
84
95
end
85
96
params .body = core .json .encode (request_table )
86
97
98
+ httpc :set_timeout (conf .keepalive_timeout )
87
99
local res , err = httpc :request (params )
88
100
if not res then
89
101
return nil , err
0 commit comments