Skip to content

Commit a0dcf80

Browse files
authored
Merge pull request #533 from iceljc/features/split-azure-openai
add open ai plugin
2 parents fe2878b + 7f60133 commit a0dcf80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1050
-174
lines changed

BotSharp.sln

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaGLM", "
8989
EndProject
9090
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AnthropicAI", "src\Plugins\BotSharp.Plugin.AnthropicAI\BotSharp.Plugin.AnthropicAI.csproj", "{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}"
9191
EndProject
92-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.ServiceDefaults", "src\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj", "{6406DC61-0F30-42E8-A1DB-B38CDF454273}"
92+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.ServiceDefaults", "src\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj", "{6406DC61-0F30-42E8-A1DB-B38CDF454273}"
9393
EndProject
94-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.AppHost", "src\BotSharp.AppHost\BotSharp.AppHost.csproj", "{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}"
94+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.AppHost", "src\BotSharp.AppHost\BotSharp.AppHost.csproj", "{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}"
95+
EndProject
96+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.OpenAI", "src\Plugins\BotSharp.Plugin.OpenAI\BotSharp.Plugin.OpenAI.csproj", "{6507D336-3A4D-41D4-81C0-2B900173A5FE}"
9597
EndProject
9698
Global
9799
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -373,6 +375,14 @@ Global
373375
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|Any CPU.Build.0 = Release|Any CPU
374376
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.ActiveCfg = Release|Any CPU
375377
{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.Build.0 = Release|Any CPU
378+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
379+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
380+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Debug|x64.ActiveCfg = Debug|Any CPU
381+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Debug|x64.Build.0 = Debug|Any CPU
382+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
383+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Release|Any CPU.Build.0 = Release|Any CPU
384+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Release|x64.ActiveCfg = Release|Any CPU
385+
{6507D336-3A4D-41D4-81C0-2B900173A5FE}.Release|x64.Build.0 = Release|Any CPU
376386
EndGlobalSection
377387
GlobalSection(SolutionProperties) = preSolution
378388
HideSolutionNode = FALSE
@@ -417,6 +427,7 @@ Global
417427
{289E25C8-63F1-4D52-9909-207724DB40CB} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
418428
{CCF745F2-0C95-4ED0-983B-507C528B39EA} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
419429
{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
430+
{6507D336-3A4D-41D4-81C0-2B900173A5FE} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
420431
EndGlobalSection
421432
GlobalSection(ExtensibilityGlobals) = postSolution
422433
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ public class BuiltInAgentId
55
public const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a";
66
public const string Chatbot = "01e2fc5c-2c89-4ec7-8470-7688608b496c";
77
public const string HumanSupport = "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b";
8+
public const string UtilityAssistant = "6745151e-6d46-4a02-8de4-1c4f21c7da95";
89
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using BotSharp.Abstraction.Templating;
2-
31
namespace BotSharp.Core.Agents.Services;
42

53
public partial class AgentService
64
{
75
[MemoryCache(10 * 60, perInstanceCache: true)]
86
public async Task<Agent> LoadAgent(string id)
97
{
10-
if (string.IsNullOrEmpty(id))
8+
if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString())
119
{
1210
return null;
1311
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
</PropertyGroup>
4747

4848
<ItemGroup>
49-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\agent.json" />
50-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json" />
51-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\read_file.json" />
52-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\instruction.liquid" />
53-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\generate_image.fn.liquid" />
54-
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\read_file.fn.liquid" />
49+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
50+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json" />
51+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\read_file.json" />
52+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid" />
53+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid" />
54+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\read_file.fn.liquid" />
5555
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
5656
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions.json" />
5757
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions\human_intervention_needed.json" />
@@ -153,22 +153,22 @@
153153
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid">
154154
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
155155
</Content>
156-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\agent.json">
156+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json">
157157
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
158158
</Content>
159-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\instruction.liquid">
159+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid">
160160
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
161161
</Content>
162-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\read_file.json">
162+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\read_file.json">
163163
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
164164
</Content>
165-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\read_file.fn.liquid">
165+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\read_file.fn.liquid">
166166
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
167167
</Content>
168-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json">
168+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json">
169169
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
170170
</Content>
171-
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\generate_image.fn.liquid">
171+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid">
172172
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
173173
</Content>
174174
<Content Include="data\plugins\config.json">

src/Infrastructure/BotSharp.Core/Files/Functions/GenerateImageFn.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public class GenerateImageFn : IFunctionCallback
99

1010
private readonly IServiceProvider _services;
1111
private readonly ILogger<GenerateImageFn> _logger;
12-
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
1312
private string _conversationId;
1413
private string _messageId;
1514

@@ -29,7 +28,7 @@ public async Task<bool> Execute(RoleDialogModel message)
2928
SetImageOptions();
3029

3130
var agentService = _services.GetRequiredService<IAgentService>();
32-
var agent = await agentService.LoadAgent(UTILITY_ASSISTANT);
31+
var agent = await agentService.LoadAgent(BuiltInAgentId.UtilityAssistant);
3332
var imageAgent = new Agent
3433
{
3534
Id = agent?.Id ?? Guid.Empty.ToString(),

src/Infrastructure/BotSharp.Core/Files/Functions/ReadFileFn.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class ReadFileFn : IFunctionCallback
1212
private readonly ILogger<ReadFileFn> _logger;
1313
private readonly IEnumerable<string> _imageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
1414
private readonly IEnumerable<string> _pdfTypes = new List<string> { "pdf" };
15-
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
1615

1716
public ReadFileFn(
1817
IServiceProvider services,
@@ -31,7 +30,7 @@ public async Task<bool> Execute(RoleDialogModel message)
3130
var wholeDialogs = conv.GetDialogHistory();
3231
var fileTypes = args?.FileTypes?.Split(",", StringSplitOptions.RemoveEmptyEntries)?.ToList() ?? new List<string>();
3332
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes);
34-
var agent = await agentService.LoadAgent(UTILITY_ASSISTANT);
33+
var agent = await agentService.LoadAgent(BuiltInAgentId.UtilityAssistant);
3534
var fileAgent = new Agent
3635
{
3736
Id = agent?.Id ?? Guid.Empty.ToString(),

src/Infrastructure/BotSharp.Core/Files/Hooks/FileReaderHook.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace BotSharp.Core.Files.Hooks;
22

33
public class FileReaderHook : AgentHookBase
44
{
5-
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
65
private static string FUNCTION_NAME = "read_file";
76

87
public override string SelfId => string.Empty;
@@ -45,7 +44,7 @@ public override void OnAgentLoaded(Agent agent)
4544
private (string, FunctionDef?) GetPromptAndFunction()
4645
{
4746
var db = _services.GetRequiredService<IBotSharpRepository>();
48-
var agent = db.GetAgent(UTILITY_ASSISTANT);
47+
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
4948
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
5049
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
5150
return (prompt, loadAttachmentFn);

src/Infrastructure/BotSharp.Core/Files/Hooks/ImageGeneratorHook.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace BotSharp.Core.Files.Hooks;
22

33
public class ImageGeneratorHook : AgentHookBase
44
{
5-
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
65
private static string FUNCTION_NAME = "generate_image";
76

87
public override string SelfId => string.Empty;
@@ -45,7 +44,7 @@ public override void OnAgentLoaded(Agent agent)
4544
private (string, FunctionDef?) GetPromptAndFunction()
4645
{
4746
var db = _services.GetRequiredService<IBotSharpRepository>();
48-
var agent = db.GetAgent(UTILITY_ASSISTANT);
47+
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
4948
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
5049
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
5150
return (prompt, loadAttachmentFn);

src/Infrastructure/BotSharp.Core/data/agents/00000000-0000-0000-0000-000000000000/agent.json renamed to src/Infrastructure/BotSharp.Core/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "00000000-0000-0000-0000-000000000000",
2+
"id": "6745151e-6d46-4a02-8de4-1c4f21c7da95",
33
"name": "Utility Assistant",
44
"description": "Utility assistant that can be used to complete many different tasks",
55
"type": "static",

src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] IncomingM
116116
try
117117
{
118118
var completion = CompletionProvider.GetImageGeneration(_services, provider: input.Provider ?? "openai",
119-
modelId: input.ModelId ?? "dall-e", imageGenerate: true);
119+
model: input.Model ?? "dall-e-3", imageGenerate: true);
120120
var message = await completion.GetImageGeneration(new Agent()
121121
{
122122
Id = Guid.Empty.ToString(),

src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace BotSharp.Platform.AzureAi;
1414
public class AzureOpenAiPlugin : IBotSharpPlugin
1515
{
1616
public string Id => "65185362-392c-44fd-a023-95a198824436";
17-
public string Name => "OpenAI/ Azure OpenAI";
18-
public string Description => "OpenAI/ Azure OpenAI Service including text generation, text to image and other AI services.";
17+
public string Name => "Azure OpenAI";
18+
public string Description => "Azure OpenAI Service including text generation, text to image and other AI services.";
1919
public string IconUrl => "https://nanfor.com/cdn/shop/files/cursos-propios-Azure-openAI.jpg?v=1692877741";
2020

2121
public void RegisterDI(IServiceCollection services, IConfiguration config)
@@ -27,12 +27,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2727
});
2828

2929
services.AddScoped<ITextCompletion, TextCompletionProvider>();
30-
services.AddScoped<ITextCompletion, OpenAiTextCompletionProvider>();
3130
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
32-
services.AddScoped<IChatCompletion, OpenAiChatCompletionProvider>();
3331
services.AddScoped<IImageGeneration, ImageGenerationProvider>();
34-
services.AddScoped<IImageGeneration, OpenAiImageGenerationProvider>();
3532
services.AddScoped<ITextEmbedding, TextEmbeddingProvider>();
36-
services.AddScoped<ITextEmbedding, OpenAiTextEmbeddingProvider>();
3733
}
3834
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/Models/OpenAiTextCompletionRequest.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.AzureOpenAI/Models/TextCompletionRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace BotSharp.Plugin.AzureOpenAI.Models;
44

55
public class TextCompletionRequest
66
{
7-
87
[JsonPropertyName("prompt")]
98
public string Prompt { get; set; } = string.Empty;
109

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class ChatCompletionProvider : IChatCompletion
1212

1313
public virtual string Provider => "azure-openai";
1414

15-
public ChatCompletionProvider(AzureOpenAiSettings settings,
15+
public ChatCompletionProvider(
16+
AzureOpenAiSettings settings,
1617
ILogger<ChatCompletionProvider> logger,
1718
IServiceProvider services)
1819
{

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/OpenAiChatCompletionProvider.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Embedding/OpenAiTextEmbeddingProvider.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageGenerationProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Files.Models;
21
using OpenAI.Images;
32

43
namespace BotSharp.Plugin.AzureOpenAI.Providers.Image;
@@ -10,7 +9,7 @@ public class ImageGenerationProvider : IImageGeneration
109
protected readonly ILogger _logger;
1110

1211
private const int DEFAULT_IMAGE_COUNT = 1;
13-
private const int IMAGE_COUNT_LIMIT = 3;
12+
private const int IMAGE_COUNT_LIMIT = 5;
1413

1514
protected string _model;
1615

@@ -44,7 +43,6 @@ public async Task<RoleDialogModel> GetImageGeneration(Agent agent, List<RoleDial
4443
var response = imageClient.GenerateImages(prompt, imageCount, options);
4544
var values = response.Value;
4645

47-
var content = string.Empty;
4846
var images = new List<ImageGeneration>();
4947
foreach (var value in values)
5048
{
@@ -67,9 +65,9 @@ public async Task<RoleDialogModel> GetImageGeneration(Agent agent, List<RoleDial
6765
}
6866

6967
images.Add(image);
70-
content += $"{image.Description}\r\n";
7168
}
7269

70+
var content = string.Join("\r\n", images.Select(x => x.Description));
7371
var responseMessage = new RoleDialogModel(AgentRole.Assistant, content)
7472
{
7573
CurrentAgentId = agent.Id,

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/OpenAiImageGenerationProvider.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
using Azure.AI.OpenAI;
22
using Azure;
3-
using OpenAI;
4-
using System.ClientModel;
53

64
namespace BotSharp.Plugin.AzureOpenAI.Providers;
75

86
public class ProviderHelper
97
{
10-
public static OpenAIClient GetClient(string provider, string model, IServiceProvider services)
8+
public static AzureOpenAIClient GetClient(string provider, string model, IServiceProvider services)
119
{
1210
var settingsService = services.GetRequiredService<ILlmProviderService>();
1311
var settings = settingsService.GetSetting(provider, model);
14-
var client = provider == "openai" ?
15-
new OpenAIClient(new ApiKeyCredential(settings.ApiKey)) :
16-
new AzureOpenAIClient(new Uri(settings.Endpoint), new AzureKeyCredential(settings.ApiKey));
12+
var client = new AzureOpenAIClient(new Uri(settings.Endpoint), new AzureKeyCredential(settings.ApiKey));
1713
return client;
1814
}
1915

0 commit comments

Comments
 (0)