Skip to content

Commit

Permalink
fix: 如果超时导致聊天无法正常记录
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin committed Apr 15, 2023
1 parent a9ae1ea commit f25e9c7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ services:
# reverse proxy, optional
API_REVERSE_PROXY: xxx
# timeout, in milliseconds, optional
TIMEOUT_MS: 60000
TIMEOUT_MS: 600000
# socks proxy, optional, effective with SOCKS_PROXY_PORT
SOCKS_PROXY_HOST: xxxx
# socks proxy port, optional, effective with SOCKS_PROXY_HOST
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ services:
# 每小时最大请求次数,可选,默认无限
MAX_REQUEST_PER_HOUR: 0
# 超时,单位毫秒,可选
TIMEOUT_MS: 60000
TIMEOUT_MS: 600000
# Socks代理,可选,和 SOCKS_PROXY_PORT 一起时生效
SOCKS_PROXY_HOST: xxx
# Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
Expand Down
2 changes: 1 addition & 1 deletion docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
# 每小时最大请求次数,可选,默认无限
MAX_REQUEST_PER_HOUR: 0
# 超时,单位毫秒,可选
TIMEOUT_MS: 60000
TIMEOUT_MS: 600000
# Socks代理,可选,和 SOCKS_PROXY_PORT 一起时生效
SOCKS_PROXY_HOST:
# Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spec:
- name: AUTH_SECRET_KEY
value: '123456'
- name: TIMEOUT_MS
value: '60000'
value: '600000'
- name: SOCKS_PROXY_HOST
value: ''
- name: SOCKS_PROXY_PORT
Expand Down
42 changes: 28 additions & 14 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ChatContext, ChatMessage } from './chatgpt'
import { chatConfig, chatReplyProcess, currentModel, initApi } from './chatgpt'
import { auth } from './middleware/auth'
import { clearConfigCache, getCacheConfig, getOriginConfig } from './storage/config'
import type { ChatOptions, Config, MailConfig, SiteConfig, UsageResponse, UserInfo } from './storage/model'
import type { ChatInfo, ChatOptions, Config, MailConfig, SiteConfig, UsageResponse, UserInfo } from './storage/model'
import { Status } from './storage/model'
import {
clearChat,
Expand Down Expand Up @@ -271,16 +271,21 @@ router.post('/chat', auth, async (req, res) => {
router.post('/chat-process', [auth, limiter], async (req, res) => {
res.setHeader('Content-type', 'application/octet-stream')

const { roomId, uuid, regenerate, prompt, options = {}, systemMessage, temperature, top_p } = req.body as RequestProps

let lastResponse
let result
let message: ChatInfo
try {
const { roomId, uuid, regenerate, prompt, options = {}, systemMessage, temperature, top_p } = req.body as RequestProps
const message = regenerate
message = regenerate
? await getChat(roomId, uuid)
: await insertChat(uuid, prompt, roomId, options as ChatOptions)
let firstChunk = true
const result = await chatReplyProcess({
result = await chatReplyProcess({
message: prompt,
lastContext: options,
process: (chat: ChatMessage) => {
lastResponse = chat
const chuck = {
id: chat.id,
conversationId: chat.conversationId,
Expand All @@ -300,7 +305,22 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
temperature,
top_p,
})
if (result.status === 'Success') {
// return the whole response including usage
res.write(`\n${JSON.stringify(result.data)}`)
}
catch (error) {
res.write(JSON.stringify(error))
}
finally {
res.end()
try {
if (result == null || result === undefined || result.status !== 'Success')
result = { data: lastResponse }

if (result.data === undefined)
// eslint-disable-next-line no-unsafe-finally
return

if (regenerate && message.options.messageId) {
const previousResponse = message.previousResponse || []
previousResponse.push({ response: message.response, options: message.options })
Expand All @@ -325,15 +345,9 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
result.data.detail.usage as UsageResponse)
}
}

// return the whole response including usage
res.write(`\n${JSON.stringify(result.data)}`)
}
catch (error) {
res.write(JSON.stringify(error))
}
finally {
res.end()
catch (error) {
global.console.log(error)
}
}
})

Expand Down
10 changes: 6 additions & 4 deletions service/src/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ export class ChatUsage {
this.roomId = roomId
this.chatId = chatId
this.messageId = messageId
this.promptTokens = usage.prompt_tokens
this.completionTokens = usage.completion_tokens
this.totalTokens = usage.total_tokens
this.estimated = usage.estimated
if (usage) {
this.promptTokens = usage.prompt_tokens
this.completionTokens = usage.completion_tokens
this.totalTokens = usage.total_tokens
this.estimated = usage.estimated
}
this.dateTime = new Date().getTime()
}
}
Expand Down
10 changes: 5 additions & 5 deletions service/src/storage/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function insertChat(uuid: number, text: string, roomId: number, opt
}

export async function getChat(roomId: number, uuid: number) {
return await chatCol.findOne({ roomId, uuid })
return await chatCol.findOne({ roomId, uuid }) as ChatInfo
}

export async function updateChat(chatId: string, response: string, messageId: string, usage: UsageResponse, previousResponse?: []) {
Expand All @@ -36,10 +36,10 @@ export async function updateChat(chatId: string, response: string, messageId: st
$set: {
'response': response,
'options.messageId': messageId,
'options.prompt_tokens': usage.prompt_tokens,
'options.completion_tokens': usage.completion_tokens,
'options.total_tokens': usage.total_tokens,
'options.estimated': usage.estimated,
'options.prompt_tokens': usage?.prompt_tokens,
'options.completion_tokens': usage?.completion_tokens,
'options.total_tokens': usage?.total_tokens,
'options.estimated': usage?.estimated,
},
}

Expand Down

0 comments on commit f25e9c7

Please sign in to comment.