-
+
From dd522eb404a4f03b6f9c2161c8555485f1e55b0d Mon Sep 17 00:00:00 2001
From: ChenZhaoYu <790348264@qq.com>
Date: Mon, 13 Feb 2023 15:08:55 +0800
Subject: [PATCH 11/17] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80=E6=9C=8D?=
=?UTF-8?q?=E5=8A=A1=E7=AB=AF=E8=BF=94=E5=9B=9E=E5=8F=82=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
service/src/chatgpt.ts | 64 +++++++++++++++++++++++++++-----------
service/src/index.ts | 25 ++++++++++++---
service/src/utils/index.ts | 22 +++++++++++++
3 files changed, 88 insertions(+), 23 deletions(-)
create mode 100644 service/src/utils/index.ts
diff --git a/service/src/chatgpt.ts b/service/src/chatgpt.ts
index 823324a..43ff8d0 100644
--- a/service/src/chatgpt.ts
+++ b/service/src/chatgpt.ts
@@ -1,7 +1,9 @@
import * as dotenv from 'dotenv'
+import type { SendMessageOptions } from 'chatgpt'
import { ChatGPTAPI } from 'chatgpt'
+import { sendResponse } from './utils'
-interface ChatContext {
+export interface ChatContext {
conversationId?: string
parentMessageId?: string
}
@@ -22,33 +24,57 @@ const api = new ChatGPTAPI({ apiKey })
async function chatReply(message: string) {
if (!message)
- return
-
- // Get the last context from the chat context
- // If there is a last context, add it to the options
- let options = {}
- const lastContext = Array.from(chatContext).pop()
- if (lastContext) {
- const { conversationId, parentMessageId } = lastContext
- options = { conversationId, parentMessageId }
+ 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) {
+ return sendResponse({ type: 'fail', message: error.message })
+ }
+}
+
+async function chatReplayOne(message: string, options?: ChatContext) {
+ if (!message)
+ return sendResponse({ type: 'fail', message: 'Message is empty' })
- // Send the message to the API
- const response = await api.sendMessage(message, { ...options })
+ try {
+ let messageOptions: SendMessageOptions = {}
- const { conversationId, id } = response
+ if (options) {
+ const { conversationId, parentMessageId } = options
+ messageOptions = { conversationId, parentMessageId }
- // Add the new context to the chat context
- if (conversationId && id)
- chatContext.add({ conversationId, parentMessageId: id })
+ const response = await api.sendMessage(message, { ...messageOptions })
- return response
+ 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 Promise.resolve({ message: 'Chat context cleared' })
+ return sendResponse({ type: 'success', message: 'Chat context cleared' })
}
-export { chatReply, clearChatContext }
+export { chatReply, chatReplayOne, clearChatContext }
diff --git a/service/src/index.ts b/service/src/index.ts
index d6317b5..91ba3e5 100644
--- a/service/src/index.ts
+++ b/service/src/index.ts
@@ -1,5 +1,6 @@
import express from 'express'
-import { chatReply, clearChatContext } from './chatgpt'
+import type { ChatContext } from './chatgpt'
+import { chatReplayOne, chatReply, clearChatContext } from './chatgpt'
const app = express()
@@ -15,9 +16,25 @@ app.all('*', (req, res, next) => {
app.listen(3002, () => globalThis.console.log('Server is running on port 3002'))
app.post('/chat', async (req, res) => {
- const { message } = req.body
- const response = await chatReply(message)
- res.send(response)
+ 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)
+ res.send(response)
+ }
+ catch (error) {
+ res.send(error)
+ }
})
app.post('/clear', async (req, res) => {
diff --git a/service/src/utils/index.ts b/service/src/utils/index.ts
new file mode 100644
index 0000000..392f9d9
--- /dev/null
+++ b/service/src/utils/index.ts
@@ -0,0 +1,22 @@
+interface SendResponseOptions {
+ type: 'success' | 'fail'
+ message?: string
+ data?: any
+}
+
+export function sendResponse(options: SendResponseOptions) {
+ if (options.type === 'success') {
+ return Promise.resolve({
+ message: options.message ?? 'Success',
+ data: options.data ?? null,
+ status: options.type,
+ })
+ }
+
+ // eslint-disable-next-line prefer-promise-reject-errors
+ return Promise.reject({
+ message: options.message ?? 'Failed',
+ data: options.data ?? null,
+ status: options.type,
+ })
+}
From 847623e22adeb2a0da65e5924bab35462310bd75 Mon Sep 17 00:00:00 2001
From: ChenZhaoYu <790348264@qq.com>
Date: Mon, 13 Feb 2023 15:54:17 +0800
Subject: [PATCH 12/17] =?UTF-8?q?feat:=20=E5=89=8D=E5=90=8E=E7=AB=AF?=
=?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=8E=A5=E5=8F=A3=E7=B1=BB=E5=9E=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
service/src/chatgpt.ts | 15 ++--
service/src/utils/index.ts | 6 +-
src/api/index.ts | 14 ++++
.../business/Chat/components/Message/Text.vue | 3 +-
.../Chat/components/Message/index.vue | 3 +-
src/components/business/Chat/index.vue | 30 +++++---
.../business/Chat/layout/sider/index.vue | 12 +++-
src/components/business/Chat/request.ts | 41 -----------
src/components/common/UserAvatar/index.vue | 8 +--
src/components/custom/GithubSite.vue | 12 ++--
src/utils/request/axios.ts | 29 ++++++++
src/utils/request/index.ts | 72 +++++++++++++++++++
12 files changed, 167 insertions(+), 78 deletions(-)
create mode 100644 src/api/index.ts
delete mode 100644 src/components/business/Chat/request.ts
create mode 100644 src/utils/request/axios.ts
create mode 100644 src/utils/request/index.ts
diff --git a/service/src/chatgpt.ts b/service/src/chatgpt.ts
index 43ff8d0..9e310b1 100644
--- a/service/src/chatgpt.ts
+++ b/service/src/chatgpt.ts
@@ -24,7 +24,7 @@ const api = new ChatGPTAPI({ apiKey })
async function chatReply(message: string) {
if (!message)
- return sendResponse({ type: 'fail', message: 'Message is empty' })
+ return sendResponse({ type: 'Fail', message: 'Message is empty' })
try {
// Get the last context from the chat context
@@ -43,16 +43,17 @@ async function chatReply(message: string) {
if (conversationId && id)
chatContext.add({ conversationId, parentMessageId: id })
- return sendResponse({ type: 'success', data: response })
+ return sendResponse({ type: 'Success', data: response })
}
catch (error: any) {
- return sendResponse({ type: 'fail', message: error.message })
+ 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' })
+ return sendResponse({ type: 'Fail', message: 'Message is empty' })
try {
let messageOptions: SendMessageOptions = {}
@@ -63,18 +64,18 @@ async function chatReplayOne(message: string, options?: ChatContext) {
const response = await api.sendMessage(message, { ...messageOptions })
- return sendResponse({ type: 'success', data: response })
+ return sendResponse({ type: 'Success', data: response })
}
}
catch (error: any) {
- return sendResponse({ type: 'fail', message: error.message })
+ return sendResponse({ type: 'Fail', message: error.message })
}
}
async function clearChatContext() {
// Clear the chat context
chatContext.clear()
- return sendResponse({ type: 'success', message: 'Chat context cleared' })
+ return sendResponse({ type: 'Success', message: 'Chat context cleared' })
}
export { chatReply, chatReplayOne, clearChatContext }
diff --git a/service/src/utils/index.ts b/service/src/utils/index.ts
index 392f9d9..1d83253 100644
--- a/service/src/utils/index.ts
+++ b/service/src/utils/index.ts
@@ -1,13 +1,13 @@
interface SendResponseOptions {
- type: 'success' | 'fail'
+ type: 'Success' | 'Fail'
message?: string
data?: any
}
export function sendResponse(options: SendResponseOptions) {
- if (options.type === 'success') {
+ if (options.type === 'Success') {
return Promise.resolve({
- message: options.message ?? 'Success',
+ message: options.message ?? null,
data: options.data ?? null,
status: options.type,
})
diff --git a/src/api/index.ts b/src/api/index.ts
new file mode 100644
index 0000000..b49b52c
--- /dev/null
+++ b/src/api/index.ts
@@ -0,0 +1,14 @@
+import { post } from '@/utils/request'
+
+export function fetchChatAPI
(prompt: string) {
+ return post({
+ url: '/chat',
+ data: { prompt },
+ })
+}
+
+export function clearConversations() {
+ return post({
+ url: '/clear',
+ })
+}
diff --git a/src/components/business/Chat/components/Message/Text.vue b/src/components/business/Chat/components/Message/Text.vue
index 4422dbe..d04fa2c 100644
--- a/src/components/business/Chat/components/Message/Text.vue
+++ b/src/components/business/Chat/components/Message/Text.vue
@@ -1,6 +1,7 @@
@@ -82,8 +86,12 @@ function addMessage(message: string, reversal = false) {
diff --git a/src/components/business/Chat/layout/sider/index.vue b/src/components/business/Chat/layout/sider/index.vue
index bd03a93..f00cea6 100644
--- a/src/components/business/Chat/layout/sider/index.vue
+++ b/src/components/business/Chat/layout/sider/index.vue
@@ -1,6 +1,6 @@
@@ -8,11 +8,11 @@