Skip to content

Commit f3c81b5

Browse files
fix(ai-proxy): missing parsed_url nil check (#11637)
1 parent 1f89705 commit f3c81b5

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

apisix/plugins/ai-proxy/drivers/openai.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ function _M.request(conf, request_table, ctx)
4242
end
4343

4444
local ok, err = httpc:connect({
45-
scheme = parsed_url.scheme or "https",
46-
host = parsed_url.host or DEFAULT_HOST,
47-
port = parsed_url.port or DEFAULT_PORT,
45+
scheme = endpoint and parsed_url.scheme or "https",
46+
host = endpoint and parsed_url.host or DEFAULT_HOST,
47+
port = endpoint and parsed_url.port or DEFAULT_PORT,
4848
ssl_verify = conf.ssl_verify,
49-
ssl_server_name = parsed_url.host or DEFAULT_HOST,
49+
ssl_server_name = endpoint and parsed_url.host or DEFAULT_HOST,
5050
pool_size = conf.keepalive and conf.keepalive_pool,
5151
})
5252

5353
if not ok then
5454
return nil, "failed to connect to LLM server: " .. err
5555
end
5656

57-
local path = (parsed_url.path or DEFAULT_PATH)
57+
local path = (endpoint and parsed_url.path or DEFAULT_PATH)
5858

5959
local headers = (conf.auth.header or {})
6060
headers["Content-Type"] = "application/json"

t/APISIX.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ _EOC_
465465
$block->set_value("stream_config", $stream_config);
466466
}
467467

468+
my $custom_trusted_cert = $block->custom_trusted_cert // 'cert/apisix.crt';
469+
468470
my $stream_server_config = $block->stream_server_config // <<_EOC_;
469471
listen 2005 ssl;
470472
ssl_certificate cert/apisix.crt;
@@ -737,7 +739,7 @@ _EOC_
737739
http3 off;
738740
ssl_certificate cert/apisix.crt;
739741
ssl_certificate_key cert/apisix.key;
740-
lua_ssl_trusted_certificate cert/apisix.crt;
742+
lua_ssl_trusted_certificate $custom_trusted_cert;
741743
742744
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
743745

t/plugin/ai-proxy2.t

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,58 @@ POST /anything
198198
--- error_code: 200
199199
--- response_body
200200
passed
201+
202+
203+
204+
=== TEST 5: set route without overriding the endpoint_url
205+
--- config
206+
location /t {
207+
content_by_lua_block {
208+
local t = require("lib.test_admin").test
209+
local code, body = t('/apisix/admin/routes/1',
210+
ngx.HTTP_PUT,
211+
[[{
212+
"uri": "/anything",
213+
"plugins": {
214+
"ai-proxy": {
215+
"auth": {
216+
"header": {
217+
"Authorization": "some-key"
218+
}
219+
},
220+
"model": {
221+
"provider": "openai",
222+
"name": "gpt-4",
223+
"options": {
224+
"max_tokens": 512,
225+
"temperature": 1.0
226+
}
227+
}
228+
}
229+
},
230+
"upstream": {
231+
"type": "roundrobin",
232+
"nodes": {
233+
"httpbin.org": 1
234+
}
235+
}
236+
}]]
237+
)
238+
239+
if code >= 300 then
240+
ngx.status = code
241+
end
242+
ngx.say(body)
243+
}
244+
}
245+
--- response_body
246+
passed
247+
248+
249+
250+
=== TEST 6: send request
251+
--- custom_trusted_cert: /etc/ssl/certs/ca-certificates.crt
252+
--- request
253+
POST /anything
254+
{ "messages": [ { "role": "system", "content": "You are a mathematician" }, { "role": "user", "content": "What is 1+1?"} ] }
255+
--- error_code: 401

0 commit comments

Comments
 (0)