Skip to content

Commit

Permalink
feat: 配置文件优化,去掉非官方api支持以及非通用能力支持,前端配置简化去掉后端proxy地址配置
Browse files Browse the repository at this point in the history
  • Loading branch information
chenweiyi committed Nov 8, 2023
1 parent ebff463 commit f2f8cf7
Show file tree
Hide file tree
Showing 19 changed files with 460 additions and 794 deletions.
34 changes: 13 additions & 21 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
# Server Port
# Server Port, eg: 3000
SERVER_PORT=3000

# TIMEOUT Time
CHATGPT_REQUEST_TIMEOUT=2 * 60 * 1000
# TIMEOUT Time,eg: 2 * 60 * 1000
CHATGPT_REQUEST_TIMEOUT=

# Default is OpenAI API Base URL Or use a third-party self-built service address, such as the `endpoint` of api2d
# Default is OpenAI API Base URL Or use a third-party self-built service address,
# such as the `endpoint` of api2d
# eg: https://openai.api2d.net/v1
OPENAI_API_BASE_URL=

# Default is OpenAI API Key Or use a third-party self-built service address, such as the `endpoint` of api2d
# Default is OpenAI API Key Or use a third-party self-built service address,
# such as the `endpoint` of api2d
# eg: fkxxx
OPENAI_API_KEY=

# PROXY_ADDRESS represents an http proxy. If left blank or not passed any value, it means that the http proxy is not enabled.
PROXY_ADDRESS=

# change this to an `accessToken` extracted from the ChatGPT site's `https://chat.openai.com/api/auth/session` response
OPENAI_ACCESS_TOKEN=

# Reverse Proxy - Available on accessToken
# Default: https://ai.fakeopen.com/api/conversation
# More: https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy
API_REVERSE_PROXY=

# Third-party service API address
CUSTOM_API_URL=
# Third-party service API may need cookie
CUSTOM_COOKIE=

# CUSTOM_PROXY represents an http proxy. If left blank or not passed any value,
# it means that the http proxy is not enabled.
# eg: http://127.0.0.1:7890
CUSTOM_PROXY=
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,36 @@ Node version >= 18 is required.
In root directory, copy an `.env.example` file and rename it to `.env`, then modify its fields:

