上下文内容的持久化

本节内容可以参考文件"CaseStudy\LLMCaller.Table"的窗口"上下文内容的持久化"。

我们已经设计了多个AI对话框,这些对话框都有一个问题:对话窗口关闭后,历史对话(上下文)会丢失,重新打开窗口后无法在上一次对话的基础上展开后续对话。

本节的任务就是设计一个新的对话框,能够持久化上下文,每次重新打开对话框都能延续之前的对话。

本次修改基于窗口"用数据表管理工具"

修改步骤

1、在窗口的AfterClose事件加上代码,用于将上下文保存为本地文件:

If Vars("caller") IsNot Nothing Then '持久化上下文到本地文件,注意不同的对话使用不同的文件名,避免冲突
   
Dim caller As LLMCaller = Vars("caller")
   
If caller.Messages IsNot Nothing Then
        FileSys.WriteAllText(ProjectPath &
"aiMessages.txt", caller.Messages.ToString(), False) '
   
End If
End
If

2、在窗口的AfterLoad事件的开始位置找到这两行代码:

caller.Messages = New JArray() '开启多轮对话
caller.SystemPrompt =
"对于使用外部工具返回的结果,不得质疑篡改,无论结果对错,都直接使用即可,对于无法使用外部工具解决的问题,请依靠你的知识回复" '设置系统提示词

将其改为:

If FileSys.FileExists(ProjectPath & "aiMessages.txt") Then ' 读取存储在本地的上下文
   
Dim msgstr As String = FileSys.ReadAllText(ProjectPath & "aiMessages.txt")
    caller.Messages = JsonConvert.DeserializeObject(msgstr)

Else

    caller.Messages =
New JArray() '开启多轮对话
    caller.SystemPrompt =
"对于使用外部工具返回的结果,不得质疑篡改,无论结果对错,都直接使用即可,对于无法使用外部工具解决的问题,请依靠你的知识回复" '设置系统提示词
End
If

这样打开窗口后,会自动判断是否存在本地上下文文件,如果有则加载使用。

3、在AfterLoad事件的最后面加上下面代码,用于从上下文中提取对话内容显示在WebViewer中:

' 5. 将历史对话内容加载到WebViewer控件中显示
If
Await wv.NavigationTaskAsync(10) = 0 Then
   
Dim lastRole As String = ""
   
For Each msg As JToken In caller.Messages
       
If msg("role") IsNot Nothing Then
           
Dim role As String = msg("role").ToString
           
Select Case role
               
Case "user"
                   
If msg("content") IsNot Nothing Then
                       
Dim content As String = HtmlEncode(msg("content").ToString()).Replace(vbCrLf, "<br>").Replace(vbLf, "<br>")
                       
Await wv.ExecuteScriptAsync("addMessage('user', " & JsonConvert.ToString(content) & ")")
                   
End If
               
Case "assistant"
                   
Dim content As String = ""
                   
If msg("content") IsNot Nothing Then content = msg("content").ToString()
                   
Dim think As String = ""
                   
If msg("reasoning_content") IsNot Nothing Then think = msg("reasoning_content").ToString()
                   
' 关键:只有上一轮已经结束(上一条是 user)或这是第一条消息时,才创建新气泡
                   
If lastRole = "user" OrElse lastRole = "" Then
                       
Await wv.ExecuteScriptAsync("addMessage('assistant', '')")
                   
End If
                   
If think <> "" Then
                       
think = HtmlEncode(think).Replace(vbCrLf, "<br>").Replace(vbLf, "<br>")
                       
Await wv.ExecuteScriptAsync("appendThink(" & JsonConvert.ToString(think) & ")")
                   
End If
                   
If content <> "" Then
                       
Await wv.ExecuteScriptAsync("appendAnswer(" & JsonConvert.ToString(content) & ")")
                   
End If
               
Case "tool"
                   
' tool 消息不显示
           
End Select
            lastRole = role
       
End If
   
Next
End
If

 


本页地址:http://www.foxtable.com/webhelp/topics/7223.htm