fix: 调整部分问题

This commit is contained in:
ChenZhaoYu
2023-03-06 20:34:29 +08:00
parent c8fa086565
commit 50e672e79b
7 changed files with 135 additions and 160 deletions

View File

@@ -24,6 +24,9 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
if (process.env.OPENAI_API_KEY) {
const options: ChatGPTAPIOptions = {
apiKey: process.env.OPENAI_API_KEY,
completionParams: {
model: 'gpt-3.5-turbo',
},
debug: false,
}
@@ -67,28 +70,6 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
}
})()
async function chatReply(
message: string,
lastContext?: { conversationId?: string; parentMessageId?: string },
) {
if (!message)
return sendResponse({ type: 'Fail', message: 'Message is empty' })
try {
let options: SendMessageOptions = { timeoutMs }
if (lastContext)
options = { ...lastContext }
const response = await api.sendMessage(message, { ...options })
return sendResponse({ type: 'Success', data: response })
}
catch (error: any) {
return sendResponse({ type: 'Fail', message: error.message })
}
}
async function chatReplyProcess(
message: string,
lastContext?: { conversationId?: string; parentMessageId?: string },
@@ -100,8 +81,12 @@ async function chatReplyProcess(
try {
let options: SendMessageOptions = { timeoutMs }
if (lastContext)
options = { ...lastContext }
if (lastContext) {
if (apiModel === 'ChatGPTAPI')
options = { parentMessageId: lastContext.parentMessageId }
else
options = { ...lastContext }
}
const response = await api.sendMessage(message, {
...options,
@@ -113,6 +98,7 @@ async function chatReplyProcess(
return sendResponse({ type: 'Success', data: response })
}
catch (error: any) {
global.console.error(error)
return sendResponse({ type: 'Fail', message: error.message })
}
}
@@ -131,4 +117,4 @@ async function chatConfig() {
export type { ChatContext, ChatMessage }
export { chatReply, chatReplyProcess, chatConfig }
export { chatReplyProcess, chatConfig }

View File

@@ -1,6 +1,6 @@
import express from 'express'
import type { ChatContext, ChatMessage } from './chatgpt'
import { chatConfig, chatReply, chatReplyProcess } from './chatgpt'
import { chatConfig, chatReplyProcess } from './chatgpt'
const app = express()
const router = express.Router()
@@ -15,17 +15,6 @@ app.all('*', (_, res, next) => {
next()
})
router.post('/chat', async (req, res) => {
try {
const { prompt, options = {} } = req.body as { prompt: string; options?: ChatContext }
const response = await chatReply(prompt, options)
res.send(response)
}
catch (error) {
res.send(error)
}
})
router.post('/chat-process', async (req, res) => {
res.setHeader('Content-type', 'application/octet-stream')