Skip to content

Commit

Permalink
fix: Fixed some AnyOf property names.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 26, 2024
1 parent cf5d547 commit 5b767cd
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public override string ToString()
{
return IsText
? Text?.Text ?? string.Empty
: IsImage
? Image?.ImageUrl.Url ?? string.Empty
: IsImageUrl
? ImageUrl?.ImageUrl.Url ?? string.Empty
: string.Empty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ListMessagesResponse messages
Console.WriteLine($"[{message.Role.ToString().ToUpper()}]: ");
foreach (ContentItem2 contentItem in message.Content)
{
if (contentItem.MessageImageFileObject is {} imageFile)
if (contentItem.ImageFile is {} imageFile)
{
OpenAIFile imageInfo = await api.Files.RetrieveFileAsync(imageFile.ImageFile.FileId);
byte[] imageBytes = await api.Files.DownloadFileAsync(imageFile.ImageFile.FileId);
Expand All @@ -117,11 +117,11 @@ ListMessagesResponse messages

Console.WriteLine($"<image: {new Uri(fileInfo.FullName).AbsoluteUri}>");
}
if (contentItem.MessageImageUrlObject is {} imageUrl)
if (contentItem.ImageUrl is {} imageUrl)
{
Console.WriteLine($" <Image URL> {imageUrl.ImageUrl.Url}");
}
if (contentItem.MessageTextObject is {} text)
if (contentItem.Text is {} text)
{
Console.WriteLine($"{text.Text.Value}");

Expand All @@ -131,13 +131,13 @@ ListMessagesResponse messages
Console.WriteLine();
foreach (AnnotationsItem annotation in text.Text.Annotations)
{
if (annotation.MessageContentTextFileCitationObject is {} fileCitation)
if (annotation.FileCitation is {} fileCitation)
{
Console.WriteLine($"* File citation, file ID: {fileCitation.FileCitation.FileId}");
Console.WriteLine($"* Text to replace: {fileCitation.Text}");
Console.WriteLine($"* Message content index range: {fileCitation.StartIndex}-{fileCitation.EndIndex}");
}
if (annotation.MessageContentTextFilePathObject is {} filePath)
if (annotation.FilePath is {} filePath)
{
Console.WriteLine($"* File output, new file ID: {filePath.FilePath.FileId}");
Console.WriteLine($"* Text to replace: {filePath.Text}");
Expand All @@ -146,7 +146,7 @@ ListMessagesResponse messages
}
}
}
if (contentItem.MessageRefusalObject is {} refusal)
if (contentItem.Refusal is {} refusal)
{
Console.WriteLine($"Refusal: {refusal.Refusal}");
}
Expand All @@ -165,7 +165,7 @@ ListMessagesResponse messages
Console.WriteLine($"Run step: {step.Status}");
foreach (var toolCall in step.StepDetails.ToolCalls?.ToolCalls ?? [])
{
if (toolCall.RunStepDetailsCodeObject is {} codeInterpreter)
if (toolCall.CodeInterpreter is {} codeInterpreter)
{
Console.WriteLine($" --> Tool call: {codeInterpreter.Type}");
foreach (var output in codeInterpreter.CodeInterpreter.Outputs)
Expand All @@ -180,15 +180,15 @@ ListMessagesResponse messages
}
}
}
if (toolCall.RunStepDetailsFileSearchObject is {} fileSearch)
if (toolCall.FileSearch is {} fileSearch)
{
Console.WriteLine($" --> Tool call: {fileSearch.Type}");
foreach (var output in fileSearch.FileSearch.Results ?? [])
{
Console.WriteLine($" --> Output: {output.FileId}");
}
}
if (toolCall.RunStepDetailsFunctionObject is {} tool)
if (toolCall.Function is {} tool)
{
Console.WriteLine($" --> Tool call: {tool.Type}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,104 +1,95 @@
// using System.Diagnostics.CodeAnalysis;
//
// namespace OpenAI.IntegrationTests.Examples;
//
// public partial class Examples
// {
// [Test]
// [Explicit]
// [Experimental("OPENAI_BETA_001")]
// public async Task AssistantsWithVision()
// {
// using var api = GetAuthenticatedClient();
//
// ImagesResponse appleImage = await api.Images.CreateImageAsync(
// prompt: "picture of apple",
// responseFormat: CreateImageRequestResponseFormat.B64Json);
// byte[] appleBytes = appleImage.Data[0].Bytes;
//
// FileInfo appleFileInfo = new($"{Guid.NewGuid()}.png");
//
// await File.WriteAllBytesAsync(appleFileInfo.FullName, appleBytes);
//
// Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.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);
//
// AssistantObject assistant = await api.Assistants.CreateAssistantAsync(
// model: CreateAssistantRequestModel.Gpt4o,
// instructions: "When asked a question, attempt to answer very concisely. " +
// "Prefer one-sentence answers whenever feasible.");
//
// ThreadObject thread = await api.Assistants.CreateThreadAsync(new CreateThreadRequest
// {
// Messages = [
// "Hello, assistant! Please compare these two images for me:",
// pictureOfAppleFile,
// new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"),
// ]
// });
//
// var streamingUpdates = api.Assistants.CreateRunAsStreamAsync(
// threadId: thread.Id,
// assistantId: assistant.Id,
// instructions: "When possible, try to sneak in puns if you're asked to compare things.");
//
// 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 })
// {
// Console.WriteLine("--- Run created! ---");
// }
// }
// if (streamingUpdate.Message is {} message)
// {
// if (message.Value3 is
// {
// Event: MessageStreamEventVariant3Event.ThreadMessageDelta
// } delta)
// {
// foreach (var deltaVariation in delta.Data.Delta.Content ?? [])
// {
// if (deltaVariation.Value1 is {} imageFile)
// {
// Console.WriteLine();
// Console.WriteLine(imageFile.ImageFile?.FileId);
// }
// if (deltaVariation.Value2 is {} text)
// {
// Console.Write(text.Text?.Value);
// }
// if (deltaVariation.Value3 is {} refusal)
// {
// Console.WriteLine();
// Console.WriteLine(refusal.Refusal);
// }
// if (deltaVariation.Value4 is {} imageUrl)
// {
// Console.WriteLine();
// Console.WriteLine(imageUrl.ImageUrl?.Url);
// }
// }
// }
// }
// }
//
// _ = await api.Files.DeleteFileAsync(pictureOfAppleFile.Id);
// _ = await api.Assistants.DeleteThreadAsync(thread.Id);
// _ = await api.Assistants.DeleteAssistantAsync(assistant.Id);
// }
// }
using System.Diagnostics.CodeAnalysis;

namespace OpenAI.IntegrationTests.Examples;

public partial class Examples
{
[Test]
[Explicit]
[Experimental("OPENAI_BETA_001")]
public async Task AssistantsWithVision()
{
using var api = GetAuthenticatedClient();

ImagesResponse appleImage = await api.Images.CreateImageAsync(
prompt: "picture of apple",
responseFormat: CreateImageRequestResponseFormat.B64Json);
byte[] appleBytes = appleImage.Data[0].Bytes;

FileInfo appleFileInfo = new($"{Guid.NewGuid()}.png");

await File.WriteAllBytesAsync(appleFileInfo.FullName, appleBytes);

Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.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);

AssistantObject assistant = await api.Assistants.CreateAssistantAsync(
model: CreateAssistantRequestModel.Gpt4o,
instructions: "When asked a question, attempt to answer very concisely. " +
"Prefer one-sentence answers whenever feasible.");

ThreadObject thread = await api.Assistants.CreateThreadAsync(new CreateThreadRequest
{
Messages = [
"Hello, assistant! Please compare these two images for me:",
pictureOfAppleFile,
new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"),
]
});

var streamingUpdates = api.Assistants.CreateRunAsStreamAsync(
threadId: thread.Id,
assistantId: assistant.Id,
instructions: "When possible, try to sneak in puns if you're asked to compare things.");

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.ThreadRunCreated is not null)
{
Console.WriteLine("--- Run created! ---");
}
if (streamingUpdate.ThreadMessageDelta is {} delta)
{
foreach (var deltaVariation in delta.Data.Delta.Content ?? [])
{
if (deltaVariation.ImageFile is {} imageFile)
{
Console.WriteLine();
Console.WriteLine(imageFile.ImageFile?.FileId);
}
if (deltaVariation.Text is {} text)
{
Console.Write(text.Text?.Value);
}
if (deltaVariation.Refusal is {} refusal)
{
Console.WriteLine();
Console.WriteLine(refusal.Refusal);
}
if (deltaVariation.ImageUrl is {} imageUrl)
{
Console.WriteLine();
Console.WriteLine(imageUrl.ImageUrl?.Url);
}
}
}
}

_ = await api.Files.DeleteFileAsync(pictureOfAppleFile.Id);
_ = await api.Assistants.DeleteThreadAsync(thread.Id);
_ = await api.Assistants.DeleteAssistantAsync(assistant.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ ListMessagesResponse messages
Console.WriteLine($"[{message.Role.ToString().ToUpper()}]: ");
foreach (ContentItem2 contentItem in message.Content)
{
if (contentItem.MessageImageFileObject is {} imageFile)
if (contentItem.ImageFile is {} imageFile)
{
Console.WriteLine($" <Image File ID> {imageFile.ImageFile.FileId}");
}
if (contentItem.MessageImageUrlObject is {} imageUrl)
if (contentItem.ImageUrl is {} imageUrl)
{
Console.WriteLine($" <Image URL> {imageUrl.ImageUrl.Url}");
}
if (contentItem.MessageTextObject is {} text)
if (contentItem.Text is {} text)
{
Console.WriteLine($"{text.Text.Value}");

Expand All @@ -91,13 +91,13 @@ ListMessagesResponse messages
Console.WriteLine();
foreach (AnnotationsItem annotation in text.Text.Annotations)
{
if (annotation.MessageContentTextFileCitationObject is {} fileCitation)
if (annotation.FileCitation is {} fileCitation)
{
Console.WriteLine($"* File citation, file ID: {fileCitation.FileCitation.FileId}");
Console.WriteLine($"* Text to replace: {fileCitation.Text}");
Console.WriteLine($"* Message content index range: {fileCitation.StartIndex}-{fileCitation.EndIndex}");
}
if (annotation.MessageContentTextFilePathObject is {} filePath)
if (annotation.FilePath is {} filePath)
{
Console.WriteLine($"* File output, new file ID: {filePath.FilePath.FileId}");
Console.WriteLine($"* Text to replace: {filePath.Text}");
Expand All @@ -106,7 +106,7 @@ ListMessagesResponse messages
}
}
}
if (contentItem.MessageRefusalObject is {} refusal)
if (contentItem.Refusal is {} refusal)
{
Console.WriteLine($"Refusal: {refusal.Refusal}");
}
Expand Down
Loading

0 comments on commit 5b767cd

Please sign in to comment.