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 @@