Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add agent id as ref #886

Merged
merged 6 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IAgentService
Task<Agent> CreateAgent(Agent agent);
Task<string> RefreshAgents();
Task<PagedItems<Agent>> GetAgents(AgentFilter filter);
Task<List<IdName>> GetAgentOptions();

/// <summary>
/// Load agent configurations and trigger hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace BotSharp.Abstraction.Files;
public interface IFileInstructService
{
#region Image
Task<string> ReadImages(string? provider, string? model, string text, IEnumerable<InstructFileModel> images);
Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text);
Task<RoleDialogModel> VaryImage(string? provider, string? model, InstructFileModel image);
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image);
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, InstructFileModel mask);
Task<string> ReadImages(string? provider, string? model, string text, IEnumerable<InstructFileModel> images, string? agentId = null);
Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text, string? agentId = null);
Task<RoleDialogModel> VaryImage(string? provider, string? model, InstructFileModel image, string? agentId = null);
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, string? agentId = null);
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, InstructFileModel mask, string? agentId = null);
#endregion

#region Pdf
Expand All @@ -17,7 +17,7 @@ public interface IFileInstructService
/// <param name="prompt"></param>
/// <param name="files">Pdf files</param>
/// <returns></returns>
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<InstructFileModel> files);
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<InstructFileModel> files, string? agentId = null);
#endregion

#region Audio
Expand Down
21 changes: 21 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Models/IdName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace BotSharp.Abstraction.Models;

public class IdName
{
[JsonPropertyName("id")]
public string Id { get; set; } = default!;

[JsonPropertyName("name")]
public string Name { get; set; } = default!;

public IdName(string id, string name)
{
Id = id;
Name = name;
}

public override string ToString()
{
return $"{Id}: {Name}";
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Routing.Settings;

namespace BotSharp.Core.Agents.Services;
Expand Down Expand Up @@ -26,6 +27,15 @@ public async Task<PagedItems<Agent>> GetAgents(AgentFilter filter)
};
}

#if !DEBUG
[SharpCache(10, perInstanceCache: true)]
#endif
public async Task<List<IdName>> GetAgentOptions()
{
var agents = _db.GetAgents(AgentFilter.Empty());
return agents?.Select(x => new IdName(x.Id, x.Name))?.OrderBy(x => x.Name)?.ToList() ?? [];
}

#if !DEBUG
[SharpCache(10, perInstanceCache: true)]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace BotSharp.Core.Files.Services;

public partial class FileInstructService
{
public async Task<string> ReadImages(string? provider, string? model, string text, IEnumerable<InstructFileModel> images)
public async Task<string> ReadImages(string? provider, string? model, string text, IEnumerable<InstructFileModel> images, string? agentId = null)
{
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider ?? "openai", model: model ?? "gpt-4o", multiModal: true);
var message = await completion.GetChatCompletions(new Agent()
{
Id = Guid.Empty.ToString(),
Id = agentId ?? Guid.Empty.ToString(),
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, text)
Expand All @@ -20,17 +20,17 @@ public async Task<string> ReadImages(string? provider, string? model, string tex
return message.Content;
}

public async Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text)
public async Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text, string? agentId = null)
{
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-3");
var message = await completion.GetImageGeneration(new Agent()
{
Id = Guid.Empty.ToString(),
Id = agentId ?? Guid.Empty.ToString(),
}, new RoleDialogModel(AgentRole.User, text));
return message;
}

