Skip to content

Commit 9e34661

Browse files
authored
fix(ai-proxy-multi): panic when instance dont have custom endpoint (#12584)
1 parent 38ea6f8 commit 9e34661

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

apisix/plugins/ai-proxy-multi.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,24 @@ end
170170

171171

172172
local function resolve_endpoint(instance_conf)
173+
local scheme, host, port
173174
local endpoint = core.table.try_read_attr(instance_conf, "override", "endpoint")
174-
local scheme, host, port, _ = endpoint:match(endpoint_regex)
175-
if port == "" then
176-
port = (scheme == "https") and "443" or "80"
175+
if endpoint then
176+
scheme, host, port = endpoint:match(endpoint_regex)
177+
if port == "" then
178+
port = (scheme == "https") and "443" or "80"
179+
end
180+
port = tonumber(port)
181+
else
182+
local ai_driver = require("apisix.plugins.ai-drivers." .. instance_conf.provider)
183+
-- built-in ai driver always use https
184+
scheme = "https"
185+
host = ai_driver.host
186+
port = ai_driver.port
177187
end
178188
local node = {
179189
host = host,
180-
port = tonumber(port),
190+
port = port,
181191
scheme = scheme,
182192
}
183193
parse_domain_for_node(node)
@@ -216,7 +226,7 @@ local function fetch_health_instances(conf, checkers)
216226
if ok then
217227
transform_instances(new_instances, ins)
218228
elseif err then
219-
core.log.error("failed to get health check target status, addr: ",
229+
core.log.warn("failed to get health check target status, addr: ",
220230
node.host, ":", port or node.port, ", host: ", host, ", err: ", err)
221231
end
222232
else

t/plugin/ai-proxy-multi3.t

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,3 +835,77 @@ passed
835835
--- timeout: 20
836836
--- response_body
837837
passed
838+
839+
840+
841+
=== TEST 11: configure health check for well-known ai service
842+
--- config
843+
location /t {
844+
content_by_lua_block {
845+
local t = require("lib.test_admin").test
846+
local code, body = t('/apisix/admin/routes/1',
847+
ngx.HTTP_PUT,
848+
[[{
849+
"uri": "/ai",
850+
"plugins": {
851+
"ai-proxy-multi": {
852+
"instances": [
853+
{
854+
"name": "openai-gpt4",
855+
"provider": "openai",
856+
"weight": 1,
857+
"priority": 1,
858+
"auth": {
859+
"header": {
860+
"Authorization": "Bearer token"
861+
}
862+
},
863+
"options": {
864+
"model": "gpt-4"
865+
},
866+
"checks": {
867+
"active": {
868+
"timeout": 5,
869+
"http_path": "/",
870+
"healthy": {
871+
"interval": 1,
872+
"successes": 1
873+
},
874+
"unhealthy": {
875+
"interval": 1,
876+
"http_failures": 1
877+
},
878+
"req_headers": ["User-Agent: curl/7.29.0"]
879+
}
880+
}
881+
},
882+
{"name":"openai-gpt3","provider":"openai","weight":1,"priority":1,"auth":{"header":{"Authorization":"Bearer token"}},"options":{"model":"gpt-3"}}
883+
],
884+
"ssl_verify": false
885+
}
886+
}
887+
}]]
888+
)
889+
if code >= 300 then
890+
ngx.status = code
891+
end
892+
ngx.say(body)
893+
}
894+
}
895+
--- response_body
896+
passed
897+
898+
899+
900+
=== TEST 12: send request to /ai should failed with 401
901+
--- request
902+
POST /ai
903+
{
904+
"messages": [
905+
{
906+
"role": "user",
907+
"content": "write a haiku about ai"
908+
}
909+
]
910+
}
911+
--- error_code: 401

0 commit comments

Comments
 (0)