add_block_preprocessor()

in t/plugin/ai-proxy.t [35:178]


add_block_preprocessor(sub {
    my ($block) = @_;

    if (!defined $block->request) {
        $block->set_value("request", "GET /t");
    }

    my $http_config = $block->http_config // <<_EOC_;
        server {
            server_name openai;
            listen 6724;

            default_type 'application/json';

            location /v1/chat/completions {
                content_by_lua_block {
                    local json = require("cjson.safe")

                    if ngx.req.get_method() ~= "POST" then
                        ngx.status = 400
                        ngx.say("Unsupported request method: ", ngx.req.get_method())
                    end
                    ngx.req.read_body()
                    local body, err = ngx.req.get_body_data()
                    body, err = json.decode(body)

                    local test_type = ngx.req.get_headers()["test-type"]
                    if test_type == "options" then
                        if body.foo == "bar" then
                            ngx.status = 200
                            ngx.say("options works")
                        else
                            ngx.status = 500
                            ngx.say("model options feature doesn't work")
                        end
                        return
                    end

                    local header_auth = ngx.req.get_headers()["authorization"]
                    local query_auth = ngx.req.get_uri_args()["apikey"]

                    if header_auth ~= "Bearer token" and query_auth ~= "apikey" then
                        ngx.status = 401
                        ngx.say("Unauthorized")
                        return
                    end

                    if header_auth == "Bearer token" or query_auth == "apikey" then
                        ngx.req.read_body()
                        local body, err = ngx.req.get_body_data()
                        body, err = json.decode(body)

                        if not body.messages or #body.messages < 1 then
                            ngx.status = 400
                            ngx.say([[{ "error": "bad request"}]])
                            return
                        end

                        if body.messages[1].content == "write an SQL query to get all rows from student table" then
                            ngx.print("SELECT * FROM STUDENTS")
                            return
                        end

                        ngx.status = 200
                        ngx.say([[$resp]])
                        return
                    end


                    ngx.status = 503
                    ngx.say("reached the end of the test suite")
                }
            }

            location /v1/embeddings {
                content_by_lua_block {
                    if ngx.req.get_method() ~= "POST" then
                        ngx.status = 400
                        ngx.say("unsupported request method: ", ngx.req.get_method())
                    end

                    local header_auth = ngx.req.get_headers()["authorization"]
                    if header_auth ~= "Bearer token" then
                        ngx.status = 401
                        ngx.say("unauthorized")
                        return
                    end

                    ngx.req.read_body()
                    local body, err = ngx.req.get_body_data()
                    local json = require("cjson.safe")
                    body, err = json.decode(body)
                    if err then
                        ngx.status = 400
                        ngx.say("failed to get request body: ", err)
                    end

                    if body.model ~= "text-embedding-ada-002" then
                        ngx.status = 400
                        ngx.say("unsupported model: ", body.model)
                        return
                    end

                    if body.encoding_format ~= "float" then
                        ngx.status = 400
                        ngx.say("unsupported encoding format: ", body.encoding_format)
                        return
                    end

                    ngx.status = 200
                    ngx.say([[
                        {
                          "object": "list",
                          "data": [
                            {
                              "object": "embedding",
                              "embedding": [
                                0.0023064255,
                                -0.009327292,
                                -0.0028842222
                              ],
                              "index": 0
                            }
                          ],
                          "model": "text-embedding-ada-002",
                          "usage": {
                            "prompt_tokens": 8,
                            "total_tokens": 8
                          }
                        }
                    ]])
                }
            }

            location /random {
                content_by_lua_block {
                    ngx.say("path override works")
                }
            }
        }
_EOC_

    $block->set_value("http_config", $http_config);
});