mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-18 01:06:39 +08:00
do not show the hello message when reconnected
This commit is contained in:
parent
a4ea1e47e2
commit
9ea4060790
@ -29,9 +29,9 @@ func (s *Server) ChatHandle(c *gin.Context) {
|
|||||||
client := NewWsClient(ws)
|
client := NewWsClient(ws)
|
||||||
// TODO: 这里需要先判断一下角色是否存在,并且角色是被启用的
|
// TODO: 这里需要先判断一下角色是否存在,并且角色是被启用的
|
||||||
// 发送打招呼信息
|
// 发送打招呼信息
|
||||||
replyMessage(types.WsMessage{Type: types.WsStart}, client)
|
replyMessage(types.WsMessage{Type: types.WsStart, IsHelloMsg: true}, client)
|
||||||
replyMessage(types.WsMessage{Type: types.WsMiddle, Content: s.Config.ChatRoles[role].HelloMsg}, client)
|
replyMessage(types.WsMessage{Type: types.WsMiddle, Content: s.Config.ChatRoles[role].HelloMsg, IsHelloMsg: true}, client)
|
||||||
replyMessage(types.WsMessage{Type: types.WsEnd}, client)
|
replyMessage(types.WsMessage{Type: types.WsEnd, IsHelloMsg: true}, client)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
_, message, err := client.Receive()
|
_, message, err := client.Receive()
|
||||||
@ -41,7 +41,7 @@ func (s *Server) ChatHandle(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info(string(message))
|
logger.Info("Receive a message: ", string(message))
|
||||||
// TODO: 当前只保持当前会话的上下文,部保存用户的所有的聊天历史记录,后期要考虑保存所有的历史记录
|
// TODO: 当前只保持当前会话的上下文,部保存用户的所有的聊天历史记录,后期要考虑保存所有的历史记录
|
||||||
err = s.sendMessage(token, role, string(message), client)
|
err = s.sendMessage(token, role, string(message), client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -150,10 +150,10 @@ func (s *Server) sendMessage(sessionId string, role string, text string, ws Clie
|
|||||||
// 初始化 role
|
// 初始化 role
|
||||||
if responseBody.Choices[0].Delta.Role != "" && message.Role == "" {
|
if responseBody.Choices[0].Delta.Role != "" && message.Role == "" {
|
||||||
message.Role = responseBody.Choices[0].Delta.Role
|
message.Role = responseBody.Choices[0].Delta.Role
|
||||||
replyMessage(types.WsMessage{Type: types.WsStart}, ws)
|
replyMessage(types.WsMessage{Type: types.WsStart, IsHelloMsg: false}, ws)
|
||||||
continue
|
continue
|
||||||
} else if responseBody.Choices[0].FinishReason != "" { // 输出完成或者输出中断了
|
} else if responseBody.Choices[0].FinishReason != "" { // 输出完成或者输出中断了
|
||||||
replyMessage(types.WsMessage{Type: types.WsEnd}, ws)
|
replyMessage(types.WsMessage{Type: types.WsEnd, IsHelloMsg: false}, ws)
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
content := responseBody.Choices[0].Delta.Content
|
content := responseBody.Choices[0].Delta.Content
|
||||||
@ -161,6 +161,7 @@ func (s *Server) sendMessage(sessionId string, role string, text string, ws Clie
|
|||||||
replyMessage(types.WsMessage{
|
replyMessage(types.WsMessage{
|
||||||
Type: types.WsMiddle,
|
Type: types.WsMiddle,
|
||||||
Content: responseBody.Choices[0].Delta.Content,
|
Content: responseBody.Choices[0].Delta.Content,
|
||||||
|
IsHelloMsg: false,
|
||||||
}, ws)
|
}, ws)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,8 +115,8 @@ func GetDefaultChatRole() map[string]ChatRole {
|
|||||||
Key: "english_trainer",
|
Key: "english_trainer",
|
||||||
Name: "英语陪练员",
|
Name: "英语陪练员",
|
||||||
Context: []Message{
|
Context: []Message{
|
||||||
{Role: "user", Content: "现在你将扮演一位优秀的英语教练,你非常有耐心,你的主要工作就是跟学生使用英语对话,如果发现学生的回答中有语法错误,你将使用中文将错误指出。"},
|
{Role: "user", Content: "现在你将扮演一位优秀的英语练习教练,你非常有耐心,接下来你将全程使用英文跟我对话,并及时指出我的语法错误,要求在你的每次回复后面附上本次回复的中文解释。"},
|
||||||
{Role: "user", Content: "好的,让我们开始对话练习吧!请问你的名字是什么?\n(Translation: Okay, let's start our conversation practice! What's your name?)"},
|
{Role: "user", Content: "Okay, let's start our conversation practice! What's your name?(Translation: 好的,让我们开始对话练习吧!请问你的名字是什么?)"},
|
||||||
},
|
},
|
||||||
HelloMsg: "Okay, let's start our conversation practice! What's your name?",
|
HelloMsg: "Okay, let's start our conversation practice! What's your name?",
|
||||||
Icon: "images/avatar/english_trainer.jpg",
|
Icon: "images/avatar/english_trainer.jpg",
|
||||||
|
@ -13,6 +13,7 @@ type BizVo struct {
|
|||||||
// WsMessage Websocket message
|
// WsMessage Websocket message
|
||||||
type WsMessage struct {
|
type WsMessage struct {
|
||||||
Type WsMsgType `json:"type"` // 消息类别,start, end
|
Type WsMsgType `json:"type"` // 消息类别,start, end
|
||||||
|
IsHelloMsg bool `json:"is_hello_msg"` // 是否是打招呼的消息
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
type WsMsgType string
|
type WsMsgType string
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
</div><!-- end chat box -->
|
</div><!-- end chat box -->
|
||||||
|
|
||||||
<div class="input-box">
|
<div class="input-box" :style="{width: inputBoxWidth+'px'}">
|
||||||
<div class="input-container">
|
<div class="input-container">
|
||||||
<el-input
|
<el-input
|
||||||
ref="text-input"
|
ref="text-input"
|
||||||
@ -114,10 +114,11 @@ export default defineComponent({
|
|||||||
replyIcon: 'images/avatar/gpt.png', // 回复信息的头像
|
replyIcon: 'images/avatar/gpt.png', // 回复信息的头像
|
||||||
|
|
||||||
lineBuffer: '', // 输出缓冲行
|
lineBuffer: '', // 输出缓冲行
|
||||||
connectingMessageBox: null, // 保存重连的消息框对象
|
connectingMessageBox: null, // 保存重连的消息框对象load
|
||||||
socket: null,
|
socket: null,
|
||||||
toolBoxHeight: 61 + 42, // 工具框的高度
|
toolBoxHeight: 61 + 42, // 工具框的高度
|
||||||
sending: false,
|
inputBoxWidth: window.innerWidth - 20,
|
||||||
|
sending: true,
|
||||||
loading: false
|
loading: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -197,17 +198,18 @@ export default defineComponent({
|
|||||||
const token = getSessionId();
|
const token = getSessionId();
|
||||||
const socket = new WebSocket(process.env.VUE_APP_WS_HOST + `/api/chat?token=${token}&role=${this.role}`);
|
const socket = new WebSocket(process.env.VUE_APP_WS_HOST + `/api/chat?token=${token}&role=${this.role}`);
|
||||||
socket.addEventListener('open', () => {
|
socket.addEventListener('open', () => {
|
||||||
ElMessage.success('创建会话成功!');
|
|
||||||
|
|
||||||
// 获取聊天角色
|
// 获取聊天角色
|
||||||
httpGet("/api/config/chat-roles/get").then((res) => {
|
httpGet("/api/config/chat-roles/get").then((res) => {
|
||||||
|
ElMessage.success('创建会话成功!');
|
||||||
this.chatRoles = res.data;
|
this.chatRoles = res.data;
|
||||||
|
this.loading = false
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
ElMessage.error("获取聊天角色失败");
|
ElMessage.error("获取聊天角色失败");
|
||||||
})
|
})
|
||||||
|
|
||||||
if (this.connectingMessageBox && typeof this.connectingMessageBox.close === 'function') {
|
if (this.connectingMessageBox && typeof this.connectingMessageBox.close === 'function') {
|
||||||
this.connectingMessageBox.close();
|
this.connectingMessageBox.close();
|
||||||
|
this.connectingMessageBox = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -217,6 +219,11 @@ export default defineComponent({
|
|||||||
reader.readAsText(event.data, "UTF-8");
|
reader.readAsText(event.data, "UTF-8");
|
||||||
reader.onload = () => {
|
reader.onload = () => {
|
||||||
const data = JSON.parse(String(reader.result));
|
const data = JSON.parse(String(reader.result));
|
||||||
|
// 过滤掉重复的打招呼信息
|
||||||
|
if (data['is_hello_msg'] && this.chatData.length > 3) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (data.type === 'start') {
|
if (data.type === 'start') {
|
||||||
this.chatData.push({
|
this.chatData.push({
|
||||||
type: "reply",
|
type: "reply",
|
||||||
@ -253,6 +260,7 @@ export default defineComponent({
|
|||||||
socket.addEventListener('close', () => {
|
socket.addEventListener('close', () => {
|
||||||
// 检查会话
|
// 检查会话
|
||||||
httpGet("/api/session/get").then(() => {
|
httpGet("/api/session/get").then(() => {
|
||||||
|
if (this.connectingMessageBox === null) {
|
||||||
this.connectingMessageBox = ElMessageBox.confirm(
|
this.connectingMessageBox = ElMessageBox.confirm(
|
||||||
'^_^ 会话发生异常,您已经从服务器断开连接!',
|
'^_^ 会话发生异常,您已经从服务器断开连接!',
|
||||||
'注意:',
|
'注意:',
|
||||||
@ -271,6 +279,7 @@ export default defineComponent({
|
|||||||
message: '您关闭了会话',
|
message: '您关闭了会话',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}).catch((res) => {
|
}).catch((res) => {
|
||||||
if (res.code === 400) {
|
if (res.code === 400) {
|
||||||
this.showLoginDialog = true;
|
this.showLoginDialog = true;
|
||||||
@ -286,6 +295,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
// 更换角色
|
// 更换角色
|
||||||
changeRole: function () {
|
changeRole: function () {
|
||||||
|
this.loading = true
|
||||||
// 清空对话列表
|
// 清空对话列表
|
||||||
this.chatData = [];
|
this.chatData = [];
|
||||||
this.connect();
|
this.connect();
|
||||||
|
Loading…
Reference in New Issue
Block a user