in t/plugin/ai-request-rewrite.t [25:107]
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 {
ngx.req.read_body()
local body = ngx.req.get_body_data()
local json = require("cjson.safe")
local request_data = json.decode(body)
local header_auth = ngx.req.get_headers()["authorization"]
local query_auth = ngx.req.get_uri_args()["api_key"]
if header_auth ~= "Bearer token" and query_auth ~= "apikey" then
ngx.status = 401
ngx.say("Unauthorized")
return
end
local response = {
choices = {
{
message = {
content = request_data.messages[1].content .. ' ' .. request_data.messages[2].content
}
}
}
}
local json = require("cjson.safe")
local json_response = json.encode(response)
ngx.say(json_response)
}
}
location /random {
content_by_lua_block {
local response = {
choices = {
{
message = {
content = 'return by random endpoint'
}
}
}
}
local json = require("cjson.safe")
local json_response = json.encode(response)
ngx.say(json_response)
}
}
location /internalservererror {
content_by_lua_block {
ngx.status = 500
ngx.say("Internal Server Error")
return
}
}
location /bad_request {
content_by_lua_block {
ngx.status = 400
ngx.say("Bad Request")
return
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});