Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
plus 帐号支持 gpt4 模型 (#20)
Browse files Browse the repository at this point in the history
* gpt-4 model

* update docs

* ignore

* @vercel/node 移到 devDependencies

* rename

* 移除axios,使用内置的fetch
  • Loading branch information
zhuweiyou committed Apr 13, 2023
1 parent 9179a66 commit 433ad3c
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
.vscode
.DS_Store
node_modules
venv
.vercel
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ RUN apk add -U tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&

WORKDIR /app
COPY . .
RUN npm install
RUN npm install --production

CMD ["npm", "start"]
14 changes: 6 additions & 8 deletions get_access_token.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import axios from 'axios'

export async function get_access_token(req, res) {
const { email, password } = req.body
if (!email || !password) {
throw new Error('invalid [email] or [password]')
}

const { data } = await axios({
const response = await fetch('https://chat.gateway.do/api/auth/login', {
method: 'POST',
url: 'https://chat.gateway.do/api/auth/login',
data: new URLSearchParams({
body: new URLSearchParams({
username: email,
password,
}),
})
if (!data.accessToken) {
throw new Error('failed to get [access_token]')
const json = await response.json()
if (!json?.accessToken) {
throw new Error(json?.detail || 'failed to get [access_token]')
}

res.json({ access_token: data.accessToken, expires: data.expires })
res.json({ access_token: json.accessToken, expires: json.expires })
}
2 changes: 1 addition & 1 deletion home.mjs → home_page.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function home(req, res) {
export function home_page(req, res) {
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.send(`
<!DOCTYPE html>
Expand Down
6 changes: 3 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import express from 'express'
import 'express-async-errors'
import morgan from 'morgan'
import { home } from './home.mjs'
import { home_page } from './home_page.mjs'
import { send_message } from './send_message.mjs'
import { catch_error } from './catch_error.mjs'
import { get_access_token } from './get_access_token.mjs'
import { catch_error } from './catch_error.mjs'

const app = express()
app.use(morgan('combined'))
app.use(express.urlencoded({ extended: false }))

app.get('/', home)
app.get('/', home_page)
app.post('/send_message', send_message)
app.post('/get_access_token', get_access_token)
app.use(catch_error)
Expand Down
468 changes: 270 additions & 198 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"start": "node index.mjs"
},
"dependencies": {
"@vercel/node": "^2.10.3",
"axios": "^1.3.5",
"chatgpt": "^5.1.2",
"chatgpt": "^5.2.2",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"@vercel/node": "^2.10.3"
}
}
31 changes: 10 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

## 更新日志

<details>
<summary>20230411</summary>
### 最近更新 20230413

- 打印请求日志
- 失败时使用 status 500, 以区分错误请求
- prompt_prefix 默认为空
</details>
- `/send_message` 增加 `model` 参数. 默认 `gpt-3.5-turbo`, 如果你是 ChatGPT Plus 账号可以传入 `gpt-4` 来切换模型
- 增加 API 演示站点 <https://zhuweiyou-chatgpt-api.vercel.app>

<details>
<summary>20230409</summary>

- `增加` 使用 API 登录 `/get_access_token`
</details>

<details>
<summary>20230408</summary>

- `修改` `reverse_proxy` 内置为 `https://api.pawan.krd/backend-api/conversation`
</details>
### [历史更新](https://github.com/zhuweiyou/chatgpt-api/releases)

## 部署方式

Expand All @@ -31,15 +18,15 @@
### 方式一

```bash
docker run -d -p 3000:3000 zhuweiyou/chatgpt-api:20230411
docker run -d -p 3000:3000 zhuweiyou/chatgpt-api:20230413
```

### 方式二

安装 `nodejs 18.x` 环境

```bash
npm install
npm install --production
npm start
```

Expand Down Expand Up @@ -94,14 +81,16 @@ POST Body 格式为 `x-www-form-urlencoded`

- `access_token`[第一步] 中获取的 access_token
- `prompt` 提问内容
- `model` 可选. 默认 `gpt-3.5-turbo`, 如果你是 ChatGPT Plus 账号可以传入 `gpt-4` 来切换模型
- `timeout` 可选. 超时时间(毫秒), 默认无限等待
- `conversation_id` 可选. 前一次 /send_message 的结果中返回, 用于上下文连续会话
- `parent_message_id` 可选. 前一次 /send_message 的结果中返回, 用于上下文连续会话
- `prompt_prefix` 可选. 默认为 ``. 比如你可以传入 `return the result in Chinese` 它会尽量用中文回答
- `prompt_suffix` 可选. 默认为 ``
- `reverse_proxy` 可选. 反向代理服务器, 用于绕过 cloudflare 人机验证
- 默认内置 `https://api.pawan.krd/backend-api/conversation` 50 req / 15 seconds (~3 r/s)
- 如果出现报错, 尝试更换 `https://bypass.churchless.tech/api/conversation` 5 req / 10 seconds by IP
1. `https://api.pawan.krd/backend-api/conversation` (当前默认, 如果出现错误尝试更换以下两个)
2. `https://bypass.churchless.tech/api/conversation`
3. `https://chat.gateway.do/api/conversation`

#### 成功响应

Expand Down
12 changes: 7 additions & 5 deletions send_message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ export async function send_message(req, res) {
conversation_id,
parent_message_id,
timeout = 0,
model = 'gpt-3.5-turbo',
} = req.body
if (!access_token || !prompt) {
throw new Error('invalid [access_token] or [prompt]')
}

const chatgpt_api = new ChatGPTUnofficialProxyAPI({
const chatgpt = new ChatGPTUnofficialProxyAPI({
accessToken: access_token,
apiReverseProxyUrl: reverse_proxy,
model,
})
const send_message_response = await chatgpt_api.sendMessage(prompt, {
const response = await chatgpt.sendMessage(prompt, {
conversationId: conversation_id,
parentMessageId: parent_message_id,
promptPrefix: prompt_prefix,
promptSuffix: prompt_suffix,
timeoutMs: Number(timeout),
})
res.json({
text: send_message_response.text,
conversation_id: send_message_response.conversationId,
parent_message_id: send_message_response.id,
text: response.text,
conversation_id: response.conversationId,
parent_message_id: response.id,
})
}

1 comment on commit 433ad3c

@vercel
Copy link

@vercel vercel bot commented on 433ad3c Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.