Merge pull request #89 from hchen2020/master
Load function from conversation history.
This commit is contained in:
commit
323ce61eba
|
|
@ -0,0 +1,19 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Abstraction.Conversations.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();
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
public enum FunctionExecutionStatus
|
||||
{
|
||||
Success = 1,
|
||||
Failure = 2
|
||||
}
|
||||
|
|
@ -4,6 +4,11 @@ namespace BotSharp.Abstraction.Conversations.Models;
|
|||
|
||||
public class FunctionExecutionValidationResult
|
||||
{
|
||||
public FunctionExecutionValidationResult()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public FunctionExecutionValidationResult(string validationStatus, string validationMessage = "")
|
||||
{
|
||||
ValidationStatus = validationStatus;
|
||||
|
|
|
|||
|
|
@ -81,17 +81,19 @@ public class ConversationService : IConversationService
|
|||
if (msg.Role == "function")
|
||||
{
|
||||
var result = msg.ExecutionResult.Replace("\r", " ").Replace("\n", " ");
|
||||
var content = $"{msg.FunctionName} {result}";
|
||||
Console.WriteLine(content);
|
||||
/*_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content)
|
||||
var content = $"{result}";
|
||||
Console.WriteLine($"{msg.Role}: {content}");
|
||||
_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content)
|
||||
{
|
||||
FunctionName = msg.FunctionName,
|
||||
});*/
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var content = msg.Content.Replace("\r", " ").Replace("\n", " ");
|
||||
Console.WriteLine($"{msg.Role}: {content}");
|
||||
_storage.Append(agentId, conversationId, new RoleDialogModel(msg.Role, content));
|
||||
|
||||
await onMessageReceived(msg);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -120,7 +120,6 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
|
||||
choice = response.Value.Choices[0];
|
||||
message = choice.Message;
|
||||
Console.Write(message.Content);
|
||||
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content));
|
||||
|
||||
return true;
|
||||
|
|
@ -197,10 +196,21 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
Parameters = BinaryData.FromObjectAsJson(function.Parameters)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
foreach (var message in conversations)
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
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
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
}
|
||||
}
|
||||
|
||||
return chatCompletionsOptions;
|
||||
|
|
|
|||
Loading…
Reference in a new issue