Skip to content

Commit b4f87fd

Browse files
authored
Merge pull request #537 from iceljc/features/refine-image-reading
refine chat image reading
2 parents c27acab + 5aa4595 commit b4f87fd

File tree

11 files changed

+54
-70
lines changed

11 files changed

+54
-70
lines changed

src/Infrastructure/BotSharp.Core/Using.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
global using BotSharp.Abstraction.Files.Enums;
2929
global using BotSharp.Abstraction.Translation.Attributes;
3030
global using BotSharp.Abstraction.Messaging.Enums;
31-
global using BotSharp.Abstraction.Http.Settings;
3231
global using BotSharp.Core.Repository;
3332
global using BotSharp.Core.Routing;
3433
global using BotSharp.Core.Agents.Services;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task<string> MultiModalCompletion([FromBody] IncomingMessageModel i
8585
try
8686
{
8787
var completion = CompletionProvider.GetChatCompletion(_services, provider: input.Provider ?? "openai",
88-
modelId: input.ModelId ?? "gpt-4", multiModal: true);
88+
model: input.Model ?? "gpt-4o", multiModal: true);
8989
var message = await completion.GetChatCompletions(new Agent()
9090
{
9191
Id = Guid.Empty.ToString(),

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
193193
return true;
194194
}
195195

196-
197196
protected (string, IEnumerable<ChatMessage>, ChatCompletionOptions) PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
198197
{
199198
var agentService = _services.GetRequiredService<IAgentService>();
@@ -257,40 +256,34 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
257256
{
258257
var text = !string.IsNullOrWhiteSpace(message.Payload) ? message.Payload : message.Content;
259258
var textPart = ChatMessageContentPart.CreateTextMessageContentPart(text);
260-
var chat = new UserChatMessage(textPart)
261-
{
262-
ParticipantName = message.FunctionName
263-
};
259+
var contentParts = new List<ChatMessageContentPart> { textPart };
264260

265-
if (allowMultiModal)
261+
if (allowMultiModal && !message.Files.IsNullOrEmpty())
266262
{
267-
if (!message.Files.IsNullOrEmpty())
263+
foreach (var file in message.Files)
268264
{
269-
foreach (var file in message.Files)
265+
if (!string.IsNullOrEmpty(file.FileUrl))
266+
{
267+
var uri = new Uri(file.FileUrl);
268+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(uri, ImageChatMessageContentPartDetail.Low);
269+
contentParts.Add(contentPart);
270+
}
271+
else if (!string.IsNullOrEmpty(file.FileData))
270272
{
271-
if (!string.IsNullOrEmpty(file.FileUrl))
272-
{
273-
var uri = new Uri(file.FileUrl);
274-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(uri, ImageChatMessageContentPartDetail.Low);
275-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
276-
}
277-
else if (!string.IsNullOrEmpty(file.FileData))
278-
{
279-
var (contentType, bytes) = fileService.GetFileInfoFromData(file.FileData);
280-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromBytes(bytes), contentType, ImageChatMessageContentPartDetail.Low);
281-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
282-
}
283-
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
284-
{
285-
var contentType = fileService.GetFileContentType(file.FileStorageUrl);
286-
using var stream = File.OpenRead(file.FileStorageUrl);
287-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromStream(stream), contentType, ImageChatMessageContentPartDetail.Low);
288-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
289-
}
273+
var (contentType, bytes) = fileService.GetFileInfoFromData(file.FileData);
274+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromBytes(bytes), contentType, ImageChatMessageContentPartDetail.Low);
275+
contentParts.Add(contentPart);
276+
}
277+
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
278+
{
279+
var contentType = fileService.GetFileContentType(file.FileStorageUrl);
280+
using var stream = File.OpenRead(file.FileStorageUrl);
281+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromStream(stream), contentType, ImageChatMessageContentPartDetail.Low);
282+
contentParts.Add(contentPart);
290283
}
291284
}
292285
}
293-
messages.Add(chat);
286+
messages.Add(new UserChatMessage(contentParts) { ParticipantName = message.FunctionName });
294287
}
295288
else if (message.Role == AgentRole.Assistant)
296289
{
@@ -302,7 +295,6 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
302295
return (prompt, messages, options);
303296
}
304297

