Merge pull request #80 from AIDotNet/fix_chatkmsbug

fix 修改聊天记录知识库保存bug
This commit is contained in:
zyxucp
2024-04-23 12:54:49 +08:00
committed by GitHub
2 changed files with 32 additions and 28 deletions

View File

@@ -86,7 +86,7 @@ namespace AntSK.Domain.Domain.Service
{
var memory = _kMService.GetMemoryByApp(app);
var fileId = Guid.NewGuid().ToString();
var result = await memory.ImportDocumentAsync(new Microsoft.KernelMemory.Document(fileId).AddFile(filePath)
var result = await memory.ImportDocumentAsync(new Document(fileId).AddFile(filePath)
.AddTag(KmsConstantcs.KmsIdTag, app.Id)
, index: KmsConstantcs.KmsIndex);
@@ -99,6 +99,7 @@ namespace AntSK.Domain.Domain.Service
Text = Markdown.ToHtml(part.Text),
Relevance = part.Relevance
})));
app.Prompt = KmsConstantcs.KmsPrompt;
}
@@ -155,11 +156,13 @@ namespace AntSK.Domain.Domain.Service
}
else
{
string fileName = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName).FileName;
fileDic.Add(item.SourceName, fileName);
item.SourceName = fileName;
var fileDetail = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName);
if (fileDetail.IsNotNull())
{
string fileName = fileDetail.FileName;
fileDic.Add(item.SourceName, fileName);
item.SourceName = fileName;
}
}
item.Text = Markdown.ToHtml(item.Text);
}
@@ -169,7 +172,7 @@ namespace AntSK.Domain.Domain.Service
//KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask1");
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var func = _kernel.CreateFunctionFromPrompt(app.Prompt , settings);
var chatResult = _kernel.InvokeStreamingAsync(function: func,
arguments: new KernelArguments() { ["doc"] = dataMsg.ToString(), ["history"] = string.Join("\n", history.Select(x => x.Role + ": " + x.Content)), ["input"] = questions });

View File

@@ -8,6 +8,7 @@ using AntSK.Domain.Utils;
using AntSK.LLM.StableDiffusion;
using AntSK.Models;
using Blazored.LocalStorage;
using DocumentFormat.OpenXml.InkML;
using Markdig;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@@ -85,7 +86,6 @@ namespace AntSK.Pages.ChatPage.Components
/// <returns></returns>
private async Task GetMsgList()
{
MessageList.Clear();
List<Chats> msgs = new List<Chats>();
if (string.IsNullOrEmpty(_userName))
{
@@ -122,7 +122,7 @@ namespace AntSK.Pages.ChatPage.Components
/// 保存聊天记录
/// </summary>
/// <returns></returns>
private async Task SaveMsg()
private async Task SaveMsg(List<Chats> MessageList)
{
if (string.IsNullOrEmpty(_userName))
{
@@ -130,7 +130,10 @@ namespace AntSK.Pages.ChatPage.Components
}
else
{
await _chats_Repositories.InsertAsync(MessageList.LastOrDefault());
if (MessageList.Count() > 0)
{
await _chats_Repositories.InsertAsync(MessageList.LastOrDefault());
}
}
}
@@ -246,7 +249,11 @@ namespace AntSK.Pages.ChatPage.Components
//缓存消息记录
if (app.Type != AppType.img.ToString())
{
await SaveMsg();
await SaveMsg(MessageList);
if (OnRelevantSources.IsNotNull())
{
await OnRelevantSources.InvokeAsync(_relevantSources);
}
}
@@ -287,29 +294,23 @@ namespace AntSK.Pages.ChatPage.Components
/// <returns></returns>
private async Task SendKms(string questions, ChatHistory history, Apps app, string? filePath)
{
Chats info = null;
Chats info = new Chats()
{
Id = Guid.NewGuid().ToString(),
AppId = AppId,
UserName = _userName,
CreateTime = DateTime.Now,
Context=""
};
MessageList.Add(info);
var chatResult = _chatService.SendKmsByAppAsync(app, questions, history, filePath, _relevantSources);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new Chats();
info.Id = Guid.NewGuid().ToString();
info.UserName = _userName;
info.AppId = AppId;
info.Context = content.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.Context += content.ConvertToString();
await Task.Delay(50);
}
info.Context += content.ConvertToString();
await Task.Delay(50);
await InvokeAsync(StateHasChanged);
}
await OnRelevantSources.InvokeAsync(_relevantSources);
//全部处理完后再处理一次Markdown
await MarkDown(info);
}