This commit is contained in:
2026-05-18 23:05:27 +08:00
parent 23df7b415f
commit c131269497

View File

@@ -57,89 +57,89 @@ server {
proxy_read_timeout 3600s; proxy_read_timeout 3600s;
proxy_send_timeout 3600s; proxy_send_timeout 3600s;
location = /v1/chat/completions { # location = /v1/chat/completions {
client_body_buffer_size 16k; # client_body_buffer_size 16k;
content_by_lua_block { # content_by_lua_block {
local backend = "@ai_sggai_site_backend" # local backend = "@ai_sggai_site_backend"
local max_body_bytes = 1024 # local max_body_bytes = 1024
if ngx.req.get_method() ~= "POST" then # if ngx.req.get_method() ~= "POST" then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
local content_length = tonumber(ngx.var.http_content_length) # local content_length = tonumber(ngx.var.http_content_length)
if not content_length then # if not content_length then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
if content_length > max_body_bytes then # if content_length > max_body_bytes then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
ngx.req.read_body() # ngx.req.read_body()
local body = ngx.req.get_body_data() # local body = ngx.req.get_body_data()
if not body or #body > max_body_bytes then # if not body or #body > max_body_bytes then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
local cjson = require "cjson.safe" # local cjson = require "cjson.safe"
local short_post_log = { # local short_post_log = {
method = ngx.req.get_method(), # method = ngx.req.get_method(),
uri = ngx.var.request_uri, # uri = ngx.var.request_uri,
content_length = content_length, # content_length = content_length,
content_type = ngx.var.http_content_type, # content_type = ngx.var.http_content_type,
user_agent = ngx.var.http_user_agent, # user_agent = ngx.var.http_user_agent,
body = body # body = body
} # }
ngx.log(ngx.NOTICE, "[ai.sggai.site] short POST request: ", cjson.encode(short_post_log) or body) # ngx.log(ngx.NOTICE, "[ai.sggai.site] short POST request: ", cjson.encode(short_post_log) or body)
local payload = cjson.decode(body) # local payload = cjson.decode(body)
if type(payload) ~= "table" or payload.stream ~= false then # if type(payload) ~= "table" or payload.stream ~= false then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
if type(payload.messages) ~= "table" then # if type(payload.messages) ~= "table" then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
local has_hi_content = false # local has_hi_content = false
for _, message in ipairs(payload.messages) do # for _, message in ipairs(payload.messages) do
if type(message) == "table" and message.content == "hi" then # if type(message) == "table" and message.content == "hi" then
has_hi_content = true # has_hi_content = true
break # break
end # end
end # end
if not has_hi_content then # if not has_hi_content then
return ngx.exec(backend) # return ngx.exec(backend)
end # end
ngx.status = ngx.HTTP_OK # ngx.status = ngx.HTTP_OK
ngx.header["Content-Type"] = "application/json; charset=utf-8" # ngx.header["Content-Type"] = "application/json; charset=utf-8"
ngx.say([[{ # ngx.say([[{
"id": "chatcmpl-mock", # "id": "chatcmpl-mock",
"object": "chat.completion", # "object": "chat.completion",
"created": 1716030000, # "created": 1716030000,
"model": "xxx", # "model": "xxx",
"choices": [ # "choices": [
{ # {
"index": 0, # "index": 0,
"message": { "role": "assistant", "content": "ok" }, # "message": { "role": "assistant", "content": "ok" },
"finish_reason": "stop" # "finish_reason": "stop"
} # }
], # ],
"usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 } # "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 }
}]]) # }]])
return ngx.exit(ngx.HTTP_OK) # return ngx.exit(ngx.HTTP_OK)
} # }
} # }
location @ai_sggai_site_backend { # location @ai_sggai_site_backend {
proxy_pass http://10.1.0.1:3001; # proxy_pass http://10.1.0.1:3001;
} # }
location / { location / {
proxy_pass http://10.1.0.1:3001; proxy_pass http://10.1.0.1:3001;