zhoujiexiong commented on code in PR #11499: URL: https://github.com/apache/apisix/pull/11499#discussion_r1753044355
########## apisix/plugins/ai-proxy.lua: ########## @@ -0,0 +1,136 @@ +-- +-- 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 core = require("apisix.core") +local schema = require("apisix.plugins.ai-proxy.schema") +local require = require +local pcall = pcall +local ngx_req = ngx.req +local ngx_print = ngx.print +local ngx_flush = ngx.flush + +local plugin_name = "ai-proxy" +local _M = { + version = 0.5, + priority = 1004, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + local ai_driver = pcall(require, "apisix.plugins.ai-proxy.drivers." .. conf.model.provider) + if not ai_driver then + return false, "provider: " .. conf.model.provider .. " is not supported." + end + return core.schema.check(schema.plugin_schema, conf) +end + + +local CONTENT_TYPE_JSON = "application/json" + + +local function keepalive_or_close(conf, httpc) + if conf.set_keepalive then + httpc:set_keepalive(10000, 100) + return + end + httpc:close() +end + + +function _M.access(conf, ctx) + local ct = core.request.header(ctx, "Content-Type") or CONTENT_TYPE_JSON + if not core.string.has_prefix(ct, CONTENT_TYPE_JSON) then + return 400, "unsupported content-type: " .. ct + end + + local request_table, err = core.request.get_request_body_table() + if not request_table then + return 400, err + end + + local ok, err = core.schema.check(schema.chat_request_schema, request_table) + if not ok then + return 400, "request format doesn't match schema: " .. err + end + + if conf.model.name then + request_table.model = conf.model.name + end + + if core.table.try_read_attr(conf, "model", "options", "stream") then + request_table.stream = true + end + + local ai_driver = require("apisix.plugins.ai-proxy.drivers." .. conf.model.provider) + local res, err, httpc = ai_driver.request(conf, request_table, ctx) + if not res then + core.log.error("failed to send request to AI service: ", err) + return 500 Review Comment: use the predefined Ngx constants? > ngx.HTTP_NOT_ACCEPTABLE = 406 > ngx.HTTP_REQUEST_TIMEOUT = 408 > ngx.HTTP_CONFLICT = 409 > ngx.HTTP_GONE = 410 > ngx.HTTP_UPGRADE_REQUIRED = 426 > ngx.HTTP_TOO_MANY_REQUESTS = 429 > ngx.HTTP_CLOSE = 444 > ngx.HTTP_ILLEGAL = 451 > ngx.HTTP_INTERNAL_SERVER_ERROR = 500 > ngx.HTTP_METHOD_NOT_IMPLEMENTED = 501 > ngx.HTTP_BAD_GATEWAY = 502 > ngx.HTTP_SERVICE_UNAVAILABLE = 503 > ngx.HTTP_GATEWAY_TIMEOUT = 504 > ngx.HTTP_VERSION_NOT_SUPPORTED = 505 > ngx.HTTP_INSUFFICIENT_STORAGE = 507 -- 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]
