Skip to content

Commit

Permalink
Merge branch 'apache:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
bestjane committed Sep 14, 2024
2 parents 19986b2 + b5ea128 commit 1e77749
Show file tree
Hide file tree
Showing 33 changed files with 2,041 additions and 33 deletions.
5 changes: 3 additions & 2 deletions apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dependencies = {
"nginx-lua-prometheus-api7 = 0.20240201-1",
"jsonschema = 0.9.8",
"lua-resty-ipmatcher = 0.6.1",
"lua-resty-kafka = 0.22-0",
"lua-resty-kafka = 0.23-0",
"lua-resty-logger-socket = 2.0.1-0",
"skywalking-nginx-lua = 0.6.0",
"base64 = 1.5-2",
Expand All @@ -81,7 +81,8 @@ dependencies = {
"lua-resty-ldap = 0.1.0-0",
"lua-resty-t1k = 1.1.5",
"brotli-ffi = 0.3-1",
"lua-ffi-zlib = 0.6-0"
"lua-ffi-zlib = 0.6-0",
"api7-lua-resty-aws == 2.0.1-1",
}

build = {
Expand Down
2 changes: 2 additions & 0 deletions apisix/cli/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ local _M = {
"authz-keycloak",
"proxy-cache",
"body-transformer",
"ai-prompt-template",
"ai-prompt-decorator",
"proxy-mirror",
"proxy-rewrite",
"workflow",
Expand Down
2 changes: 1 addition & 1 deletion apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ Please modify "admin_key" in conf/config.yaml .

for name, value in pairs(exported_vars) do
if value then
table_insert(sys_conf["envs"], name .. "=" .. value)
table_insert(sys_conf["envs"], name)
end
end
end
Expand Down
117 changes: 117 additions & 0 deletions apisix/plugins/ai-prompt-decorator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
--
-- 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 ngx = ngx
local pairs = pairs
local EMPTY = {}

local prompt_schema = {
properties = {
role = {
type = "string",
enum = { "system", "user", "assistant" }
},
content = {
type = "string",
minLength = 1,
}
},
required = { "role", "content" }
}

local prompts = {
type = "array",
items = prompt_schema
}

local schema = {
type = "object",
properties = {
prepend = prompts,
append = prompts,
},
anyOf = {
{ required = { "prepend" } },
{ required = { "append" } },
{ required = { "append", "prepend" } },
},
}


local _M = {
version = 0.1,
priority = 1070,
name = "ai-prompt-decorator",
schema = schema,
}


function _M.check_schema(conf)
return core.schema.check(schema, conf)
end


local function get_request_body_table()
local body, err = core.request.get_body()
if not body then
return nil, { message = "could not get body: " .. err }
end

local body_tab, err = core.json.decode(body)
if not body_tab then
return nil, { message = "could not get parse JSON request body: " .. err }
end

return body_tab
end


local function decorate(conf, body_tab)
local new_messages = conf.prepend or EMPTY
for _, message in pairs(body_tab.messages) do
core.table.insert_tail(new_messages, message)
end

for _, message in pairs(conf.append or EMPTY) do
core.table.insert_tail(new_messages, message)
end

body_tab.messages = new_messages
end


function _M.rewrite(conf, ctx)
local body_tab, err = get_request_body_table()
if not body_tab then
return 400, err
end

if not body_tab.messages then
return 400, "messages missing from request body"
end
decorate(conf, body_tab) -- will decorate body_tab in place

local new_jbody, err = core.json.encode(body_tab)
if not new_jbody then
return 500, { message = "failed to parse modified JSON request body: " .. err }
end

ngx.req.set_body_data(new_jbody)
end


return _M
146 changes: 146 additions & 0 deletions apisix/plugins/ai-prompt-template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
--
-- 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 body_transformer = require("apisix.plugins.body-transformer")
local ipairs = ipairs

local prompt_schema = {
properties = {
role = {
type = "string",
enum = { "system", "user", "assistant" }
},
content = {
type = "string",
minLength = 1,
}
},
required = { "role", "content" }
}

local prompts = {
type = "array",
minItems = 1,
items = prompt_schema
}

local schema = {
type = "object",
properties = {
templates = {
type = "array",
minItems = 1,
items = {
type = "object",
properties = {
name = {
type = "string",
minLength = 1,
},
template = {
type = "object",
properties = {
model = {
type = "string",
minLength = 1,
},
messages = prompts
}
}
},
required = {"name", "template"}
}
},
},
required = {"templates"},
}


local _M = {
version = 0.1,
priority = 1071,
name = "ai-prompt-template",
schema = schema,
}

local templates_lrucache = core.lrucache.new({
ttl = 300, count = 256
})

local templates_json_lrucache = core.lrucache.new({
ttl = 300, count = 256
})

function _M.check_schema(conf)
return core.schema.check(schema, conf)
end


local function get_request_body_table()
local body, err = core.request.get_body()
if not body then
return nil, { message = "could not get body: " .. err }
end

local body_tab, err = core.json.decode(body)
if not body_tab then
return nil, { message = "could not get parse JSON request body: ", err }
end

return body_tab
end


local function find_template(conf, template_name)
for _, template in ipairs(conf.templates) do
if template.name == template_name then
return template.template
end
end
return nil
end

function _M.rewrite(conf, ctx)
local body_tab, err = get_request_body_table()
if not body_tab then
return 400, err
end
local template_name = body_tab.template_name
if not template_name then
return 400, { message = "template name is missing in request." }
end

local template = templates_lrucache(template_name, conf, find_template, conf, template_name)
if not template then
return 400, { message = "template: " .. template_name .. " not configured." }
end

local template_json = templates_json_lrucache(template, template, core.json.encode, template)
core.log.info("sending template to body_transformer: ", template_json)
return body_transformer.rewrite(
{
request = {
template = template_json,
input_format = "json"
}
},
ctx
)
end


return _M
4 changes: 2 additions & 2 deletions apisix/plugins/grpc-transcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ local plugin_name = "grpc-transcode"
local pb_option_def = {
{ description = "enum as result",
type = "string",
enum = {"int64_as_number", "int64_as_string", "int64_as_hexstring"},
enum = {"enum_as_name", "enum_as_value"},
},
{ description = "int64 as result",
type = "string",
enum = {"enum_as_name", "enum_as_value"},
enum = {"int64_as_number", "int64_as_string", "int64_as_hexstring"},
},
{ description ="default values option",
type = "string",
Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/multi-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function _M.check_schema(conf)
local auth = require("apisix.plugins." .. auth_plugin_name)
if auth == nil then
return false, auth_plugin_name .. " plugin did not found"
else
else
if auth.type ~= 'auth' then
return false, auth_plugin_name .. " plugin is not supported"
end
Expand Down
Loading

0 comments on commit 1e77749

Please sign in to comment.