-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.conf
115 lines (100 loc) · 3.66 KB
/
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
worker_processes 1;
# error_log logs/debug.log debug;
user root;
events {
worker_connections 1024;
}
env OPENAI_API_HOST;
env OPENAI_MODEL;
env OPENAI_VISION_API_HOST;
env OPENAI_VISION_MODEL;
env OPENAI_EMBEDDING_API_HOST;
env OPENAI_EMBEDDING_MODEL;
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
fastcgi_intercept_errors on;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 20m;
client_body_buffer_size 20m;
proxy_buffer_size 20m;
proxy_buffers 4 20m;
proxy_busy_buffers_size 20m;
proxy_request_buffering on;
proxy_http_version 1.1;
proxy_set_header Connection "";
resolver 8.8.8.8;
# lua_code_cache off;
lua_need_request_body on;
# cache for discovery metadata documents
lua_shared_dict discovery 1m;
# cache for JWKs
lua_shared_dict jwks 1m;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
lua_ssl_verify_depth 3;
server {
listen 80;
# listen 443 ssl;
server_name 127.0.0.1;
charset utf-8;
default_type text/html;
# lua_code_cache off;
set $session_check_ssi off;
set $session_name model_api_proxy_session;
set $session_secret 623q4hR325t36VsCD3g567922IC0073T;
expires 0;
add_header Cache-Control private;
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_cache off;
proxy_buffering off;
chunked_transfer_encoding on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 300;
if ($request_method = POST ) {
set $upstream '';
access_by_lua '
local OPENAI_MODELS = {
["gpt-3.5-turbo"] = {model = os.getenv("OPENAI_MODEL"), host = os.getenv("OPENAI_API_HOST")},
["gpt-4o-mini"] = {model = os.getenv("OPENAI_VISION_MODEL"), host = os.getenv("OPENAI_VISION_API_HOST")},
["text-embedding-ada-002"] = {model = os.getenv("OPENAI_EMBEDDING_MODEL"), host = os.getenv("OPENAI_EMBEDDING_API_HOST")}
}
ngx.req.read_body()
local data = ngx.req.get_body_data()
if data then
local json = require("cjson")
local decoded_data = json.decode(data)
if decoded_data and OPENAI_MODELS[decoded_data["model"]] then
local selected_model = OPENAI_MODELS[decoded_data["model"]]
decoded_data["model"] = selected_model.model
data = json.encode(decoded_data)
ngx.req.set_body_data(data)
ngx.req.set_header("Content-Length", string.len(data))
ngx.var.upstream = selected_model.host
end
end
';
proxy_pass http://$upstream;
}
if ($request_method = GET ) {
set $upstream '';
access_by_lua '
ngx.var.upstream = os.getenv("OPENAI_API_HOST")
';
proxy_pass http://$upstream;
}
}
}
}