305-
306298
private string GetPrompt(IEnumerable<ChatMessage> messages, ChatCompletionOptions options)
307299
{
308300
var prompt = string.Empty;

src/Plugins/BotSharp.Plugin.HttpHandler/Functions/HandleHttpRequestFn.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net.Http;
2-
using BotSharp.Plugin.HttpHandler.LlmContexts;
32
using Microsoft.AspNetCore.Http;
43
using Microsoft.Extensions.Logging;
54

@@ -58,7 +57,7 @@ public async Task<bool> Execute(RoleDialogModel message)
5857
{
5958
if (string.IsNullOrEmpty(url)) return null;
6059

61-
var settings = _services.GetRequiredService<HttpSettings>();
60+
var settings = _services.GetRequiredService<HttpHandlerSettings>();
6261
using var client = _httpClientFactory.CreateClient();
6362
AddRequestHeaders(client);
6463

@@ -82,7 +81,7 @@ private void AddRequestHeaders(HttpClient client)
8281
{
8382
client.DefaultRequestHeaders.Add("Authorization", $"{_context.HttpContext.Request.Headers["Authorization"]}");
8483

85-
var settings = _services.GetRequiredService<HttpSettings>();
84+
var settings = _services.GetRequiredService<HttpHandlerSettings>();
8685
var origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : $"{_context.HttpContext.Request.Headers["Origin"]}";
8786
if (!string.IsNullOrEmpty(origin))
8887
{

src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Agents.Settings;
44
using BotSharp.Abstraction.Functions.Models;
55
using BotSharp.Abstraction.Repositories;
6-
using BotSharp.Plugin.HttpHandler.Enums;
76

87
namespace BotSharp.Plugin.HttpHandler.Hooks;
98

src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerUtilityHook.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Agents;
2-
using BotSharp.Plugin.HttpHandler.Enums;
32

43
namespace BotSharp.Plugin.HttpHandler.Hooks;
54

src/Plugins/BotSharp.Plugin.HttpHandler/HttpHandlerPlugin.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Agents;
22
using BotSharp.Abstraction.Settings;
3-
using BotSharp.Plugin.HttpHandler.Hooks;
43
using Microsoft.Extensions.Configuration;
54

65
namespace BotSharp.Plugin.HttpHandler;
@@ -18,7 +17,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
1817
services.AddScoped(provider =>
1918
{
2019
var settingService = provider.GetRequiredService<ISettingService>();
21-
return settingService.Bind<HttpSettings>("Http");
20+
return settingService.Bind<HttpHandlerSettings>("HttpHandler");
2221
});
2322

2423
services.AddScoped<IAgentHook, HttpHandlerHook>();

src/Infrastructure/BotSharp.Abstraction/Http/Settings/HttpSettings.cs renamed to src/Plugins/BotSharp.Plugin.HttpHandler/Settings/HttpHandlerSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace BotSharp.Abstraction.Http.Settings;
1+
namespace BotSharp.Plugin.HttpHandler.Settings;
22

3-
public class HttpSettings
3+
public class HttpHandlerSettings
44
{
55
public string BaseAddress { get; set; } = string.Empty;
66
public string Origin { get; set; } = string.Empty;

src/Plugins/BotSharp.Plugin.HttpHandler/Using.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
global using BotSharp.Abstraction.Messaging;
1616
global using BotSharp.Abstraction.Messaging.Models.RichContent;
1717
global using BotSharp.Abstraction.Options;
18-
global using BotSharp.Abstraction.Http.Settings;
19-
global using BotSharp.Abstraction.Messaging.Enums;
18+
global using BotSharp.Abstraction.Messaging.Enums;
19+
global using BotSharp.Plugin.HttpHandler.Hooks;
20+
global using BotSharp.Plugin.HttpHandler.Settings;
21+
global using BotSharp.Plugin.HttpHandler.LlmContexts;
22+
global using BotSharp.Plugin.HttpHandler.Enums;

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -257,40 +257,34 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
257257
{
258258
var text = !string.IsNullOrWhiteSpace(message.Payload) ? message.Payload : message.Content;
259259
var textPart = ChatMessageContentPart.CreateTextMessageContentPart(text);
260-
var chat = new UserChatMessage(textPart)
261-
{
262-
ParticipantName = message.FunctionName
263-
};
260+
var contentParts = new List<ChatMessageContentPart> { textPart };
264261

265-
if (allowMultiModal)
262+
if (allowMultiModal && !message.Files.IsNullOrEmpty())
266263
{
267-
if (!message.Files.IsNullOrEmpty())
264+
foreach (var file in message.Files)
268265
{
269-
foreach (var file in message.Files)
266+
if (!string.IsNullOrEmpty(file.FileUrl))
267+
{
268+
var uri = new Uri(file.FileUrl);
269+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(uri, ImageChatMessageContentPartDetail.Low);
270+
contentParts.Add(contentPart);
271+
}
272+
else if (!string.IsNullOrEmpty(file.FileData))
273+
{
274+
var (contentType, bytes) = fileService.GetFileInfoFromData(file.FileData);
275+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromBytes(bytes), contentType, ImageChatMessageContentPartDetail.Low);
276+
contentParts.Add(contentPart);
277+
}
278+
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
270279
{
271-
if (!string.IsNullOrEmpty(file.FileUrl))
272-
{
273-
var uri = new Uri(file.FileUrl);
274-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(uri, ImageChatMessageContentPartDetail.Low);
275-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
276-
}
277-
else if (!string.IsNullOrEmpty(file.FileData))
278-
{
279-
var (contentType, bytes) = fileService.GetFileInfoFromData(file.FileData);
280-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromBytes(bytes), contentType, ImageChatMessageContentPartDetail.Low);
281-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
282-
}
283-
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
284-
{
285-
var contentType = fileService.GetFileContentType(file.FileStorageUrl);
286-
using var stream = File.OpenRead(file.FileStorageUrl);
287-
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromStream(stream), contentType, ImageChatMessageContentPartDetail.Low);
288-
chat = new UserChatMessage(textPart, contentPart) { ParticipantName = message.FunctionName };
289-
}
280+
var contentType = fileService.GetFileContentType(file.FileStorageUrl);
281+
using var stream = File.OpenRead(file.FileStorageUrl);
282+
var contentPart = ChatMessageContentPart.CreateImageMessageContentPart(BinaryData.FromStream(stream), contentType, ImageChatMessageContentPartDetail.Low);
283+
contentParts.Add(contentPart);
290284
}
291285
}
292286
}
293-
messages.Add(chat);
287+
messages.Add(new UserChatMessage(contentParts) { ParticipantName = message.FunctionName });
294288
}
295289
else if (message.Role == AgentRole.Assistant)
296290
{

0 commit comments

Comments
 (0)