diff --git a/apisix/plugins/ai-proxy/drivers/openai.lua b/apisix/plugins/ai-proxy/drivers/openai.lua index c8f7f4b6223f..cefc5a728eaa 100644 --- a/apisix/plugins/ai-proxy/drivers/openai.lua +++ b/apisix/plugins/ai-proxy/drivers/openai.lua @@ -42,11 +42,11 @@ function _M.request(conf, request_table, ctx) end local ok, err = httpc:connect({ - scheme = parsed_url.scheme or "https", - host = parsed_url.host or DEFAULT_HOST, - port = parsed_url.port or DEFAULT_PORT, + scheme = endpoint and parsed_url.scheme or "https", + host = endpoint and parsed_url.host or DEFAULT_HOST, + port = endpoint and parsed_url.port or DEFAULT_PORT, ssl_verify = conf.ssl_verify, - ssl_server_name = parsed_url.host or DEFAULT_HOST, + ssl_server_name = endpoint and parsed_url.host or DEFAULT_HOST, pool_size = conf.keepalive and conf.keepalive_pool, }) @@ -54,7 +54,7 @@ function _M.request(conf, request_table, ctx) return nil, "failed to connect to LLM server: " .. err end - local path = (parsed_url.path or DEFAULT_PATH) + local path = (endpoint and parsed_url.path or DEFAULT_PATH) local headers = (conf.auth.header or {}) headers["Content-Type"] = "application/json" diff --git a/t/APISIX.pm b/t/APISIX.pm index 50f7cfaecab6..2e1724a12aa1 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -465,6 +465,8 @@ _EOC_ $block->set_value("stream_config", $stream_config); } + my $custom_trusted_cert = $block->custom_trusted_cert // 'cert/apisix.crt'; + my $stream_server_config = $block->stream_server_config // <<_EOC_; listen 2005 ssl; ssl_certificate cert/apisix.crt; @@ -737,7 +739,7 @@ _EOC_ http3 off; ssl_certificate cert/apisix.crt; ssl_certificate_key cert/apisix.key; - lua_ssl_trusted_certificate cert/apisix.crt; + lua_ssl_trusted_certificate $custom_trusted_cert; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; diff --git a/t/plugin/ai-proxy2.t b/t/plugin/ai-proxy2.t index 6e398e5665a4..cda3786b7e75 100644 --- a/t/plugin/ai-proxy2.t +++ b/t/plugin/ai-proxy2.t @@ -198,3 +198,58 @@ POST /anything --- error_code: 200 --- response_body passed + + + +=== TEST 5: set route without overriding the endpoint_url +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/anything", + "plugins": { + "ai-proxy": { + "auth": { + "header": { + "Authorization": "some-key" + } + }, + "model": { + "provider": "openai", + "name": "gpt-4", + "options": { + "max_tokens": 512, + "temperature": 1.0 + } + } + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "httpbin.org": 1 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 6: send request +--- custom_trusted_cert: /etc/ssl/certs/ca-certificates.crt +--- request +POST /anything +{ "messages": [ { "role": "system", "content": "You are a mathematician" }, { "role": "user", "content": "What is 1+1?"} ] } +--- error_code: 401