aukaru commited on
Commit
5c5b371
·
verified ·
1 Parent(s): 07e9c20

Upload 236 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .env.example +215 -0
  2. .gitattributes +35 -1
  3. .gitignore +11 -0
  4. .husky/pre-push +4 -0
  5. .prettierrc +13 -0
  6. .windsurfrules +33 -0
  7. CODEBASE_GUIDE.md +321 -0
  8. README.md +72 -10
  9. data/.gitignore +2 -0
  10. data/user-files/.gitkeep +0 -0
  11. docker/ci/.gitlab-ci.yml +21 -0
  12. docker/ci/Dockerfile +22 -0
  13. docker/docker-compose-selfhost.yml +17 -0
  14. docker/huggingface/Dockerfile +15 -0
  15. docker/render/Dockerfile +26 -0
  16. docs/assets/aws-request-model-access.png +0 -0
  17. docs/assets/huggingface-createspace.png +3 -0
  18. docs/assets/huggingface-dockerfile.png +0 -0
  19. docs/assets/huggingface-savedockerfile.png +0 -0
  20. docs/assets/openapi-admin-users.yaml +245 -0
  21. docs/aws-configuration.md +58 -0
  22. docs/azure-configuration.md +30 -0
  23. docs/dall-e-configuration.md +71 -0
  24. docs/deploy-huggingface.md +104 -0
  25. docs/deploy-render.md +56 -0
  26. docs/gcp-configuration.md +35 -0
  27. docs/logging-sheets.md +61 -0
  28. docs/pow-captcha.md +135 -0
  29. docs/self-hosting.md +150 -0
  30. docs/user-management.md +85 -0
  31. docs/user-quotas.md +36 -0
  32. http-client.env.json +9 -0
  33. package-lock.json +0 -0
  34. package.json +96 -0
  35. patches/README.md +23 -0
  36. patches/http-proxy+1.18.1.patch +13 -0
  37. public/css/reset.css +349 -0
  38. public/css/sakura-dark.css +231 -0
  39. public/css/sakura.css +237 -0
  40. public/js/hash-worker.js +120 -0
  41. render.yaml +10 -0
  42. scripts/migrate.ts +39 -0
  43. scripts/oai-reverse-proxy.http +309 -0
  44. scripts/seed-events.ts +102 -0
  45. scripts/test-aws-signing.ts +118 -0
  46. scripts/test-concurrency.js +45 -0
  47. scripts/test-queue.js +53 -0
  48. src/admin/api/events.ts +49 -0
  49. src/admin/api/users.ts +117 -0
  50. src/admin/auth.ts +54 -0
