├── .gitignore
├── LICENSE
├── README.md
├── about.html
├── assets
├── css
│ └── style.css
├── img
│ └── Authorization.png
└── js
│ └── openai.js
├── cf_worker.js
├── cf_worker_old.js
├── check.sh
├── docs
├── cloudflare_pages.md
├── cloudflare_proxy_pages.md
├── cloudflare_workers.md
└── img
│ ├── Cloudflare_Worker1.png
│ ├── Cloudflare_Worker2.png
│ ├── Cloudflare_Worker3.png
│ ├── Cloudflare_Worker4.png
│ ├── Cloudflare_Worker5.png
│ ├── Cloudflare_Worker6.png
│ ├── Cloudflare_pages1.png
│ ├── Cloudflare_pages2.png
│ ├── Cloudflare_pages3.png
│ ├── Cloudflare_pages4.png
│ ├── Cloudflare_pages5.png
│ ├── Cloudflare_pages6.png
│ ├── Cloudflare_pages7.png
│ ├── jpg.webp
│ ├── pages
│ ├── pages1.png
│ ├── pages2.png
│ ├── pages3.png
│ ├── pages4.png
│ └── pages5.png
│ ├── post.png
│ └── worker.png
├── functions
├── helloworld.js
└── v1
│ └── [[path]].js
└── index.html
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 | .pnpm-debug.log*
9 |
10 | # Diagnostic reports (https://nodejs.org/api/report.html)
11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12 |
13 | # Runtime data
14 | pids
15 | *.pid
16 | *.seed
17 | *.pid.lock
18 |
19 | # Directory for instrumented libs generated by jscoverage/JSCover
20 | lib-cov
21 |
22 | # Coverage directory used by tools like istanbul
23 | coverage
24 | *.lcov
25 |
26 | # nyc test coverage
27 | .nyc_output
28 |
29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30 | .grunt
31 |
32 | # Bower dependency directory (https://bower.io/)
33 | bower_components
34 |
35 | # node-waf configuration
36 | .lock-wscript
37 |
38 | # Compiled binary addons (https://nodejs.org/api/addons.html)
39 | build/Release
40 |
41 | # Dependency directories
42 | node_modules/
43 | jspm_packages/
44 |
45 | # Snowpack dependency directory (https://snowpack.dev/)
46 | web_modules/
47 |
48 | # TypeScript cache
49 | *.tsbuildinfo
50 |
51 | # Optional npm cache directory
52 | .npm
53 |
54 | # Optional eslint cache
55 | .eslintcache
56 |
57 | # Optional stylelint cache
58 | .stylelintcache
59 |
60 | # Microbundle cache
61 | .rpt2_cache/
62 | .rts2_cache_cjs/
63 | .rts2_cache_es/
64 | .rts2_cache_umd/
65 |
66 | # Optional REPL history
67 | .node_repl_history
68 |
69 | # Output of 'npm pack'
70 | *.tgz
71 |
72 | # Yarn Integrity file
73 | .yarn-integrity
74 |
75 | # dotenv environment variable files
76 | .env
77 | .env.development.local
78 | .env.test.local
79 | .env.production.local
80 | .env.local
81 |
82 | # parcel-bundler cache (https://parceljs.org/)
83 | .cache
84 | .parcel-cache
85 |
86 | # Next.js build output
87 | .next
88 | out
89 |
90 | # Nuxt.js build / generate output
91 | .nuxt
92 | dist
93 |
94 | # Gatsby files
95 | .cache/
96 | # Comment in the public line in if your project uses Gatsby and not Next.js
97 | # https://nextjs.org/blog/next-9-1#public-directory-support
98 | # public
99 |
100 | # vuepress build output
101 | .vuepress/dist
102 |
103 | # vuepress v2.x temp and cache directory
104 | .temp
105 | .cache
106 |
107 | # Docusaurus cache and generated files
108 | .docusaurus
109 |
110 | # Serverless directories
111 | .serverless/
112 |
113 | # FuseBox cache
114 | .fusebox/
115 |
116 | # DynamoDB Local files
117 | .dynamodb/
118 |
119 | # TernJS port file
120 | .tern-port
121 |
122 | # Stores VSCode versions used for testing VSCode extensions
123 | .vscode-test
124 |
125 | # yarn v2
126 | .yarn/cache
127 | .yarn/unplugged
128 | .yarn/build-state.yml
129 | .yarn/install-state.gz
130 | .pnp.*
131 |
132 |
133 | test*
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 x-dr
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 | *经测试有些ip访问时会显示下图*
2 |
3 |
4 |
5 |
6 | ---
7 | ### Demo
8 |
9 | [https://chatai.451024.xyz](https://chatai.451024.xyz)
10 |
11 | > api
12 |
13 | ```url
14 | https://openai.451024.xyz
15 | ```
16 |
17 |
18 | ```url
19 | https://openai-proxy-api.pages.dev/api
20 | ```
21 | ***
22 |
23 | > 新项目 [基于OpenAI的微信机器人](https://github.com/x-dr/wechat-bot)
24 |
25 | ***
26 |
27 | ### 演示站为公共服务,如有大规模使用需求请自行部署,演示站有点不堪重负
28 |
29 | 
30 |
31 |
32 | ## 利用Cloudflare Worker中转api.openai.com
33 |
34 | 1. 新建一个 Cloudflare Worker
35 | 2. 复制 [cf_worker.js](https://cdn.jsdelivr.net/gh/x-dr/chatgptProxyAPI@main/cf_worker.js) 里的代码粘贴到 Worker 中并部署
36 | 3. 给 Worker 绑定一个没有被墙的域名
37 | 4. 使用自己的域名代替 api.openai.com
38 |
39 |
40 | **[详细教程](./docs/cloudflare_workers.md)**
41 |
42 |
43 |
44 | ***
45 |
46 |
47 |
48 | ## 使用CloudFlare Pages进行中转
49 |
50 | ### 1、部署中转API+ Openai API余额查询 (使用sess-xxxx的Authorization查询,有效时间未知)
51 |
52 | > [官方文档](https://developers.cloudflare.com/pages)
53 |
54 | 1. ~~Fork本项目~~ 点击[Use this template](https://github.com/x-dr/chatgptProxyAPI/generate)按钮创建一个新的代码库。
55 | 2. 登录到[Cloudflare](https://dash.cloudflare.com/)控制台.
56 | 3. 在帐户主页中,选择`pages`> ` Create a project` > `Connect to Git`
57 | 4. 选择你 Fork 的项目存储库,在`Set up builds and deployments`部分中,全部默认即可。
58 |
59 |
60 | 5. 点击`Save and Deploy`部署,然后点`Continue to project`即可看到访问域名
61 |
62 |
63 | > 把官方接口的`https://api.openai.com`替换为`https://xxx.pages.dev` 即可
64 |
65 | **Demo**
66 |
67 | [https://chatai.451024.xyz](https://chatai.451024.xyz)
68 |
69 | **[详细教程](./docs/cloudflare_pages.md)**
70 |
71 |
72 | ### 2、只部署中转API
73 |
74 |
75 | **[详细教程](./docs/cloudflare_proxy_pages.md)**
76 |
77 |
78 |
79 |
80 | ## docker 部署(要境外vps)
81 |
82 | > 好像不支持sse 所以不建议
83 |
84 |
e.g.
87 |
88 | ```bash
89 | docker run -itd --name openaiproxy \
90 | -p 3000:3000 \
91 | --restart=always \
92 | gindex/openaiproxy:latest
93 | ```
94 |
95 | #### 使用
96 |
97 | *api : http://vpsip:3000/proxy/v1/chat/completions*
98 |
99 | ```bash
100 | curl --location 'http://vpsip:3000/proxy/v1/chat/completions' \
101 | --header 'Authorization: Bearer sk-xxxxxxxxxxxxxxx' \
102 | --header 'Content-Type: application/json' \
103 | --data '{
104 | "model": "gpt-3.5-turbo",
105 | "messages": [{"role": "user", "content": "Hello!"}]
106 | }'
107 |
108 | ```
109 |
110 | JavaScript用fetch
120 |
121 | ```javascript
122 | const requestOptions = {
123 | method: 'POST',
124 | headers: {
125 | "Authorization": "Bearer sk-xxxxxxxxxxxx",
126 | "Content-Type": "application/json"
127 | },
128 | body: JSON.stringify({
129 | "model": "gpt-3.5-turbo",
130 | "messages": [
131 | {
132 | "role": "user",
133 | "content": "hello word"
134 | }
135 | ]
136 | })
137 | };
138 |
139 | fetch("https://openai.1rmb.tk/v1/chat/completions", requestOptions)
140 | .then(response => response.text())
141 | .then(result => console.log(result))
142 | .catch(error => console.log('error', error));
143 |
144 | ```
145 |
146 | 用python
153 |
154 | ```python
155 | import requests
156 |
157 | url = "https://openai.1rmb.tk/v1/chat/completions"
158 | api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'
159 |
160 | headers = {
161 | 'Authorization': f'Bearer {api_key}',
162 | 'Content-Type': 'application/json'
163 | }
164 |
165 | payload = {
166 | "model": "gpt-3.5-turbo",
167 | "messages": [
168 | {
169 | "role": "user",
170 | "content": "hello word"
171 | }
172 | ]
173 | }
174 |
175 | try:
176 | response = requests.post(url, headers=headers, json=payload)
177 | response.raise_for_status() # 抛出异常,如果响应码不是200
178 | data = response.json()
179 | print(data)
180 | except requests.exceptions.RequestException as e:
181 | print(f"请求错误: {e}")
182 | except json.JSONDecodeError as e:
183 | print(f"无效的 JSON 响应: {e}")
184 | ```
185 |
186 | 用nodejs chatgpt库
192 |
193 | [transitive-bullshit/chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api)
194 |
195 | ```javascript
196 | import { ChatGPTAPI } from 'chatgpt'
197 |
198 | async function example() {
199 | const api = new ChatGPTAPI({
200 | apiKey: "sk-xxxxxxxxxxxxxx",
201 | // proxy+/v1
202 | apiBaseUrl:"https://openai.1rmb.tk/v1"
203 |
204 |
205 | })
206 |
207 | const res = await api.sendMessage('Hello World!')
208 | console.log(res.text)
209 | }
210 |
211 | example()
212 |
213 | ```
214 |
215 | 查询余额
222 |
223 | ```javascript
224 | const headers = {
225 | 'content-type': 'application/json',
226 | 'Authorization': `Bearer sk-xxxxxxxxxxxxxxxxx`
227 | }
228 | // 查是否订阅
229 | const subscription = await fetch("https://openai.1rmb.tk/v1/dashboard/billing/subscription", {
230 | method: 'get',
231 | headers: headers
232 | })
233 | if (!subscription.ok) {
234 | const data = await subscription.json()
235 | // console.log(data);
236 | return data
237 | // throw new Error('API request failed')
238 | } else {
239 | const subscriptionData = await subscription.json()
240 | const endDate = subscriptionData.access_until
241 | const startDate = new Date(endDate - 90 * 24 * 60 * 60);
242 | console.log(formatDate(endDate, "YYYY-MM-DD"));
243 | console.log(formatDate(startDate, "YYYY-MM-DD"));
244 | const response = await fetch(`https://openai.1rmb.tk/v1/dashboard/billing/usage?start_date=${formatDate(startDate, "YYYY-MM-DD")}&end_date=${formatDate(endDate, "YYYY-MM-DD")}`, {
245 | method: 'get',
246 | headers: headers
247 | })
248 |
249 | const usageData = await response.json();
250 | // 账号类型
251 | const plan = subscriptionData.plan.id
252 | console.log(usageData);
253 | }
254 |
255 | ```
256 |
257 |