public async Task<RoleDialogModel> VaryImage(string? provider, string? model, InstructFileModel image)
public async Task<RoleDialogModel> VaryImage(string? provider, string? model, InstructFileModel image, string? agentId = null)
{
if (string.IsNullOrWhiteSpace(image?.FileUrl) && string.IsNullOrWhiteSpace(image?.FileData))
{
Expand All @@ -46,14 +46,14 @@ public async Task<RoleDialogModel> VaryImage(string? provider, string? model, In
var fileName = $"{image.FileName ?? "image"}.{image.FileExtension ?? "png"}";
var message = await completion.GetImageVariation(new Agent()
{
Id = Guid.Empty.ToString()
Id = agentId ?? Guid.Empty.ToString()
}, new RoleDialogModel(AgentRole.User, string.Empty), stream, fileName);

stream.Close();
return message;
}

public async Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image)
public async Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, string? agentId = null)
{
if (string.IsNullOrWhiteSpace(image?.FileUrl) && string.IsNullOrWhiteSpace(image?.FileData))
{
Expand All @@ -69,14 +69,14 @@ public async Task<RoleDialogModel> EditImage(string? provider, string? model, st
var fileName = $"{image.FileName ?? "image"}.{image.FileExtension ?? "png"}";
var message = await completion.GetImageEdits(new Agent()
{
Id = Guid.Empty.ToString()
Id = agentId ?? Guid.Empty.ToString()
}, new RoleDialogModel(AgentRole.User, text), stream, fileName);

stream.Close();
return message;
}

public async Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, InstructFileModel mask)
public async Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, InstructFileModel mask, string? agentId = null)
{
if ((string.IsNullOrWhiteSpace(image?.FileUrl) && string.IsNullOrWhiteSpace(image?.FileData)) ||
(string.IsNullOrWhiteSpace(mask?.FileUrl) && string.IsNullOrWhiteSpace(mask?.FileData)))
Expand All @@ -100,7 +100,7 @@ public async Task<RoleDialogModel> EditImage(string? provider, string? model, st
var maskName = $"{mask.FileName ?? "mask"}.{mask.FileExtension ?? "png"}";
var message = await completion.GetImageEdits(new Agent()
{
Id = Guid.Empty.ToString()
Id = agentId ?? Guid.Empty.ToString()
}, new RoleDialogModel(AgentRole.User, text), imageStream, imageName, maskStream, maskName);

imageStream.Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BotSharp.Core.Files.Services;

public partial class FileInstructService
{
public async Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<InstructFileModel> files)
public async Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<InstructFileModel> files, string? agentId = null)
{
var content = string.Empty;

Expand All @@ -14,7 +14,6 @@ public async Task<string> ReadPdf(string? provider, string? model, string? model
}

var guid = Guid.NewGuid().ToString();

var sessionDir = _fileStorage.BuildDirectory(SESSION_FOLDER, guid);
DeleteIfExistDirectory(sessionDir, true);

Expand All @@ -28,7 +27,7 @@ public async Task<string> ReadPdf(string? provider, string? model, string? model
model: model, modelId: modelId ?? "gpt-4", multiModal: true);
var message = await completion.GetChatCompletions(new Agent()
{
Id = Guid.Empty.ToString(),
Id = agentId ?? Guid.Empty.ToString(),
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static IServiceCollection AddBotSharpLogger(this IServiceCollection servi
services.AddScoped<IContentGeneratingHook, CommonContentGeneratingHook>();
services.AddScoped<IContentGeneratingHook, TokenStatsConversationHook>();
services.AddScoped<IContentGeneratingHook, VerboseLogHook>();
services.AddScoped<IContentGeneratingHook, GlobalStatsConversationHook>();
services.AddScoped<IConversationHook, RateLimitConversationHook>();
services.AddScoped<IConversationHook, GlobalStatsConversationHook>();
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace BotSharp.Logger.Hooks;

public class GlobalStatsConversationHook : ConversationHookBase
public class GlobalStatsConversationHook : IContentGeneratingHook
{
private readonly IServiceProvider _services;

Expand All @@ -14,14 +14,10 @@ public GlobalStatsConversationHook(
_services = services;
}

public override async Task OnMessageReceived(RoleDialogModel message)
{
UpdateAgentCall(message);
}

public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
{
UpdateAgentCall(message);
await Task.CompletedTask;
}

private void UpdateAgentCall(RoleDialogModel message)
Expand All @@ -33,7 +29,7 @@ private void UpdateAgentCall(RoleDialogModel message)
{
Metric = StatsMetric.AgentCall,
Dimension = "agent",
DimRefVal = message.CurrentAgentId,
DimRefVal = message.CurrentAgentId ?? string.Empty,
RecordTime = DateTime.UtcNow,
IntervalType = StatsInterval.Day,
Data = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public async Task<bool> DeleteAgent([FromRoute] string agentId)
return await _agentService.DeleteAgent(agentId);
}

[HttpGet("/agent/options")]
public async Task<List<IdName>> GetAgentOptions()
{
return await _agentService.GetAgentOptions();
}

[HttpGet("/agent/utility/options")]
public IEnumerable<AgentUtility> GetAgentUtilityOptions()
{
Expand Down
Loading
Loading