├── README.md └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # cloudflare-chatgpt 2 | run chatgpt in cloudflare worker, then you can call it anywhere 3 | 4 | 5 | 6 | ### How to use cloudflare 7 | 8 | cloudflare worker : https://workers.cloudflare.com/ 9 | 10 | You can have 100000 requests/day for free 11 | 12 | ### how to get cookies 13 | 14 | Go to https://chat.openai.com/chat and log in or sign up. 15 | Open dev tools. 16 | Open Application > Cookies. 17 | 18 | ![image](https://user-images.githubusercontent.com/29261120/206084836-4f86f741-c560-4d0d-92c7-6cd8960c831a.png) 19 | 20 | 21 | ### How to request data 22 | #### id && message_id :Generated using uuid, it is recommended to use v4 23 | url: xxxxxx 24 | Method : POST 25 | #### body : 26 | { 27 | "id" : "d8cc4969-23c8-4d2c-b5a9-7b85331d678c", 28 | "message" : "hello", 29 | "message_id" :"7e50d02c-4c79-407a-b8a9-56127d197c86" 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | const url = "https://chat.openai.com/api/auth/session" 4 | const ua = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36' 5 | 6 | const cook = "your cookie" 7 | 8 | 9 | 10 | async function gatherResponse() { 11 | 12 | const init = { 13 | headers: { 14 | 'cookie': `__Secure-next-auth.session-token=${cook}`, 15 | 'user-agent': ua 16 | }, 17 | }; 18 | const response = await fetch(url, init); 19 | const json = await response.json() 20 | const token = json['accessToken'] 21 | return token 22 | } 23 | 24 | async function getMessage(token, jsonData) { 25 | console.log(token) 26 | const body = { 27 | action: 'next', 28 | messages: [ 29 | { 30 | id: jsonData['id'], 31 | role: 'user', 32 | content: { 33 | content_type: 'text', 34 | parts: [jsonData['message']] 35 | } 36 | } 37 | ], 38 | model: 'text-davinci-002-render', 39 | parent_message_id: jsonData['message_id'] 40 | } 41 | const url = `https://chat.openai.com/backend-api/conversation` 42 | 43 | const init = { 44 | body: JSON.stringify(body), 45 | method: 'POST', 46 | headers: { 47 | 'Authorization': `Bearer ${token}`, 48 | 'Content-Type': 'application/json', 49 | 'user-agent': ua 50 | }, 51 | }; 52 | const response = await fetch(url, init); 53 | const json = await response.text() 54 | return json 55 | } 56 | 57 | export default { 58 | async fetch(request, env) { 59 | return await handleRequest(request) 60 | } 61 | } 62 | 63 | 64 | 65 | 66 | async function handleRequest(request) { 67 | const corsHeader = { 68 | 'Access-Control-Allow-Headers': "*", 69 | 'Access-Control-Allow-Methods': "POST", 70 | 'Access-Control-Allow-Origin': "*", 71 | } 72 | 73 | if (request.method === "OPTIONS") { 74 | return new Response("Ok", { headers: corsHeader }) 75 | } 76 | 77 | 78 | 79 | 80 | 81 | if (request.method === 'POST') { 82 | const body = await request.text() 83 | const jsonData = JSON.parse(body) 84 | const token = await gatherResponse() 85 | const json = await getMessage(token, jsonData) 86 | 87 | 88 | const headers = { 89 | 'Access-Control-Allow-Origin': '*', 90 | 'Content-type': 'application/json', 91 | }; 92 | return new Response(json, { headers }); 93 | 94 | } 95 | 96 | let response = new Response(JSON.stringify({ 'message': 'only post' })); 97 | return response; 98 | } 99 | --------------------------------------------------------------------------------