Baoyuantop commented on code in PR #12857:
URL: https://github.com/apache/apisix/pull/12857#discussion_r2666863948
##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table,
extra_opts)
end
end
- local path = (parsed_url and parsed_url.path or self.path)
+ local path_mode = extra_opts.path_mode or "fixed"
+ local endpoint_path = parsed_url and parsed_url.path
+ local req_path = ctx.var.uri
+ local path
+
+ if path_mode == "preserve" then
+ path = req_path
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
+ end
+ elseif path_mode == "append" then
+ local prefix = endpoint_path or ""
+ if prefix == "" or prefix == "/" then
+ path = req_path
+ else
+ path = prefix .. req_path
+ path = path:gsub("//+", "/")
+ end
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
+ end
+ else
+ path = (endpoint_path and endpoint_path ~= "" and endpoint_path) or
self.path
Review Comment:
It needs to be confirmed whether the behavior remains consistent when
endpoint_path is an empty string.
##########
apisix/plugins/ai-proxy/schema.lua:
##########
@@ -89,6 +89,15 @@ local ai_instance_schema = {
type = "string",
description = "To be specified to override the
endpoint of the AI Instance",
},
+ path_mode = {
Review Comment:
It is recommended to extract it as a common schema variable to avoid
duplication.
##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table,
extra_opts)
end
end
- local path = (parsed_url and parsed_url.path or self.path)
+ local path_mode = extra_opts.path_mode or "fixed"
+ local endpoint_path = parsed_url and parsed_url.path
+ local req_path = ctx.var.uri
+ local path
+
+ if path_mode == "preserve" then
+ path = req_path
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
+ end
+ elseif path_mode == "append" then
+ local prefix = endpoint_path or ""
+ if prefix == "" or prefix == "/" then
+ path = req_path
+ else
+ path = prefix .. req_path
+ path = path:gsub("//+", "/")
+ end
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
Review Comment:
It can be extracted into a common function.
##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table,
extra_opts)
end
end
- local path = (parsed_url and parsed_url.path or self.path)
+ local path_mode = extra_opts.path_mode or "fixed"
+ local endpoint_path = parsed_url and parsed_url.path
+ local req_path = ctx.var.uri
+ local path
+
+ if path_mode == "preserve" then
+ path = req_path
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
+ end
+ elseif path_mode == "append" then
+ local prefix = endpoint_path or ""
+ if prefix == "" or prefix == "/" then
+ path = req_path
+ else
+ path = prefix .. req_path
+ path = path:gsub("//+", "/")
+ end
+ if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+ local req_args_tab = core.string.decode_args(ctx.var.args)
+ if type(req_args_tab) == "table" then
+ core.table.merge(query_params, req_args_tab)
+ end
Review Comment:
It is recommended to use `ctx.var.is_args == "?"` to check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]