Skip to content

Commit 55050ce

Browse files
author
Kerwin
committed
chore: api or accesstoken
1 parent 5b9daba commit 55050ce

File tree

16 files changed

+109
-60
lines changed

16 files changed

+109
-60
lines changed

README.en.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ pnpm dev
189189
- `OPENAI_API_KEY` one of two
190190
- `OPENAI_ACCESS_TOKEN` one of two, `OPENAI_API_KEY` takes precedence when both are present
191191
- `OPENAI_API_BASE_URL` optional, available when `OPENAI_API_KEY` is set
192-
- `OPENAI_API_MODEL` optional, available when `OPENAI_API_KEY` is set
192+
- `OPENAI_API_MODEL` `ChatGPTAPI` OR `ChatGPTUnofficialProxyAPI`
193+
- `OPENAI_CHAT_MODEL` gpt-4 gpt-3.5-turbo-0301
193194
- `API_REVERSE_PROXY` optional, available when `OPENAI_ACCESS_TOKEN` is set [Reference](#introduction)
194195
- `AUTH_SECRET_KEY` Access Password,optional
195196
- `TIMEOUT_MS` timeout, in milliseconds, optional
@@ -240,8 +241,10 @@ services:
240241
OPENAI_ACCESS_TOKEN: xxxxxx
241242
# api interface url, optional, available when OPENAI_API_KEY is set
242243
OPENAI_API_BASE_URL: xxxx
243-
# api model, optional, available when OPENAI_API_KEY is set
244+
# ChatGPTAPI 或者 ChatGPTUnofficialProxyAPI
244245
OPENAI_API_MODEL: xxxx
246+
# gpt-4 gpt-3.5-turbo-0301
247+
OPENAI_CHAT_MODEL: xxxx
245248
# reverse proxy, optional
246249
API_REVERSE_PROXY: xxx
247250
# timeout, in milliseconds, optional
@@ -307,7 +310,6 @@ volumes:
307310
mongodb: {}
308311
```
309312
The `OPENAI_API_BASE_URL` is optional and only used when setting the `OPENAI_API_KEY`.
310-
The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KEY`.
311313

312314
### Deployment with Railway
313315

@@ -323,7 +325,7 @@ The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KE
323325
| `OPENAI_API_KEY` | Optional | Required for `OpenAI API`. `apiKey` can be obtained from [here](https://platform.openai.com/overview). |
324326
| `OPENAI_ACCESS_TOKEN` | Optional | Required for `Web API`. `accessToken` can be obtained from [here](https://chat.openai.com/api/auth/session). |
325327
| `OPENAI_API_BASE_URL` | Optional, only for `OpenAI API` | API endpoint. |
326-
| `OPENAI_API_MODEL` | Optional, only for `OpenAI API` | API model. |
328+
| `OPENAI_API_MODEL` | `ChatGPTAPI` OR `ChatGPTUnofficialProxyAPI` | API model. |
327329
| `API_REVERSE_PROXY` | Optional, only for `Web API` | Reverse proxy address for `Web API`. [Details](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) |
328330
| `SOCKS_PROXY_HOST` | Optional, effective with `SOCKS_PROXY_PORT` | Socks proxy. |
329331
| `SOCKS_PROXY_PORT` | Optional, effective with `SOCKS_PROXY_HOST` | Socks proxy port. |

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pnpm dev
180180
`API` 可用:
181181

182182
- `OPENAI_API_KEY``OPENAI_ACCESS_TOKEN` 二选一
183-
- `OPENAI_API_MODEL` 设置模型,可选,默认:`gpt-3.5-turbo`
183+
- `OPENAI_CHAT_MODEL` 设置模型,可选,默认:`gpt-3.5-turbo`
184184
- `OPENAI_API_BASE_URL` 设置接口地址,可选,默认:`https://api.openai.com`
185185
- `OPENAI_API_DISABLE_DEBUG` 设置接口关闭 debug 日志,可选,默认:empty 不关闭
186186

@@ -245,9 +245,11 @@ services:
245245
OPENAI_ACCESS_TOKEN: xxx
246246
# API接口地址,可选,设置 OPENAI_API_KEY 时可用
247247
OPENAI_API_BASE_URL: xxx
248+
# ChatGPTAPI ChatGPTUnofficialProxyAPI
249+
OPENAI_API_MODEL: ChatGPTAPI
248250
# API模型,可选,设置 OPENAI_API_KEY 时可用,https://platform.openai.com/docs/models
249251
# gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301, text-davinci-003, text-davinci-002, code-davinci-002
250-
OPENAI_API_MODEL: xxx
252+
OPENAI_CHAT_MODEL: xxx
251253
# 反向代理,可选
252254
API_REVERSE_PROXY: xxx
253255
# 每小时最大请求次数,可选,默认无限
@@ -311,7 +313,6 @@ volumes:
311313
mongodb: {}
312314
```
313315
- `OPENAI_API_BASE_URL` 可选,设置 `OPENAI_API_KEY` 时可用
314-
- `OPENAI_API_MODEL` 可选,设置 `OPENAI_API_KEY` 时可用
315316

316317
#### 防止爬虫抓取
317318

@@ -341,7 +342,8 @@ volumes:
341342
| `OPENAI_API_KEY` | `OpenAI API` 二选一 | 使用 `OpenAI API` 所需的 `apiKey` [(获取 apiKey)](https://platform.openai.com/overview) |
342343
| `OPENAI_ACCESS_TOKEN` | `Web API` 二选一 | 使用 `Web API` 所需的 `accessToken` [(获取 accessToken)](https://chat.openai.com/api/auth/session) |
343344
| `OPENAI_API_BASE_URL` | 可选,`OpenAI API` 时可用 | `API`接口地址 |
344-
| `OPENAI_API_MODEL` | 可选,`OpenAI API` 时可用 | `API`模型 |
345+
| `OPENAI_API_MODEL` | ChatGPTAPI OR ChatGPTUnofficialProxyAPI | `API`模型 |
346+
| `OPENAI_CHAT_MODEL` | 可选,`OpenAI API` 时可用 | `API`模型 |
345347
| `API_REVERSE_PROXY` | 可选,`Web API` 时可用 | `Web API` 反向代理地址 [详情](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) |
346348
| `SOCKS_PROXY_HOST` | 可选,和 `SOCKS_PROXY_PORT` 一起时生效 | Socks代理 |
347349
| `SOCKS_PROXY_PORT` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理端口 |

docker-compose/docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ services:
1717
OPENAI_ACCESS_TOKEN:
1818
# API接口地址,可选,设置 OPENAI_API_KEY 时可用
1919
OPENAI_API_BASE_URL:
20-
# API模型,可选,设置 OPENAI_API_KEY 时可用
20+
# ChatGPTAPI 或者 ChatGPTUnofficialProxyAPI
2121
OPENAI_API_MODEL:
22+
# gpt-4 gpt-3.5-turbo-0301 等
23+
OPENAI_CHAT_MODEL:
2224
# 反向代理,可选
2325
API_REVERSE_PROXY:
2426
# 访问jwt加密参数,可选 不为空则允许登录 同时需要设置 MONGODB_URL

kubernetes/deploy.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ spec:
2828
- name: OPENAI_API_BASE_URL
2929
value: 'https://api.openai.com'
3030
- name: OPENAI_API_MODEL
31+
value: ChatGPTAPI
32+
- name: OPENAI_CHAT_MODEL
3133
value: gpt-3.5-turbo
3234
- name: API_REVERSE_PROXY
3335
value: https://ai.fakeopen.com/api/conversation

service/.env.example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ OPENAI_ACCESS_TOKEN=
77
# OpenAI API Base URL - https://api.openai.com
88
OPENAI_API_BASE_URL=
99

10-
# OpenAI API Model - https://platform.openai.com/docs/models
11-
OPENAI_API_MODEL=
10+
# ChatGPTAPI 或者 ChatGPTUnofficialProxyAPI
11+
OPENAI_API_MODEL:
12+
13+
# OpenAI Chat Model - https://platform.openai.com/docs/models
14+
OPENAI_CHAT_MODEL:
1215

1316
# set `true` to disable OpenAI API debug log
1417
OPENAI_API_DISABLE_DEBUG=

service/src/chatgpt/index.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { textAuditServices } from '../utils/textAudit'
1111
import { getCacheConfig, getOriginConfig } from '../storage/config'
1212
import { sendResponse } from '../utils'
1313
import { isNotEmptyString } from '../utils/is'
14-
import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
14+
import type { ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
1515
import type { RequestOptions } from './types'
1616

1717
const { HttpsProxyAgent } = httpsProxyAgent
@@ -27,7 +27,6 @@ const ErrorCodeMessage: Record<string, string> = {
2727
500: '[OpenAI] 服务器繁忙,请稍后再试 | Internal Server Error',
2828
}
2929

30-
let apiModel: ApiModel
3130
let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
3231
let auditService: TextAuditService
3332

@@ -38,10 +37,9 @@ export async function initApi() {
3837
if (!config.apiKey && !config.accessToken)
3938
throw new Error('Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable')
4039

41-
if (isNotEmptyString(config.apiKey)) {
40+
if (config.apiModel === 'ChatGPTAPI') {
4241
const OPENAI_API_BASE_URL = config.apiBaseUrl
43-
const OPENAI_API_MODEL = config.apiModel
44-
const model = isNotEmptyString(OPENAI_API_MODEL) ? OPENAI_API_MODEL : 'gpt-3.5-turbo'
42+
const model = config.chatModel
4543

4644
const options: ChatGPTAPIOptions = {
4745
apiKey: config.apiKey,
@@ -67,10 +65,9 @@ export async function initApi() {
6765
await setupProxy(options)
6866

6967
api = new ChatGPTAPI({ ...options })
70-
apiModel = 'ChatGPTAPI'
7168
}
7269
else {
73-
const model = isNotEmptyString(config.apiModel) ? config.apiModel : 'gpt-3.5-turbo'
70+
const model = config.chatModel
7471
const options: ChatGPTUnofficialProxyAPIOptions = {
7572
accessToken: config.accessToken,
7673
apiReverseProxyUrl: isNotEmptyString(config.reverseProxy) ? config.reverseProxy : 'https://ai.fakeopen.com/api/conversation',
@@ -81,27 +78,26 @@ export async function initApi() {
8178
await setupProxy(options)
8279

8380
api = new ChatGPTUnofficialProxyAPI({ ...options })
84-
apiModel = 'ChatGPTUnofficialProxyAPI'
8581
}
8682
}
8783

8884
async function chatReplyProcess(options: RequestOptions) {
8985
const config = await getCacheConfig()
90-
const model = isNotEmptyString(config.apiModel) ? config.apiModel : 'gpt-3.5-turbo'
86+
const model = config.chatModel
9187
const { message, lastContext, process, systemMessage, temperature, top_p } = options
9288

9389
try {
9490
const timeoutMs = (await getCacheConfig()).timeoutMs
9591
let options: SendMessageOptions = { timeoutMs }
9692

97-
if (apiModel === 'ChatGPTAPI') {
93+
if (config.apiModel === 'ChatGPTAPI') {
9894
if (isNotEmptyString(systemMessage))
9995
options.systemMessage = systemMessage
10096
options.completionParams = { model, temperature, top_p }
10197
}
10298

10399
if (lastContext != null) {
104-
if (apiModel === 'ChatGPTAPI')
100+
if (config.apiModel === 'ChatGPTAPI')
105101
options.parentMessageId = lastContext.parentMessageId
106102
else
107103
options = { ...lastContext }
@@ -265,12 +261,8 @@ async function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPI
265261
}
266262
}
267263

268-
function currentModel(): ApiModel {
269-
return apiModel
270-
}
271-
272264
initApi()
273265

274266
export type { ChatContext, ChatMessage }
275267

276-
export { chatReplyProcess, chatConfig, currentModel, containsSensitiveWords }
268+
export { chatReplyProcess, chatConfig, containsSensitiveWords }

service/src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import jwt from 'jsonwebtoken'
33
import * as dotenv from 'dotenv'
44
import type { RequestProps } from './types'
55
import type { ChatContext, ChatMessage } from './chatgpt'
6-
import { chatConfig, chatReplyProcess, containsSensitiveWords, currentModel, initApi, initAuditService } from './chatgpt'
6+
import { chatConfig, chatReplyProcess, containsSensitiveWords, initApi, initAuditService } from './chatgpt'
77
import { auth } from './middleware/auth'
88
import { clearConfigCache, getCacheConfig, getOriginConfig } from './storage/config'
99
import type { AuditConfig, ChatInfo, ChatOptions, Config, MailConfig, SiteConfig, UsageResponse, UserInfo } from './storage/model'
@@ -463,7 +463,10 @@ router.post('/session', async (req, res) => {
463463
const config = await getCacheConfig()
464464
const hasAuth = config.siteConfig.loginEnabled
465465
const allowRegister = (await getCacheConfig()).siteConfig.registerEnabled
466-
res.send({ status: 'Success', message: '', data: { auth: hasAuth, allowRegister, model: currentModel(), title: config.siteConfig.siteTitle } })
466+
if (config.apiModel !== 'ChatGPTAPI' && config.apiModel !== 'ChatGPTUnofficialProxyAPI')
467+
config.apiModel = 'ChatGPTAPI'
468+
469+
res.send({ status: 'Success', message: '', data: { auth: hasAuth, allowRegister, model: config.apiModel, title: config.siteConfig.siteTitle } })
467470
}
468471
catch (error) {
469472
res.send({ status: 'Fail', message: error.message, data: null })
@@ -604,14 +607,17 @@ router.post('/verifyadmin', async (req, res) => {
604607

605608
router.post('/setting-base', rootAuth, async (req, res) => {
606609
try {
607-
const { apiKey, apiModel, apiBaseUrl, accessToken, timeoutMs, reverseProxy, socksProxy, socksAuth, httpsProxy } = req.body as Config
610+
const { apiKey, apiModel, chatModel, apiBaseUrl, accessToken, timeoutMs, reverseProxy, socksProxy, socksAuth, httpsProxy } = req.body as Config
608611

609-
if (apiKey == null && accessToken == null)
610-
throw new Error('Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable.')
612+
if (apiModel === 'ChatGPTAPI' && !isNotEmptyString(apiKey))
613+
throw new Error('Missing OPENAI_API_KEY environment variable.')
614+
else if (!isNotEmptyString(accessToken))
615+
throw new Error('Missing OPENAI_ACCESS_TOKEN environment variable.')
611616

612617
const thisConfig = await getOriginConfig()
613618
thisConfig.apiKey = apiKey
614619
thisConfig.apiModel = apiModel
620+
thisConfig.chatModel = chatModel
615621
thisConfig.apiBaseUrl = apiBaseUrl
616622
thisConfig.accessToken = accessToken
617623
thisConfig.reverseProxy = reverseProxy

service/src/storage/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export async function getOriginConfig() {
3232
process.env.OPENAI_API_DISABLE_DEBUG === 'true',
3333
process.env.OPENAI_ACCESS_TOKEN,
3434
process.env.OPENAI_API_BASE_URL,
35-
process.env.OPENAI_API_MODEL || 'gpt-3.5-turbo',
35+
process.env.OPENAI_API_MODEL || 'ChatGPTAPI',
36+
process.env.OPENAI_CHAT_MODEL || 'gpt-3.5-turbo',
3637
process.env.API_REVERSE_PROXY,
3738
(process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT)
3839
? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`)
@@ -70,6 +71,14 @@ export async function getOriginConfig() {
7071
if (config.siteConfig.registerReview === undefined)
7172
config.siteConfig.registerReview = process.env.REGISTER_REVIEW === 'true'
7273
}
74+
if (!isNotEmptyString(config.chatModel))
75+
config.chatModel = 'gpt-3.5-turbo'
76+
if (config.apiModel !== 'ChatGPTAPI' && config.apiModel !== 'ChatGPTUnofficialProxyAPI') {
77+
if (isNotEmptyString(config.accessToken))
78+
config.apiModel = 'ChatGPTUnofficialProxyAPI'
79+
else
80+
config.apiModel = 'ChatGPTAPI'
81+
}
7382

7483
if (config.auditConfig === undefined) {
7584
config.auditConfig = new AuditConfig(

service/src/storage/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class Config {
128128
public accessToken?: string,
129129
public apiBaseUrl?: string,
130130
public apiModel?: string,
131+
public chatModel?: string,
131132
public reverseProxy?: string,
132133
public socksProxy?: string,
133134
public socksAuth?: string,

service/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ChatGPTUnofficialProxyAPIOptions {
2727

2828
export interface ModelConfig {
2929
apiModel?: ApiModel
30+
chatModel?: string
3031
reverseProxy?: string
3132
timeoutMs?: number
3233
socksProxy?: string

0 commit comments

Comments
 (0)