shreemaan-abhishek commented on code in PR #11568:
URL: https://github.com/apache/apisix/pull/11568#discussion_r1796744554


##########
apisix/plugins/ai-rag.lua:
##########
@@ -0,0 +1,168 @@
+--
+-- 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 http = require("resty.http")
+local url = require("socket.url")
+local core = require("apisix.core")
+local decorate = require("apisix.plugins.ai-prompt-decorator").__decorate
+local next = next
+
+local azure_ai_search_schema = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "string",
+        },
+        api_key = {
+            type = "string",
+        },
+    }
+}
+
+local azure_openai_embeddings = {
+    type = "object",
+    properties = {
+        endpoint = {
+            type = "string",
+        },
+        api_key = {
+            type = "string",
+        },
+    },
+    required = { "endpoint", "api_key" }
+}
+
+
+local schema = {
+    type = "object",
+    properties = {
+        type = "object",
+        embeddings_provider = {
+            type = "object",
+            properties = {
+                azure_openai = azure_openai_embeddings
+            },
+            -- change to enum while implementing support for other search 
services
+            required = { "azure_openai" },
+        },
+        vector_search_provider = {
+            type = "object",
+            properties = {
+                azure_ai_search = azure_ai_search_schema
+            },
+            -- change to enum while implementing support for other search 
services
+            required = { "azure_ai_search" }
+        },
+    },
+    required = { "embeddings_provider", "vector_search_provider" }
+}
+
+local request_schema = {
+    type = "object",
+    properties = {
+        ai_rag = {
+            type = "object",
+            properties = {
+                vector_search = {},
+                embeddings = {},
+            },
+            required = { "vector_search", "embeddings" }
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1004, -- TODO check with other ai plugins
+    name = "ai-rag",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    -- TODO: check endpoint validity
+    return core.schema.check(schema, conf)
+end
+
+function _M.access(conf, ctx)
+    -- local conf = conf.rag
+    -- if conf then
+    local httpc = http.new()
+    local body_tab = core.request.get_json_request_body_table()
+
+    if not body_tab["ai_rag"] then
+        core.log.error("request body must have \"ai-rag\" field")
+        return 400
+    end
+
+    local embeddings_provider = next(conf.embeddings_provider)
+    local embeddings_provider_conf = 
conf.embeddings_provider[next(conf.embeddings_provider)]
+    local embeddings_driver = require("apisix.plugins.ai-rag.embeddings." .. 
embeddings_provider)
+
+    local vector_search_provider = next(conf.vector_search_provider)
+    local vector_search_provider_conf = 
conf.vector_search_provider[vector_search_provider]
+    local vector_search_driver = 
require("apisix.plugins.ai-rag.vector-search." .. vector_search_provider)
+
+    local vs_req_schema = vector_search_driver.request_schema
+    local emb_req_schema = embeddings_driver.request_schema
+
+    request_schema.properties.ai_rag.properties.vector_search = vs_req_schema
+    request_schema.properties.ai_rag.properties.embeddings = emb_req_schema
+
+    local ok, err = core.schema.check(request_schema, body_tab)
+    if not ok then
+        core.log.error("request body fails schema check: ", err)
+        return 400
+    end
+
+    local embeddings, err = 
embeddings_driver.get_embeddings(embeddings_provider_conf, 
body_tab["ai_rag"].embeddings, httpc)
+    if not embeddings then
+        -- TODO: bring order
+        core.log.error("could not get embeddings: ", err)
+        return 500
+    end
+    core.log.error("dibag err: ", err)
+    core.log.warn("dibag res: ", core.json.encode(embeddings))
+
+    local search_body = body_tab["ai_rag"].vector_search
+    search_body.embeddings = embeddings
+    local res, err = vector_search_driver.search(vector_search_provider_conf, 
search_body, httpc)
+    if not res then
+        -- TODO: bring order
+        core.log.error("could not get vector_search: ", err)
+        return 500
+    end
+    core.log.error("dibag err: ", err)
+    core.log.warn("dibag res: ", core.json.encode(res, true))
+
+    body_tab["ai_rag"] = nil

Review Comment:
   done



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