From 86fcbbff0bcba7da231c50992f8528fff7e70f34 Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Tue, 14 Feb 2023 10:04:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E9=99=90=E6=96=87=E8=81=94?= =?UTF-8?q?=E6=83=B3=E6=94=B9=E4=B8=BA=E5=89=8D=E7=AB=AF=E8=BF=9B=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 +- service/src/chatgpt.ts | 48 ++------------- service/src/index.ts | 24 ++------ src/api/index.ts | 7 ++- src/components/business/Chat/index.vue | 83 +++++++++++++------------- src/components/business/Chat/types.ts | 12 ++++ 6 files changed, 69 insertions(+), 110 deletions(-) create mode 100644 src/components/business/Chat/types.ts diff --git a/package.json b/package.json index 0976459..c55f308 100644 --- a/package.json +++ b/package.json @@ -47,9 +47,8 @@ "vue-tsc": "^1.0.24" }, "lint-staged": { - "*.{js,ts,jsx,tsx,vue}": [ - "pnpm lint:fix", - "pnpm type-check" + "*.{ts,tsx,vue}": [ + "pnpm lint:fix" ] } } diff --git a/service/src/chatgpt.ts b/service/src/chatgpt.ts index 9e310b1..f50d1af 100644 --- a/service/src/chatgpt.ts +++ b/service/src/chatgpt.ts @@ -15,67 +15,31 @@ const apiKey = process.env.OPENAI_API_KEY if (apiKey === undefined) throw new Error('OPENAI_API_KEY is not defined') -const chatContext = new Set() - /** * More Info: https://github.com/transitive-bullshit/chatgpt-api */ -const api = new ChatGPTAPI({ apiKey }) +const api = new ChatGPTAPI({ apiKey, debug: false }) -async function chatReply(message: string) { +async function chatReply( + message: string, + lastContext?: { conversationId?: string; parentMessageId?: string }, +) { if (!message) return sendResponse({ type: 'Fail', message: 'Message is empty' }) try { - // Get the last context from the chat context let options: SendMessageOptions = {} - const lastContext = Array.from(chatContext).pop() - if (lastContext) options = { ...lastContext } const response = await api.sendMessage(message, { ...options }) - const { conversationId, id } = response - - // Add the new context to the chat context - if (conversationId && id) - chatContext.add({ conversationId, parentMessageId: id }) - return sendResponse({ type: 'Success', data: response }) } catch (error: any) { - global.console.log(error) return sendResponse({ type: 'Fail', message: error.message }) } } -async function chatReplayOne(message: string, options?: ChatContext) { - if (!message) - return sendResponse({ type: 'Fail', message: 'Message is empty' }) - - try { - let messageOptions: SendMessageOptions = {} - - if (options) { - const { conversationId, parentMessageId } = options - messageOptions = { conversationId, parentMessageId } - - const response = await api.sendMessage(message, { ...messageOptions }) - - return sendResponse({ type: 'Success', data: response }) - } - } - catch (error: any) { - return sendResponse({ type: 'Fail', message: error.message }) - } -} - -async function clearChatContext() { - // Clear the chat context - chatContext.clear() - return sendResponse({ type: 'Success', message: 'Chat context cleared' }) -} - -export { chatReply, chatReplayOne, clearChatContext } +export { chatReply } diff --git a/service/src/index.ts b/service/src/index.ts index 91ba3e5..ad2bfa3 100644 --- a/service/src/index.ts +++ b/service/src/index.ts @@ -1,35 +1,22 @@ import express from 'express' import type { ChatContext } from './chatgpt' -import { chatReplayOne, chatReply, clearChatContext } from './chatgpt' +import { chatReply } from './chatgpt' const app = express() app.use(express.json()) -app.all('*', (req, res, next) => { +app.all('*', (_, res, next) => { res.header('Access-Control-Allow-Origin', '*') res.header('Access-Control-Allow-Headers', 'Content-Type') res.header('Access-Control-Allow-Methods', '*') next() }) -app.listen(3002, () => globalThis.console.log('Server is running on port 3002')) - app.post('/chat', async (req, res) => { - try { - const { prompt } = req.body as { prompt: string } - const response = await chatReply(prompt) - res.send(response) - } - catch (error) { - res.send(error) - } -}) - -app.post('./chatOne', async (req, res) => { try { const { prompt, options = {} } = req.body as { prompt: string; options?: ChatContext } - const response = await chatReplayOne(prompt, options) + const response = await chatReply(prompt, options) res.send(response) } catch (error) { @@ -37,7 +24,4 @@ app.post('./chatOne', async (req, res) => { } }) -app.post('/clear', async (req, res) => { - const response = await clearChatContext() - res.send(response) -}) +app.listen(3002, () => globalThis.console.log('Server is running on port 3002')) diff --git a/src/api/index.ts b/src/api/index.ts index b49b52c..e0e87d6 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,9 +1,12 @@ import { post } from '@/utils/request' -export function fetchChatAPI(prompt: string) { +export function fetchChatAPI( + prompt: string, + options?: { conversationId?: string; parentMessageId?: string }, +) { return post({ url: '/chat', - data: { prompt }, + data: { prompt, options }, }) } diff --git a/src/components/business/Chat/index.vue b/src/components/business/Chat/index.vue index 0903a66..279ea44 100644 --- a/src/components/business/Chat/index.vue +++ b/src/components/business/Chat/index.vue @@ -1,51 +1,27 @@