feat: 优化ai聊天输出

This commit is contained in:
橙子
2024-07-22 23:59:21 +08:00
parent 0382d4f114
commit a074cdefa5
2 changed files with 30 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NUglify.Helpers;
using Volo.Abp.Application.Services;
using Yi.Framework.ChatHub.Domain.Managers;
using Yi.Framework.ChatHub.Domain.Shared.Dtos;
@@ -25,13 +27,34 @@ namespace Yi.Framework.ChatHub.Application.Services
public async Task ChatAsync([FromBody] List<AiChatContextDto> chatContext)
{
const int maxChar = 10;
var contextId = Guid.NewGuid();
Queue<string> stringQueue = new Queue<string>();
await foreach (var aiResult in _aiManager.ChatAsStreamAsync(chatContext))
{
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(aiResult, CurrentUser.Id!.Value, contextId));
stringQueue.Enqueue(aiResult);
if (stringQueue.Count >= maxChar)
{
StringBuilder currentStr=new StringBuilder();
while (stringQueue.Count > 0)
{
var str = stringQueue.Dequeue();
currentStr.Append(str);
}
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(currentStr.ToString(), CurrentUser.Id!.Value, contextId));
}
}
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(null, CurrentUser.Id!.Value, contextId));
StringBuilder currentEndStr = new StringBuilder();
while (stringQueue.Count > 0)
{
var str = stringQueue.Dequeue();
currentEndStr.Append(str);
}
await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(currentEndStr.ToString(), CurrentUser.Id!.Value, contextId));
//await _userMessageManager.SendMessageAsync(MessageContext.CreateAi(null, CurrentUser.Id!.Value, contextId));
}
}
}

View File

@@ -45,7 +45,7 @@ const currentMsgContext = computed(() => {
// return chatStore.aiMsgContext;
let tempHtml = [];
chatStore.aiMsgContext.forEach(element => {
console.log(toMarkDownHtml(element.content), "toMarkDownHtml(element.content)");
tempHtml.push({ content: toMarkDownHtml(element.content), messageType: 'Ai', sendUserId: element.sendUserId, sendUserInfo: element.sendUserInfo });
});
@@ -93,6 +93,7 @@ let codeCopyDic=[];
//code部分处理、高亮
const codeHandler = (code, language) => {
const codeIndex = parseInt(Date.now() + "") + Math.floor(Math.random() * 10000000);
console.log(codeIndex,"codeIndex");
// 格式化第一行是右侧language和 “复制” 按钮;
if (code) {
const navCode = navHandler(code)
@@ -370,15 +371,15 @@ const getLastMessage = ((receiveId, itemType) => {
<template>
<div style="position: absolute; top: 0;left: 0;">
<p>当前版本1.3.0</p>
<p>当前版本1.4.0</p>
<p>tip:官方学习交流群每次发送消息消耗 1 钱钱</p>
<p>tip:点击聊天窗口右上角X可退出</p>
<p>tip:多人同时在聊天室时左侧可显示其他成员</p>
<p>Ai聊天当前Ai为 OpenAi ChatGpt4</p>
<p>tip:当前Ai为OpenAi ChatGpt4由于接口收费原因还请各位手下留情</p>
<p>tip:ai对话为持续对话如果想清空可以刷新页面</p>
<p>tip:ai对话只有本地存储了记录刷新后将情况</p>
<p>tip:ai对话为持续对话已优化输出速度</p>
<p>tip:ai对话只有本地存储了记录可点击清除或刷新</p>
</div>
<div class="body">
<div class="left">