```
# OpenAI API Key - https://platform.openai.com/account/api-keys
OPENAI_API_KEY=
# Server Port, eg: 3000
SERVER_PORT=3000
# PROXY_ADDRESS represents an http proxy. If left blank or not passed any value,
# it means that the http proxy is not enabled.
PROXY_ADDRESS=
# TIMEOUT Time,eg: 2 * 60 * 1000
CHATGPT_REQUEST_TIMEOUT=
# change this to an `accessToken` extracted from the ChatGPT site's
# `https://chat.openai.com/api/auth/session` response
OPENAI_ACCESS_TOKEN=
# OpenAI API Base URL - https://api.openai.com/docs
# Or self-built service address, eg api2d: https://openai.api2d.net/v1
OPENAI_API_BASE_URL =
# Reverse Proxy - Available on accessToken
# Default: https://ai.fakeopen.com/api/conversation
API_REVERSE_PROXY=
# OpenAI API Key - https://platform.openai.com/account/api-keys
# Or self-built service key, eg api2d: fkxxx
OPENAI_API_KEY=
# Third-party service API address
CUSTOM_API_URL=
# Third-party service API may need cookie
CUSTOM_COOKIE=
# CUSTOM_PROXY represents an http proxy. If left blank or not passed any value,
# it means that the http proxy is not enabled.
CUSTOM_PROXY=
```

- `OPENAI_API_BASE_URL`: Indicates the base URL used for chatgpt, which can also use a third-party self-built service address, such as the `endpoint` of [api2d](https://api2d.com/). The default value here is `https://api.openai.com/v1`.
- `OPENAI_API_KEY`: Represents the official OpenAI [API key](https://platform.openai.com/account/api-keys) that will be used. Alternatively, you can use a third-party service key, such as [api2d](https://api2d.com/).
- `PROXY_ADDRESS`: Scientific Internet access proxy configuration, for example: http://xxx.
- `OPENAI_ACCESS_TOKEN`: OpenAI's [access_token](https://chat.openai.com/api/auth/session), The field usually use with `API_REVERSE_PROXY`,Indicates that unofficial APIs will be used to access chatgpt.
- `API_REVERSE_PROXY`: Indicates available unofficial reverse proxies. By default it adopts "https://ai.fakeopen.com/api/conversation". For details please refer [transitive-bullshit 大佬](https://github.com/transitive-bullshit/chatgpt-api/tree/main#reverse-proxy).
- `CUSTOM_API_URL`: Indicates the third party support service url.
- `CUSTOM_COOKIE`: Indicates the third party may need `cookie` info.
- ~~`PROXY_ADDRESS`: Scientific Internet access proxy configuration, for example: http://xxx.~~
- ~~`OPENAI_ACCESS_TOKEN`: OpenAI's [access_token](https://chat.openai.com/api/auth/session), The field usually use with `API_REVERSE_PROXY`,Indicates that unofficial APIs will be used to access chatgpt.~~
- ~~`API_REVERSE_PROXY`: Indicates available unofficial reverse proxies. By default it adopts "https://ai.fakeopen.com/api/conversation". For details please refer [transitive-bullshit 大佬](https://github.com/transitive-bullshit/chatgpt-api/tree/main#reverse-proxy).~~
- ~~`CUSTOM_API_URL`: Indicates the third party support service url.~~
- ~~`CUSTOM_COOKIE`: Indicates the third party may need `cookie` info.~~
- `CUSTOM_PROXY`: represents an http proxy. If left blank or not passed any value, it means that the http proxy is not enabled.

> Priority: `OPENAI_API_KEY` > `OPENAI_ACCESS_TOKEN` > `CUSTOM_API_URL`
> ~~Priority: `OPENAI_API_KEY` > `OPENAI_ACCESS_TOKEN` > `CUSTOM_API_URL`~~
## How to Develop?

Expand All @@ -68,7 +67,7 @@ Execute `npm start` in the root directory.

1. Start front-end:`pnpm run dev:fe`.
2. Start back-end:`pnpm run dev:be`.
3. Access front-end project address:`http://localhost:8000`.
3. Access front-end project address, eg:`http://localhost:8000`.

### Method 3: Get from Docker

Expand Down
47 changes: 22 additions & 25 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,38 @@
在根目录下,复制一个.env.example 文件重命名为.env,修改其中的字段:

```
# Default is OpenAI API Base URL Or use a third-party self-built service address, such as the `endpoint` of api2d
OPENAI_API_BASE_URL=
# Server Port, eg: 3000
SERVER_PORT=3000
# Default is OpenAI API Key Or use a third-party self-built service address, such as the `endpoint` of api2d
OPENAI_API_KEY=
# PROXY_ADDRESS represents an http proxy. If left blank or not passed any value, it means that the http proxy is not enabled.
PROXY_ADDRESS=
# TIMEOUT Time,eg: 2 * 60 * 1000
CHATGPT_REQUEST_TIMEOUT=
# change this to an `accessToken` extracted from the ChatGPT site's `https://chat.openai.com/api/auth/session` response
OPENAI_ACCESS_TOKEN=
# OpenAI API Base URL - https://api.openai.com/docs
# Or self-built service address, eg api2d: https://openai.api2d.net/v1
OPENAI_API_BASE_URL =
# Reverse Proxy - Available on accessToken
# Default: https://ai.fakeopen.com/api/conversation
# More: https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy
API_REVERSE_PROXY=
# OpenAI API Key - https://platform.openai.com/account/api-keys
# Or self-built service key, eg api2d: fkxxx
OPENAI_API_KEY=
# Third-party service API address
CUSTOM_API_URL=
# Third-party service API may need cookie
CUSTOM_COOKIE=
# CUSTOM_PROXY represents an http proxy. If left blank or not passed any value,
# it means that the http proxy is not enabled.
CUSTOM_PROXY=
```

# OpenAI API Base URL, default is https://api.openai.com

- `OPENAI_API_BASE_URL`: 表示使用的 chatgpt 官方的 baseUrl, 也可以使用第三方自建服务地址,比如[api2d](https://api2d.com/)`endpoint`. 这里默认使用的是 `https://api.openai.com/v1`.
- `OPENAI_API_BASE_URL`: 表示使用的 chatgpt 官方的 baseUrl, 也可以使用第三方自建服务地址,比如[api2d](https://openai.api2d.net/v1)`endpoint`. 这里默认使用的是 `https://api.openai.com/v1`.
- `OPENAI_API_KEY`: 表示会使用 openAI 的官方 [api-key](https://platform.openai.com/account/api-keys) , 也可以采用自建的第三方服务 key,比如[api2d](https://api2d.com/)
- `PROXY_ADDRESS`: 科学上网的代理配置,比如:http://xxx
- `OPENAI_ACCESS_TOKEN`: openAI 的 access_token, [这里](https://chat.openai.com/api/auth/session)是获取方式, 该字段通常和`API_REVERSE_PROXY`字段一起使用,表示会使用非官方 API 访问 chatgpt,如果配置了`OPENAI_API_KEY`字段,则当前字段不生效。
- `API_REVERSE_PROXY`: 表示可用的非官方反向代理,默认采用 `https://ai.fakeopen.com/api/conversation` , 具体请参考[chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api/tree/main#reverse-proxy)
- `CUSTOM_API_URL`: 表示第三方服务地址
- `CUSTOM_COOKIE`: 表示访问第三方服务可能需要的`cookie`字段信息

> 优先级:`OPENAI_API_KEY` > `OPENAI_ACCESS_TOKEN` > `CUSTOM_API_URL`
- ~~`PROXY_ADDRESS`: 科学上网的代理配置,比如:http://xxx~~
- ~~`OPENAI_ACCESS_TOKEN`: openAI 的 access_token, [这里](https://chat.openai.com/api/auth/session)是获取方式, 该字段通常和`API_REVERSE_PROXY`字段一起使用,表示会使用非官方 API 访问 chatgpt,如果配置了`OPENAI_API_KEY`字段,则当前字段不生效。~~
- ~~`API_REVERSE_PROXY`: 表示可用的非官方反向代理,默认采用 `https://ai.fakeopen.com/api/conversation` , 具体请参考[chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api/tree/main#reverse-proxy)~~
- ~~`CUSTOM_API_URL`: 表示第三方服务地址~~
- ~~`CUSTOM_COOKIE`: 表示访问第三方服务可能需要的`cookie`字段信息~~
- `CUSTOM_PROXY`: 表示 http 代理,如果不配置,则表示没有使用 http 代理

> ~~优先级:`OPENAI_API_KEY` > `OPENAI_ACCESS_TOKEN` > `CUSTOM_API_URL`~~
## 如何开发?

Expand Down
4 changes: 2 additions & 2 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"scripts": {
"dev": "nodemon ./src/bin/www.mts",
"debug": "cross-env DEBUG=\"server,custom,message,app\" nodemon ./src/bin/www.mts",
"start": "ts-node ./src/bin/www.mts",
"debug": "cross-env DEBUG=\"server,app,,service:*,controller:*\" nodemon ./src/bin/www.mts",
"start": "cross-env DEBUG=\"server,app,,service:*,controller:*\" ts-node ./src/bin/www.mts",
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf ./build",
"build-ts": "tsc",
Expand Down
35 changes: 35 additions & 0 deletions packages/backend/src/@types/chatgpt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
interface IRes {
id: string
text: string
conversationId: string
response_first: boolean
stream: boolean
}

type chatResponse = {
id: string
text: string
} & {
data?: null
message?: string
status?: string
}

type IResponseGptCallbacks = {
onData?: (data: any) => void
onEnd?: () => void
onError?: (e: any) => void
}

type IResponseChatGptCallbacks = {
onData?: (data: ChatMessage) => void
onEnd?: (data: ChatMessage) => void
onError?: (e: any) => void
}

type ISendChatGptDataProps = {
res: IRes
finish_reason: string | null
msg?: string
tojson?: boolean
}
2 changes: 0 additions & 2 deletions packages/backend/src/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import onerror from 'koa-onerror'
import serve from 'koa-static'
import path from 'path'

import { routerChatgpt } from './routes/completions.mjs'
import { router as message } from './routes/message.mjs'
import users from './routes/users.mjs'
import conditional from './utils/koa-conditional-get.mjs'
Expand Down Expand Up @@ -79,7 +78,6 @@ app.use(
// app.use(users.routes(), users.allowedMethods())
app.use(users.routes()).use(users.allowedMethods())
app.use(message.routes()).use(message.allowedMethods())
app.use(routerChatgpt.routes()).use(routerChatgpt.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
Expand Down
Loading

0 comments on commit f2f8cf7

Please sign in to comment.