.env.example ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # To customize your server, make a copy of this file to `.env` and edit any
2
+ # values you want to change. Be sure to remove the `#` at the beginning of each
3
+ # line you want to modify.
4
+
5
+ # All values have reasonable defaults, so you only need to change the ones you
6
+ # want to override.
7
+
8
+ # Use production mode unless you are developing locally.
9
+ NODE_ENV=production
10
+
11
+ # Detail level of diagnostic logging. (trace | debug | info | warn | error)
12
+ # LOG_LEVEL=info
13
+
14
+ # ------------------------------------------------------------------------------
15
+ # General settings:
16
+
17
+ # The title displayed on the info page.
18
+ # SERVER_TITLE=Coom Tunnel
19
+
20
+ # URL for the image displayed on the login page.
21
+ # If not set, no image will be displayed.
22
+ # LOGIN_IMAGE_URL=https://example.com/your-logo.png
23
+
24
+ # Whether to enable the token-based or password-based login for the main info page.
25
+ # Defaults to true. Set to false to disable login and make the info page public.
26
+ # ENABLE_INFO_PAGE_LOGIN=true
27
+
28
+ # Authentication mode for the service info page. (token | password)
29
+ # If 'token', any valid user token is used (requires GATEKEEPER='user_token' mode).
30
+ # If 'password', SERVICE_INFO_PASSWORD is used.
31
+ # Defaults to 'token' if ENABLE_INFO_PAGE_LOGIN is true.
32
+ # SERVICE_INFO_AUTH_MODE=token
33
+
34
+ # Password for the service info page if SERVICE_INFO_AUTH_MODE is 'password'.
35
+ # SERVICE_INFO_PASSWORD=your-service-info-password
36
+
37
+ # The route name used to proxy requests to APIs, relative to the Web site root.
38
+ # PROXY_ENDPOINT_ROUTE=/proxy
39
+
40
+ # Text model requests allowed per minute per user.
41
+ # TEXT_MODEL_RATE_LIMIT=4
42
+ # Image model requests allowed per minute per user.
43
+ # IMAGE_MODEL_RATE_LIMIT=2
44
+
45
+ # Max number of context tokens a user can request at once.
46
+ # Increase this if your proxy allow GPT 32k or 128k context
47
+ # MAX_CONTEXT_TOKENS_OPENAI=32768
48
+ # MAX_CONTEXT_TOKENS_ANTHROPIC=32768
49
+
50
+ # Max number of output tokens a user can request at once.
51
+ # MAX_OUTPUT_TOKENS_OPENAI=1024
52
+ # MAX_OUTPUT_TOKENS_ANTHROPIC=1024
53
+
54
+ # Whether to show the estimated cost of consumed tokens on the info page.
55
+ # SHOW_TOKEN_COSTS=false
56
+
57
+ # Whether to automatically check API keys for validity.
58
+ # Disabled by default in local development mode, but enabled in production.
59
+ # CHECK_KEYS=true
60
+
61
+ # Which model types users are allowed to access.
62
+ # The following model families are recognized:
63
+ # turbo | gpt4 | gpt4-32k | gpt4-turbo | gpt4o | o1 | dall-e | claude
64
+ # | claude-opus | gemini-flash | gemini-pro | gemini-ultra | mistral-tiny |
65
+ # | mistral-small | mistral-medium | mistral-large | aws-claude |
66
+ # | aws-claude-opus | gcp-claude | gcp-claude-opus | azure-turbo | azure-gpt4
67
+ # | azure-gpt4-32k | azure-gpt4-turbo | azure-gpt4o | azure-o1 | azure-dall-e
68
+ # | azure-gpt45 | azure-o1-mini | azure-o3-mini | deepseek | xai | o3 | o4-mini | gpt41 | gpt41-mini | gpt41-nano
69
+ # By default, all models are allowed
70
+ # To dissalow any, uncomment line below and edit
71
+ # ALLOWED_MODEL_FAMILIES=turbo,gpt4,gpt4-32k,gpt45,gpt4-turbo,gpt4o,claude,claude-opus,gemini-flash,gemini-pro,gemini-ultra,mistral-tiny,mistral-small,mistral-medium,mistral-large,aws-claude,aws-claude-opus,gcp-claude,gcp-claude-opus,azure-turbo,azure-gpt4,azure-gpt4-32k,azure-gpt4-turbo,azure-gpt4o,azure-gpt45,azure-o1-mini,azure-o3-mini,deepseek
72
+
73
+ # Which services can be used to process prompts containing images via multimodal
74
+ # models. The following services are recognized:
75
+ # openai | anthropic | aws | gcp | azure | google-ai | mistral-ai | xai
76
+ # Do not enable this feature unless all users are trusted, as you will be liable
77
+ # for any user-submitted images containing illegal content.
78
+ # By default, no image services are allowed and image prompts are rejected.
79
+ # ALLOWED_VISION_SERVICES=
80
+
81
+ # Whether prompts should be logged to Google Sheets.
82
+ # Requires additional setup. See `docs/google-sheets.md` for more information.
83
+ # PROMPT_LOGGING=false
84
+
85
+ # Specifies the number of proxies or load balancers in front of the server.
86
+ # For Cloudflare or Hugging Face deployments, the default of 1 is correct.
87
+ # For any other deployments, please see config.ts as the correct configuration
88
+ # depends on your setup. Misconfiguring this value can result in problems
89
+ # accurately tracking IP addresses and enforcing rate limits.
90
+ # TRUSTED_PROXIES=1
91
+
92
+ # Whether cookies should be set without the Secure flag, for hosts that don't
93
+ # support SSL. True by default in development, false in production.
94
+ # USE_INSECURE_COOKIES=false
95
+
96
+ # Reorganizes requests in the queue according to their token count, placing
97
+ # larger prompts further back. The penalty is determined by (promptTokens *
98
+ # TOKENS_PUNISHMENT_FACTOR). A value of 1.0 adds one second per 1000 tokens.
99
+ # When there is no queue or it is very short, the effect is negligible (this
100
+ # setting only reorders the queue, it does not artificially delay requests).
101
+ # TOKENS_PUNISHMENT_FACTOR=0.0
102
+
103
+ # Captcha verification settings. Refer to docs/pow-captcha.md for guidance.
104
+ # CAPTCHA_MODE=none
105
+ # POW_TOKEN_HOURS=24
106
+ # POW_TOKEN_MAX_IPS=2
107
+ # POW_DIFFICULTY_LEVEL=low
108
+ # POW_CHALLENGE_TIMEOUT=30
109
+
110
+ # -------------------------------------------------------------------------------
111
+ # Blocking settings:
112
+ # Allows blocking requests depending on content, referers, or IP addresses.
113
+ # This is a convenience feature; if you need more robust functionality it is
114
+ # highly recommended to put this application behind nginx or Cloudflare, as they
115
+ # will have better performance.
116
+
117
+ # IP addresses or CIDR blocks from which requests will be blocked.
118
+ # IP_BLACKLIST=10.0.0.1/24
119
+ # URLs from which requests will be blocked.
120
+ # BLOCKED_ORIGINS=reddit.com,9gag.com
121
+ # Message to show when requests are blocked.
122
+ # BLOCK_MESSAGE="You must be over the age of majority in your country to use this service."
123
+ # Destination to redirect blocked requests to.
124
+ # BLOCK_REDIRECT="https://roblox.com/"
125
+ # Comma-separated list of phrases that will be rejected. Surround phrases with
126
+ # quotes if they contain commas. You can use regular expression tokens.
127
+ # Avoid overly broad phrases as will trigger on any match in the entire prompt.
128
+ # REJECT_PHRASES="phrase one,phrase two,"phrase three, which has a comma",phrase four"
129
+ # Message to show when requests are rejected.
130
+ # REJECT_MESSAGE="You can't say that here."
131
+
132
+ # ------------------------------------------------------------------------------
133
+ # Optional settings for user management, access control, and quota enforcement:
134
+ # See `docs/user-management.md` for more information and setup instructions.
135
+ # See `docs/user-quotas.md` to learn how to set up quotas.
136
+
137
+ # Which access control method to use. (none | proxy_key | user_token)
138
+ # GATEKEEPER=none
139
+ # Which persistence method to use. (memory | firebase_rtdb | sqlite)
140
+ # GATEKEEPER_STORE=memory
141
+ # If using sqlite store, path to the SQLite database file for user data.
142
+ # Defaults to data/user-store.sqlite in the project directory.
143
+ # SQLITE_USER_STORE_PATH=data/user-store.sqlite3
144
+
145
+ # Maximum number of unique IPs a user can connect from. (0 for unlimited)
146
+ # MAX_IPS_PER_USER=0
147
+ # Whether user_tokens should be automatically disabled when reaching the IP limit.
148
+ # MAX_IPS_AUTO_BAN=true
149
+
150
+ # With user_token gatekeeper, whether to allow users to change their nickname.
151
+ # ALLOW_NICKNAME_CHANGES=true
152
+
153
+ # Default token quotas for each model family. (0 for unlimited)
154
+ # Specify as TOKEN_QUOTA_MODEL_FAMILY=value (replacing dashes with underscores).
155
+ # eg. TOKEN_QUOTA_TURBO=0, TOKEN_QUOTA_GPT4=1000000, TOKEN_QUOTA_GPT4_32K=100000
156
+ # "Tokens" for image-generation models are counted at a rate of 100000 tokens
157
+ # per US$1.00 generated, which is similar to the cost of GPT-4 Turbo.
158
+ # DALL-E 3 costs around US$0.10 per image (10000 tokens).
159
+ # See `docs/dall-e-configuration.md` for more information.
160
+ # TOKEN_QUOTA_DALL_E=0
161
+
162
+ # How often to refresh token quotas. (hourly | daily)
163
+ # Leave unset to never automatically refresh quotas.
164
+ # QUOTA_REFRESH_PERIOD=daily
165
+
166
+ # -------------------------------------------------------------------------------
167
+ # HTTP agent settings:
168
+ # If you need to change how the proxy makes requests to other servers, such
169
+ # as when checking keys or forwarding users' requests to external services,
170
+ # you can configure an alternative HTTP agent. Otherwise the default OS settings
171
+ # will be used.
172
+
173
+ # The name of the network interface to use. The first external IPv4 address
174
+ # belonging to this interface will be used for outgoing requests.
175
+ # HTTP_AGENT_INTERFACE=enp0s3
176
+
177
+ # The URL of a proxy server to use. Supports SOCKS4, SOCKS5, HTTP, and HTTPS.
178
+ # Note that if your proxy server issues a self-signed certificate, you may need
179
+ # NODE_EXTRA_CA_CERTS set to the path to your certificate. You will need to set
180
+ # that variable in your environment, not in this file.
181
+ # HTTP_AGENT_PROXY_URL=http://test:test@127.0.0.1:8000
182
+
183
+ # ------------------------------------------------------------------------------
184
+ # Secrets and keys:
185
+ # For Huggingface, set them via the Secrets section in your Space's config UI. Dp not set them in .env.
186
+ # For Render, create a "secret file" called .env using the Environment tab.
187
+
188
+ # You can add multiple API keys by separating them with a comma.
189
+ # For AWS credentials, separate the access key ID, secret key, and region with a colon.
190
+ # For GCP credentials, separate the project ID, client email, region, and private key with a colon.
191
+ OPENAI_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
192
+ ANTHROPIC_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
193
+ GOOGLE_AI_KEY=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
194
+ # See `docs/aws-configuration.md` for more information, there may be additional steps required to set up AWS.
195
+ AWS_CREDENTIALS=myaccesskeyid:mysecretkey:us-east-1,anotheraccesskeyid:anothersecretkey:us-west-2
196
+ # See `docs/azure-configuration.md` for more information, there may be additional steps required to set up Azure.
197
+ AZURE_CREDENTIALS=azure-resource-name:deployment-id:api-key,another-azure-resource-name:another-deployment-id:another-api-key
198
+ GCP_CREDENTIALS=project-id:client-email:region:private-key
199
+
200
+ # With proxy_key gatekeeper, the password users must provide to access the API.
201
+ # PROXY_KEY=your-secret-key
202
+
203
+ # With user_token gatekeeper, the admin password used to manage users.
204
+ # ADMIN_KEY=your-very-secret-key
205
+ # Restrict access to the admin interface to specific IP addresses, specified
206
+ # as a comma-separated list of CIDR ranges.
207
+ # ADMIN_WHITELIST=0.0.0.0/0
208
+
209
+ # With firebase_rtdb gatekeeper storage, the Firebase project credentials.
210
+ # FIREBASE_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
211
+ # FIREBASE_RTDB_URL=https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.firebaseio.com
212
+
213
+ # With prompt logging, the Google Sheets credentials.
214
+ # GOOGLE_SHEETS_SPREADSHEET_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
215
+ # GOOGLE_SHEETS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.gitattributes CHANGED
@@ -1 +1,35 @@
1
- nonono-main.zip filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ docs/assets/huggingface-createspace.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .aider*
2
+ .env*
3
+ !.env.vault
4
+ .venv
5
+ .vscode
6
+ .idea
7
+ build
8
+ greeting.md
9
+ node_modules
10
+ .windsurfrules
11
+ http-client.private.env.json
.husky/pre-push ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ npm run type-check
.prettierrc ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "plugins": ["prettier-plugin-ejs"],
3
+ "overrides": [
4
+ {
5
+ "files": "*.ejs",
6
+ "options": {
7
+ "printWidth": 120,
8
+ "bracketSameLine": true
9
+ }
10
+ }
11
+ ],
12
+ "trailingComma": "es5"
13
+ }
.windsurfrules ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a Senior Full Stack Developer and an Expert in ReactJS, NextJS, JavaScript, TypeScript, HTML, CSS and modern UI/UX frameworks (e.g., TailwindCSS, Shadcn, Radix). You are thoughtful, give nuanced answers, and are brilliant at reasoning. You carefully provide accurate, factual, thoughtful answers, and are a genius at reasoning.
2
+
3
+ - Follow the user’s requirements carefully & to the letter.
4
+ - First think step-by-step - describe your plan for what to build in pseudocode, written out in great detail.
5
+ - Confirm, then write code!
6
+ - Always write correct, best practice, DRY principle (Dont Repeat Yourself), bug free, fully functional and working code also it should be aligned to listed rules down below at Code Implementation Guidelines .
7
+ - Focus on easy and readability code, over being performant.
8
+ - Fully implement all requested functionality.
9
+ - Leave NO todo’s, placeholders or missing pieces.
10
+ - Ensure code is complete! Verify thoroughly finalised.
11
+ - Include all required imports, and ensure proper naming of key components.
12
+ - Be concise Minimize any other prose.
13
+ - If you think there might not be a correct answer, you say so.
14
+ - If you do not know the answer, say so, instead of guessing.
15
+
16
+ ### Coding Environment
17
+ The user asks questions about the following coding languages:
18
+ - ReactJS
19
+ - NextJS
20
+ - JavaScript
21
+ - TypeScript
22
+ - TailwindCSS
23
+ - HTML
24
+ - CSS
25
+
26
+ ### Code Implementation Guidelines
27
+ Follow these rules when you write code:
28
+ - Use early returns whenever possible to make the code more readable.
29
+ - Always use Tailwind classes for styling HTML elements; avoid using CSS or tags.
30
+ - Use “class:” instead of the tertiary operator in class tags whenever possible.
31
+ - Use descriptive variable and function/const names. Also, event functions should be named with a “handle” prefix, like “handleClick” for onClick and “handleKeyDown” for onKeyDown.
32
+ - Implement accessibility features on elements. For example, a tag should have a tabindex=“0”, aria-label, on:click, and on:keydown, and similar attributes.
33
+ - Use consts instead of functions, for example, “const toggle = () =>”. Also, define a type if possible.
CODEBASE_GUIDE.md ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Project Codebase Guide
2
+
3
+ This document serves as a guide and index for the project codebase, designed to help developers and AI agents quickly understand its structure, components, and how to contribute.
4
+
5
+ ## Table of Contents
6
+
7
+ 1. [Project Overview](#project-overview)
8
+ 2. [Directory Structure](#directory-structure)
9
+ 3. [Core Components](#core-components)
10
+ * [Configuration (`src/config.ts`)](#configuration)
11
+ * [Server Entry Point (`src/server.ts`)](#server-entry-point)
12
+ * [Proxy Layer (`src/proxy/`)](#proxy-layer)
13
+ * [User Management (`src/user/`)](#user-management)
14
+ * [Admin Interface (`src/admin/`)](#admin-interface)
15
+ * [Shared Utilities (`src/shared/`)](#shared-utilities)
16
+ 4. [Proxy Functionality](#proxy-functionality)
17
+ * [Routing (`src/proxy/routes.ts`)](#proxy-routing)
18
+ * [Supported Models & Providers](#supported-models--providers)
19
+ * [Middleware (`src/proxy/middleware/`)](#proxy-middleware)
20
+ * [Adding New Models](#adding-new-models)
21
+ * [Adding New APIs/Providers](#adding-new-apisproviders)
22
+ 5. [Model Management](#model-management)
23
+ * [Model Family Definitions](#model-family-definitions)
24
+ * [Adding OpenAI Models](#adding-openai-models)
25
+ * [Model Mapping & Routing](#model-mapping--routing)
26
+ * [Service Information](#service-information)
27
+ * [Step-by-Step Guide for Adding a New Model](#step-by-step-guide-for-adding-a-new-model)
28
+ * [Model Patterns and Versioning](#model-patterns-and-versioning)
29
+ * [Response Format Handling](#response-format-handling)
30
+ 6. [Key Management](#key-management)
31
+ * [Key Pool System](#key-pool-system)
32
+ * [Provider-Specific Key Management](#provider-specific-key-management)
33
+ * [Key Rotation and Health Checks](#key-rotation-and-health-checks)
34
+ 7. [Data Management](#data-management)
35
+ * [Database (`src/shared/database/`)](#database)
36
+ * [File Storage (`src/shared/file-storage/`)](#file-storage)
37
+ 8. [Authentication & Authorization](#authentication--authorization)
38
+ 9. [Logging & Monitoring](#logging--monitoring)
39
+ 10. [Deployment](#deployment)
40
+ 11. [Contributing](#contributing)
41
+
42
+ ## Project Overview
43
+
44
+ This project provides a proxy layer for various Large Language Models (LLMs) and potentially other AI APIs. It aims to offer a unified interface, manage API keys securely, handle rate limiting, usage tracking, and potentially add features like response caching or prompt modification.
45
+
46
+ ## Directory Structure
47
+
48
+ ```
49
+ .
50
+ ├── .env.example # Example environment variables
51
+ ├── .gitattributes # Git attributes
52
+ ├── .gitignore # Git ignore rules
53
+ ├── .husky/ # Git hooks
54
+ ├── .prettierrc # Code formatting rules
55
+ ├── CODEBASE_GUIDE.md # This file
56
+ ├── README.md # Project README
57
+ ├── data/ # Data files (e.g., SQLite DB)
58
+ ├── docker/ # Docker configuration
59
+ ├── docs/ # Documentation files
60
+ ├── http-client.env.json # HTTP client environment
61
+ ├── package-lock.json # NPM lock file
62
+ ├── package.json # Project dependencies and scripts
63
+ ├── patches/ # Patches for dependencies
64
+ ├── public/ # Static assets served by the web server
65
+ ├── render.yaml # Render deployment configuration
66
+ ├── scripts/ # Utility scripts
67
+ ├── src/ # Source code
68
+ │ ├── admin/ # Admin interface logic
69
+ │ ├── config.ts # Application configuration
70
+ │ ├── info-page.ts # Logic for the info page
71
+ │ ├── logger.ts # Logging setup
72
+ │ ├── proxy/ # Core proxy logic for different providers
73
+ │ ├── server.ts # Express server setup and main entry point
74
+ │ ├── service-info.ts # Service information logic
75
+ │ ├── shared/ # Shared utilities, types, and modules
76
+ │ └── user/ # User management logic
77
+ ├── tsconfig.json # TypeScript configuration
78
+ ```
79
+
80
+ ## Core Components
81
+
82
+ ### Configuration (`src/config.ts`)
83
+
84
+ * Loads environment variables and defines application settings.
85
+ * Contains configuration for database connections, API keys (placeholders/retrieval methods), logging levels, rate limits, etc.
86
+ * Uses `dotenv` and potentially a schema validation library (like Zod) to ensure required variables are present.
87
+
88
+ ### Server Entry Point (`src/server.ts`)
89
+
90
+ * Initializes the Express application.
91
+ * Sets up core middleware (e.g., body parsing, CORS, logging).
92
+ * Mounts routers for different parts of the application (admin, user, proxy).
93
+ * Starts the HTTP server.
94
+
95
+ ### Proxy Layer (`src/proxy/`)
96
+
97
+ * The heart of the application, handling requests to downstream AI APIs.
98
+ * Contains individual modules for each supported provider (e.g., `openai.ts`, `anthropic.ts`).
99
+ * Handles request transformation, authentication against the target API, and response handling.
100
+ * Uses middleware for common proxy tasks.
101
+
102
+ ### User Management (`src/user/`)
103
+
104
+ * Handles user registration, login, session management, and potentially API key generation/management for end-users.
105
+ * Likely interacts with the database (`src/shared/database/`).
106
+
107
+ ### Admin Interface (`src/admin/`)
108
+
109
+ * Provides an interface for administrators to manage users, monitor usage, configure settings, etc.
110
+ * May have its own set of routes and views.
111
+
112
+ ### Shared Utilities (`src/shared/`)
113
+
114
+ * Contains reusable code across different modules.
115
+ * `api-schemas/`: Zod schemas for API request/response validation.
116
+ * `database/`: Database connection, schemas (e.g., Prisma), and query logic.
117
+ * `errors.ts`: Custom error classes.
118
+ * `key-management/`: Logic for managing API keys (if applicable).
119
+ * `models.ts`: Core data models/types used throughout the application.
120
+ * `prompt-logging/`: Logic for logging prompts and responses.
121
+ * `tokenization/`: Utilities for counting tokens.
122
+ * `utils.ts`: General utility functions.
123
+
124
+ ## Proxy Functionality
125
+
126
+ ### Proxy Routing (`src/proxy/routes.ts`)
127
+
128
+ * Defines the API endpoints for the proxy service (e.g., `/v1/chat/completions`).
129
+ * Maps incoming requests to the appropriate provider-specific handler based on the request path, headers, or body content (e.g., model requested).
130
+ * Applies relevant middleware (authentication, rate limiting, queuing, etc.).
131
+
132
+ ### Supported Models & Providers
133
+
134
+ * **OpenAI:** Handled in `src/proxy/openai.ts`. Supports models like GPT-4, GPT-3.5-turbo, as well as o-series models (o1, o1-mini, o1-pro, o3, o3-mini, o3-pro, o4-mini). Handles chat completions and potentially image generation (`src/proxy/openai-image.ts`).
135
+ * **Anthropic:** Handled in `src/proxy/anthropic.ts`. Supports Claude models. May use AWS Bedrock (`src/proxy/aws-claude.ts`) or Anthropic's direct API.
136
+ * **Google AI / Vertex AI:** Handled in `src/proxy/google-ai.ts` and `src/proxy/gcp.ts`. Supports Gemini models (gemini-flash, gemini-pro, gemini-ultra).
137
+ * **Mistral AI:** Handled in `src/proxy/mistral-ai.ts`. Supports Mistral models via their API or potentially AWS (`src/proxy/aws-mistral.ts`).
138
+ * **Azure OpenAI:** Handled in `src/proxy/azure.ts`. Provides an alternative endpoint for OpenAI models via Azure.
139
+ * **Deepseek:** Handled in `src/proxy/deepseek.ts`.
140
+ * **Xai:** Handled in `src/proxy/xai.ts`.
141
+ * **AWS (General):** `src/proxy/aws.ts` might contain shared AWS logic (e.g., authentication).
142
+
143
+ ### Middleware (`src/proxy/middleware/`)
144
+
145
+ * **`gatekeeper.ts`:** Likely handles initial request validation, authentication, and authorization checks before hitting provider logic. Checks origin (`check-origin.ts`), potentially custom tokens (`check-risu-token.ts`).
146
+ * **`rate-limit.ts`:** Implements rate limiting logic, potentially per-user or per-key.
147
+ * **`queue.ts`:** Manages request queuing, possibly to handle concurrency limits or prioritize requests.
148
+
149
+ ### Adding New Models
150
+
151
+ 1. **Identify the Provider:** Determine if the new model belongs to an existing provider (e.g., a new OpenAI model) or a new one.
152
+ 2. **Update Provider Logic (if existing):**
153
+ * Modify the relevant provider file (e.g., `src/proxy/openai.ts`).
154
+ * Update model lists or logic that selects/validates models.
155
+ * Adjust any request/response transformations if the new model has a different API schema.
156
+ * Update model information in shared files like `src/shared/models.ts` if necessary.
157
+ 3. **Update Routing (if necessary):** Modify `src/proxy/routes.ts` if the new model requires a different endpoint or routing logic.
158
+ 4. **Configuration:** Add any new API keys or configuration parameters to `.env.example` and `src/config.ts`.
159
+ 5. **Testing:** Add unit or integration tests for the new model.
160
+
161
+ ### Adding New APIs/Providers
162
+
163
+ 1. **Create Provider Module:** Create a new file in `src/proxy/` (e.g., `src/proxy/new-provider.ts`).
164
+ 2. **Implement Handler:**
165
+ * Write the core logic to handle requests for this provider. This typically involves:
166
+ * Receiving the standardized request from the router.
167
+ * Transforming the request into the format expected by the new provider's API.
168
+ * Authenticating with the new provider's API (fetching keys from config).
169
+ * Making the API call (consider using a robust HTTP client like `axios` or `node-fetch`).
170
+ * Handling streaming responses if applicable (using helpers from `src/shared/streaming.ts`).
171
+ * Transforming the provider's response back into a standardized format.
172
+ * Handling errors gracefully.
173
+ 3. **Add Routing:**
174
+ * Import the new handler in `src/proxy/routes.ts`.
175
+ * Add new routes or modify existing routing logic to direct requests to the new handler based on model name, path, or other criteria.
176
+ * Apply necessary middleware (gatekeeper, rate limiter, queue).
177
+ 4. **Create Key Management:**
178
+ * Create a new directory in `src/shared/key-management/` for the provider.
179
+ * Implement provider-specific key management (key checkers, token counters).
180
+ 5. **Configuration:**
181
+ * Add configuration variables (API keys, base URLs) to `.env.example` and `src/config.ts`.
182
+ * Update `src/config.ts` to load and validate the new variables.
183
+ 6. **Model Information:** Add details about the new provider and its models to `src/shared/models.ts` or similar shared locations.
184
+ 7. **Tokenization (if applicable):** If token counting is needed, add or update tokenization logic in `src/shared/tokenization/`.
185
+ 8. **Testing:** Implement thorough tests for the new provider integration.
186
+ 9. **Documentation:** Update this guide and any other relevant documentation.
187
+
188
+ ## Model Management
189
+
190
+ ### Model Family Definitions
191
+
192
+ * **Model Family Definitions:** The project uses a family-based approach to group similar models together. These are defined in `src/shared/models.ts`.
193
+ * Each model is part of a model family (e.g., "gpt4", "claude", "gemini-pro") which helps with routing, key management, and feature support.
194
+ * The `MODEL_FAMILIES` array contains all supported model families, and the `MODEL_FAMILY_SERVICE` mapping connects each family to its provider service.
195
+
196
+ ### Adding OpenAI Models
197
+
198
+ When adding new OpenAI models to the codebase, there are several files that must be updated:
199
+
200
+ 1. **Update Model Types (`src/shared/models.ts`):**
201
+ - Add the new model to the `OpenAIModelFamily` type
202
+ - Add the model to the `MODEL_FAMILIES` array
203
+ - Add the Azure variants for the model if applicable
204
+ - Add the model to `MODEL_FAMILY_SERVICE` mapping
205
+ - Update `OPENAI_MODEL_FAMILY_MAP` with regex patterns to match the model names
206
+
207
+ 2. **Update Context Size Limits (`src/proxy/middleware/request/preprocessors/validate-context-size.ts`):**
208
+ - Add regex matching for the new model
209
+ - Set the appropriate context token limit for the model
210
+
211
+ 3. **Update Token Cost Tracking (`src/shared/stats.ts`):**
212
+ - Add pricing information for the new model in the `getTokenCostUsd` function
213
+ - Include both input and output prices in the comments for clarity
214
+
215
+ 4. **Update Feature Support Checks (`src/proxy/openai.ts`):**
216
+ - If the model supports special features like the reasoning API parameter (`isO1Model` function), update the appropriate function
217
+ - For model feature detection, prefer using regex patterns over explicit lists when possible, as this handles date-stamped versions better
218
+
219
+ 5. **Update Display Names (`src/info-page.ts`):**
220
+ - Add friendly display names for the new models in the `MODEL_FAMILY_FRIENDLY_NAME` object
221
+
222
+ 6. **Update Key Management Provider Files:**
223
+ - For OpenAI keys in `src/shared/key-management/openai/provider.ts`, add token counters for the new models
224
+ - For Azure OpenAI keys in `src/shared/key-management/azure/provider.ts`, add token counters for the Azure versions
225
+
226
+ ### Model Patterns and Versioning
227
+
228
+ The codebase handles several patterns for model naming and versioning:
229
+
230
+ 1. **Date-stamped Models:** Many models include date stamps (e.g., `gpt-4-0125-preview`). The regex patterns in `OPENAI_MODEL_FAMILY_MAP` account for these with patterns like `^gpt-4o(-\\d{4}-\\d{2}-\\d{2})?$`.
231
+
232
+ 2. **O-Series Models:** OpenAI's o-series models (o1, o1-mini, o1-pro, o3, o3-mini, o3-pro, o4-mini) follow a different naming convention. The codebase handles these with dedicated model families and regex patterns.
233
+
234
+ 3. **Preview/Non-Preview Variants:** Some models have preview variants (e.g., `gpt-4.5-preview`). The regex patterns in `OPENAI_MODEL_FAMILY_MAP` account for these with patterns like `^gpt-4\\.5(-preview)?(-\\d{4}-\\d{2}-\\d{2})?$`.
235
+
236
+ When adding new models, try to follow the existing patterns for consistency.
237
+
238
+ ### Response Format Handling
239
+
240
+ The codebase includes special handling for different API response formats:
241
+
242
+ 1. **Chat vs. Text Completions:** There's transformation logic in `openai.ts` to convert between chat completions and text completions formats (`transformTurboInstructResponse`).
243
+
244
+ 2. **Newer API Formats:** For newer APIs like the Responses API, there's transformation logic (`transformResponsesApiResponse`) to convert responses to a format compatible with existing clients.
245
+
246
+ When adding support for new models or APIs, consider whether transformation is needed to maintain compatibility with existing clients.
247
+
248
+ ## Key Management
249
+
250
+ ### Key Pool System
251
+
252
+ The project uses a sophisticated key pool system (`src/shared/key-management/key-pool.ts`) to manage API keys for different providers. Key features include:
253
+
254
+ * **Key Selection:** The system selects the appropriate key based on model family, region preferences, and other criteria.
255
+ * **Rotation:** Keys are rotated to distribute usage and avoid hitting rate limits.
256
+ * **Health Checks:** Keys are checked periodically to ensure they're still valid and within rate limits.
257
+
258
+ ### Provider-Specific Key Management
259
+
260
+ Each provider has its own key management module in `src/shared/key-management/`:
261
+
262
+ * **Key Checkers:** Each provider implements key checkers to validate keys and check their status.
263
+ * **Token Counters:** Providers implement token counting logic specific to their pricing model.
264
+ * **Models Support:** Keys are associated with specific model families they support.
265
+
266
+ When adding a new model or provider, you'll need to update or create the appropriate key management files.
267
+
268
+ ### Key Rotation and Health Checks
269
+
270
+ The key pool system includes logic for:
271
+
272
+ * **Rotation Strategy:** Keys are selected based on a prioritization strategy (`prioritize-keys.ts`).
273
+ * **Disabling Unhealthy Keys:** Keys that fail health checks are temporarily disabled.
274
+ * **Rate Limit Awareness:** The system tracks usage to avoid hitting provider rate limits.
275
+
276
+ ## Data Management
277
+
278
+ ### Database (`src/shared/database/`)
279
+
280
+ * Likely uses Prisma or a similar ORM.
281
+ * Defines database schemas (e.g., for users, API keys, usage logs).
282
+ * Provides functions for interacting with the database.
283
+ * Configuration is managed in `src/config.ts`.
284
+
285
+ ### File Storage (`src/shared/file-storage/`)
286
+
287
+ * May be used for storing logs, cached data, or user-uploaded files.
288
+ * Could integrate with local storage or cloud providers (e.g., S3, GCS).
289
+
290
+ ## Authentication & Authorization
291
+
292
+ * **User Auth:** Handled in `src/user/` potentially using sessions (`src/shared/with-session.ts`) or JWTs.
293
+ * **Proxy Auth:** The `gatekeeper.ts` middleware likely verifies incoming requests to the proxy endpoints. This could involve checking:
294
+ * Custom API keys stored in the database (`src/shared/database/`).
295
+ * Specific tokens (`check-risu-token.ts`).
296
+ * HMAC signatures (`src/shared/hmac-signing.ts`).
297
+ * Origin checks (`check-origin.ts`).
298
+ * **Downstream Auth:** Each provider module (`src/proxy/*.ts`) handles authentication with the actual AI service API using keys from the configuration.
299
+
300
+ ## Logging & Monitoring
301
+
302
+ * **Logging:** Configured in `src/logger.ts`, likely using a library like `pino` or `winston`. Logs requests, errors, and important events.
303
+ * **Prompt Logging:** Specific logic for logging prompts and responses might exist in `src/shared/prompt-logging/`.
304
+ * **Stats/Monitoring:** `src/shared/stats.ts` might handle collecting and exposing application metrics.
305
+
306
+ ## Deployment
307
+
308
+ * **Docker:** The project likely includes Docker configuration for containerized deployment.
309
+ * **Render:** The `render.yaml` file suggests the project is or can be deployed on Render.
310
+ * **Environment Variables:** The `.env.example` file provides a template for required environment variables in production.
311
+
312
+ ## Contributing
313
+
314
+ When contributing to this project:
315
+
316
+ 1. **Follow Coding Standards:** Use the established patterns and standards in the codebase. The `.prettierrc` file defines code formatting rules.
317
+ 2. **Update Documentation:** Keep this guide updated when adding new components or changing existing ones.
318
+ 3. **Add Tests:** Ensure your changes are tested appropriately.
319
+ 4. **Update Configuration:** If your changes require new environment variables, update `.env.example`.
320
+
321
+ *This guide provides a high-level overview. For detailed information, refer to the specific source code files.*
README.md CHANGED
@@ -1,10 +1,72 @@
1
- ---
2
- title: '344'
3
- emoji: 🏃
4
- colorFrom: green
5
- colorTo: gray
6
- sdk: static
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OAI Reverse Proxy - just a shitty fork
2
+ Reverse proxy server for various LLM APIs.
3
+
4
+ ### Table of Contents
5
+ <!-- TOC -->
6
+ * [OAI Reverse Proxy](#oai-reverse-proxy)
7
+ * [Table of Contents](#table-of-contents)
8
+ * [What is this?](#what-is-this)
9
+ * [Features](#features)
10
+ * [Usage Instructions](#usage-instructions)
11
+ * [Personal Use (single-user)](#personal-use-single-user)
12
+ * [Updating](#updating)
13
+ * [Local Development](#local-development)
14
+ * [Self-hosting](#self-hosting)
15
+ * [Building](#building)
16
+ * [Forking](#forking)
17
+ <!-- TOC -->
18
+
19
+ ## What is this?
20
+ This project allows you to run a reverse proxy server for various LLM APIs.
21
+
22
+ ## Features
23
+ - [x] Support for multiple APIs
24
+ - [x] [OpenAI](https://openai.com/)
25
+ - [x] [Anthropic](https://www.anthropic.com/)
26
+ - [x] [AWS Bedrock](https://aws.amazon.com/bedrock/) (Claude4 is fucked, dont care)
27
+ - [x] [Vertex AI (GCP)](https://cloud.google.com/vertex-ai/)
28
+ - [x] [Google MakerSuite/Gemini API](https://ai.google.dev/)
29
+ - [x] [Azure OpenAI](https://azure.microsoft.com/en-us/products/ai-services/openai-service)
30
+ - [x] Translation from OpenAI-formatted prompts to any other API, including streaming responses
31
+ - [x] Multiple API keys with rotation and rate limit handling
32
+ - [x] Basic user management
33
+ - [x] Simple role-based permissions
34
+ - [x] Per-model token quotas
35
+ - [x] Temporary user accounts
36
+ - [x] Event audit logging
37
+ - [x] Optional full logging of prompts and completions
38
+ - [x] Abuse detection and prevention
39
+ - [x] IP address and user token model invocation rate limits
40
+ - [x] IP blacklists
41
+ - [x] Proof-of-work challenge for access by anonymous users
42
+
43
+ ## Usage Instructions
44
+ If you'd like to run your own instance of this server, you'll need to deploy it somewhere and configure it with your API keys. A few easy options are provided below, though you can also deploy it to any other service you'd like if you know what you're doing and the service supports Node.js.
45
+
46
+ ### Personal Use (single-user)
47
+ If you just want to run the proxy server to use yourself without hosting it for others:
48
+ 1. Install [Node.js](https://nodejs.org/en/download/) >= 18.0.0
49
+ 2. Clone this repository
50
+ 3. Create a `.env` file in the root of the project and add your API keys. See the [.env.example](./.env.example) file for an example.
51
+ 4. Install dependencies with `npm install`
52
+ 5. Run `npm run build`
53
+ 6. Run `npm start`
54
+
55
+ #### Updating
56
+ You must re-run `npm install` and `npm run build` whenever you pull new changes from the repository.
57
+
58
+ #### Local Development
59
+ Use `npm run start:dev` to run the proxy in development mode with watch mode enabled. Use `npm run type-check` to run the type checker across the project.
60
+
61
+ ### Self-hosting
62
+ [See here for instructions on how to self-host the application on your own VPS or local machine and expose it to the internet for others to use.](./docs/self-hosting.md)
63
+
64
+ **Ensure you set the `TRUSTED_PROXIES` environment variable according to your deployment.** Refer to [.env.example](./.env.example) and [config.ts](./src/config.ts) for more information.
65
+
66
+ ## Building
67
+ To build the project, run `npm run build`. This will compile the TypeScript code to JavaScript and output it to the `build` directory. You should run this whenever you pull new changes from the repository.
68
+
69
+ Note that if you are trying to build the server on a very memory-constrained (<= 1GB) VPS, you may need to run the build with `NODE_OPTIONS=--max_old_space_size=2048 npm run build` to avoid running out of memory during the build process, assuming you have swap enabled. The application itself should run fine on a 512MB VPS for most reasonable traffic levels.
70
+
71
+ ## Forking
72
+ If you are forking the repository on GitGud, you may wish to disable GitLab CI/CD or you will be spammed with emails about failed builds due not having any CI runners. You can do this by going to *Settings > General > Visibility, project features, permissions* and then disabling the "CI/CD" feature.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ *
2
+ !.gitkeep
data/user-files/.gitkeep ADDED
File without changes
docker/ci/.gitlab-ci.yml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ stages:
2
+ - build
3
+
4
+ build_image:
5
+ stage: build
6
+ image:
7
+ name: gcr.io/kaniko-project/executor:debug
8
+ entrypoint: [""]
9
+ script:
10
+ - |
11
+ if [ "$CI_COMMIT_REF_NAME" = "main" ]; then
12
+ TAG="latest"
13
+ else
14
+ TAG=$CI_COMMIT_REF_NAME
15
+ fi
16
+ - echo "Building image with tag $TAG"
17
+ - BASE64_AUTH=$(echo -n "$DOCKER_HUB_USERNAME:$DOCKER_HUB_ACCESS_TOKEN" | base64)
18
+ - echo "{\"auths\":{\"https://index.docker.io/v1/\":{\"auth\":\"$BASE64_AUTH\"}}}" > /kaniko/.docker/config.json
19
+ - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/ci/Dockerfile --destination docker.io/khanonci/oai-reverse-proxy:$TAG --build-arg CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME --build-arg CI_COMMIT_SHA=$CI_COMMIT_SHA --build-arg CI_PROJECT_PATH=$CI_PROJECT_PATH
20
+ only:
21
+ - main
docker/ci/Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:18-bullseye-slim
2
+
3
+ WORKDIR /app
4
+ COPY . .
5
+
6
+ RUN npm ci
7
+ RUN npm run build
8
+ RUN npm prune --production
9
+
10
+ EXPOSE 7860
11
+ ENV PORT=7860
12
+ ENV NODE_ENV=production
13
+
14
+ ARG CI_COMMIT_REF_NAME
15
+ ARG CI_COMMIT_SHA
16
+ ARG CI_PROJECT_PATH
17
+
18
+ ENV GITGUD_BRANCH=$CI_COMMIT_REF_NAME
19
+ ENV GITGUD_COMMIT=$CI_COMMIT_SHA
20
+ ENV GITGUD_PROJECT=$CI_PROJECT_PATH
21
+
22
+ CMD [ "npm", "start" ]
docker/docker-compose-selfhost.yml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Before running this, create a .env and greeting.md file.
2
+ # Refer to .env.example for the required environment variables.
3
+ # User-generated content is stored in the data directory.
4
+ # When self-hosting, it's recommended to run this behind a reverse proxy like
5
+ # nginx or Caddy to handle SSL/TLS and rate limiting. Refer to
6
+ # docs/self-hosting.md for more information and an example nginx config.
7
+ version: '3.8'
8
+ services:
9
+ oai-reverse-proxy:
10
+ image: khanonci/oai-reverse-proxy:latest
11
+ ports:
12
+ - "127.0.0.1:7860:7860"
13
+ env_file:
14
+ - ./.env
15
+ volumes:
16
+ - ./greeting.md:/app/greeting.md
17
+ - ./data:/app/data
docker/huggingface/Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:18-bullseye-slim
2
+ RUN apt-get update && \
3
+ apt-get install -y git
4
+ RUN git clone https://gitgud.io/khanon/oai-reverse-proxy.git /app
5
+ WORKDIR /app
6
+ RUN chown -R 1000:1000 /app
7
+ USER 1000
8
+ RUN npm install
9
+ COPY Dockerfile greeting.md* .env* ./
10
+ RUN npm run build
11
+ EXPOSE 7860
12
+ ENV NODE_ENV=production
13
+ # Huggigface free VMs have 16GB of RAM so we can be greedy
14
+ ENV NODE_OPTIONS="--max-old-space-size=12882"
15
+ CMD [ "npm", "start" ]
docker/render/Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # syntax = docker/dockerfile:1.2
2
+
3
+ FROM node:18-bullseye-slim
4
+ RUN apt-get update && \
5
+ apt-get install -y curl
6
+
7
+ # Unlike Huggingface, Render can only deploy straight from a git repo and
8
+ # doesn't allow you to create or modify arbitrary files via the web UI.
9
+ # To use a greeting file, set `GREETING_URL` to a URL that points to a raw
10
+ # text file containing your greeting, such as a GitHub Gist.
11
+
12
+ # You may need to clear the build cache if you change the greeting, otherwise
13
+ # Render will use the cached layer from the previous build.
14
+
15
+ WORKDIR /app
16
+ ARG GREETING_URL
17
+ RUN if [ -n "$GREETING_URL" ]; then \
18
+ curl -sL "$GREETING_URL" > greeting.md; \
19
+ fi
20
+ COPY . .
21
+ RUN npm install
22
+ RUN npm run build
23
+ RUN --mount=type=secret,id=_env,dst=/etc/secrets/.env cat /etc/secrets/.env >> .env
24
+ EXPOSE 10000
25
+ ENV NODE_ENV=production
26
+ CMD [ "npm", "start" ]
docs/assets/aws-request-model-access.png ADDED
docs/assets/huggingface-createspace.png ADDED

Git LFS Details

  • SHA256: dcb1aa84ef28c7bd228776d8c3fcafa7b006b2f52f4fe2da7b5e9a6e4c9b0931
  • Pointer size: 131 Bytes
  • Size of remote file: 157 kB
docs/assets/huggingface-dockerfile.png ADDED
docs/assets/huggingface-savedockerfile.png ADDED
docs/assets/openapi-admin-users.yaml ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ openapi: 3.0.0
3
+ info:
4
+ version: 1.0.0
5
+ title: User Management API
6
+ paths:
7
+ /admin/users:
8
+ get:
9
+ summary: List all users
10
+ operationId: getUsers
11
+ responses:
12
+ "200":
13
+ description: A list of users
14
+ content:
15
+ application/json:
16
+ schema:
17
+ type: object
18
+ properties:
19
+ users:
20
+ type: array
21
+ items:
22
+ $ref: "#/components/schemas/User"
23
+ count:
24
+ type: integer
25
+ format: int32
26
+ post:
27
+ summary: Create a new user
28
+ operationId: createUser
29
+ requestBody:
30
+ content:
31
+ application/json:
32
+ schema:
33
+ oneOf:
34
+ - type: object
35
+ properties:
36
+ type:
37
+ type: string
38
+ enum: ["normal", "special"]
39
+ - type: object
40
+ properties:
41
+ type:
42
+ type: string
43
+ enum: ["temporary"]
44
+ expiresAt:
45
+ type: integer
46
+ format: int64
47
+ tokenLimits:
48
+ $ref: "#/components/schemas/TokenCount"
49
+ responses:
50
+ "200":
51
+ description: The created user's token
52
+ content:
53
+ application/json:
54
+ schema:
55
+ type: object
56
+ properties:
57
+ token:
58
+ type: string
59
+ put:
60
+ summary: Bulk upsert users
61
+ operationId: bulkUpsertUsers
62
+ requestBody:
63
+ content:
64
+ application/json:
65
+ schema:
66
+ type: object
67
+ properties:
68
+ users:
69
+ type: array
70
+ items:
71
+ $ref: "#/components/schemas/User"
72
+ responses:
73
+ "200":
74
+ description: The upserted users
75
+ content:
76
+ application/json:
77
+ schema:
78
+ type: object
79
+ properties:
80
+ upserted_users:
81
+ type: array
82
+ items:
83
+ $ref: "#/components/schemas/User"
84
+ count:
85
+ type: integer
86
+ format: int32
87
+ "400":
88
+ description: Bad request
89
+ content:
90
+ application/json:
91
+ schema:
92
+ type: object
93
+ properties:
94
+ error:
95
+ type: string
96
+
97
+ /admin/users/{token}:
98
+ get:
99
+ summary: Get a user by token
100
+ operationId: getUser
101
+ parameters:
102
+ - name: token
103
+ in: path
104
+ required: true
105
+ schema:
106
+ type: string
107
+ responses:
108
+ "200":
109
+ description: A user
110
+ content:
111
+ application/json:
112
+ schema:
113
+ $ref: "#/components/schemas/User"
114
+ "404":
115
+ description: Not found
116
+ content:
117
+ application/json:
118
+ schema:
119
+ type: object
120
+ properties:
121
+ error:
122
+ type: string
123
+ put:
124
+ summary: Update a user by token
125
+ operationId: upsertUser
126
+ parameters:
127
+ - name: token
128
+ in: path
129
+ required: true
130
+ schema:
131
+ type: string
132
+ requestBody:
133
+ content:
134
+ application/json:
135
+ schema:
136
+ $ref: "#/components/schemas/User"
137
+ responses:
138
+ "200":
139
+ description: The updated user
140
+ content:
141
+ application/json:
142
+ schema:
143
+ $ref: "#/components/schemas/User"
144
+ "400":
145
+ description: Bad request
146
+ content:
147
+ application/json:
148
+ schema:
149
+ type: object
150
+ properties:
151
+ error:
152
+ type: string
153
+ delete:
154
+ summary: Disables the user with the given token
155
+ description: Optionally accepts a `disabledReason` query parameter. Returns the disabled user.
156
+ parameters:
157
+ - in: path
158
+ name: token
159
+ required: true
160
+ schema:
161
+ type: string
162
+ description: The token of the user to disable
163
+ - in: query
164
+ name: disabledReason
165
+ required: false
166
+ schema:
167
+ type: string
168
+ description: The reason for disabling the user
169
+ responses:
170
+ '200':
171
+ description: The disabled user
172
+ content:
173
+ application/json:
174
+ schema:
175
+ $ref: '#/components/schemas/User'
176
+ '400':
177
+ description: Bad request
178
+ content:
179
+ application/json:
180
+ schema:
181
+ type: object
182
+ properties:
183
+ error:
184
+ type: string
185
+ '404':
186
+ description: Not found
187
+ content:
188
+ application/json:
189
+ schema:
190
+ type: object
191
+ properties:
192
+ error:
193
+ type: string
194
+ components:
195
+ schemas:
196
+ TokenCount:
197
+ type: object
198
+ properties:
199
+ turbo:
200
+ type: integer
201
+ format: int32
202
+ gpt4:
203
+ type: integer
204
+ format: int32
205
+ "gpt4-32k":
206
+ type: integer
207
+ format: int32
208
+ claude:
209
+ type: integer
210
+ format: int32
211
+ User:
212
+ type: object
213
+ properties:
214
+ token:
215
+ type: string
216
+ ip:
217
+ type: array
218
+ items:
219
+ type: string
220
+ nickname:
221
+ type: string
222
+ type:
223
+ type: string
224
+ enum: ["normal", "special"]
225
+ promptCount:
226
+ type: integer
227
+ format: int32
228
+ tokenLimits:
229
+ $ref: "#/components/schemas/TokenCount"
230
+ tokenCounts:
231
+ $ref: "#/components/schemas/TokenCount"
232
+ createdAt:
233
+ type: integer
234
+ format: int64
235
+ lastUsedAt:
236
+ type: integer
237
+ format: int64
238
+ disabledAt:
239
+ type: integer
240
+ format: int64
241
+ disabledReason:
242
+ type: string
243
+ expiresAt:
244
+ type: integer
245
+ format: int64
docs/aws-configuration.md ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuring the proxy for AWS Bedrock
2
+
3
+ The proxy supports AWS Bedrock models via the `/proxy/aws/claude` endpoint. There are a few extra steps necessary to use AWS Bedrock compared to the other supported APIs.
4
+
5
+ - [Setting keys](#setting-keys)
6
+ - [Attaching policies](#attaching-policies)
7
+ - [Provisioning models](#provisioning-models)
8
+ - [Note regarding logging](#note-regarding-logging)
9
+
10
+ ## Setting keys
11
+
12
+ Use the `AWS_CREDENTIALS` environment variable to set the AWS API keys.
13
+
14
+ Like other APIs, you can provide multiple keys separated by commas. Each AWS key, however, is a set of credentials including the access key, secret key, and region. These are separated by a colon (`:`).
15
+
16
+ For example:
17
+
18
+ ```
19
+ AWS_CREDENTIALS=AKIA000000000000000:somesecretkey:us-east-1,AKIA111111111111111:anothersecretkey:us-west-2
20
+ ```
21
+
22
+ ## Attaching policies
23
+
24
+ Unless your credentials belong to the root account, the principal will need to be granted the following permissions:
25
+
26
+ - `bedrock:InvokeModel`
27
+ - `bedrock:InvokeModelWithResponseStream`
28
+ - `bedrock:GetModelInvocationLoggingConfiguration`
29
+ - The proxy needs this to determine whether prompt/response logging is enabled. By default, the proxy won't use credentials unless it can conclusively determine that logging is disabled, for privacy reasons.
30
+
31
+ Use the IAM console or the AWS CLI to attach these policies to the principal associated with the credentials.
32
+
33
+ ## Provisioning models
34
+
35
+ AWS does not automatically provide accounts with access to every model. You will need to provision the models you want to use, in the regions you want to use them in. You can do this from the AWS console.
36
+
37
+ ⚠️ **Models are region-specific.** Currently AWS only offers Claude in a small number of regions. Switch to the AWS region you want to use, then go to the models page and request access to **Anthropic / Claude**.
38
+
39
+ ![](./assets/aws-request-model-access.png)
40
+
41
+ Access is generally granted more or less instantly. Once your account has access, you can enable the model by checking the box next to it.
42
+
43
+ You can also request Claude Instant, but support for this isn't fully implemented yet.
44
+
45
+ ### Supported model IDs
46
+ Users can send these model IDs to the proxy to invoke the corresponding models.
47
+ - **Claude**
48
+ - `anthropic.claude-v1` (~18k context, claude 1.3 -- EOL 2024-02-28)
49
+ - `anthropic.claude-v2` (~100k context, claude 2.0)
50
+ - `anthropic.claude-v2:1` (~200k context, claude 2.1)
51
+ - **Claude Instant**
52
+ - `anthropic.claude-instant-v1` (~100k context, claude instant 1.2)
53
+
54
+ ## Note regarding logging
55
+
56
+ By default, the proxy will refuse to use keys if it finds that logging is enabled, or if it doesn't have permission to check logging status.
57
+
58
+ If you can't attach the `bedrock:GetModelInvocationLoggingConfiguration` policy to the principal, you can set the `ALLOW_AWS_LOGGING` environment variable to `true` to force the proxy to use the keys anyway. A warning will appear on the info page when this is enabled.
docs/azure-configuration.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuring the proxy for Azure
2
+
3
+ The proxy supports Azure OpenAI Service via the `/proxy/azure/openai` endpoint. The process of setting it up is slightly different from regular OpenAI.
4
+
5
+ - [Setting keys](#setting-keys)
6
+ - [Model assignment](#model-assignment)
7
+
8
+ ## Setting keys
9
+
10
+ Use the `AZURE_CREDENTIALS` environment variable to set the Azure API keys.
11
+
12
+ Like other APIs, you can provide multiple keys separated by commas. Each Azure key, however, is a set of values including the Resource Name, Deployment ID, and API key. These are separated by a colon (`:`).
13
+
14
+ For example:
15
+ ```
16
+ AZURE_CREDENTIALS=contoso-ml:gpt4-8k:0123456789abcdef0123456789abcdef,northwind-corp:testdeployment:0123456789abcdef0123456789abcdef
17
+ ```
18
+
19
+ ## Model assignment
20
+ Note that each Azure deployment is assigned a model when you create it in the Azure OpenAI Service portal. If you want to use a different model, you'll need to create a new deployment, and therefore a new key to be added to the AZURE_CREDENTIALS environment variable. Each credential only grants access to one model.
21
+
22
+ ### Supported model IDs
23
+ Users can send normal OpenAI model IDs to the proxy to invoke the corresponding models. For the most part they work the same with Azure. GPT-3.5 Turbo has an ID of "gpt-35-turbo" because Azure doesn't allow periods in model names, but the proxy should automatically convert this to the correct ID.
24
+
25
+ As noted above, you can only use model IDs for which a deployment has been created and added to the proxy.
26
+
27
+ ## On content filtering
28
+ Be aware that all Azure OpenAI Service deployments have content filtering enabled by default at a Medium level. Prompts or responses which are deemed to be inappropriate will be rejected by the API. This is a feature of the Azure OpenAI Service and not the proxy.
29
+
30
+ You can disable this from deployment's settings within Azure, but you would need to request an exemption from Microsoft for your organization first. See [this page](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/content-filters) for more information.
docs/dall-e-configuration.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuring the proxy for DALL-E
2
+
3
+ The proxy supports DALL-E 2 and DALL-E 3 image generation via the `/proxy/openai-images` endpoint. By default it is disabled as it is somewhat expensive and potentially more open to abuse than text generation.
4
+
5
+ - [Updating your Dockerfile](#updating-your-dockerfile)
6
+ - [Enabling DALL-E](#enabling-dall-e)
7
+ - [Setting quotas](#setting-quotas)
8
+ - [Rate limiting](#rate-limiting)
9
+
10
+ ## Updating your Dockerfile
11
+ If you are using a previous version of the Dockerfile supplied with the proxy, it doesn't have the necessary permissions to let the proxy save temporary files.
12
+
13
+ You can replace the entire thing with the new Dockerfile at [./docker/huggingface/Dockerfile](../docker/huggingface/Dockerfile) (or the equivalent for Render deployments).
14
+
15
+ You can also modify your existing Dockerfile; just add the following lines after the `WORKDIR` line:
16
+
17
+ ```Dockerfile
18
+ # Existing
19
+ RUN git clone https://gitgud.io/khanon/oai-reverse-proxy.git /app
20
+ WORKDIR /app
21
+
22
+ # Take ownership of the app directory and switch to the non-root user
23
+ RUN chown -R 1000:1000 /app
24
+ USER 1000
25
+
26
+ # Existing
27
+ RUN npm install
28
+ ```
29
+
30
+ ## Enabling DALL-E
31
+ Add `dall-e` to the `ALLOWED_MODEL_FAMILIES` environment variable to enable DALL-E. For example:
32
+
33
+ ```
34
+ # GPT3.5 Turbo, GPT-4, GPT-4 Turbo, and DALL-E
35
+ ALLOWED_MODEL_FAMILIES=turbo,gpt-4,gpt-4turbo,dall-e
36
+
37
+ # All models as of this writing
38
+ ALLOWED_MODEL_FAMILIES=turbo,gpt4,gpt4-32k,gpt4-turbo,claude,gemini-pro,aws-claude,dall-e
39
+ ```
40
+
41
+ Refer to [.env.example](../.env.example) for a full list of supported model families. You can add `dall-e` to that list to enable all models.
42
+
43
+ ## Setting quotas
44
+ DALL-E doesn't bill by token like text generation models. Instead there is a fixed cost per image generated, depending on the model, image size, and selected quality.
45
+
46
+ The proxy still uses tokens to set quotas for users. The cost for each generated image will be converted to "tokens" at a rate of 100000 tokens per US$1.00. This works out to a similar cost-per-token as GPT-4 Turbo, so you can use similar token quotas for both.
47
+
48
+ Use `TOKEN_QUOTA_DALL_E` to set the default quota for image generation. Otherwise it works the same as token quotas for other models.
49
+
50
+ ```
51
+ # ~50 standard DALL-E images per refresh period, or US$2.00
52
+ TOKEN_QUOTA_DALL_E=200000
53
+ ```
54
+
55
+ Refer to [https://openai.com/pricing](https://openai.com/pricing) for the latest pricing information. As of this writing, the cheapest DALL-E 3 image costs $0.04 per generation, which works out to 4000 tokens. Higher resolution and quality settings can cost up to $0.12 per image, or 12000 tokens.
56
+
57
+ ## Rate limiting
58
+ The old `MODEL_RATE_LIMIT` setting has been split into `TEXT_MODEL_RATE_LIMIT` and `IMAGE_MODEL_RATE_LIMIT`. Whatever value you previously set for `MODEL_RATE_LIMIT` will be used for text models.
59
+
60
+ If you don't specify a `IMAGE_MODEL_RATE_LIMIT`, it defaults to half of the `TEXT_MODEL_RATE_LIMIT`, to a minimum of 1 image per minute.
61
+
62
+ ```
63
+ # 4 text generations per minute, 2 images per minute
64
+ TEXT_MODEL_RATE_LIMIT=4
65
+ IMAGE_MODEL_RATE_LIMIT=2
66
+ ```
67
+
68
+ If a prompt is filtered by OpenAI's content filter, it won't count towards the rate limit.
69
+
70
+ ## Hiding recent images
71
+ By default, the proxy shows the last 12 recently generated images by users. You can hide this section by setting `SHOW_RECENT_IMAGES` to `false`.
docs/deploy-huggingface.md ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Deploy to Huggingface Space
2
+
3
+ **⚠️ This method is no longer recommended. Please use the [self-hosting instructions](./self-hosting.md) instead.**
4
+
5
+ This repository can be deployed to a [Huggingface Space](https://huggingface.co/spaces). This is a free service that allows you to run a simple server in the cloud. You can use it to safely share your OpenAI API key with a friend.
6
+
7
+ ### 1. Get an API key
8
+ - Go to [OpenAI](https://openai.com/) and sign up for an account. You can use a free trial key for this as long as you provide SMS verification.
9
+ - Claude is not publicly available yet, but if you have access to it via the [Anthropic](https://www.anthropic.com/) closed beta, you can also use that key with the proxy.
10
+
11
+ ### 2. Create an empty Huggingface Space
12
+ - Go to [Huggingface](https://huggingface.co/) and sign up for an account.
13
+ - Once logged in, [create a new Space](https://huggingface.co/new-space).
14
+ - Provide a name for your Space and select "Docker" as the SDK. Select "Blank" for the template.
15
+ - Click "Create Space" and wait for the Space to be created.
16
+
17
+ ![Create Space](assets/huggingface-createspace.png)
18
+
19
+ ### 3. Create an empty Dockerfile
20
+ - Once your Space is created, you'll see an option to "Create the Dockerfile in your browser". Click that link.
21
+
22
+ ![Create Dockerfile](assets/huggingface-dockerfile.png)
23
+ - Paste the following into the text editor and click "Save".
24
+ ```dockerfile
25
+ FROM node:18-bullseye-slim
26
+ RUN apt-get update && \
27
+ apt-get install -y git
28
+ RUN git clone https://gitgud.io/khanon/oai-reverse-proxy.git /app
29
+ WORKDIR /app
30
+ RUN chown -R 1000:1000 /app
31
+ USER 1000
32
+ RUN npm install
33
+ COPY Dockerfile greeting.md* .env* ./
34
+ RUN npm run build
35
+ EXPOSE 7860
36
+ ENV NODE_ENV=production
37
+ ENV NODE_OPTIONS="--max-old-space-size=12882"
38
+ CMD [ "npm", "start" ]
39
+ ```
40
+ - Click "Commit new file to `main`" to save the Dockerfile.
41
+
42
+ ![Commit](assets/huggingface-savedockerfile.png)
43
+
44
+ ### 4. Set your API key as a secret
45
+ - Click the Settings button in the top right corner of your repository.
46
+ - Scroll down to the `Repository Secrets` section and click `New Secret`.
47
+
48
+ ![Secrets](https://files.catbox.moe/irrp2p.png)
49
+
50
+ - Enter `OPENAI_KEY` as the name and your OpenAI API key as the value.
51
+ - For Claude, set `ANTHROPIC_KEY` instead.
52
+ - You can use both types of keys at the same time if you want.
53
+
54
+ ![New Secret](https://files.catbox.moe/ka6s1a.png)
55
+
56
+ ### 5. Deploy the server
57
+ - Your server should automatically deploy when you add the secret, but if not you can select `Factory Reboot` from that same Settings menu.
58
+
59
+ ### 6. Share the link
60
+ - The Service Info section below should show the URL for your server. You can share this with anyone to safely give them access to your API key.
61
+ - Your friend doesn't need any API key of their own, they just need your link.
62
+
63
+ # Optional
64
+
65
+ ## Updating the server
66
+
67
+ To update your server, go to the Settings menu and select `Factory Reboot`. This will pull the latest version of the code from GitHub and restart the server.
68
+
69
+ Note that if you just perform a regular Restart, the server will be restarted with the same code that was running before.
70
+
71
+ ## Adding a greeting message
72
+
73
+ You can create a Markdown file called `greeting.md` to display a message on the Server Info page. This is a good place to put instructions for how to use the server.
74
+
75
+ ## Customizing the server
76
+
77
+ The server will be started with some default configuration, but you can override it by adding a `.env` file to your Space. You can use Huggingface's web editor to create a new `.env` file alongside your Dockerfile. Huggingface will restart your server automatically when you save the file.
78
+
79
+ Here are some example settings:
80
+ ```shell
81
+ # Requests per minute per IP address
82
+ MODEL_RATE_LIMIT=4
83
+ # Max tokens to request from OpenAI
84
+ MAX_OUTPUT_TOKENS_OPENAI=256
85
+ # Max tokens to request from Anthropic (Claude)
86
+ MAX_OUTPUT_TOKENS_ANTHROPIC=512
87
+ # Block prompts containing disallowed characters
88
+ REJECT_DISALLOWED=false
89
+ REJECT_MESSAGE="This content violates /aicg/'s acceptable use policy."
90
+ ```
91
+
92
+ See `.env.example` for a full list of available settings, or check `config.ts` for details on what each setting does.
93
+
94
+ ## Restricting access to the server
95
+
96
+ If you want to restrict access to the server, you can set a `PROXY_KEY` secret. This key will need to be passed in the Authentication header of every request to the server, just like an OpenAI API key. Set the `GATEKEEPER` mode to `proxy_key`, and then set the `PROXY_KEY` variable to whatever password you want.
97
+
98
+ Add this using the same method as the OPENAI_KEY secret above. Don't add this to your `.env` file because that file is public and anyone can see it.
99
+
100
+ Example:
101
+ ```
102
+ GATEKEEPER=proxy_key
103
+ PROXY_KEY=your_secret_password
104
+ ```
docs/deploy-render.md ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Deploy to Render.com
2
+
3
+ **⚠️ This method is no longer supported or recommended and may not work. Please use the [self-hosting instructions](./self-hosting.md) instead.**
4
+
5
+ Render.com offers a free tier that includes 750 hours of compute time per month. This is enough to run a single proxy instance 24/7. Instances shut down after 15 minutes without traffic but start up again automatically when a request is received. You can use something like https://app.checklyhq.com/ to ping your proxy every 15 minutes to keep it alive.
6
+
7
+ ### 1. Create account
8
+ - [Sign up for Render.com](https://render.com/) to create an account and access the dashboard.
9
+
10
+ ### 2. Create a service using a Blueprint
11
+ Render allows you to deploy and auutomatically configure a repository containing a [render.yaml](../render.yaml) file using its Blueprints feature. This is the easiest way to get started.
12
+
13
+ - Click the **Blueprints** tab at the top of the dashboard.
14
+ - Click **New Blueprint Instance**.
15
+ - Under **Public Git repository**, enter `https://gitlab.com/khanon/oai-proxy`.
16
+ - Note that this is not the GitGud repository, but a mirror on GitLab.
17
+ - Click **Continue**.
18
+ - Under **Blueprint Name**, enter a name.
19
+ - Under **Branch**, enter `main`.
20
+ - Click **Apply**.
21
+
22
+ The service will be created according to the instructions in the `render.yaml` file. Don't wait for it to complete as it will fail due to missing environment variables. Instead, proceed to the next step.
23
+
24
+ ### 3. Set environment variables
25
+ - Return to the **Dashboard** tab.
26
+ - Click the name of the service you just created, which may show as "Deploy failed".
27
+ - Click the **Environment** tab.
28
+ - Click **Add Secret File**.
29
+ - Under **Filename**, enter `.env`.
30
+ - Under **Contents**, enter all of your environment variables, one per line, in the format `NAME=value`.
31
+ - For example, `OPENAI_KEY=sk-abc123`.
32
+ - Click **Save Changes**.
33
+
34
+ **IMPORTANT:** Set `TRUSTED_PROXIES=3`, otherwise users' IP addresses will not be recorded correctly (the server will see the IP address of Render's load balancer instead of the user's real IP address).
35
+
36
+ The service will automatically rebuild and deploy with the new environment variables. This will take a few minutes. The link to your deployed proxy will appear at the top of the page.
37
+
38
+ If you want to change the URL, go to the **Settings** tab of your Web Service and click the **Edit** button next to **Name**. You can also set a custom domain, though I haven't tried this yet.
39
+
40
+ # Optional
41
+
42
+ ## Updating the server
43
+
44
+ To update your server, go to the page for your Web Service and click **Manual Deploy** > **Deploy latest commit**. This will pull the latest version of the code and redeploy the server.
45
+
46
+ _If you have trouble with this, you can also try selecting **Clear build cache & deploy** instead from the same menu._
47
+
48
+ ## Adding a greeting message
49
+
50
+ To show a greeting message on the Server Info page, set the `GREETING_URL` environment variable within Render to the URL of a Markdown file. This URL should point to a raw text file, not an HTML page. You can use a public GitHub Gist or GitLab Snippet for this. For example: `GREETING_URL=https://gitlab.com/-/snippets/2542011/raw/main/greeting.md`. You can change the title of the page by setting the `SERVER_TITLE` environment variable.
51
+
52
+ Don't set `GREETING_URL` in the `.env` secret file you created earlier; it must be set in Render's environment variables section for it to work correctly.
53
+
54
+ ## Customizing the server
55
+
56
+ You can customize the server by editing the `.env` configuration you created earlier. Refer to [.env.example](../.env.example) for a list of all available configuration options. Further information can be found in the [config.ts](../src/config.ts) file.
docs/gcp-configuration.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuring the proxy for Vertex AI (GCP)
2
+
3
+ The proxy supports GCP models via the `/proxy/gcp/claude` endpoint. There are a few extra steps necessary to use GCP compared to the other supported APIs.
4
+
5
+ - [Setting keys](#setting-keys)
6
+ - [Setup Vertex AI](#setup-vertex-ai)
7
+ - [Supported model IDs](#supported-model-ids)
8
+
9
+ ## Setting keys
10
+
11
+ Use the `GCP_CREDENTIALS` environment variable to set the GCP API keys.
12
+
13
+ Like other APIs, you can provide multiple keys separated by commas. Each GCP key, however, is a set of credentials including the project id, client email, region and private key. These are separated by a colon (`:`).
14
+
15
+ For example:
16
+
17
+ ```
18
+ GCP_CREDENTIALS=my-first-project:xxx@yyy.com:us-east5:-----BEGIN PRIVATE KEY-----xxx-----END PRIVATE KEY-----,my-first-project2:xxx2@yyy.com:us-east5:-----BEGIN PRIVATE KEY-----xxx-----END PRIVATE KEY-----
19
+ ```
20
+
21
+ ## Setup Vertex AI
22
+ 1. Go to [https://cloud.google.com/vertex-ai](https://cloud.google.com/vertex-ai) and sign up for a GCP account. ($150 free credits without credit card or $300 free credits with credit card, credits expire in 90 days)
23
+ 2. Go to [https://console.cloud.google.com/marketplace/product/google/aiplatform.googleapis.com](https://console.cloud.google.com/marketplace/product/google/aiplatform.googleapis.com) to enable Vertex AI API.
24
+ 3. Go to [https://console.cloud.google.com/vertex-ai](https://console.cloud.google.com/vertex-ai) and navigate to Model Garden to apply for access to the Claude models.
25
+ 4. Create a [Service Account](https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create?walkthrough_id=iam--create-service-account#step_index=1) , and make sure to grant the role of "Vertex AI User" or "Vertex AI Administrator".
26
+ 5. On the service account page you just created, create a new key and select "JSON". The JSON file will be downloaded automatically.
27
+ 6. The required credential is in the JSON file you just downloaded.
28
+
29
+ ## Supported model IDs
30
+ Users can send these model IDs to the proxy to invoke the corresponding models.
31
+ - **Claude**
32
+ - `claude-3-haiku@20240307`
33
+ - `claude-3-sonnet@20240229`
34
+ - `claude-3-opus@20240229`
35
+ - `claude-3-5-sonnet@20240620`
docs/logging-sheets.md ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Warning
2
+ **I strongly suggest against using this feature with a Google account that you care about.** Depending on the content of the prompts people submit, Google may flag the spreadsheet as containing inappropriate content. This seems to prevent you from sharing that spreadsheet _or any others on the account. This happened with my throwaway account during testing; the existing shared spreadsheet continues to work but even completely new spreadsheets are flagged and cannot be shared.
3
+
4
+ I'll be looking into alternative storage backends but you should not use this implementation with a Google account you care about, or even one remotely connected to your main accounts (as Google has a history of linking accounts together via IPs/browser fingerprinting). Use a VPN and completely isolated VM to be safe.
5
+
6
+ # Configuring Google Sheets Prompt Logging
7
+ This proxy can log incoming prompts and model responses to Google Sheets. Some configuration on the Google side is required to enable this feature. The APIs used are free, but you will need a Google account and a Google Cloud Platform project.
8
+
9
+ NOTE: Concurrency is not supported. Don't connect two instances of the server to the same spreadsheet or bad things will happen.
10
+
11
+ ## Prerequisites
12
+ - A Google account
13
+ - **USE A THROWAWAY ACCOUNT!**
14
+ - A Google Cloud Platform project
15
+
16
+ ### 0. Create a Google Cloud Platform Project
17
+ _A Google Cloud Platform project is required to enable programmatic access to Google Sheets. If you already have a project, skip to the next step. You can also see the [Google Cloud Platform documentation](https://developers.google.com/workspace/guides/create-project) for more information._
18
+
19
+ - Go to the Google Cloud Platform Console and [create a new project](https://console.cloud.google.com/projectcreate).
20
+
21
+ ### 1. Enable the Google Sheets API
22
+ _The Google Sheets API must be enabled for your project. You can also see the [Google Sheets API documentation](https://developers.google.com/sheets/api/quickstart/nodejs) for more information._
23
+
24
+ - Go to the [Google Sheets API page](https://console.cloud.google.com/apis/library/sheets.googleapis.com) and click **Enable**, then fill in the form to enable the Google Sheets API for your project.
25
+ <!-- TODO: Add screenshot of Enable page and describe filling out the form -->
26
+
27
+ ### 2. Create a Service Account
28
+ _A service account is required to authenticate the proxy to Google Sheets._
29
+
30
+ - Once the Google Sheets API is enabled, click the **Credentials** tab on the Google Sheets API page.
31
+ - Click **Create credentials** and select **Service account**.
32
+ - Provide a name for the service account and click **Done** (the second and third steps can be skipped).
33
+
34
+ ### 3. Download the Service Account Key
35
+ _Once your account is created, you'll need to download the key file and include it in the proxy's secrets configuration._
36
+
37
+ - Click the Service Account you just created in the list of service accounts for the API.
38
+ - Click the **Keys** tab and click **Add key**, then select **Create new key**.
39
+ - Select **JSON** as the key type and click **Create**.
40
+
41
+ The JSON file will be downloaded to your computer.
42
+
43
+ ### 4. Set the Service Account key as a Secret
44
+ _The JSON key file must be set as a secret in the proxy's configuration. Because files cannot be included in the secrets configuration, you'll need to base64 encode the file's contents and paste the encoded string as the value of the `GOOGLE_SHEETS_KEY` secret._
45
+
46
+ - Open the JSON key file in a text editor and copy the contents.
47
+ - Visit the [base64 encode/decode tool](https://www.base64encode.org/) and paste the contents into the box, then click **Encode**.
48
+ - Copy the encoded string and paste it as the value of the `GOOGLE_SHEETS_KEY` secret in the deployment's secrets configuration.
49
+ - **WARNING:** Don't reveal this string publically. The `.env` file is NOT private -- unless you're running the proxy locally, you should not use it to store secrets!
50
+
51
+ ### 5. Create a new spreadsheet and share it with the service account
52
+ _The service account must be given permission to access the logging spreadsheet. Each service account has a unique email address, which can be found in the JSON key file; share the spreadsheet with that email address just as you would share it with another user._
53
+
54
+ - Open the JSON key file in a text editor and copy the value of the `client_email` field.
55
+ - Open the spreadsheet you want to log to, or create a new one, and click **File > Share**.
56
+ - Paste the service account's email address into the **Add people or groups** field. Ensure the service account has **Editor** permissions, then click **Done**.
57
+
58
+ ### 6. Set the spreadsheet ID as a Secret
59
+ _The spreadsheet ID must be set as a secret in the proxy's configuration. The spreadsheet ID can be found in the URL of the spreadsheet. For example, the spreadsheet ID for `https://docs.google.com/spreadsheets/d/1X2Y3Z/edit#gid=0` is `1X2Y3Z`. The ID isn't necessarily a sensitive value if you intend for the spreadsheet to be public, but it's still recommended to set it as a secret._
60
+
61
+ - Copy the spreadsheet ID and paste it as the value of the `GOOGLE_SHEETS_SPREADSHEET_ID` secret in the deployment's secrets configuration.
docs/pow-captcha.md ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Proof-of-work Verification
2
+
3
+ You can require users to complete a proof-of-work before they can access the
4
+ proxy. This can increase the cost of denial of service attacks and slow down
5
+ automated abuse.
6
+
7
+ When configured, users access the challenge UI and request a token. The server
8
+ sends a challenge to the client, which asks the user's browser to find a
9
+ solution to the challenge that meets a certain constraint (the difficulty
10
+ level). Once the user has found a solution, they can submit it to the server
11
+ and get a user token valid for a period you specify.
12
+
13
+ The proof-of-work challenge uses the argon2id hash function.
14
+
15
+ ## Configuration
16
+
17
+ To enable proof-of-work verification, set the following environment variables:
18
+
19
+ ```
20
+ GATEKEEPER=user_token
21
+ CAPTCHA_MODE=proof_of_work
22
+ # Validity of the token in hours
23
+ POW_TOKEN_HOURS=24
24
+ # Max number of IPs that can use a user_token issued via proof-of-work
25
+ POW_TOKEN_MAX_IPS=2
26
+ # The difficulty level of the proof-of-work challenge. You can use one of the
27
+ # predefined levels specified below, or you can specify a custom number of
28
+ # expected hash iterations.
29
+ POW_DIFFICULTY_LEVEL=low
30
+ # The time limit for solving the challenge, in minutes
31
+ POW_CHALLENGE_TIMEOUT=30
32
+ ```
33
+
34
+ ## Difficulty Levels
35
+
36
+ The difficulty level controls how long, on average, it will take for a user to
37
+ solve the proof-of-work challenge. Due to randomness, the actual time can very
38
+ significantly; lucky users may solve the challenge in a fraction of the average
39
+ time, while unlucky users may take much longer.
40
+
41
+ The difficulty level doesn't affect the speed of the hash function itself, only
42
+ the number of hashes that will need to be computed. Therefore, the time required
43
+ to complete the challenge scales linearly with the difficulty level's iteration
44
+ count.
45
+
46
+ You can adjust the difficulty level while the proxy is running from the admin
47
+ interface.
48
+
49
+ Be aware that there is a time limit for solving the challenge, by default set to
50
+ 30 minutes. Above 'high' difficulty, you will probably need to increase the time
51
+ limit or it will be very hard for users with slow devices to find a solution
52
+ within the time limit.
53
+
54
+ ### Low
55
+
56
+ - Average of 200 iterations required
57
+ - Default setting.
58
+
59
+ ### Medium
60
+
61
+ - Average of 900 iterations required
62
+
63
+ ### High
64
+
65
+ - Average of 1900 iterations required
66
+
67
+ ### Extreme
68
+
69
+ - Average of 4000 iterations required
70
+ - Not recommended unless you are expecting very high levels of abuse
71
+ - May require increasing `POW_CHALLENGE_TIMEOUT`
72
+
73
+ ### Custom
74
+
75
+ Setting `POW_DIFFICULTY_LEVEL` to an integer will use that number of iterations
76
+ as the difficulty level.
77
+
78
+ ## Other challenge settings
79
+
80
+ - `POW_CHALLENGE_TIMEOUT`: The time limit for solving the challenge, in minutes.
81
+ Default is 30.
82
+ - `POW_TOKEN_HOURS`: The period of time for which a user token issued via proof-
83
+ of-work can be used. Default is 24 hours. Starts when the challenge is solved.
84
+ - `POW_TOKEN_MAX_IPS`: The maximum number of unique IPs that can use a single
85
+ user token issued via proof-of-work. Default is 2.
86
+ - `POW_TOKEN_PURGE_HOURS`: The period of time after which an expired user token
87
+ issued via proof-of-work will be removed from the database. Until it is
88
+ purged, users can refresh expired tokens by completing a half-difficulty
89
+ challenge. Default is 48 hours.
90
+ - `POW_MAX_TOKENS_PER_IP`: The maximum number of active user tokens that can
91
+ be associated with a single IP address. After this limit is reached, the
92
+ oldest token will be forcibly expired when a new token is issued. Set to 0
93
+ to disable this feature. Default is 0.
94
+
95
+ ## Custom argon2id parameters
96
+
97
+ You can set custom argon2id parameters for the proof-of-work challenge.
98
+ Generally, you should not need to change these unless you have a specific
99
+ reason to do so.
100
+
101
+ The listed values are the defaults.
102
+
103
+ ```
104
+ ARGON2_TIME_COST=8
105
+ ARGON2_MEMORY_KB=65536
106
+ ARGON2_PARALLELISM=1
107
+ ARGON2_HASH_LENGTH=32
108
+ ```
109
+
110
+ Increasing parallelism will not do much except increase memory consumption for
111
+ both the client and server, because browser proof-of-work implementations are
112
+ single-threaded. It's better to increase the time cost if you want to increase
113
+ the difficulty.
114
+
115
+ Increasing memory too much may cause memory exhaustion on some mobile devices,
116
+ particularly on iOS due to the way Safari handles WebAssembly memory allocation.
117
+
118
+ ## Tested hash rates
119
+
120
+ These were measured with the default argon2id parameters listed above. These
121
+ tests were not at all scientific so take them with a grain of salt.
122
+
123
+ Safari does not like large WASM memory usage, so concurrency is limited to 4 to
124
+ avoid overallocating memory on mobile WebKit browsers. Thermal throttling can
125
+ also significantly reduce hash rates on mobile devices.
126
+
127
+ - Intel Core i9-13900K (Chrome): 33-35 H/s
128
+ - Intel Core i9-13900K (Firefox): 29-32 H/s
129
+ - Intel Core i9-13900K (Chrome, in VM limited to 4 cores): 12.2 - 13.0 H/s
130
+ - iPad Pro (M2) (Safari, 6 workers): 8.0 - 10 H/s
131
+ - Thermal throttles early. 8 cores is normal concurrency, but unstable.
132
+ - iPhone 15 Pro Max (Safari): 4.0 - 4.6 H/s
133
+ - Samsung Galaxy S10e (Chrome): 3.6 - 3.8 H/s
134
+ - This is a 2019 phone almost matching an iPhone five years newer because of
135
+ bad Safari performance.
docs/self-hosting.md ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Quick self-hosting guide
2
+
3
+ Temporary guide for self-hosting. This will be improved in the future to provide more robust instructions and options. Provided commands are for Ubuntu.
4
+
5
+ This uses prebuilt Docker images for convenience. If you want to make adjustments to the code you can instead clone the repo and follow the Local Development guide in the [README](../README.md).
6
+
7
+ ## Table of Contents
8
+ - [Requirements](#requirements)
9
+ - [Running the application](#running-the-application)
10
+ - [Setting up a reverse proxy](#setting-up-a-reverse-proxy)
11
+ - [trycloudflare](#trycloudflare)
12
+ - [nginx](#nginx)
13
+ - [Example basic nginx configuration (no SSL)](#example-basic-nginx-configuration-no-ssl)
14
+ - [Example with Cloudflare SSL](#example-with-cloudflare-ssl)
15
+ - [Updating/Restarting the application](#updatingrestarting-the-application)
16
+
17
+ ## Requirements
18
+
19
+ - Docker
20
+ - Docker Compose
21
+ - A VPS with at least 512MB of RAM (1GB recommended)
22
+ - A domain name
23
+
24
+ If you don't have a VPS and domain name you can use TryCloudflare to set up a temporary URL that you can share with others. See [trycloudflare](#trycloudflare) for more information.
25
+
26
+ ## Running the application
27
+
28
+ - Install Docker and Docker Compose
29
+ - Create a new directory for the application
30
+ - This will contain your .env file, greeting file, and any user-generated files
31
+ - Execute the following commands:
32
+ - ```
33
+ touch .env
34
+ touch greeting.md
35
+ echo "OPENAI_KEY=your-openai-key" >> .env
36
+ curl https://gitgud.io/khanon/oai-reverse-proxy/-/raw/main/docker/docker-compose-selfhost.yml -o docker-compose.yml
37
+ ```
38
+ - You can set further environment variables and keys in the `.env` file. See [.env.example](../.env.example) for a list of available options.
39
+ - You can set a custom greeting in `greeting.md`. This will be displayed on the homepage.
40
+ - Run `docker compose up -d`
41
+
42
+ You can check logs with `docker compose logs -n 100 -f`.
43
+
44
+ The provided docker-compose file listens on port 7860 but binds to localhost only. You should use a reverse proxy to expose the application to the internet as described in the next section.
45
+
46
+ ## Setting up a reverse proxy
47
+
48
+ Rather than exposing the application directly to the internet, it is recommended to set up a reverse proxy. This will allow you to use HTTPS and add additional security measures.
49
+
50
+ ### trycloudflare
51
+
52
+ This will give you a temporary (72 hours) URL that you can use to let others connect to your instance securely, without having to set up a reverse proxy. If you are running the server on your home network, this is probably the best option.
53
+ - Install `cloudflared` following the instructions at [try.cloudflare.com](https://try.cloudflare.com/).
54
+ - Run `cloudflared tunnel --url http://localhost:7860`
55
+ - You will be given a temporary URL that you can share with others.
56
+
57
+ If you have a VPS, you should use a proper reverse proxy like nginx instead for a more permanent solution which will allow you to use your own domain name, handle SSL, and add additional security/anti-abuse measures.
58
+
59
+ ### nginx
60
+
61
+ First, install nginx.
62
+ - `sudo apt update && sudo apt install nginx`
63
+
64
+ #### Example basic nginx configuration (no SSL)
65
+
66
+ - `sudo nano /etc/nginx/sites-available/oai.conf`
67
+ - ```
68
+ server {
69
+ listen 80;
70
+ server_name example.com;
71
+
72
+ location / {
73
+ proxy_pass http://localhost:7860;
74
+ }
75
+ }
76
+ ```
77
+ - Replace `example.com` with your domain name.
78
+ - Ctrl+X to exit, Y to save, Enter to confirm.
79
+ - `sudo ln -s /etc/nginx/sites-available/oai.conf /etc/nginx/sites-enabled`
80
+ - `sudo nginx -t`
81
+ - This will check the configuration file for errors.
82
+ - `sudo systemctl restart nginx`
83
+ - This will restart nginx and apply the new configuration.
84
+
85
+ #### Example with Cloudflare SSL
86
+
87
+ This allows you to use a self-signed certificate on the server, and have Cloudflare handle client SSL. You need to have a Cloudflare account and have your domain set up with Cloudflare already, pointing to your server's IP address.
88
+
89
+ - Set Cloudflare to use Full SSL mode. Since we are using a self-signed certificate, don't use Full (strict) mode.
90
+ - Create a self-signed certificate:
91
+ - `openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt`
92
+ - `sudo nano /etc/nginx/sites-available/oai.conf`
93
+ - ```
94
+ server {
95
+ listen 443 ssl;
96
+ server_name yourdomain.com www.yourdomain.com;
97
+
98
+ ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
99
+ ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
100
+
101
+ # Only allow inbound traffic from Cloudflare
102
+ allow 173.245.48.0/20;
103
+ allow 103.21.244.0/22;
104
+ allow 103.22.200.0/22;
105
+ allow 103.31.4.0/22;
106
+ allow 141.101.64.0/18;
107
+ allow 108.162.192.0/18;
108
+ allow 190.93.240.0/20;
109
+ allow 188.114.96.0/20;
110
+ allow 197.234.240.0/22;
111
+ allow 198.41.128.0/17;
112
+ allow 162.158.0.0/15;
113
+ allow 104.16.0.0/13;
114
+ allow 104.24.0.0/14;
115
+ allow 172.64.0.0/13;
116
+ allow 131.0.72.0/22;
117
+ deny all;
118
+
119
+ location / {
120
+ proxy_pass http://localhost:7860;
121
+ proxy_http_version 1.1;
122
+ proxy_set_header Upgrade $http_upgrade;
123
+ proxy_set_header Connection 'upgrade';
124
+ proxy_set_header Host $host;
125
+ proxy_cache_bypass $http_upgrade;
126
+ }
127
+
128
+ ssl_protocols TLSv1.2 TLSv1.3;
129
+ ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
130
+ ssl_prefer_server_ciphers on;
131
+ ssl_session_cache shared:SSL:10m;
132
+ }
133
+ ```
134
+ - Replace `yourdomain.com` with your domain name.
135
+ - Ctrl+X to exit, Y to save, Enter to confirm.
136
+ - `sudo ln -s /etc/nginx/sites-available/oai.conf /etc/nginx/sites-enabled`
137
+
138
+ ## Updating/Restarting the application
139
+
140
+ After making an .env change, you need to restart the application for it to take effect.
141
+
142
+ - `docker compose down`
143
+ - `docker compose up -d`
144
+
145
+ To update the application to the latest version:
146
+
147
+ - `docker compose pull`
148
+ - `docker compose down`
149
+ - `docker compose up -d`
150
+ - `docker image prune -f`
docs/user-management.md ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # User Management
2
+
3
+ The proxy supports several different user management strategies. You can choose the one that best fits your needs by setting the `GATEKEEPER` environment variable.
4
+
5
+ Several of these features require you to set secrets in your environment. If using Huggingface Spaces to deploy, do not set these in your `.env` file because that file is public and anyone can see it.
6
+
7
+ ## Table of Contents
8
+
9
+ - [No user management](#no-user-management-gatekeepernone)
10
+ - [Single-password authentication](#single-password-authentication-gatekeeperproxy_key)
11
+ - [Per-user authentication](#per-user-authentication-gatekeeperuser_token)
12
+ - [Memory](#memory)
13
+ - [Firebase Realtime Database](#firebase-realtime-database)
14
+ - [Firebase setup instructions](#firebase-setup-instructions)
15
+ - [SQLite Database](#sqlite-database)
16
+ - [Whitelisting admin IP addresses](#whitelisting-admin-ip-addresses)
17
+
18
+ ## No user management (`GATEKEEPER=none`)
19
+
20
+ This is the default mode. The proxy will not require any authentication to access the server and offers basic IP-based rate limiting and anti-abuse features.
21
+
22
+ ## Single-password authentication (`GATEKEEPER=proxy_key`)
23
+
24
+ This mode allows you to set a password that must be passed in the `Authentication` header of every request to the server as a bearer token. This is useful if you want to restrict access to the server, but don't want to create a separate account for every user.
25
+
26
+ To set the password, create a `PROXY_KEY` secret in your environment.
27
+
28
+ ## Per-user authentication (`GATEKEEPER=user_token`)
29
+
30
+ This mode allows you to provision separate Bearer tokens for each user. You can manage users via the /admin/users via REST or through the admin interface at `/admin`.
31
+
32
+ To begin, set `ADMIN_KEY` to a secret value. This will be used to authenticate requests to the REST API or to log in to the UI.
33
+
34
+ [You can find an OpenAPI specification for the /admin/users REST API here.](openapi-admin-users.yaml)
35
+
36
+ By default, the proxy will store user data in memory. Naturally, this means that user data will be lost when the proxy is restarted, though you can use the user import/export feature to save and restore user data manually or via a script. However, the proxy also supports persisting user data to an external data store with some additional configuration.
37
+
38
+ Below are the supported data stores and their configuration options.
39
+
40
+ ### Memory
41
+
42
+ This is the default data store (`GATEKEEPER_STORE=memory`) User data will be stored in memory and will be lost when the server is restarted. You are responsible for exporting and re-importing user data after a restart.
43
+
44
+ ### Firebase Realtime Database
45
+
46
+ To use Firebase Realtime Database to persist user data, set the following environment variables:
47
+
48
+ - `GATEKEEPER_STORE`: Set this to `firebase_rtdb`
49
+ - **Secret** `FIREBASE_RTDB_URL`: The URL of your Firebase Realtime Database, e.g. `https://my-project-default-rtdb.firebaseio.com`
50
+ - **Secret** `FIREBASE_KEY`: A base-64 encoded service account key for your Firebase project. Refer to the instructions below for how to create this key.
51
+
52
+ **Firebase setup instructions**
53
+
54
+ 1. Go to the [Firebase console](https://console.firebase.google.com/) and click "Add project", then follow the prompts to create a new project.
55
+ 2. From the **Project Overview** page, click **All products** in the left sidebar, then click **Realtime Database**.
56
+ 3. Click **Create database** and choose **Start in test mode**. Click **Enable**.
57
+ - Test mode is fine for this use case as it still requires authentication to access the database. You may wish to set up more restrictive rules if you plan to use the database for other purposes.
58
+ - The reference URL for the database will be displayed on the page. You will need this later.
59
+ 4. Click the gear icon next to **Project Overview** in the left sidebar, then click **Project settings**.
60
+ 5. Click the **Service accounts** tab, then click **Generate new private key**.
61
+ 6. The downloaded file contains your key. Encode it as base64 and set it as the `FIREBASE_KEY` secret in your environment.
62
+ 7. Set `FIREBASE_RTDB_URL` to the reference URL of your Firebase Realtime Database, e.g. `https://my-project-default-rtdb.firebaseio.com`.
63
+ 8. Set `GATEKEEPER_STORE` to `firebase_rtdb` in your environment if you haven't already.
64
+
65
+ The proxy server will attempt to connect to your Firebase Realtime Database at startup and will throw an error if it cannot connect. If you see this error, check that your `FIREBASE_RTDB_URL` and `FIREBASE_KEY` secrets are set correctly.
66
+
67
+ ### SQLite Database
68
+
69
+ To use a local SQLite database file to persist user data, set the following environment variables:
70
+
71
+ - `GATEKEEPER_STORE`: Set this to `sqlite`.
72
+ - `SQLITE_USER_STORE_PATH` (Optional): Specifies the path to the SQLite database file.
73
+ - If not set, it defaults to `data/user-store.sqlite` within the project directory.
74
+ - Ensure that the directory where the SQLite file will be created (e.g., the `data/` directory) is writable by the application process.
75
+
76
+ Using SQLite provides a simple way to persist user data locally without relying on external services. User data will be saved to the specified file and will be available across server restarts.
77
+
78
+ ## Whitelisting admin IP addresses
79
+ You can add your own IP ranges to the `ADMIN_WHITELIST` environment variable for additional security.
80
+
81
+ You can provide a comma-separated list containing individual IPv4 or IPv6 addresses, or CIDR ranges.
82
+
83
+ To whitelist an entire IP range, use CIDR notation. For example, `192.168.0.1/24` would whitelist all addresses from `192.168.0.0` to `192.168.0.255`.
84
+
85
+ To disable the whitelist, set `ADMIN_WHITELIST=0.0.0.0/0,::0`, which will allow access from any IPv4 or IPv6 address. This is the default behavior.
docs/user-quotas.md ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # User Quotas
2
+
3
+ When using `user_token` authentication, you can set (model) token quotas for user. These quotas are enforced by the proxy server and are separate from the quotas enforced by OpenAI.
4
+
5
+ You can set the default quota via environment variables. Quotas are enforced on a per-model basis, and count both prompt tokens and completion tokens. By default, all quotas are disabled.
6
+
7
+ Set the following environment variables to set the default quotas:
8
+ - `TOKEN_QUOTA_TURBO`
9
+ - `TOKEN_QUOTA_GPT4`
10
+ - `TOKEN_QUOTA_CLAUDE`
11
+
12
+ Quotas only apply to `normal`-type users; `special`-type users are exempt from quotas. You can change users' types via the REST API.
13
+
14
+ **Note that changes to these environment variables will only apply to newly created users.** To modify existing users' quotas, use the REST API or the admin UI.
15
+
16
+ ## Automatically refreshing quotas
17
+
18
+ You can use the `QUOTA_REFRESH_PERIOD` environment variable to automatically refresh users' quotas periodically. This is useful if you want to give users a certain number of tokens per day, for example. The entire quota will be refreshed at the start of the specified period, and any tokens a user has not used will not be carried over.
19
+
20
+ Quotas for all models and users will be refreshed. If you haven't set `TOKEN_QUOTA_*` for a particular model, quotas for that model will not be refreshed (so any manually set quotas will not be overwritten).
21
+
22
+ Set the `QUOTA_REFRESH_PERIOD` environment variable to one of the following values:
23
+ - `daily` (at midnight)
24
+ - `hourly`
25
+ - leave unset to disable automatic refreshing
26
+
27
+ You can also use a cron expression, for example:
28
+ - Every 45 seconds: `"*/45 * * * * *"`
29
+ - Every 30 minutes: `"*/30 * * * *"`
30
+ - Every 6 hours: `"0 */6 * * *"`
31
+ - Every 3 days: `"0 0 */3 * *"`
32
+ - Daily, but at mid-day: `"0 12 * * *"`
33
+
34
+ Make sure to enclose the cron expression in quotation marks.
35
+
36
+ All times are in the server's local time zone. Refer to [crontab.guru](https://crontab.guru/) for more examples.
http-client.env.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "dev": {
3
+ "proxy-host": "http://localhost:7860",
4
+ "oai-key-1": "override in http-client.private.env.json",
5
+ "proxy-key": "override in http-client.private.env.json",
6
+ "azu-resource-name": "override in http-client.private.env.json",
7
+ "azu-deployment-id": "override in http-client.private.env.json"
8
+ }
9
+ }
package-lock.json ADDED
The diff for this file is too large to render. See raw diff
 
package.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "oai-reverse-proxy",
3
+ "version": "1.0.0",
4
+ "description": "Reverse proxy for the OpenAI API",
5
+ "scripts": {
6
+ "build": "tsc && copyfiles -u 1 src/**/*.ejs build",
7
+ "database:migrate": "ts-node scripts/migrate.ts",
8
+ "postinstall": "patch-package",
9
+ "prepare": "husky install",
10
+ "start": "node --trace-deprecation --trace-warnings build/server.js",
11
+ "start:dev": "nodemon --watch src --exec ts-node --transpile-only src/server.ts",
12
+ "start:debug": "ts-node --inspect --transpile-only src/server.ts",
13
+ "start:watch": "nodemon --require source-map-support/register build/server.js",
14
+ "type-check": "tsc --noEmit"
15
+ },
16
+ "engines": {
17
+ "node": ">=18.0.0"
18
+ },
19
+ "author": "",
20
+ "license": "MIT",
21
+ "dependencies": {
22
+ "@anthropic-ai/tokenizer": "^0.0.4",
23
+ "@aws-crypto/sha256-js": "^5.2.0",
24
+ "@huggingface/jinja": "^0.3.0",
25
+ "@node-rs/argon2": "^1.8.3",
26
+ "@smithy/eventstream-codec": "^2.1.3",
27
+ "@smithy/eventstream-serde-node": "^2.1.3",
28
+ "@smithy/protocol-http": "^3.2.1",
29
+ "@smithy/signature-v4": "^2.1.3",
30
+ "@smithy/util-utf8": "^2.1.1",
31
+ "axios": "^1.7.4",
32
+ "better-sqlite3": "^10.0.0",
33
+ "check-disk-space": "^3.4.0",
34
+ "cookie-parser": "^1.4.6",
35
+ "copyfiles": "^2.4.1",
36
+ "cors": "^2.8.5",
37
+ "csrf-csrf": "^2.3.0",
38
+ "dotenv": "^16.3.1",
39
+ "ejs": "^3.1.10",
40
+ "express": "^4.19.3",
41
+ "express-session": "^1.17.3",
42
+ "firebase-admin": "^12.5.0",
43
+ "glob": "^10.3.12",
44
+ "googleapis": "^122.0.0",
45
+ "http-proxy": "1.18.1",
46
+ "http-proxy-middleware": "^3.0.2",
47
+ "ipaddr.js": "^2.1.0",
48
+ "memorystore": "^1.6.7",
49
+ "multer": "^1.4.5-lts.1",
50
+ "node-schedule": "^2.1.1",
51
+ "patch-package": "^8.0.0",
52
+ "pino": "^8.11.0",
53
+ "pino-http": "^8.3.3",
54
+ "proxy-agent": "^6.4.0",
55
+ "sanitize-html": "^2.13.0",
56
+ "sharp": "^0.32.6",
57
+ "showdown": "^2.1.0",
58
+ "source-map-support": "^0.5.21",
59
+ "stream-json": "^1.8.0",
60
+ "tiktoken": "^1.0.10",
61
+ "tinyws": "^0.1.0",
62
+ "uuid": "^9.0.0",
63
+ "zlib": "^1.0.5",
64
+ "zod": "^3.22.3",
65
+ "zod-error": "^1.5.0"
66
+ },
67
+ "devDependencies": {
68
+ "@smithy/types": "^3.3.0",
69
+ "@types/better-sqlite3": "^7.6.10",
70
+ "@types/cookie-parser": "^1.4.3",
71
+ "@types/cors": "^2.8.13",
72
+ "@types/express": "^4.17.17",
73
+ "@types/express-session": "^1.17.7",
74
+ "@types/multer": "^1.4.7",
75
+ "@types/node-schedule": "^2.1.0",
76
+ "@types/sanitize-html": "^2.9.0",
77
+ "@types/showdown": "^2.0.0",
78
+ "@types/stream-json": "^1.7.7",
79
+ "@types/uuid": "^9.0.1",
80
+ "concurrently": "^8.0.1",
81
+ "esbuild": "^0.25.5",
82
+ "esbuild-register": "^3.4.2",
83
+ "husky": "^8.0.3",
84
+ "nodemon": "^3.0.1",
85
+ "pino-pretty": "^10.2.3",
86
+ "prettier": "^3.0.3",
87
+ "prettier-plugin-ejs": "^1.0.3",
88
+ "ts-node": "^10.9.1",
89
+ "typescript": "^5.4.2"
90
+ },
91
+ "overrides": {
92
+ "node-fetch@2.x": {
93
+ "whatwg-url": "14.x"
94
+ }
95
+ }
96
+ }
patches/README.md ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Patches
2
+ Contains monkey patches for certain packages, applied using `patch-package`.
3
+
4
+ ## `http-proxy+1.18.1.patch`
5
+ Modifies the `http-proxy` package to work around an incompatibility with
6
+ body-parser and SOCKS5 proxies due to some esoteric stream handling behavior
7
+ when `socks-proxy-agent` is used instead of a generic http.Agent.
8
+
9
+ Modification involves adjusting the `buffer` property on ProxyServer's `options`
10
+ object to be a function that returns a stream instead of a stream itself. This
11
+ allows us to give it a function which produces a new Readable from the already-
12
+ parsed request body.
13
+
14
+ With the old implementation we would need to create an entirely new ProxyServer
15
+ instance for each request, which is not ideal under heavy load.
16
+
17
+ `http-proxy` hasn't been updated in six years so it's unlikely that this patch
18
+ will be broken by future updates, but it's stil pinned to 1.18.1 for now.
19
+
20
+ ### See also
21
+ https://github.com/chimurai/http-proxy-middleware/issues/40
22
+ https://github.com/chimurai/http-proxy-middleware/issues/299
23
+ https://github.com/http-party/node-http-proxy/pull/1027
patches/http-proxy+1.18.1.patch ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diff --git a/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js b/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js
2
+ index 7ae7355..c825c27 100644
3
+ --- a/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js
4
+ +++ b/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js
5
+ @@ -167,7 +167,7 @@ module.exports = {
6
+ }
7
+ }
8
+
9
+ - (options.buffer || req).pipe(proxyReq);
10
+ + (options.buffer(req) || req).pipe(proxyReq);
11
+
12
+ proxyReq.on('response', function(proxyRes) {
13
+ if(server) { server.emit('proxyRes', proxyRes, req, res); }
public/css/reset.css ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
2
+
3
+ /* Document
4
+ ========================================================================== */
5
+
6
+ /**
7
+ * 1. Correct the line height in all browsers.
8
+ * 2. Prevent adjustments of font size after orientation changes in iOS.
9
+ */
10
+
11
+ html {
12
+ line-height: 1.15; /* 1 */
13
+ -webkit-text-size-adjust: 100%; /* 2 */
14
+ }
15
+
16
+ /* Sections
17
+ ========================================================================== */
18
+
19
+ /**
20
+ * Remove the margin in all browsers.
21
+ */
22
+
23
+ body {
24
+ margin: 0;
25
+ }
26
+
27
+ /**
28
+ * Render the `main` element consistently in IE.
29
+ */
30
+
31
+ main {
32
+ display: block;
33
+ }
34
+
35
+ /**
36
+ * Correct the font size and margin on `h1` elements within `section` and
37
+ * `article` contexts in Chrome, Firefox, and Safari.
38
+ */
39
+
40
+ h1 {
41
+ font-size: 2em;
42
+ margin: 0.67em 0;
43
+ }
44
+
45
+ /* Grouping content
46
+ ========================================================================== */
47
+
48
+ /**
49
+ * 1. Add the correct box sizing in Firefox.
50
+ * 2. Show the overflow in Edge and IE.
51
+ */
52
+
53
+ hr {
54
+ box-sizing: content-box; /* 1 */
55
+ height: 0; /* 1 */
56
+ overflow: visible; /* 2 */
57
+ }
58
+
59
+ /**
60
+ * 1. Correct the inheritance and scaling of font size in all browsers.
61
+ * 2. Correct the odd `em` font sizing in all browsers.
62
+ */
63
+
64
+ pre {
65
+ font-family: monospace, monospace; /* 1 */
66
+ font-size: 1em; /* 2 */
67
+ }
68
+
69
+ /* Text-level semantics
70
+ ========================================================================== */
71
+
72
+ /**
73
+ * Remove the gray background on active links in IE 10.
74
+ */
75
+
76
+ a {
77
+ background-color: transparent;
78
+ }
79
+
80
+ /**
81
+ * 1. Remove the bottom border in Chrome 57-
82
+ * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
83
+ */
84
+
85
+ abbr[title] {
86
+ border-bottom: none; /* 1 */
87
+ text-decoration: underline; /* 2 */
88
+ text-decoration: underline dotted; /* 2 */
89
+ }
90
+
91
+ /**
92
+ * Add the correct font weight in Chrome, Edge, and Safari.
93
+ */
94
+
95
+ b,
96
+ strong {
97
+ font-weight: bolder;
98
+ }
99
+
100
+ /**
101
+ * 1. Correct the inheritance and scaling of font size in all browsers.
102
+ * 2. Correct the odd `em` font sizing in all browsers.
103
+ */
104
+
105
+ code,
106
+ kbd,
107
+ samp {
108
+ font-family: monospace, monospace; /* 1 */
109
+ font-size: 1em; /* 2 */
110
+ }
111
+
112
+ /**
113
+ * Add the correct font size in all browsers.
114
+ */
115
+
116
+ small {
117
+ font-size: 80%;
118
+ }
119
+
120
+ /**
121
+ * Prevent `sub` and `sup` elements from affecting the line height in
122
+ * all browsers.
123
+ */
124
+
125
+ sub,
126
+ sup {
127
+ font-size: 75%;
128
+ line-height: 0;
129
+ position: relative;
130
+ vertical-align: baseline;
131
+ }
132
+
133
+ sub {
134
+ bottom: -0.25em;
135
+ }
136
+
137
+ sup {
138
+ top: -0.5em;
139
+ }
140
+
141
+ /* Embedded content
142
+ ========================================================================== */
143
+
144
+ /**
145
+ * Remove the border on images inside links in IE 10.
146
+ */
147
+
148
+ img {
149
+ border-style: none;
150
+ }
151
+
152
+ /* Forms
153
+ ========================================================================== */
154
+
155
+ /**
156
+ * 1. Change the font styles in all browsers.
157
+ * 2. Remove the margin in Firefox and Safari.
158
+ */
159
+
160
+ button,
161
+ input,
162
+ optgroup,
163
+ select,
164
+ textarea {
165
+ font-family: inherit; /* 1 */
166
+ font-size: 100%; /* 1 */
167
+ line-height: 1.15; /* 1 */
168
+ margin: 0; /* 2 */
169
+ }
170
+
171
+ /**
172
+ * Show the overflow in IE.
173
+ * 1. Show the overflow in Edge.
174
+ */
175
+
176
+ button,
177
+ input { /* 1 */
178
+ overflow: visible;
179
+ }
180
+
181
+ /**
182
+ * Remove the inheritance of text transform in Edge, Firefox, and IE.
183
+ * 1. Remove the inheritance of text transform in Firefox.
184
+ */
185
+
186
+ button,
187
+ select { /* 1 */
188
+ text-transform: none;
189
+ }
190
+
191
+ /**
192
+ * Correct the inability to style clickable types in iOS and Safari.
193
+ */
194
+
195
+ button,
196
+ [type="button"],
197
+ [type="reset"],
198
+ [type="submit"] {
199
+ -webkit-appearance: button;
200
+ }
201
+
202
+ /**
203
+ * Remove the inner border and padding in Firefox.
204
+ */
205
+
206
+ button::-moz-focus-inner,
207
+ [type="button"]::-moz-focus-inner,
208
+ [type="reset"]::-moz-focus-inner,
209
+ [type="submit"]::-moz-focus-inner {
210
+ border-style: none;
211
+ padding: 0;
212
+ }
213
+
214
+ /**
215
+ * Restore the focus styles unset by the previous rule.
216
+ */
217
+
218
+ button:-moz-focusring,
219
+ [type="button"]:-moz-focusring,
220
+ [type="reset"]:-moz-focusring,
221
+ [type="submit"]:-moz-focusring {
222
+ outline: 1px dotted ButtonText;
223
+ }
224
+
225
+ /**
226
+ * Correct the padding in Firefox.
227
+ */
228
+
229
+ fieldset {
230
+ padding: 0.35em 0.75em 0.625em;
231
+ }
232
+
233
+ /**
234
+ * 1. Correct the text wrapping in Edge and IE.
235
+ * 2. Correct the color inheritance from `fieldset` elements in IE.
236
+ * 3. Remove the padding so developers are not caught out when they zero out
237
+ * `fieldset` elements in all browsers.
238
+ */
239
+
240
+ legend {
241
+ box-sizing: border-box; /* 1 */
242
+ color: inherit; /* 2 */
243
+ display: table; /* 1 */
244
+ max-width: 100%; /* 1 */
245
+ padding: 0; /* 3 */
246
+ white-space: normal; /* 1 */
247
+ }
248
+
249
+ /**
250
+ * Add the correct vertical alignment in Chrome, Firefox, and Opera.
251
+ */
252
+
253
+ progress {
254
+ vertical-align: baseline;
255
+ }
256
+
257
+ /**
258
+ * Remove the default vertical scrollbar in IE 10+.
259
+ */
260
+
261
+ textarea {
262
+ overflow: auto;
263
+ }
264
+
265
+ /**
266
+ * 1. Add the correct box sizing in IE 10.
267
+ * 2. Remove the padding in IE 10.
268
+ */
269
+
270
+ [type="checkbox"],
271
+ [type="radio"] {
272
+ box-sizing: border-box; /* 1 */
273
+ padding: 0; /* 2 */
274
+ }
275
+
276
+ /**
277
+ * Correct the cursor style of increment and decrement buttons in Chrome.
278
+ */
279
+
280
+ [type="number"]::-webkit-inner-spin-button,
281
+ [type="number"]::-webkit-outer-spin-button {
282
+ height: auto;
283
+ }
284
+
285
+ /**
286
+ * 1. Correct the odd appearance in Chrome and Safari.
287
+ * 2. Correct the outline style in Safari.
288
+ */
289
+
290
+ [type="search"] {
291
+ -webkit-appearance: textfield; /* 1 */
292
+ outline-offset: -2px; /* 2 */
293
+ }
294
+
295
+ /**
296
+ * Remove the inner padding in Chrome and Safari on macOS.
297
+ */
298
+
299
+ [type="search"]::-webkit-search-decoration {
300
+ -webkit-appearance: none;
301
+ }
302
+
303
+ /**
304
+ * 1. Correct the inability to style clickable types in iOS and Safari.
305
+ * 2. Change font properties to `inherit` in Safari.
306
+ */
307
+
308
+ ::-webkit-file-upload-button {
309
+ -webkit-appearance: button; /* 1 */
310
+ font: inherit; /* 2 */
311
+ }
312
+
313
+ /* Interactive
314
+ ========================================================================== */
315
+
316
+ /*
317
+ * Add the correct display in Edge, IE 10+, and Firefox.
318
+ */
319
+
320
+ details {
321
+ display: block;
322
+ }
323
+
324
+ /*
325
+ * Add the correct display in all browsers.
326
+ */
327
+
328
+ summary {
329
+ display: list-item;
330
+ }
331
+
332
+ /* Misc
333
+ ========================================================================== */
334
+
335
+ /**
336
+ * Add the correct display in IE 10+.
337
+ */
338
+
339
+ template {
340
+ display: none;
341
+ }
342
+
343
+ /**
344
+ * Add the correct display in IE 10.
345
+ */
346
+
347
+ [hidden] {
348
+ display: none;
349
+ }
public/css/sakura-dark.css ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* modified https://github.com/oxalorg/sakura */
2
+ html {
3
+ font-size: 62.5%;
4
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
5
+ "Helvetica Neue", Arial, "Noto Sans", sans-serif;
6
+ }
7
+ body {
8
+ font-size: 1.8rem;
9
+ line-height: 1.618;
10
+ max-width: 38em;
11
+ margin: auto;
12
+ color: #c9c9c9;
13
+ background-color: #222222;
14
+ padding: 13px;
15
+ }
16
+ @media (max-width: 684px) {
17
+ body {
18
+ font-size: 1.53rem;
19
+ }
20
+ }
21
+ @media (max-width: 382px) {
22
+ body {
23
+ font-size: 1.35rem;
24
+ }
25
+ }
26
+ h1,
27
+ h2,
28
+ h3,
29
+ h4,
30
+ h5,
31
+ h6 {
32
+ line-height: 1.1;
33
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
34
+ "Helvetica Neue", Arial, "Noto Sans", sans-serif;
35
+ font-weight: 700;
36
+ margin-top: 3rem;
37
+ margin-bottom: 1.5rem;
38
+ overflow-wrap: break-word;
39
+ word-wrap: break-word;
40
+ -ms-word-break: break-all;
41
+ word-break: break-word;
42
+ }
43
+ h1 {
44
+ font-size: 2.35em;
45
+ }
46
+ h2 {
47
+ font-size: 2em;
48
+ }
49
+ h3 {
50
+ font-size: 1.75em;
51
+ }
52
+ h4 {
53
+ font-size: 1.5em;
54
+ }
55
+ h5 {
56
+ font-size: 1.25em;
57
+ }
58
+ h6 {
59
+ font-size: 1em;
60
+ }
61
+ p {
62
+ margin-top: 0px;
63
+ margin-bottom: 2.5rem;
64
+ }
65
+ small,
66
+ sub,
67
+ sup {
68
+ font-size: 75%;
69
+ }
70
+ hr {
71
+ border-color: #ffffff;
72
+ }
73
+ a {
74
+ text-decoration: none;
75
+ color: #ffffff;
76
+ }
77
+ a:visited {
78
+ color: #e6e6e6;
79
+ }
80
+ a:hover {
81
+ color: #c9c9c9;
82
+ text-decoration: underline;
83
+ }
84
+ ul {
85
+ padding-left: 1.4em;
86
+ margin-top: 0px;
87
+ margin-bottom: 2.5rem;
88
+ }
89
+ li {
90
+ margin-bottom: 0.4em;
91
+ }
92
+ blockquote {
93
+ margin-left: 0px;
94
+ margin-right: 0px;
95
+ padding-left: 1em;
96
+ padding-top: 0.8em;
97
+ padding-bottom: 0.8em;
98
+ padding-right: 0.8em;
99
+ border-left: 5px solid #ffffff;
100
+ margin-bottom: 2.5rem;
101
+ background-color: #4a4a4a;
102
+ }
103
+ blockquote p {
104
+ margin-bottom: 0;
105
+ }
106
+ img,
107
+ video {
108
+ height: auto;
109
+ max-width: 100%;
110
+ margin-top: 0px;
111
+ margin-bottom: 2.5rem;
112
+ }
113
+ pre {
114
+ background-color: #4a4a4a;
115
+ display: block;
116
+ padding: 1em;
117
+ overflow-x: auto;
118
+ margin-top: 0px;
119
+ margin-bottom: 2.5rem;
120
+ font-size: 0.9em;
121
+ }
122
+ code,
123
+ kbd,
124
+ samp {
125
+ font-size: 0.9em;
126
+ padding: 0 0.5em;
127
+ background-color: #4a4a4a;
128
+ white-space: pre-wrap;
129
+ }
130
+ pre > code {
131
+ padding: 0;
132
+ background-color: transparent;
133
+ white-space: pre;
134
+ font-size: 1em;
135
+ }
136
+ table {
137
+ text-align: justify;
138
+ width: 100%;
139
+ border-collapse: collapse;
140
+ margin-bottom: 2rem;
141
+ }
142
+ td,
143
+ th {
144
+ padding: 0.5em;
145
+ border-bottom: 1px solid #4a4a4a;
146
+ }
147
+ input,
148
+ textarea {
149
+ border: 1px solid #c9c9c9;
150
+ }
151
+ input:focus,
152
+ textarea:focus {
153
+ border: 1px solid #ffffff;
154
+ }
155
+ textarea {
156
+ width: 100%;
157
+ }
158
+ .button,
159
+ button,
160
+ input[type="submit"],
161
+ input[type="reset"],
162
+ input[type="button"],
163
+ input[type="file"]::file-selector-button {
164
+ display: inline-block;
165
+ padding: 5px 10px;
166
+ text-align: center;
167
+ text-decoration: none;
168
+ white-space: nowrap;
169
+ background-color: #ffffff;
170
+ color: #222222;
171
+ border-radius: 1px;
172
+ border: 1px solid #ffffff;
173
+ cursor: pointer;
174
+ box-sizing: border-box;
175
+ }
176
+ .button[disabled],
177
+ button[disabled],
178
+ input[type="submit"][disabled],
179
+ input[type="reset"][disabled],
180
+ input[type="button"][disabled],
181
+ input[type="file"][disabled] {
182
+ cursor: default;
183
+ opacity: 0.5;
184
+ }
185
+ .button:hover,
186
+ button:hover,
187
+ input[type="submit"]:hover,
188
+ input[type="reset"]:hover,
189
+ input[type="button"]:hover,
190
+ input[type="file"]::file-selector-button:hover {
191
+ background-color: #c9c9c9;
192
+ color: #222222;
193
+ outline: 0;
194
+ }
195
+ .button:focus-visible,
196
+ button:focus-visible,
197
+ input[type="submit"]:focus-visible,
198
+ input[type="reset"]:focus-visible,
199
+ input[type="button"]:focus-visible,
200
+ input[type="file"]::file-selector-button:focus-visible {
201
+ outline-style: solid;
202
+ outline-width: 2px;
203
+ }
204
+ textarea,
205
+ select,
206
+ input {
207
+ color: #c9c9c9;
208
+ padding: 6px 10px;
209
+ margin-bottom: 10px;
210
+ background-color: #4a4a4a;
211
+ border: 1px solid #4a4a4a;
212
+ border-radius: 4px;
213
+ box-shadow: none;
214
+ box-sizing: border-box;
215
+ }
216
+ textarea:focus,
217
+ select:focus,
218
+ input:focus {
219
+ border: 1px solid #ffffff;
220
+ outline: 0;
221
+ }
222
+ input[type="checkbox"]:focus {
223
+ outline: 1px dotted #ffffff;
224
+ }
225
+ label,
226
+ legend,
227
+ fieldset {
228
+ display: block;
229
+ margin-bottom: 0.5rem;
230
+ font-weight: 600;
231
+ }
public/css/sakura.css ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* modified https://github.com/oxalorg/sakura */
2
+ :root {
3
+ --accent-color: #4a4a4a;
4
+ --accent-color-hover: #5a5a5a;
5
+ --link-color: #58739c;
6
+ --link-visted-color: #6f5e6f;
7
+ }
8
+ html {
9
+ font-size: 62.5%;
10
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
11
+ "Helvetica Neue", Arial, "Noto Sans", sans-serif;
12
+ }
13
+ body {
14
+ font-size: 1.8rem;
15
+ line-height: 1.618;
16
+ max-width: 38em;
17
+ margin: auto;
18
+ color: #4a4a4a;
19
+ background-color: #f9f9f9;
20
+ padding: 13px;
21
+ }
22
+ @media (max-width: 684px) {
23
+ body {
24
+ font-size: 1.53rem;
25
+ }
26
+ }
27
+ @media (max-width: 382px) {
28
+ body {
29
+ font-size: 1.35rem;
30
+ }
31
+ }
32
+ h1,
33
+ h2,
34
+ h3,
35
+ h4,
36
+ h5,
37
+ h6 {
38
+ line-height: 1.1;
39
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
40
+ "Helvetica Neue", Arial, "Noto Sans", sans-serif;
41
+ font-weight: 700;
42
+ margin-top: 3rem;
43
+ margin-bottom: 1.5rem;
44
+ overflow-wrap: break-word;
45
+ word-wrap: break-word;
46
+ -ms-word-break: break-all;
47
+ word-break: break-word;
48
+ }
49
+ h1 {
50
+ font-size: 2.35em;
51
+ }
52
+ h2 {
53
+ font-size: 2em;
54
+ }
55
+ h3 {
56
+ font-size: 1.75em;
57
+ }
58
+ h4 {
59
+ font-size: 1.5em;
60
+ }
61
+ h5 {
62
+ font-size: 1.25em;
63
+ }
64
+ h6 {
65
+ font-size: 1em;
66
+ }
67
+ p {
68
+ margin-top: 0;
69
+ margin-bottom: 2.5rem;
70
+ }
71
+ small,
72
+ sub,
73
+ sup {
74
+ font-size: 75%;
75
+ }
76
+ hr {
77
+ border-color: var(--accent-color);
78
+ }
79
+ a {
80
+ text-decoration: none;
81
+ color: var(--link-color);
82
+ }
83
+ a:visited {
84
+ color: var(--link-visted-color);
85
+ }
86
+ a:hover {
87
+ color: var(--accent-color-hover);
88
+ text-decoration: underline;
89
+ }
90
+ ul {
91
+ padding-left: 1.4em;
92
+ margin-top: 0;
93
+ margin-bottom: 2.5rem;
94
+ }
95
+ li {
96
+ margin-bottom: 0.4em;
97
+ }
98
+ blockquote {
99
+ margin-left: 0;
100
+ margin-right: 0;
101
+ padding-left: 1em;
102
+ padding-top: 0.8em;
103
+ padding-bottom: 0.8em;
104
+ padding-right: 0.8em;
105
+ border-left: 5px solid var(--accent-color);
106
+ margin-bottom: 2.5rem;
107
+ background-color: #f1f1f1;
108
+ }
109
+ blockquote p {
110
+ margin-bottom: 0;
111
+ }
112
+ img,
113
+ video {
114
+ height: auto;
115
+ max-width: 100%;
116
+ margin-top: 0;
117
+ margin-bottom: 2.5rem;
118
+ }
119
+ pre {
120
+ background-color: #f1f1f1;
121
+ display: block;
122
+ padding: 1em;
123
+ overflow-x: auto;
124
+ margin-top: 0;
125
+ margin-bottom: 2.5rem;
126
+ font-size: 0.9em;
127
+ }
128
+ code,
129
+ kbd,
130
+ samp {
131
+ font-size: 0.9em;
132
+ padding: 0 0.5em;
133
+ background-color: #f1f1f1;
134
+ white-space: pre-wrap;
135
+ }
136
+ pre > code {
137
+ padding: 0;
138
+ background-color: transparent;
139
+ white-space: pre;
140
+ font-size: 1em;
141
+ }
142
+ table {
143
+ text-align: justify;
144
+ width: 100%;
145
+ border-collapse: collapse;
146
+ margin-bottom: 2rem;
147
+ }
148
+ td,
149
+ th {
150
+ padding: 0.5em;
151
+ border-bottom: 1px solid #f1f1f1;
152
+ }
153
+ input,
154
+ textarea {
155
+ border: 1px solid #4a4a4a;
156
+ }
157
+ input:focus,
158
+ textarea:focus {
159
+ border: 1px solid var(--accent-color);
160
+ }
161
+ textarea {
162
+ width: 100%;
163
+ }
164
+ .button,
165
+ button,
166
+ input[type="submit"],
167
+ input[type="reset"],
168
+ input[type="button"],
169
+ input[type="file"]::file-selector-button {
170
+ display: inline-block;
171
+ padding: 5px 10px;
172
+ text-align: center;
173
+ text-decoration: none;
174
+ white-space: nowrap;
175
+ background-color: var(--accent-color);
176
+ color: #f9f9f9;
177
+ border-radius: 2px;
178
+ border: 1px solid var(--accent-color);
179
+ cursor: pointer;
180
+ box-sizing: border-box;
181
+ }
182
+ .button[disabled],
183
+ button[disabled],
184
+ input[type="submit"][disabled],
185
+ input[type="reset"][disabled],
186
+ input[type="button"][disabled],
187
+ input[type="file"][disabled] {
188
+ cursor: default;
189
+ opacity: 0.5;
190
+ }
191
+ .button:hover,
192
+ button:hover,
193
+ input[type="submit"]:hover,
194
+ input[type="reset"]:hover,
195
+ input[type="button"]:hover,
196
+ input[type="file"]::file-selector-button:hover {
197
+ background-color: var(--accent-color-hover);
198
+ color: #f9f9f9;
199
+ outline: 0;
200
+ }
201
+ .button:focus-visible,
202
+ button:focus-visible,
203
+ input[type="submit"]:focus-visible,
204
+ input[type="reset"]:focus-visible,
205
+ input[type="button"]:focus-visible,
206
+ input[type="file"]::file-selector-button:focus-visible {
207
+ outline-style: solid;
208
+ outline-width: 2px;
209
+ }
210
+ textarea,
211
+ select,
212
+ input {
213
+ color: #4a4a4a;
214
+ padding: 6px 10px;
215
+ margin-bottom: 10px;
216
+ background-color: #f1f1f1;
217
+ border: 1px solid #f1f1f1;
218
+ border-radius: 4px;
219
+ box-shadow: none;
220
+ box-sizing: border-box;
221
+ }
222
+ textarea:focus,
223
+ select:focus,
224
+ input:focus {
225
+ border: 1px solid var(--accent-color);
226
+ outline: 0;
227
+ }
228
+ input[type="checkbox"]:focus {
229
+ outline: 1px dotted var(--accent-color);
230
+ }
231
+ label,
232
+ legend,
233
+ fieldset {
234
+ display: block;
235
+ margin-bottom: 0.5rem;
236
+ font-weight: 600;
237
+ }
public/js/hash-worker.js ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ importScripts(
2
+ "https://cdn.jsdelivr.net/npm/hash-wasm@4.11.0/dist/argon2.umd.min.js"
3
+ );
4
+
5
+ let active = false;
6
+ let nonce = 0;
7
+ let signature = "";
8
+ let lastNotify = 0;
9
+ let hashesSinceLastNotify = 0;
10
+ let params = {
11
+ salt: null,
12
+ hashLength: 0,
13
+ iterations: 0,
14
+ memorySize: 0,
15
+ parallelism: 0,
16
+ targetValue: BigInt(0),
17
+ safariFix: false,
18
+ };
19
+
20
+ self.onmessage = async (event) => {
21
+ const { data } = event;
22
+ switch (data.type) {
23
+ case "stop":
24
+ active = false;
25
+ self.postMessage({ type: "paused", hashes: hashesSinceLastNotify });
26
+ return;
27
+ case "start":
28
+ active = true;
29
+ signature = data.signature;
30
+ nonce = data.nonce;
31
+
32
+ const c = data.challenge;
33
+ const salt = new Uint8Array(c.s.length / 2);
34
+ for (let i = 0; i < c.s.length; i += 2) {
35
+ salt[i / 2] = parseInt(c.s.slice(i, i + 2), 16);
36
+ }
37
+
38
+ params = {
39
+ salt: salt,
40
+ hashLength: c.hl,
41
+ iterations: c.t,
42
+ memorySize: c.m,
43
+ parallelism: c.p,
44
+ targetValue: BigInt(c.d.slice(0, -1)),
45
+ safariFix: data.isMobileWebkit,
46
+ };
47
+
48
+ console.log("Started", params);
49
+ self.postMessage({ type: "started" });
50
+ setTimeout(solve, 0);
51
+ break;
52
+ }
53
+ };
54
+
55
+ const doHash = async (password) => {
56
+ const { salt, hashLength, iterations, memorySize, parallelism } = params;
57
+ return await self.hashwasm.argon2id({
58
+ password,
59
+ salt,
60
+ hashLength,
61
+ iterations,
62
+ memorySize,
63
+ parallelism,
64
+ });
65
+ };
66
+
67
+ const checkHash = (hash) => {
68
+ const { targetValue } = params;
69
+ const hashValue = BigInt(`0x${hash}`);
70
+ return hashValue <= targetValue;
71
+ };
72
+
73
+ const solve = async () => {
74
+ if (!active) {
75
+ console.log("Stopped solver", nonce);
76
+ return;
77
+ }
78
+
79
+ // Safari WASM doesn't like multiple calls in one worker
80
+ const batchSize = 1;
81
+ const batch = [];
82
+ for (let i = 0; i < batchSize; i++) {
83
+ batch.push(nonce++);
84
+ }
85
+
86
+ try {
87
+ const results = await Promise.all(
88
+ batch.map(async (nonce) => {
89
+ const hash = await doHash(String(nonce));
90
+ return { hash, nonce };
91
+ })
92
+ );
93
+ hashesSinceLastNotify += batchSize;
94
+
95
+ const solution = results.find(({ hash }) => checkHash(hash));
96
+ if (solution) {
97
+ console.log("Solution found", solution, params.salt);
98
+ self.postMessage({ type: "solved", nonce: solution.nonce });
99
+ active = false;
100
+ } else {
101
+ if (Date.now() - lastNotify >= 500) {
102
+ console.log("Last nonce", nonce, "Hashes", hashesSinceLastNotify);
103
+ self.postMessage({ type: "progress", hashes: hashesSinceLastNotify });
104
+ lastNotify = Date.now();
105
+ hashesSinceLastNotify = 0;
106
+ }
107
+ setTimeout(solve, 10);
108
+ }
109
+ } catch (error) {
110
+ console.error("Error", error);
111
+ const stack = error.stack;
112
+ const debug = {
113
+ stack,
114
+ lastNonce: nonce,
115
+ targetValue: params.targetValue,
116
+ };
117
+ self.postMessage({ type: "error", error: error.message, debug });
118
+ active = false;
119
+ }
120
+ };
render.yaml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ - type: web
3
+ name: oai-proxy
4
+ env: docker
5
+ repo: https://gitlab.com/khanon/oai-proxy.git
6
+ region: oregon
7
+ plan: free
8
+ branch: main
9
+ healthCheckPath: /health
10
+ dockerfilePath: ./docker/render/Dockerfile
scripts/migrate.ts ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Database from "better-sqlite3";
2
+ import { DATABASE_VERSION, migrateDatabase } from "../src/shared/database";
3
+ import { logger } from "../src/logger";
4
+ import { config } from "../src/config";
5
+
6
+ const log = logger.child({ module: "scripts/migrate" });
7
+
8
+ async function runMigration() {
9
+ let targetVersion = Number(process.argv[2]) || undefined;
10
+
11
+ if (!targetVersion) {
12
+ log.info("Enter target version or leave empty to use the latest version.");
13
+ process.stdin.resume();
14
+ process.stdin.setEncoding("utf8");
15
+ const input = await new Promise<string>((resolve) => {
16
+ process.stdin.on("data", (text) => {
17
+ resolve((String(text) || "").trim());
18
+ });
19
+ });
20
+ process.stdin.pause();
21
+ targetVersion = Number(input);
22
+ if (!targetVersion) {
23
+ targetVersion = DATABASE_VERSION;
24
+ }
25
+ }
26
+
27
+ const db = new Database(config.sqliteDataPath, {
28
+ verbose: (msg, ...args) => log.debug({ args }, String(msg)),
29
+ });
30
+
31
+ const currentVersion = db.pragma("user_version", { simple: true });
32
+ log.info({ currentVersion, targetVersion }, "Running migrations.");
33
+ migrateDatabase(targetVersion, db);
34
+ }
35
+
36
+ runMigration().catch((error) => {
37
+ log.error(error, "Migration failed.");
38
+ process.exit(1);
39
+ });
scripts/oai-reverse-proxy.http ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OAI Reverse Proxy
2
+
3
+ ###
4
+ # @name OpenAI -- Chat Completions
5
+ POST https://api.openai.com/v1/chat/completions
6
+ Authorization: Bearer {{oai-key-1}}
7
+ Content-Type: application/json
8
+
9
+ {
10
+ "model": "gpt-3.5-turbo",
11
+ "max_tokens": 30,
12
+ "stream": false,
13
+ "messages": [
14
+ {
15
+ "role": "user",
16
+ "content": "This is a test prompt."
17
+ }
18
+ ]
19
+ }
20
+
21
+ ###
22
+ # @name OpenAI -- Text Completions
23
+ POST https://api.openai.com/v1/completions
24
+ Authorization: Bearer {{oai-key-1}}
25
+ Content-Type: application/json
26
+
27
+ {
28
+ "model": "gpt-3.5-turbo-instruct",
29
+ "max_tokens": 30,
30
+ "stream": false,
31
+ "prompt": "This is a test prompt where"
32
+ }
33
+
34
+ ###
35
+ # @name OpenAI -- Create Embedding
36
+ POST https://api.openai.com/v1/embeddings
37
+ Authorization: Bearer {{oai-key-1}}
38
+ Content-Type: application/json
39
+
40
+ {
41
+ "model": "text-embedding-ada-002",
42
+ "input": "This is a test embedding input."
43
+ }
44
+
45
+ ###
46
+ # @name OpenAI -- Get Organizations
47
+ GET https://api.openai.com/v1/organizations
48
+ Authorization: Bearer {{oai-key-1}}
49
+
50
+ ###
51
+ # @name OpenAI -- Get Models
52
+ GET https://api.openai.com/v1/models
53
+ Authorization: Bearer {{oai-key-1}}
54
+
55
+ ###
56
+ # @name Azure OpenAI -- Chat Completions
57
+ POST https://{{azu-resource-name}}.openai.azure.com/openai/deployments/{{azu-deployment-id}}/chat/completions?api-version=2023-09-01-preview
58
+ api-key: {{azu-key-1}}
59
+ Content-Type: application/json
60
+
61
+ {
62
+ "max_tokens": 1,
63
+ "stream": false,
64
+ "messages": [
65
+ {
66
+ "role": "user",
67
+ "content": "This is a test prompt."
68
+ }
69
+ ]
70
+ }
71
+
72
+ ###
73
+ # @name Proxy / OpenAI -- Get Models
74
+ GET {{proxy-host}}/proxy/openai/v1/models
75
+ Authorization: Bearer {{proxy-key}}
76
+
77
+ ###
78
+ # @name Proxy / OpenAI -- Native Chat Completions
79
+ POST {{proxy-host}}/proxy/openai/chat/completions
80
+ Authorization: Bearer {{proxy-key}}
81
+ Content-Type: application/json
82
+
83
+ {
84
+ "model": "gpt-4-1106-preview",
85
+ "max_tokens": 20,
86
+ "stream": true,
87
+ "temperature": 1,
88
+ "seed": 123,
89
+ "messages": [
90
+ {
91
+ "role": "user",
92
+ "content": "phrase one"
93
+ }
94
+ ]
95
+ }
96
+
97
+ ###
98
+ # @name Proxy / OpenAI -- Native Text Completions
99
+ POST {{proxy-host}}/proxy/openai/v1/turbo-instruct/chat/completions
100
+ Authorization: Bearer {{proxy-key}}
101
+ Content-Type: application/json
102
+
103
+ {
104
+ "model": "gpt-3.5-turbo-instruct",
105
+ "max_tokens": 20,
106
+ "temperature": 0,
107
+ "prompt": "Genshin Impact is a game about",
108
+ "stream": false
109
+ }
110
+
111
+ ###
112
+ # @name Proxy / OpenAI -- Chat-to-Text API Translation
113
+ # Accepts a chat completion request and reformats it to work with the text completion API. `model` is ignored.
114
+ POST {{proxy-host}}/proxy/openai/turbo-instruct/chat/completions
115
+ Authorization: Bearer {{proxy-key}}
116
+ Content-Type: application/json
117
+
118
+ {
119
+ "model": "gpt-4",
120
+ "max_tokens": 20,
121
+ "stream": true,
122
+ "messages": [
123
+ {
124
+ "role": "user",
125
+ "content": "What is the name of the fourth president of the united states?"
126
+ },
127
+ {
128
+ "role": "assistant",
129
+ "content": "That would be George Washington."
130
+ },
131
+ {
132
+ "role": "user",
133
+ "content": "I don't think that's right..."
134
+ }
135
+ ]
136
+ }
137
+
138
+ ###
139
+ # @name Proxy / OpenAI -- Create Embedding
140
+ POST {{proxy-host}}/proxy/openai/embeddings
141
+ Authorization: Bearer {{proxy-key}}
142
+ Content-Type: application/json
143
+
144
+ {
145
+ "model": "text-embedding-ada-002",
146
+ "input": "This is a test embedding input."
147
+ }
148
+
149
+
150
+ ###
151
+ # @name Proxy / Anthropic -- Native Completion (old API)
152
+ POST {{proxy-host}}/proxy/anthropic/v1/complete
153
+ Authorization: Bearer {{proxy-key}}
154
+ anthropic-version: 2023-01-01
155
+ Content-Type: application/json
156
+
157
+ {
158
+ "model": "claude-v1.3",
159
+ "max_tokens_to_sample": 20,
160
+ "temperature": 0.2,
161
+ "stream": true,
162
+ "prompt": "What is genshin impact\n\n:Assistant:"
163
+ }
164
+
165
+ ###
166
+ # @name Proxy / Anthropic -- Native Completion (2023-06-01 API)
167
+ POST {{proxy-host}}/proxy/anthropic/v1/complete
168
+ Authorization: Bearer {{proxy-key}}
169
+ anthropic-version: 2023-06-01
170
+ Content-Type: application/json
171
+
172
+ {
173
+ "model": "claude-v1.3",
174
+ "max_tokens_to_sample": 20,
175
+ "temperature": 0.2,
176
+ "stream": true,
177
+ "prompt": "What is genshin impact\n\n:Assistant:"
178
+ }
179
+
180
+ ###
181
+ # @name Proxy / Anthropic -- OpenAI-to-Anthropic API Translation
182
+ POST {{proxy-host}}/proxy/anthropic/v1/chat/completions
183
+ Authorization: Bearer {{proxy-key}}
184
+ #anthropic-version: 2023-06-01
185
+ Content-Type: application/json
186
+
187
+ {
188
+ "model": "gpt-3.5-turbo",
189
+ "max_tokens": 20,
190
+ "stream": false,
191
+ "temperature": 0,
192
+ "messages": [
193
+ {
194
+ "role": "user",
195
+ "content": "What is genshin impact"
196
+ }
197
+ ]
198
+ }
199
+
200
+ ###
201
+ # @name Proxy / AWS Claude -- Native Completion
202
+ POST {{proxy-host}}/proxy/aws/claude/v1/complete
203
+ Authorization: Bearer {{proxy-key}}
204
+ anthropic-version: 2023-01-01
205
+ Content-Type: application/json
206
+
207
+ {
208
+ "model": "claude-v2",
209
+ "max_tokens_to_sample": 10,
210
+ "temperature": 0,
211
+ "stream": true,
212
+ "prompt": "What is genshin impact\n\n:Assistant:"
213
+ }
214
+
215
+ ###
216
+ # @name Proxy / AWS Claude -- OpenAI-to-Anthropic API Translation
217
+ POST {{proxy-host}}/proxy/aws/claude/chat/completions
218
+ Authorization: Bearer {{proxy-key}}
219
+ Content-Type: application/json
220
+
221
+ {
222
+ "model": "gpt-3.5-turbo",
223
+ "max_tokens": 50,
224
+ "stream": true,
225
+ "messages": [
226
+ {
227
+ "role": "user",
228
+ "content": "What is genshin impact?"
229
+ }
230
+ ]
231
+ }
232
+
233
+ ###
234
+ # @name Proxy / GCP Claude -- Native Completion
235
+ POST {{proxy-host}}/proxy/gcp/claude/v1/complete
236
+ Authorization: Bearer {{proxy-key}}
237
+ anthropic-version: 2023-01-01
238
+ Content-Type: application/json
239
+
240
+ {
241
+ "model": "claude-v2",
242
+ "max_tokens_to_sample": 10,
243
+ "temperature": 0,
244
+ "stream": true,
245
+ "prompt": "What is genshin impact\n\n:Assistant:"
246
+ }
247
+
248
+ ###
249
+ # @name Proxy / GCP Claude -- OpenAI-to-Anthropic API Translation
250
+ POST {{proxy-host}}/proxy/gcp/claude/chat/completions
251
+ Authorization: Bearer {{proxy-key}}
252
+ Content-Type: application/json
253
+
254
+ {
255
+ "model": "gpt-3.5-turbo",
256
+ "max_tokens": 50,
257
+ "stream": true,
258
+ "messages": [
259
+ {
260
+ "role": "user",
261
+ "content": "What is genshin impact?"
262
+ }
263
+ ]
264
+ }
265
+
266
+ ###
267
+ # @name Proxy / Azure OpenAI -- Native Chat Completions
268
+ POST {{proxy-host}}/proxy/azure/openai/chat/completions
269
+ Authorization: Bearer {{proxy-key}}
270
+ Content-Type: application/json
271
+
272
+ {
273
+ "model": "gpt-4",
274
+ "max_tokens": 20,
275
+ "stream": true,
276
+ "temperature": 1,
277
+ "seed": 2,
278
+ "messages": [
279
+ {
280
+ "role": "user",
281
+ "content": "Hi what is the name of the fourth president of the united states?"
282
+ },
283
+ {
284
+ "role": "assistant",
285
+ "content": "That would be George Washington."
286
+ },
287
+ {
288
+ "role": "user",
289
+ "content": "That's not right."
290
+ }
291
+ ]
292
+ }
293
+
294
+ ###
295
+ # @name Proxy / Google AI -- OpenAI-to-Google AI API Translation
296
+ POST {{proxy-host}}/proxy/google-ai/v1/chat/completions
297
+ Authorization: Bearer {{proxy-key}}
298
+ Content-Type: application/json
299
+
300
+ {
301
+ "model": "gpt-4",
302
+ "max_tokens": 42,
303
+ "messages": [
304
+ {
305
+ "role": "user",
306
+ "content": "Hi what is the name of the fourth president of the united states?"
307
+ }
308
+ ]
309
+ }
scripts/seed-events.ts ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Database from "better-sqlite3";
2
+ import { v4 as uuidv4 } from "uuid";
3
+ import { config } from "../src/config";
4
+
5
+ function generateRandomIP() {
6
+ return (
7
+ Math.floor(Math.random() * 255) +
8
+ "." +
9
+ Math.floor(Math.random() * 255) +
10
+ "." +
11
+ Math.floor(Math.random() * 255) +
12
+ "." +
13
+ Math.floor(Math.random() * 255)
14
+ );
15
+ }
16
+
17
+ function generateRandomDate() {
18
+ const end = new Date();
19
+ const start = new Date(end);
20
+ start.setDate(end.getDate() - 90);
21
+ const randomDate = new Date(
22
+ start.getTime() + Math.random() * (end.getTime() - start.getTime())
23
+ );
24
+ return randomDate.toISOString();
25
+ }
26
+
27
+ function generateMockSHA256() {
28
+ const characters = 'abcdef0123456789';
29
+ let hash = '';
30
+
31
+ for (let i = 0; i < 64; i++) {
32
+ const randomIndex = Math.floor(Math.random() * characters.length);
33
+ hash += characters[randomIndex];
34
+ }
35
+
36
+ return hash;
37
+ }
38
+
39
+ function getRandomModelFamily() {
40
+ const modelFamilies = [
41
+ "turbo",
42
+ "gpt4",
43
+ "gpt4-32k",
44
+ "gpt4-turbo",
45
+ "claude",
46
+ "claude-opus",
47
+ "gemini-pro",
48
+ "mistral-tiny",
49
+ "mistral-small",
50
+ "mistral-medium",
51
+ "mistral-large",
52
+ "aws-claude",
53
+ "aws-claude-opus",
54
+ "gcp-claude",
55
+ "gcp-claude-opus",
56
+ "azure-turbo",
57
+ "azure-gpt4",
58
+ "azure-gpt4-32k",
59
+ "azure-gpt4-turbo",
60
+ "dall-e",
61
+ "azure-dall-e",
62
+ ];
63
+ return modelFamilies[Math.floor(Math.random() * modelFamilies.length)];
64
+ }
65
+
66
+ (async () => {
67
+ const db = new Database(config.sqliteDataPath);
68
+ const numRows = 100;
69
+ const insertStatement = db.prepare(`
70
+ INSERT INTO events (type, ip, date, model, family, hashes, userToken, inputTokens, outputTokens)
71
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
72
+ `);
73
+
74
+ const users = Array.from({ length: 10 }, () => uuidv4());
75
+ function getRandomUser() {
76
+ return users[Math.floor(Math.random() * users.length)];
77
+ }
78
+
79
+ const transaction = db.transaction(() => {
80
+ for (let i = 0; i < numRows; i++) {
81
+ insertStatement.run(
82
+ "chat_completion",
83
+ generateRandomIP(),
84
+ generateRandomDate(),
85
+ getRandomModelFamily() + "-" + Math.floor(Math.random() * 100),
86
+ getRandomModelFamily(),
87
+ Array.from(
88
+ { length: Math.floor(Math.random() * 10) },
89
+ generateMockSHA256
90
+ ).join(","),
91
+ getRandomUser(),
92
+ Math.floor(Math.random() * 500),
93
+ Math.floor(Math.random() * 6000)
94
+ );
95
+ }
96
+ });
97
+
98
+ transaction();
99
+
100
+ console.log(`Inserted ${numRows} rows into the events table.`);
101
+ db.close();
102
+ })();
scripts/test-aws-signing.ts ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // uses the aws sdk to sign a request, then uses axios to send it to the bedrock REST API manually
2
+ import axios from "axios";
3
+ import { Sha256 } from "@aws-crypto/sha256-js";
4
+ import { SignatureV4 } from "@smithy/signature-v4";
5
+ import { HttpRequest } from "@smithy/protocol-http";
6
+
7
+ const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID!;
8
+ const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY!;
9
+
10
+ // Copied from amazon bedrock docs
11
+
12
+ // List models
13
+ // ListFoundationModels
14
+ // Service: Amazon Bedrock
15
+ // List of Bedrock foundation models that you can use. For more information, see Foundation models in the
16
+ // Bedrock User Guide.
17
+ // Request Syntax
18
+ // GET /foundation-models?
19
+ // byCustomizationType=byCustomizationType&byInferenceType=byInferenceType&byOutputModality=byOutputModality&byProvider=byProvider
20
+ // HTTP/1.1
21
+ // URI Request Parameters
22
+ // The request uses the following URI parameters.
23
+ // byCustomizationType (p. 38)
24
+ // List by customization type.
25
+ // Valid Values: FINE_TUNING
26
+ // byInferenceType (p. 38)
27
+ // List by inference type.
28
+ // Valid Values: ON_DEMAND | PROVISIONED
29
+ // byOutputModality (p. 38)
30
+ // List by output modality type.
31
+ // Valid Values: TEXT | IMAGE | EMBEDDING
32
+ // byProvider (p. 38)
33
+ // A Bedrock model provider.
34
+ // Pattern: ^[a-z0-9-]{1,63}$
35
+ // Request Body
36
+ // The request does not have a request body
37
+
38
+ // Run inference on a text model
39
+ // Send an invoke request to run inference on a Titan Text G1 - Express model. We set the accept
40
+ // parameter to accept any content type in the response.
41
+ // POST https://bedrock.us-east-1.amazonaws.com/model/amazon.titan-text-express-v1/invoke
42
+ // -H accept: */*
43
+ // -H content-type: application/json
44
+ // Payload
45
+ // {"inputText": "Hello world"}
46
+ // Example response
47
+ // Response for the above request.
48
+ // -H content-type: application/json
49
+ // Payload
50
+ // <the model response>
51
+
52
+ const AMZ_REGION = "us-east-1";
53
+ const AMZ_HOST = "invoke-bedrock.us-east-1.amazonaws.com";
54
+
55
+ async function listModels() {
56
+ const httpRequest = new HttpRequest({
57
+ method: "GET",
58
+ protocol: "https:",
59
+ hostname: AMZ_HOST,
60
+ path: "/foundation-models",
61
+ headers: { ["Host"]: AMZ_HOST },
62
+ });
63
+
64
+ const signedRequest = await signRequest(httpRequest);
65
+ const response = await axios.get(
66
+ `https://${signedRequest.hostname}${signedRequest.path}`,
67
+ { headers: signedRequest.headers }
68
+ );
69
+ console.log(response.data);
70
+ }
71
+
72
+ async function invokeModel() {
73
+ const model = "anthropic.claude-v1";
74
+ const httpRequest = new HttpRequest({
75
+ method: "POST",
76
+ protocol: "https:",
77
+ hostname: AMZ_HOST,
78
+ path: `/model/${model}/invoke`,
79
+ headers: {
80
+ ["Host"]: AMZ_HOST,
81
+ ["accept"]: "*/*",
82
+ ["content-type"]: "application/json",
83
+ },
84
+ body: JSON.stringify({
85
+ temperature: 0.5,
86
+ prompt: "\n\nHuman:Hello world\n\nAssistant:",
87
+ max_tokens_to_sample: 10,
88
+ }),
89
+ });
90
+ console.log("httpRequest", httpRequest);
91
+
92
+ const signedRequest = await signRequest(httpRequest);
93
+ const response = await axios.post(
94
+ `https://${signedRequest.hostname}${signedRequest.path}`,
95
+ signedRequest.body,
96
+ { headers: signedRequest.headers }
97
+ );
98
+ console.log(response.status);
99
+ console.log(response.headers);
100
+ console.log(response.data);
101
+ console.log("full url", response.request.res.responseUrl);
102
+ }
103
+
104
+ async function signRequest(request: HttpRequest) {
105
+ const signer = new SignatureV4({
106
+ sha256: Sha256,
107
+ credentials: {
108
+ accessKeyId: AWS_ACCESS_KEY_ID,
109
+ secretAccessKey: AWS_SECRET_ACCESS_KEY,
110
+ },
111
+ region: AMZ_REGION,
112
+ service: "bedrock",
113
+ });
114
+ return await signer.sign(request, { signingDate: new Date() });
115
+ }
116
+
117
+ // listModels();
118
+ // invokeModel();
scripts/test-concurrency.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require("axios");
2
+
3
+ const concurrentRequests = 75;
4
+ const headers = {
5
+ Authorization: "Bearer test",
6
+ "Content-Type": "application/json",
7
+ };
8
+
9
+ const payload = {
10
+ model: "gpt-4",
11
+ max_tokens: 1,
12
+ stream: false,
13
+ messages: [{ role: "user", content: "Hi" }],
14
+ };
15
+
16
+ const makeRequest = async (i) => {
17
+ try {
18
+ const response = await axios.post(
19
+ "http://localhost:7860/proxy/google-ai/v1/chat/completions",
20
+ payload,
21
+ { headers }
22
+ );
23
+ console.log(
24
+ `Req ${i} finished with status code ${response.status} and response:`,
25
+ response.data
26
+ );
27
+ } catch (error) {
28
+ const msg = error.response
29
+ console.error(`Error in req ${i}:`, error.message, msg || "");
30
+ }
31
+ };
32
+
33
+ const executeRequestsConcurrently = () => {
34
+ const promises = [];
35
+ for (let i = 1; i <= concurrentRequests; i++) {
36
+ console.log(`Starting request ${i}`);
37
+ promises.push(makeRequest(i));
38
+ }
39
+
40
+ Promise.all(promises).then(() => {
41
+ console.log("All requests finished");
42
+ });
43
+ };
44
+
45
+ executeRequestsConcurrently();
scripts/test-queue.js ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require("axios");
2
+
3
+ function randomInteger(max) {
4
+ return Math.floor(Math.random() * max + 1);
5
+ }
6
+
7
+ async function testQueue() {
8
+ const requests = Array(10).fill(undefined).map(async function() {
9
+ const maxTokens = randomInteger(2000);
10
+
11
+ const headers = {
12
+ "Authorization": "Bearer test",
13
+ "Content-Type": "application/json",
14
+ "X-Forwarded-For": `${randomInteger(255)}.${randomInteger(255)}.${randomInteger(255)}.${randomInteger(255)}`,
15
+ };
16
+
17
+ const payload = {
18
+ model: "gpt-4o-mini-2024-07-18",
19
+ max_tokens: 20 + maxTokens,
20
+ stream: false,
21
+ messages: [{role: "user", content: "You are being benchmarked regarding your reliability at outputting exact, machine-comprehensible data. Output the sentence \"The quick brown fox jumps over the lazy dog.\" Do not precede it with quotemarks or any form of preamble, and do not output anything after the sentence."}],
22
+ temperature: 0,
23
+ };
24
+
25
+ try {
26
+ const response = await axios.post(
27
+ "http://localhost:7860/proxy/openai/v1/chat/completions",
28
+ payload,
29
+ { headers }
30
+ );
31
+
32
+ if (response.status !== 200) {
33
+ console.error(`Request {$maxTokens} finished with status code ${response.status} and response`, response.data);
34
+ return;
35
+ }
36
+
37
+ const content = response.data.choices[0].message.content;
38
+
39
+ console.log(
40
+ `Request ${maxTokens} `,
41
+ content === "The quick brown fox jumps over the lazy dog." ? "OK" : `mangled: ${content}`
42
+ );
43
+ } catch (error) {
44
+ const msg = error.response;
45
+ console.error(`Error in req ${maxTokens}:`, error.message, msg || "");
46
+ }
47
+ });
48
+
49
+ await Promise.all(requests);
50
+ console.log("All requests finished");
51
+ }
52
+
53
+ testQueue();
src/admin/api/events.ts ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Router } from "express";
2
+ import { z } from "zod";
3
+ import { encodeCursor, decodeCursor } from "../../shared/utils";
4
+ import { eventsRepo } from "../../shared/database/repos/event";
5
+
6
+ const router = Router();
7
+
8
+ /**
9
+ * Returns events for the given user token.
10
+ * GET /admin/events/:token
11
+ * @query first - The number of events to return.
12
+ * @query after - The cursor to start returning events from (exclusive).
13
+ */
14
+ router.get("/:token", (req, res) => {
15
+ const schema = z.object({
16
+ token: z.string(),
17
+ first: z.coerce.number().int().positive().max(200).default(25),
18
+ after: z
19
+ .string()
20
+ .optional()
21
+ .transform((v) => {
22
+ try {
23
+ return decodeCursor(v);
24
+ } catch {
25
+ return null;
26
+ }
27
+ })
28
+ .nullable(),
29
+ sort: z.string().optional(),
30
+ });
31
+ const args = schema.safeParse({ ...req.params, ...req.query });
32
+ if (!args.success) {
33
+ return res.status(400).json({ error: args.error });
34
+ }
35
+
36
+ const data = eventsRepo
37
+ .getUserEvents(args.data.token, {
38
+ limit: args.data.first,
39
+ cursor: args.data.after,
40
+ })
41
+ .map((e) => ({ node: e, cursor: encodeCursor(e.date) }));
42
+
43
+ res.json({
44
+ data,
45
+ endCursor: data[data.length - 1]?.cursor,
46
+ });
47
+ });
48
+
49
+ export { router as eventsApiRouter };
src/admin/api/users.ts ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Router } from "express";
2
+ import { z } from "zod";
3
+ import * as userStore from "../../shared/users/user-store";
4
+ import { parseSort, sortBy } from "../../shared/utils";
5
+ import { UserPartialSchema, UserSchema } from "../../shared/users/schema";
6
+
7
+ const router = Router();
8
+
9
+ /**
10
+ * Returns a list of all users, sorted by prompt count and then last used time.
11
+ * GET /admin/users
12
+ */
13
+ router.get("/", (req, res) => {
14
+ const sort = parseSort(req.query.sort) || ["promptCount", "lastUsedAt"];
15
+ const users = userStore.getUsers().sort(sortBy(sort, false));
16
+ res.json({ users, count: users.length });
17
+ });
18
+
19
+ /**
20
+ * Returns the user with the given token.
21
+ * GET /admin/users/:token
22
+ */
23
+ router.get("/:token", (req, res) => {
24
+ const user = userStore.getUser(req.params.token);
25
+ if (!user) {
26
+ return res.status(404).json({ error: "Not found" });
27
+ }
28
+ res.json(user);
29
+ });
30
+
31
+ /**
32
+ * Creates a new user.
33
+ * Optionally accepts a JSON body containing `type`, and for temporary-type
34
+ * users, `tokenLimits` and `expiresAt` fields.
35
+ * Returns the created user's token.
36
+ * POST /admin/users
37
+ */
38
+ router.post("/", (req, res) => {
39
+ const body = req.body;
40
+
41
+ const base = z.object({
42
+ type: UserSchema.shape.type.exclude(["temporary"]).default("normal"),
43
+ });
44
+ const tempUser = base
45
+ .extend({
46
+ type: z.literal("temporary"),
47
+ expiresAt: UserSchema.shape.expiresAt,
48
+ tokenLimits: UserSchema.shape.tokenLimits,
49
+ })
50
+ .required();
51
+
52
+ const schema = z.union([base, tempUser]);
53
+ const result = schema.safeParse(body);
54
+ if (!result.success) {
55
+ return res.status(400).json({ error: result.error });
56
+ }
57
+
58
+ const token = userStore.createUser({ ...result.data });
59
+ res.json({ token });
60
+ });
61
+
62
+ /**
63
+ * Updates the user with the given token, creating them if they don't exist.
64
+ * Accepts a JSON body containing at least one field on the User type.
65
+ * Returns the upserted user.
66
+ * PUT /admin/users/:token
67
+ */
68
+ router.put("/:token", (req, res) => {
69
+ const result = UserPartialSchema.safeParse({
70
+ ...req.body,
71
+ token: req.params.token,
72
+ });
73
+ if (!result.success) {
74
+ return res.status(400).json({ error: result.error });
75
+ }
76
+ userStore.upsertUser(result.data);
77
+ res.json(userStore.getUser(req.params.token));
78
+ });
79
+
80
+ /**
81
+ * Bulk-upserts users given a list of User updates.
82
+ * Accepts a JSON body with the field `users` containing an array of updates.
83
+ * Returns an object containing the upserted users and the number of upserts.
84
+ * PUT /admin/users
85
+ */
86
+ router.put("/", (req, res) => {
87
+ const result = z.array(UserPartialSchema).safeParse(req.body.users);
88
+ if (!result.success) {
89
+ return res.status(400).json({ error: result.error });
90
+ }
91
+ const upserts = result.data.map((user) => userStore.upsertUser(user));
92
+ res.json({ upserted_users: upserts, count: upserts.length });
93
+ });
94
+
95
+ /**
96
+ * Disables the user with the given token. Optionally accepts a `disabledReason`
97
+ * query parameter.
98
+ * Returns the disabled user.
99
+ * DELETE /admin/users/:token
100
+ */
101
+ router.delete("/:token", (req, res) => {
102
+ const user = userStore.getUser(req.params.token);
103
+ const disabledReason = z
104
+ .string()
105
+ .optional()
106
+ .safeParse(req.query.disabledReason);
107
+ if (!disabledReason.success) {
108
+ return res.status(400).json({ error: disabledReason.error });
109
+ }
110
+ if (!user) {
111
+ return res.status(404).json({ error: "Not found" });
112
+ }
113
+ userStore.disableUser(req.params.token, disabledReason.data);
114
+ res.json(userStore.getUser(req.params.token));
115
+ });
116
+
117
+ export { router as usersApiRouter };
src/admin/auth.ts ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Request, Response, RequestHandler } from "express";
2
+ import { config } from "../config";
3
+
4
+ const ADMIN_KEY = config.adminKey;
5
+ const failedAttempts = new Map<string, number>();
6
+
7
+ type AuthorizeParams = { via: "cookie" | "header" };
8
+
9
+ export const authorize: ({ via }: AuthorizeParams) => RequestHandler =
10
+ ({ via }) =>
11
+ (req, res, next) => {
12
+ const bearerToken = req.headers.authorization?.slice("Bearer ".length);
13
+ const cookieToken = req.session.adminToken;
14
+ const token = via === "cookie" ? cookieToken : bearerToken;
15
+ const attempts = failedAttempts.get(req.ip) ?? 0;
16
+
17
+ if (!ADMIN_KEY) {
18
+ req.log.warn(
19
+ { ip: req.ip },
20
+ `Blocked admin request because no admin key is configured`
21
+ );
22
+ return res.status(401).json({ error: "Unauthorized" });
23
+ }
24
+
25
+ if (attempts > 5) {
26
+ req.log.warn(
27
+ { ip: req.ip, token: bearerToken },
28
+ `Blocked admin request due to too many failed attempts`
29
+ );
30
+ return res.status(401).json({ error: "Too many attempts" });
31
+ }
32
+
33
+ if (token && token === ADMIN_KEY) {
34
+ return next();
35
+ }
36
+
37
+ req.log.warn(
38
+ { ip: req.ip, attempts, invalidToken: String(token) },
39
+ `Attempted admin request with invalid token`
40
+ );
41
+ return handleFailedLogin(req, res);
42
+ };
43
+
44
+ function handleFailedLogin(req: Request, res: Response) {
45
+ const attempts = failedAttempts.get(req.ip) ?? 0;
46
+ const newAttempts = attempts + 1;
47
+ failedAttempts.set(req.ip, newAttempts);
48
+ if (req.accepts("json", "html") === "json") {
49
+ return res.status(401).json({ error: "Unauthorized" });
50
+ }
51
+ delete req.session.adminToken;
52
+ req.session.flash = { type: "error", message: `Invalid admin key.` };
53
+ return res.redirect("/admin/login");
54
+ }