Skip to content

Commit cc7441f

Browse files
feat(plugin): support ai-proxy-multi (#11986)
1 parent 8e900ac commit cc7441f

15 files changed

+2176
-24
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ install: runtime
374374
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/ai-proxy
375375
$(ENV_INSTALL) apisix/plugins/ai-proxy/*.lua $(ENV_INST_LUADIR)/apisix/plugins/ai-proxy
376376

377-
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/ai-proxy/drivers
378-
$(ENV_INSTALL) apisix/plugins/ai-proxy/drivers/*.lua $(ENV_INST_LUADIR)/apisix/plugins/ai-proxy/drivers
377+
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/ai-drivers
378+
$(ENV_INSTALL) apisix/plugins/ai-drivers/*.lua $(ENV_INST_LUADIR)/apisix/plugins/ai-drivers
379379

380380
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/ai-rag/embeddings
381381
$(ENV_INSTALL) apisix/plugins/ai-rag/embeddings/*.lua $(ENV_INST_LUADIR)/apisix/plugins/ai-rag/embeddings

apisix/cli/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ local _M = {
223223
"workflow",
224224
"api-breaker",
225225
"ai-proxy",
226+
"ai-proxy-multi",
226227
"limit-conn",
227228
"limit-count",
228229
"limit-req",
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
18+
return require("apisix.plugins.ai-drivers.openai-compatible").new(
19+
{
20+
host = "api.deepseek.com",
21+
path = "/chat/completions",
22+
port = 443
23+
}
24+
)

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

+26-14
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,57 @@
1616
--
1717
local _M = {}
1818

19+
local mt = {
20+
__index = _M
21+
}
22+
1923
local core = require("apisix.core")
2024
local http = require("resty.http")
2125
local url = require("socket.url")
2226

2327
local pairs = pairs
2428
local type = type
29+
local setmetatable = setmetatable
30+
31+
32+
function _M.new(opts)
2533

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
3041

3142

32-
function _M.request(conf, request_table, ctx)
43+
function _M.request(self, conf, request_table, extra_opts)
3344
local httpc, err = http.new()
3445
if not httpc then
3546
return nil, "failed to create http client to send request to LLM server: " .. err
3647
end
3748
httpc:set_timeout(conf.timeout)
3849

39-
local endpoint = core.table.try_read_attr(conf, "override", "endpoint")
50+
local endpoint = extra_opts.endpoint
4051
local parsed_url
4152
if endpoint then
4253
parsed_url = url.parse(endpoint)
4354
end
4455

4556
local ok, err = httpc:connect({
4657
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,
4960
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,
5162
pool_size = conf.keepalive and conf.keepalive_pool,
5263
})
5364

5465
if not ok then
5566
return nil, "failed to connect to LLM server: " .. err
5667
end
5768

58-
local query_params = conf.auth.query or {}
69+
local query_params = extra_opts.query_params
5970

6071
if type(parsed_url) == "table" and parsed_url.query and #parsed_url.query > 0 then
6172
local args_tab = core.string.decode_args(parsed_url.query)
@@ -64,9 +75,9 @@ function _M.request(conf, request_table, ctx)
6475
end
6576
end
6677

67-
local path = (endpoint and parsed_url.path or DEFAULT_PATH)
78+
local path = (endpoint and parsed_url.path or self.path)
6879

69-
local headers = (conf.auth.header or {})
80+
local headers = extra_opts.headers
7081
headers["Content-Type"] = "application/json"
7182
local params = {
7283
method = "POST",
@@ -77,13 +88,14 @@ function _M.request(conf, request_table, ctx)
7788
query = query_params
7889
}
7990

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
8293
request_table[opt] = val
8394
end
8495
end
8596
params.body = core.json.encode(request_table)
8697

98+
httpc:set_timeout(conf.keepalive_timeout)
8799
local res, err = httpc:request(params)
88100
if not res then
89101
return nil, err

apisix/plugins/ai-drivers/openai.lua

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
18+
return require("apisix.plugins.ai-drivers.openai-compatible").new(
19+
{
20+
host = "api.openai.com",
21+
path = "/v1/chat/completions",
22+
port = 443
23+
}
24+
)

0 commit comments

Comments
 (0)