├── LICENSE ├── README.md ├── assets └── img │ ├── cleaning_services.svg │ └── menu.svg ├── functions └── api │ └── completions.ts └── index.html /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Siyu Long 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # chatgpt-api-frontend 2 | Frontend of ChatGPT API, with an optional Cloudflare Pages Functions backend to proxy requests and store tokens. 3 | 4 | ## Features 5 | - [x] Chat with gpt-3.5-turbo model 6 | - [x] Stream HTTP responses 7 | - [x] Markdown support 8 | - [x] Code highlighting 9 | - [x] Save chat history 10 | - [x] Store token in backend or require user to provide it 11 | 12 | ## Usage 13 | 14 | Open `index.html`. That's it! 15 | -------------------------------------------------------------------------------- /assets/img/cleaning_services.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /assets/img/menu.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /functions/api/completions.ts: -------------------------------------------------------------------------------- 1 | export async function onRequestPost(context) { 2 | const { request, env } = context; 3 | const token = 4 | (request.headers.get("Authorization") || "").replace("Bearer ", "") || 5 | env.OPENAI_API_KEY; 6 | 7 | return fetch(env.OPENAI_API_URL, { 8 | method: "POST", 9 | headers: { 10 | Authorization: `Bearer ${token}`, 11 | "Content-Type": "application/json", 12 | }, 13 | body: request.body, 14 | }); 15 | } 16 | 17 | export async function onRequestOptions(context) { 18 | const { env } = context; 19 | if (!env.OPENAI_API_URL) return new Response("Not found", { status: 404 }); 20 | const tokenProvided = env.OPENAI_API_KEY; 21 | const headers = { 22 | "access-control-allow-origin": "*", 23 | "access-control-allow-methods": "POST", 24 | "access-control-allow-headers": "content-type", 25 | }; 26 | if (!tokenProvided) 27 | headers["access-control-allow-headers"] += ", authorization"; 28 | return new Response(null, { headers }); 29 | } 30 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | OpenAI Chat 8 | 130 | 131 | 132 |
133 | 137 | 141 | 142 | 146 | 152 | 512 | 513 | 514 | --------------------------------------------------------------------------------