From fc09ff54879c2b2b9c625e9852e9de1247a01bcb Mon Sep 17 00:00:00 2001 From: HavenDV Date: Sat, 28 Sep 2024 16:17:19 +0400 Subject: [PATCH] fix: Update AssistantsWithVision example to use urls. --- .../Conversions/CreateMessageRequest.cs | 52 ++++++++++++++++--- .../MessageContentImageFileObject.cs | 2 +- .../MessageContentImageUrlObject.cs | 36 +++++++++++++ ...xamples.Assistants.AssistantsWithVision.cs | 24 ++++----- 4 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 src/libs/OpenAI/Conversions/MessageContentImageUrlObject.cs diff --git a/src/libs/OpenAI/Conversions/CreateMessageRequest.cs b/src/libs/OpenAI/Conversions/CreateMessageRequest.cs index aa9713d2..753fba39 100644 --- a/src/libs/OpenAI/Conversions/CreateMessageRequest.cs +++ b/src/libs/OpenAI/Conversions/CreateMessageRequest.cs @@ -22,6 +22,16 @@ public static implicit operator CreateMessageRequest(string content) return FromString(content); } + /// + /// + /// + /// + /// + public static implicit operator CreateMessageRequest(Uri uri) + { + return FromUri(uri); + } + /// /// /// @@ -31,33 +41,61 @@ public static implicit operator CreateMessageRequest(string content) /// public static CreateMessageRequest FromOpenAIFile( OpenAIFile file, - CreateMessageRequestRole role = CreateMessageRequestRole.User, - MessageContentImageFileObjectImageFileDetail? detail = null) + MessageContentImageFileObjectImageFileDetail detail = MessageContentImageFileObjectImageFileDetail.Auto, + CreateMessageRequestRole role = CreateMessageRequestRole.User) { file = file ?? throw new ArgumentNullException(nameof(file)); return new CreateMessageRequest { - Role = CreateMessageRequestRole.User, + Role = role, Content = new List> { - MessageContentImageFileObject.FromOpenAIFile(file), + MessageContentImageFileObject.FromOpenAIFile(file, detail), }, }; } - + /// /// /// /// + /// /// - public static CreateMessageRequest FromString(string content) + public static CreateMessageRequest FromString( + string content, + CreateMessageRequestRole role = CreateMessageRequestRole.User) { return new CreateMessageRequest { - Role = CreateMessageRequestRole.User, + Role = role, Content = content, }; } + + /// + /// + /// + /// + /// + /// + /// + public static CreateMessageRequest FromUri( + Uri uri, + MessageContentImageUrlObjectImageUrlDetail detail = MessageContentImageUrlObjectImageUrlDetail.Auto, + CreateMessageRequestRole role = CreateMessageRequestRole.User) + { + uri = uri ?? throw new ArgumentNullException(nameof(uri)); + + return new CreateMessageRequest + { + Role = role, + Content = new List> + { + MessageContentImageUrlObject.FromUri(uri, detail), + }, + }; + } } \ No newline at end of file diff --git a/src/libs/OpenAI/Conversions/MessageContentImageFileObject.cs b/src/libs/OpenAI/Conversions/MessageContentImageFileObject.cs index 1f913c71..e7cccc8f 100644 --- a/src/libs/OpenAI/Conversions/MessageContentImageFileObject.cs +++ b/src/libs/OpenAI/Conversions/MessageContentImageFileObject.cs @@ -20,7 +20,7 @@ public static implicit operator MessageContentImageFileObject(OpenAIFile file) /// public static MessageContentImageFileObject FromOpenAIFile( OpenAIFile file, - MessageContentImageFileObjectImageFileDetail? detail = null) + MessageContentImageFileObjectImageFileDetail detail = MessageContentImageFileObjectImageFileDetail.Auto) { file = file ?? throw new ArgumentNullException(nameof(file)); diff --git a/src/libs/OpenAI/Conversions/MessageContentImageUrlObject.cs b/src/libs/OpenAI/Conversions/MessageContentImageUrlObject.cs new file mode 100644 index 00000000..b0e17910 --- /dev/null +++ b/src/libs/OpenAI/Conversions/MessageContentImageUrlObject.cs @@ -0,0 +1,36 @@ +namespace OpenAI; + +public partial class MessageContentImageUrlObject +{ + /// + /// + /// + /// + /// + public static implicit operator MessageContentImageUrlObject(Uri uri) + { + return FromUri(uri); + } + + /// + /// + /// + /// + /// + /// + public static MessageContentImageUrlObject FromUri( + Uri uri, + MessageContentImageUrlObjectImageUrlDetail detail = MessageContentImageUrlObjectImageUrlDetail.Auto) + { + uri = uri ?? throw new ArgumentNullException(nameof(uri)); + + return new MessageContentImageUrlObject + { + ImageUrl = new MessageContentImageUrlObjectImageUrl + { + Detail = detail, + Url = uri.ToString(), + } + }; + } +} \ No newline at end of file diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AssistantsWithVision.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AssistantsWithVision.cs index 3abf3386..25f4ad8c 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AssistantsWithVision.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AssistantsWithVision.cs @@ -19,25 +19,12 @@ public async Task AssistantsWithVision() Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.FullName).AbsoluteUri}"); - ImagesResponse orangeImage = await api.Images.CreateImageAsync( - prompt: "picture of orange", - responseFormat: CreateImageRequestResponseFormat.B64Json); - byte[] orangeBytes = orangeImage.Data[0].Bytes; - - FileInfo orangeFileInfo = new($"{Guid.NewGuid()}.png"); - - await File.WriteAllBytesAsync(orangeFileInfo.FullName, orangeBytes); - - Console.WriteLine($"Orange image available at:\n{new Uri(orangeFileInfo.FullName).AbsoluteUri}"); + Console.WriteLine($"Orange image available at:\n{new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png")}"); OpenAIFile pictureOfAppleFile = await api.Files.CreateFileAsync( file: appleBytes, filename: appleFileInfo.Name, purpose: CreateFileRequestPurpose.Vision); - OpenAIFile pictureOfOrangeFile = await api.Files.CreateFileAsync( - file: orangeBytes, - filename: orangeFileInfo.Name, - purpose: CreateFileRequestPurpose.Vision); AssistantObject assistant = await api.Assistants.CreateAssistantAsync( model: CreateAssistantRequestModel.Gpt4o, @@ -49,7 +36,7 @@ public async Task AssistantsWithVision() Messages = [ "Hello, assistant! Please compare these two images for me:", pictureOfAppleFile, - pictureOfOrangeFile, + new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"), ] }); @@ -60,6 +47,13 @@ public async Task AssistantsWithVision() await foreach (AssistantStreamEvent streamingUpdate in streamingUpdates) { + if (streamingUpdate.Error is {} error) + { + Console.WriteLine("--- Error ---"); + Console.WriteLine($"Message: {error.Data.Message}"); + Console.WriteLine($"Code: {error.Data.Code}"); + Console.WriteLine($"Type: {error.Data.Type}"); + } if (streamingUpdate.Run is {} run) { if (run.Value1 is { Event: RunStreamEventVariant1Event.ThreadRunCreated })