Adjust conversation history format.

This commit is contained in:
hchen2020 2023-08-07 17:37:32 -05:00
parent 3f49d11eff
commit 19a31510c0
3 changed files with 7 additions and 26 deletions

View file

@ -1,19 +0,0 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Functions.Models;
public class FunctionExecutionResult<T> where T : new()
{
private readonly string _name;
public FunctionExecutionResult(string name)
{
_name = name;
}
[JsonPropertyName("function_name")]
public string Name => _name;
[JsonPropertyName("execution_result")]
public T Result { get; set; } = new T();
}

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Conversations.Models;
using System.IO;
using Tensorflow;
namespace BotSharp.Core.Conversations.Services;
@ -16,7 +15,7 @@ public class ConversationStorage : IConversationStorage
{
var conversationFile = GetStorageFile(agentId, conversationId);
var sb = new StringBuilder();
sb.AppendLine($"{dialog.Role}|{dialog.CreatedAt}");
sb.AppendLine($"{dialog.Role}|{dialog.CreatedAt}|{dialog.FunctionName}");
sb.AppendLine($" - {dialog.Content}");
var conversation = sb.ToString();
File.AppendAllText(conversationFile, conversation);
@ -35,8 +34,10 @@ public class ConversationStorage : IConversationStorage
var role = meta.Split('|')[0];
var createdAt = DateTime.Parse(meta.Split('|')[1]);
var text = dialog.Substring(4);
var funcName = meta.Split('|')[2];
results.Add(new RoleDialogModel(role, text)
{
FunctionName = funcName,
CreatedAt = createdAt
});
}

View file

@ -8,6 +8,7 @@ using BotSharp.Plugin.AzureOpenAI.Settings;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
@ -118,9 +119,7 @@ public class ChatCompletionProvider : IChatCompletion
await onMessageReceived(funcContextIn);
// After function is executed, pass the result to LLM
var fnResult = JsonSerializer.Deserialize<FunctionExecutionResult<object>>(funcContextIn.ExecutionResult);
var fnJsonResult = JsonSerializer.Serialize(fnResult.Result);
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, fnJsonResult)
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, funcContextIn.ExecutionResult)
{
Name = funcContextIn.FunctionName
});
@ -216,10 +215,9 @@ public class ChatCompletionProvider : IChatCompletion
{
if (message.Role == ChatRole.Function)
{
var funcContext = JsonSerializer.Deserialize<FunctionExecutionResult<object>>(message.Content);
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)
{
Name = funcContext.Name
Name = message.FunctionName
});
}
else
@ -228,6 +226,7 @@ public class ChatCompletionProvider : IChatCompletion
}
}
_logger.LogInformation(string.Join("\n", chatCompletionsOptions.Messages.Select(x => $"{x.Role}: {x.Content}")));
return chatCompletionsOptions;
}
}