|
| 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 | +local core = require("apisix.core") |
| 18 | +local body_transformer = require("apisix.plugins.body-transformer") |
| 19 | +local ipairs = ipairs |
| 20 | + |
| 21 | +local prompt_schema = { |
| 22 | + properties = { |
| 23 | + role = { |
| 24 | + type = "string", |
| 25 | + enum = { "system", "user", "assistant" } |
| 26 | + }, |
| 27 | + content = { |
| 28 | + type = "string", |
| 29 | + minLength = 1, |
| 30 | + } |
| 31 | + }, |
| 32 | + required = { "role", "content" } |
| 33 | +} |
| 34 | + |
| 35 | +local prompts = { |
| 36 | + type = "array", |
| 37 | + minItems = 1, |
| 38 | + items = prompt_schema |
| 39 | +} |
| 40 | + |
| 41 | +local schema = { |
| 42 | + type = "object", |
| 43 | + properties = { |
| 44 | + templates = { |
| 45 | + type = "array", |
| 46 | + minItems = 1, |
| 47 | + items = { |
| 48 | + type = "object", |
| 49 | + properties = { |
| 50 | + name = { |
| 51 | + type = "string", |
| 52 | + minLength = 1, |
| 53 | + }, |
| 54 | + template = { |
| 55 | + type = "object", |
| 56 | + properties = { |
| 57 | + model = { |
| 58 | + type = "string", |
| 59 | + minLength = 1, |
| 60 | + }, |
| 61 | + messages = prompts |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + required = {"name", "template"} |
| 66 | + } |
| 67 | + }, |
| 68 | + }, |
| 69 | + required = {"templates"}, |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +local _M = { |
| 74 | + version = 0.1, |
| 75 | + priority = 1060, |
| 76 | + name = "ai-prompt-template", |
| 77 | + schema = schema, |
| 78 | +} |
| 79 | + |
| 80 | +local templates_lrucache = core.lrucache.new({ |
| 81 | + ttl = 300, count = 256 |
| 82 | +}) |
| 83 | + |
| 84 | +local templates_json_lrucache = core.lrucache.new({ |
| 85 | + ttl = 300, count = 256 |
| 86 | +}) |
| 87 | + |
| 88 | +function _M.check_schema(conf) |
| 89 | + return core.schema.check(schema, conf) |
| 90 | +end |
| 91 | + |
| 92 | + |
| 93 | +local function get_request_body_table() |
| 94 | + local body, err = core.request.get_body() |
| 95 | + if not body then |
| 96 | + return nil, { message = "could not get body: " .. err } |
| 97 | + end |
| 98 | + |
| 99 | + local body_tab, err = core.json.decode(body) |
| 100 | + if not body_tab then |
| 101 | + return nil, { message = "could not get parse JSON request body: ", err } |
| 102 | + end |
| 103 | + |
| 104 | + return body_tab |
| 105 | +end |
| 106 | + |
| 107 | + |
| 108 | +local function find_template(conf, template_name) |
| 109 | + for _, template in ipairs(conf.templates) do |
| 110 | + if template.name == template_name then |
| 111 | + return template.template |
| 112 | + end |
| 113 | + end |
| 114 | + return nil |
| 115 | +end |
| 116 | + |
| 117 | +function _M.rewrite(conf, ctx) |
| 118 | + local body_tab, err = get_request_body_table() |
| 119 | + if not body_tab then |
| 120 | + return 400, err |
| 121 | + end |
| 122 | + local template_name = body_tab.template_name |
| 123 | + if not template_name then |
| 124 | + return 400, { message = "template name is missing in request." } |
| 125 | + end |
| 126 | + |
| 127 | + local template = templates_lrucache(template_name, conf, find_template, conf, template_name) |
| 128 | + if not template then |
| 129 | + return 400, { message = "template: " .. template_name .. " not configured." } |
| 130 | + end |
| 131 | + |
| 132 | + local template_json = templates_json_lrucache(template, template, core.json.encode, template) |
| 133 | + core.log.info("sending template to body_transformer: ", template_json) |
| 134 | + return body_transformer.rewrite( |
| 135 | + { |
| 136 | + request = { |
| 137 | + template = template_json, |
| 138 | + input_format = "json" |
| 139 | + } |
| 140 | + }, |
| 141 | + ctx |
| 142 | + ) |
| 143 | +end |
| 144 | + |
| 145 | + |
| 146 | +return _M |
0 commit comments