Skip to content

Commit

Permalink
fix: Update AssistantsWithVision example to use urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 28, 2024
1 parent d237b70 commit fc09ff5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
52 changes: 45 additions & 7 deletions src/libs/OpenAI/Conversions/CreateMessageRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public static implicit operator CreateMessageRequest(string content)
return FromString(content);
}

/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static implicit operator CreateMessageRequest(Uri uri)
{
return FromUri(uri);
}

/// <summary>
///
/// </summary>
Expand All @@ -31,33 +41,61 @@ public static implicit operator CreateMessageRequest(string content)
/// <returns></returns>
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<OneOf<MessageContentImageFileObject, MessageContentImageUrlObject,
MessageRequestContentTextObject>>
{
MessageContentImageFileObject.FromOpenAIFile(file),
MessageContentImageFileObject.FromOpenAIFile(file, detail),
},
};
}

/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <param name="role"></param>
/// <returns></returns>
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,
};
}

/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="detail"></param>
/// <param name="role"></param>
/// <returns></returns>
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<OneOf<MessageContentImageFileObject, MessageContentImageUrlObject,
MessageRequestContentTextObject>>
{
MessageContentImageUrlObject.FromUri(uri, detail),
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static implicit operator MessageContentImageFileObject(OpenAIFile file)
/// <returns></returns>
public static MessageContentImageFileObject FromOpenAIFile(
OpenAIFile file,
MessageContentImageFileObjectImageFileDetail? detail = null)
MessageContentImageFileObjectImageFileDetail detail = MessageContentImageFileObjectImageFileDetail.Auto)
{
file = file ?? throw new ArgumentNullException(nameof(file));

Expand Down
36 changes: 36 additions & 0 deletions src/libs/OpenAI/Conversions/MessageContentImageUrlObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace OpenAI;

public partial class MessageContentImageUrlObject
{
/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static implicit operator MessageContentImageUrlObject(Uri uri)
{
return FromUri(uri);
}

/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="detail"></param>
/// <returns></returns>
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(),
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
]
});

Expand All @@ -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 })
Expand Down

0 comments on commit fc09ff5

Please sign in to comment.