Add FunctionDefinition.
This commit is contained in:
parent
b8553f93a2
commit
d72204cd22
|
|
@ -18,13 +18,13 @@ public class Agent
|
|||
/// </summary>
|
||||
public string Samples { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Functions
|
||||
/// </summary>
|
||||
public string Functions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Domain knowledges
|
||||
/// </summary>
|
||||
public string Knowledges { get; set;}
|
||||
|
||||
/// <summary>
|
||||
/// Owner user id
|
||||
/// </summary>
|
||||
public string OwerId { get; set; } = string.Empty;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
|
||||
<PackageReference Include="System.Text.Json" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
using System.Text.Json;
|
||||
|
||||
namespace BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
public class FunctionDef
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public JsonDocument Parameters { get; set; }
|
||||
}
|
||||
|
|
@ -10,11 +10,9 @@ namespace BotSharp.Core.Agents;
|
|||
public class AgentController : ControllerBase, IApiAdapter
|
||||
{
|
||||
private readonly IAgentService _agentService;
|
||||
private readonly IUserIdentity _user;
|
||||
public AgentController(IAgentService agentService, IUserIdentity user)
|
||||
public AgentController(IAgentService agentService)
|
||||
{
|
||||
_agentService = agentService;
|
||||
_user = user;
|
||||
}
|
||||
|
||||
[HttpPost("/agent")]
|
||||
|
|
@ -30,7 +28,6 @@ public class AgentController : ControllerBase, IApiAdapter
|
|||
{
|
||||
var model = agent.ToAgent();
|
||||
model.Id = agentId;
|
||||
model.OwerId = _user.Id;
|
||||
await _agentService.UpdateAgent(model);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Agents.Services;
|
||||
|
|
@ -30,11 +31,29 @@ public partial class AgentService
|
|||
{
|
||||
profile.Instruction = File.ReadAllText(instructionFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError($"Can't find instruction file from {instructionFile}");
|
||||
}
|
||||
|
||||
var samplesFile = Path.Combine(dir, "samples.txt");
|
||||
if (File.Exists(samplesFile))
|
||||
{
|
||||
profile.Samples = File.ReadAllText(Path.Combine(dir, "samples.txt"));
|
||||
profile.Samples = File.ReadAllText(samplesFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Can't find samples file from {samplesFile}");
|
||||
}
|
||||
|
||||
var functionsFile = Path.Combine(dir, "functions.json");
|
||||
if (File.Exists(functionsFile))
|
||||
{
|
||||
profile.Functions = File.ReadAllText(functionsFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"Can't find functions file from {functionsFile}");
|
||||
}
|
||||
|
||||
return profile;
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public partial class AgentService
|
|||
{
|
||||
var record = (from a in db.Agent
|
||||
join ua in db.UserAgent on a.Id equals ua.AgentId
|
||||
where ua.UserId == agent.OwerId && a.Id == agent.Id
|
||||
where ua.UserId == _user.Id && a.Id == agent.Id
|
||||
select a).First();
|
||||
|
||||
record.Name = agent.Name;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Agents.Services;
|
||||
|
|
@ -5,12 +6,14 @@ namespace BotSharp.Core.Agents.Services;
|
|||
public partial class AgentService : IAgentService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserIdentity _user;
|
||||
private readonly AgentSettings _settings;
|
||||
|
||||
public AgentService(IServiceProvider services, IUserIdentity user, AgentSettings settings)
|
||||
public AgentService(IServiceProvider services, ILogger<AgentService> logger, IUserIdentity user, AgentSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_user = user;
|
||||
_settings = settings;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,27 +4,43 @@ namespace BotSharp.Core.Agents.ViewModels;
|
|||
|
||||
public class AgentUpdateModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Instruction
|
||||
/// </summary>
|
||||
public string Instruction { get; set; }
|
||||
public string? Instruction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Samples
|
||||
/// </summary>
|
||||
public string Samples { get; set; }
|
||||
public string? Samples { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Functions
|
||||
/// </summary>
|
||||
public string? Functions { get; set; }
|
||||
|
||||
public Agent ToAgent()
|
||||
{
|
||||
return new Agent
|
||||
var agent = new Agent
|
||||
{
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
Instruction = Instruction,
|
||||
Samples = Samples
|
||||
Name = Name
|
||||
};
|
||||
|
||||
if (Description != null)
|
||||
agent.Description = Description;
|
||||
|
||||
if (Instruction != null)
|
||||
agent.Instruction = Instruction;
|
||||
|
||||
if (Samples != null)
|
||||
agent.Samples = Samples;
|
||||
|
||||
if (Functions != null)
|
||||
agent.Functions = Functions;
|
||||
|
||||
return agent;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using BotSharp.Abstraction.MLTasks;
|
|||
using BotSharp.Plugin.AzureOpenAI.Settings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
||||
|
|
@ -70,6 +71,20 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
return samples;
|
||||
}
|
||||
|
||||
public List<FunctionDef> GetFunctions(string functionsJson)
|
||||
{
|
||||
var functions = new List<FunctionDef>();
|
||||
if (!string.IsNullOrEmpty(functionsJson))
|
||||
{
|
||||
functions = JsonSerializer.Deserialize<List<FunctionDef>>(functionsJson, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
AllowTrailingCommas = true
|
||||
});
|
||||
}
|
||||
|
||||
return functions;
|
||||
}
|
||||
|
||||
public async Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations)
|
||||
{
|
||||
|
|
@ -114,6 +129,17 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
}
|
||||
|
||||
var functions = GetFunctions(agent.Functions);
|
||||
foreach (var function in functions)
|
||||
{
|
||||
chatCompletionsOptions.Functions.Add(new FunctionDefinition
|
||||
{
|
||||
Name = function.Name,
|
||||
Description = function.Description,
|
||||
Parameters = BinaryData.FromObjectAsJson(function.Parameters)
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var message in conversations)
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
|
|
|
|||
Loading…
Reference in a new issue