add agent id as ref
This commit is contained in:
parent
521ce62c41
commit
115477f3ea
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -20,17 +20,17 @@ public partial class FileInstructService
|
|||
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))
|
||||
{
|
||||
|
|
@ -46,14 +46,14 @@ public partial class FileInstructService
|
|||
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))
|
||||
{
|
||||
|
|
@ -69,14 +69,14 @@ public partial class FileInstructService
|
|||
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)))
|
||||
|
|
@ -100,7 +100,7 @@ public partial class FileInstructService
|
|||
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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -14,7 +14,6 @@ public partial class FileInstructService
|
|||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
|
||||
var sessionDir = _fileStorage.BuildDirectory(SESSION_FOLDER, guid);
|
||||
DeleteIfExistDirectory(sessionDir, true);
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ public partial class FileInstructService
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ public static class BotSharpLoggerExtensions
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ using BotSharp.Abstraction.Statistics.Services;
|
|||
|
||||
namespace BotSharp.Logger.Hooks;
|
||||
|
||||
public class GlobalStatsConversationHook : ConversationHookBase
|
||||
public class GlobalStatsConversationHook : IContentGeneratingHook
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
|
|
@ -14,14 +14,10 @@ public class GlobalStatsConversationHook : ConversationHookBase
|
|||
_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)
|
||||
|
|
@ -33,7 +29,7 @@ public class GlobalStatsConversationHook : ConversationHookBase
|
|||
{
|
||||
Metric = StatsMetric.AgentCall,
|
||||
Dimension = "agent",
|
||||
DimRefVal = message.CurrentAgentId,
|
||||
DimRefVal = message.CurrentAgentId ?? string.Empty,
|
||||
RecordTime = DateTime.UtcNow,
|
||||
IntervalType = StatsInterval.Day,
|
||||
Data = [
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ public class InstructModeController : ControllerBase
|
|||
try
|
||||
{
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var content = await fileInstruct.ReadImages(input.Provider, input.Model, input.Text, input.Files);
|
||||
var content = await fileInstruct.ReadImages(input.Provider, input.Model, input.Text, input.Files, input.AgentId);
|
||||
return content;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -101,7 +101,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
[HttpPost("/instruct/multi-modal/upload")]
|
||||
public async Task<MultiModalViewModel> MultiModalCompletion(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null)
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
|
|
@ -115,7 +115,7 @@ public class InstructModeController : ControllerBase
|
|||
new InstructFileModel { FileData = data }
|
||||
};
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var content = await fileInstruct.ReadImages(provider, model, text, files);
|
||||
var content = await fileInstruct.ReadImages(provider, model, text, files, agentId);
|
||||
viewModel.Content = content;
|
||||
return viewModel;
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ public class InstructModeController : ControllerBase
|
|||
try
|
||||
{
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var message = await fileInstruct.GenerateImage(input.Provider, input.Model, input.Text);
|
||||
var message = await fileInstruct.GenerateImage(input.Provider, input.Model, input.Text, input.AgentId);
|
||||
imageViewModel.Content = message.Content;
|
||||
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
||||
return imageViewModel;
|
||||
|
|
@ -171,7 +171,7 @@ public class InstructModeController : ControllerBase
|
|||
}
|
||||
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var message = await fileInstruct.VaryImage(input.Provider, input.Model, input.File);
|
||||
var message = await fileInstruct.VaryImage(input.Provider, input.Model, input.File, input.AgentId);
|
||||
imageViewModel.Content = message.Content;
|
||||
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
||||
|
||||
|
|
@ -188,7 +188,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
[HttpPost("/instruct/image-variation/upload")]
|
||||
public async Task<ImageGenerationViewModel> ImageVariation(IFormFile file, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null)
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
|
|
@ -203,7 +203,7 @@ public class InstructModeController : ControllerBase
|
|||
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
||||
var message = await completion.GetImageVariation(new Agent()
|
||||
{
|
||||
Id = Guid.Empty.ToString()
|
||||
Id = agentId ?? Guid.Empty.ToString()
|
||||
}, new RoleDialogModel(AgentRole.User, string.Empty), stream, file.FileName);
|
||||
|
||||
imageViewModel.Content = message.Content;
|
||||
|
|
@ -235,7 +235,7 @@ public class InstructModeController : ControllerBase
|
|||
{
|
||||
return new ImageGenerationViewModel { Message = "Error! Cannot find a valid image file!" };
|
||||
}
|
||||
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, input.File);
|
||||
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, input.File, input.AgentId);
|
||||
imageViewModel.Content = message.Content;
|
||||
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
||||
return imageViewModel;
|
||||
|
|
@ -251,7 +251,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
[HttpPost("/instruct/image-edit/upload")]
|
||||
public async Task<ImageGenerationViewModel> ImageEdit(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null)
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
||||
{
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
|
|
@ -267,7 +267,7 @@ public class InstructModeController : ControllerBase
|
|||
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
||||
var message = await completion.GetImageEdits(new Agent()
|
||||
{
|
||||
Id = Guid.Empty.ToString()
|
||||
Id = agentId ?? Guid.Empty.ToString()
|
||||
}, new RoleDialogModel(AgentRole.User, text), stream, file.FileName);
|
||||
|
||||
imageViewModel.Content = message.Content;
|
||||
|
|
@ -301,7 +301,7 @@ public class InstructModeController : ControllerBase
|
|||
{
|
||||
return new ImageGenerationViewModel { Message = "Error! Cannot find a valid image or mask!" };
|
||||
}
|
||||
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image, mask);
|
||||
var message = await fileInstruct.EditImage(input.Provider, input.Model, input.Text, image, mask, input.AgentId);
|
||||
imageViewModel.Content = message.Content;
|
||||
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
|
||||
return imageViewModel;
|
||||
|
|
@ -317,7 +317,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
[HttpPost("/instruct/image-mask-edit/upload")]
|
||||
public async Task<ImageGenerationViewModel> ImageMaskEdit(IFormFile image, IFormFile mask, [FromForm] string text, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null)
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
||||
{
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
|
|
@ -337,7 +337,7 @@ public class InstructModeController : ControllerBase
|
|||
var completion = CompletionProvider.GetImageCompletion(_services, provider: provider ?? "openai", model: model ?? "dall-e-2");
|
||||
var message = await completion.GetImageEdits(new Agent()
|
||||
{
|
||||
Id = Guid.Empty.ToString()
|
||||
Id = agentId ?? Guid.Empty.ToString()
|
||||
}, new RoleDialogModel(AgentRole.User, text), imageStream, image.FileName, maskStream, mask.FileName);
|
||||
|
||||
imageViewModel.Content = message.Content;
|
||||
|
|
@ -368,7 +368,7 @@ public class InstructModeController : ControllerBase
|
|||
try
|
||||
{
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var content = await fileInstruct.ReadPdf(input.Provider, input.Model, input.ModelId, input.Text, input.Files);
|
||||
var content = await fileInstruct.ReadPdf(input.Provider, input.Model, input.ModelId, input.Text, input.Files, input.AgentId);
|
||||
viewModel.Content = content;
|
||||
return viewModel;
|
||||
}
|
||||
|
|
@ -383,7 +383,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
[HttpPost("/instruct/pdf-completion/upload")]
|
||||
public async Task<PdfCompletionViewModel> PdfCompletion(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] string? modelId = null, [FromForm] List<MessageState>? states = null)
|
||||
[FromForm] string? model = null, [FromForm] string? modelId = null, [FromForm] List<MessageState>? states = null, [FromForm] string? agentId = null)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
|
|
@ -398,7 +398,7 @@ public class InstructModeController : ControllerBase
|
|||
};
|
||||
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var content = await fileInstruct.ReadPdf(provider, model, modelId, text, files);
|
||||
var content = await fileInstruct.ReadPdf(provider, model, modelId, text, files, agentId);
|
||||
viewModel.Content = content;
|
||||
return viewModel;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@ public class InstructBaseRequest
|
|||
[JsonPropertyName("model_id")]
|
||||
public virtual string? ModelId { get; set; } = null;
|
||||
|
||||
[JsonPropertyName("agent_id")]
|
||||
public virtual string? AgentId { get; set; }
|
||||
|
||||
[JsonPropertyName("states")]
|
||||
public List<MessageState> States { get; set; } = new();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue