bzp2010 commented on code in PR #11499:
URL: https://github.com/apache/apisix/pull/11499#discussion_r1745052846


##########
apisix/plugins/ai-proxy/schema.lua:
##########
@@ -0,0 +1,153 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+
+local auth_item_schema = {
+    type = "object",
+    patternProperties = {
+        ["^[a-zA-Z0-9._-]+$"] = {
+            type = "string"
+        }
+    }
+}
+
+local auth_schema = {
+    type = "object",
+    patternProperties = {
+        ["^(query|header)$"] = auth_item_schema

Review Comment:
   I guess this could be written in a regular flattened way? Given the 
different state of implementation of json schema in various programming 
languages, I would suggest that we write it in the most common way rather than 
any special syntax.



##########
apisix/plugins/ai-proxy/drivers/openai.lua:
##########
@@ -0,0 +1,88 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+
+local core = require("apisix.core")
+local http = require("resty.http")
+local url  = require("socket.url")
+
+local pairs = pairs
+
+-- globals
+local DEFAULT_HOST = "api.openai.com"
+local DEFAULT_PORT = 443
+
+
+function _M.request(conf, request_table, ctx)
+    local httpc, err = http.new()
+    if not httpc then
+        return nil, "failed to create http client to send request to LLM 
server: " .. err
+    end
+    httpc:set_timeout(conf.timeout)
+
+    local endpoint = core.table.try_read_attr(conf, "override", "endpoint")
+    local parsed_url
+    if endpoint then
+        parsed_url = url.parse(endpoint)
+    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,
+        ssl_verify = conf.ssl_verify,
+        ssl_server_name = parsed_url.host or DEFAULT_HOST,
+        pool_size = conf.keepalive and conf.keepalive_pool,
+    })
+
+    if not ok then
+        return nil, "failed to connect to LLM server: " .. err
+    end
+
+    local query_params = core.utils.table_to_query_params(conf.auth.query)

Review Comment:
   I think `ngx.encode_args` should be exactly what you want?
   
https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#ngxencode_args



##########
apisix/plugins/ai-proxy/drivers/openai.lua:
##########
@@ -0,0 +1,88 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local _M = {}
+
+local core = require("apisix.core")
+local http = require("resty.http")
+local url  = require("socket.url")
+
+local pairs = pairs
+
+-- globals
+local DEFAULT_HOST = "api.openai.com"
+local DEFAULT_PORT = 443
+
+
+function _M.request(conf, request_table, ctx)
+    local httpc, err = http.new()
+    if not httpc then
+        return nil, "failed to create http client to send request to LLM 
server: " .. err
+    end
+    httpc:set_timeout(conf.timeout)
+
+    local endpoint = core.table.try_read_attr(conf, "override", "endpoint")
+    local parsed_url
+    if endpoint then
+        parsed_url = url.parse(endpoint)
+    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,
+        ssl_verify = conf.ssl_verify,
+        ssl_server_name = parsed_url.host or DEFAULT_HOST,
+        pool_size = conf.keepalive and conf.keepalive_pool,
+    })
+
+    if not ok then
+        return nil, "failed to connect to LLM server: " .. err
+    end
+
+    local query_params = core.utils.table_to_query_params(conf.auth.query)

Review Comment:
   If so, we can remove the custom function.



-- 
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]

Reply via email to