|
| 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 next = next |
| 18 | +local require = require |
| 19 | +local ngx_req = ngx.req |
| 20 | + |
| 21 | +local http = require("resty.http") |
| 22 | +local core = require("apisix.core") |
| 23 | + |
| 24 | +local azure_openai_embeddings = require("apisix.plugins.ai-rag.embeddings.azure_openai").schema |
| 25 | +local azure_ai_search_schema = require("apisix.plugins.ai-rag.vector-search.azure_ai_search").schema |
| 26 | + |
| 27 | +local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR |
| 28 | +local HTTP_BAD_REQUEST = ngx.HTTP_BAD_REQUEST |
| 29 | + |
| 30 | +local schema = { |
| 31 | + type = "object", |
| 32 | + properties = { |
| 33 | + type = "object", |
| 34 | + embeddings_provider = { |
| 35 | + type = "object", |
| 36 | + properties = { |
| 37 | + azure_openai = azure_openai_embeddings |
| 38 | + }, |
| 39 | + -- ensure only one provider can be configured while implementing support for |
| 40 | + -- other providers |
| 41 | + required = { "azure_openai" }, |
| 42 | + maxProperties = 1, |
| 43 | + }, |
| 44 | + vector_search_provider = { |
| 45 | + type = "object", |
| 46 | + properties = { |
| 47 | + azure_ai_search = azure_ai_search_schema |
| 48 | + }, |
| 49 | + -- ensure only one provider can be configured while implementing support for |
| 50 | + -- other providers |
| 51 | + required = { "azure_ai_search" }, |
| 52 | + maxProperties = 1 |
| 53 | + }, |
| 54 | + }, |
| 55 | + required = { "embeddings_provider", "vector_search_provider" } |
| 56 | +} |
| 57 | + |
| 58 | +local request_schema = { |
| 59 | + type = "object", |
| 60 | + properties = { |
| 61 | + ai_rag = { |
| 62 | + type = "object", |
| 63 | + properties = { |
| 64 | + vector_search = {}, |
| 65 | + embeddings = {}, |
| 66 | + }, |
| 67 | + required = { "vector_search", "embeddings" } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +local _M = { |
| 73 | + version = 0.1, |
| 74 | + priority = 1060, |
| 75 | + name = "ai-rag", |
| 76 | + schema = schema, |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +function _M.check_schema(conf) |
| 81 | + return core.schema.check(schema, conf) |
| 82 | +end |
| 83 | + |
| 84 | + |
| 85 | +function _M.access(conf, ctx) |
| 86 | + local httpc = http.new() |
| 87 | + local body_tab, err = core.request.get_json_request_body_table() |
| 88 | + if not body_tab then |
| 89 | + return HTTP_BAD_REQUEST, err |
| 90 | + end |
| 91 | + if not body_tab["ai_rag"] then |
| 92 | + core.log.error("request body must have \"ai-rag\" field") |
| 93 | + return HTTP_BAD_REQUEST |
| 94 | + end |
| 95 | + |
| 96 | + local embeddings_provider = next(conf.embeddings_provider) |
| 97 | + local embeddings_provider_conf = conf.embeddings_provider[embeddings_provider] |
| 98 | + local embeddings_driver = require("apisix.plugins.ai-rag.embeddings." .. embeddings_provider) |
| 99 | + |
| 100 | + local vector_search_provider = next(conf.vector_search_provider) |
| 101 | + local vector_search_provider_conf = conf.vector_search_provider[vector_search_provider] |
| 102 | + local vector_search_driver = require("apisix.plugins.ai-rag.vector-search." .. |
| 103 | + vector_search_provider) |
| 104 | + |
| 105 | + local vs_req_schema = vector_search_driver.request_schema |
| 106 | + local emb_req_schema = embeddings_driver.request_schema |
| 107 | + |
| 108 | + request_schema.properties.ai_rag.properties.vector_search = vs_req_schema |
| 109 | + request_schema.properties.ai_rag.properties.embeddings = emb_req_schema |
| 110 | + |
| 111 | + local ok, err = core.schema.check(request_schema, body_tab) |
| 112 | + if not ok then |
| 113 | + core.log.error("request body fails schema check: ", err) |
| 114 | + return HTTP_BAD_REQUEST |
| 115 | + end |
| 116 | + |
| 117 | + local embeddings, status, err = embeddings_driver.get_embeddings(embeddings_provider_conf, |
| 118 | + body_tab["ai_rag"].embeddings, httpc) |
| 119 | + if not embeddings then |
| 120 | + core.log.error("could not get embeddings: ", err) |
| 121 | + return status, err |
| 122 | + end |
| 123 | + |
| 124 | + local search_body = body_tab["ai_rag"].vector_search |
| 125 | + search_body.embeddings = embeddings |
| 126 | + local res, status, err = vector_search_driver.search(vector_search_provider_conf, |
| 127 | + search_body, httpc) |
| 128 | + if not res then |
| 129 | + core.log.error("could not get vector_search result: ", err) |
| 130 | + return status, err |
| 131 | + end |
| 132 | + |
| 133 | + -- remove ai_rag from request body because their purpose is served |
| 134 | + -- also, these values will cause failure when proxying requests to LLM. |
| 135 | + body_tab["ai_rag"] = nil |
| 136 | + |
| 137 | + if not body_tab.messages then |
| 138 | + body_tab.messages = {} |
| 139 | + end |
| 140 | + |
| 141 | + local augment = { |
| 142 | + role = "user", |
| 143 | + content = res |
| 144 | + } |
| 145 | + core.table.insert_tail(body_tab.messages, augment) |
| 146 | + |
| 147 | + local req_body_json, err = core.json.encode(body_tab) |
| 148 | + if not req_body_json then |
| 149 | + return HTTP_INTERNAL_SERVER_ERROR, err |
| 150 | + end |
| 151 | + |
| 152 | + ngx_req.set_body_data(req_body_json) |
| 153 | +end |
| 154 | + |
| 155 | + |
| 156 | +return _M |
0 commit comments