Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/assistant file ids stability #574

Merged
merged 10 commits into from
Jun 3, 2024
3 changes: 2 additions & 1 deletion OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@
//await AssistantTestHelper.MessagesTestHelper.RunTests(sdk);
//await AssistantTestHelper.RunTestHelper.RunTests(sdk);
//await AssistantTestHelper.VectorTestHelper.RunTests(sdk);
//await AssistantTestHelper3.RunTests(sdk);

// Vision
//await VisionTestHelper.RunSimpleVisionTest(sdk);
//await VisionTestHelper.RunSimpleVisionStreamTest(sdk);
//await VisionTestHelper.RunSimpleVisionTestUsingBase64EncodedImage(sdk);
//await VisionTestHelper.RunSimpleVisionStreamTest(sdk);

// Tools
//await ChatCompletionTestHelper.RunChatFunctionCallTest(sdk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace OpenAI.Playground.TestHelpers.AssistantHelpers;

internal static class AssistantTestHelper3
{
public static async Task RunTests(IOpenAIService sdk)
{
await RunAssistantApiTest(sdk);
await RunHowAssistantsWorkTest(sdk);
}

/// <summary>
/// Test Assistant api
/// </summary>
Expand Down Expand Up @@ -133,8 +139,11 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
var sampleFile = await FileExtensions.ReadAllBytesAsync($"SampleData/{fileName}");
var sampleFileAsString = Encoding.UTF8.GetString(sampleFile);

// HACK: drp052424 - CSV format is not supported for Assistant, so we tell OpenAI it's a TXT. A better solution would be to update the test asset.
var fileNameForOpenAI = Path.ChangeExtension(fileName, ".txt");

ConsoleExtensions.WriteLine($"Uploading file: {fileName}", ConsoleColor.DarkCyan);
var uploadFilesResponse = await sdk.Files.FileUpload(UploadFilePurposes.UploadFilePurpose.Assistants, sampleFile, fileName);
var uploadFilesResponse = await sdk.Files.FileUpload(UploadFilePurposes.UploadFilePurpose.Assistants, sampleFile, fileNameForOpenAI);
if (uploadFilesResponse.Successful)
{
ConsoleExtensions.WriteLine($"{fileName} uploaded", ConsoleColor.DarkGreen);
Expand All @@ -145,8 +154,8 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
return;
}

var uplaodFileId = uploadFilesResponse.Id;
ConsoleExtensions.WriteLine($"uplaodFileId:{uplaodFileId}, purpose:{uploadFilesResponse.Purpose}");
var uploadFileId = uploadFilesResponse.Id;
ConsoleExtensions.WriteLine($"uploadFileId:{uploadFileId}, purpose:{uploadFilesResponse.Purpose}");

#endregion

Expand All @@ -163,7 +172,6 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
Name = "Qicha",
Model = Models.Gpt_3_5_Turbo_1106,
Tools = new List<ToolDefinition>() { ToolDefinition.DefineCodeInterpreter(), ToolDefinition.DefineFileSearch(), ToolDefinition.DefineFunction(func) },
FileIds = new List<string>() { uplaodFileId }
});

if (assistantResult.Successful)
Expand Down Expand Up @@ -207,7 +215,12 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
{
Role = StaticValues.AssistantsStatics.MessageStatics.Roles.User,
Content =new("Where is Zhejiang Jiacheng Supply Chain Co., LTD."),
Attachments = [new() { FileId = uplaodFileId }]
// Tools must be specified for Attachments
Attachments = [new()
{
FileId = uploadFileId,
Tools = [ ToolDefinition.DefineFileSearch() ]
}]
});

if (messageResult.Successful)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using OpenAI.Interfaces;
using OpenAI.ObjectModels;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.ObjectModels.SharedModels;
using OpenAI.Playground.ExtensionsAndHelpers;
using static OpenAI.ObjectModels.StaticValues;

namespace OpenAI.Playground.TestHelpers.AssistantHelpers;

Expand All @@ -11,11 +13,13 @@ internal static partial class MessagesTestHelper
{
private static string? CreatedMessageId { get; set; }
private static string? CreatedThreadId { get; set; }
private static string? CreatedFileId { get; set; }

public static async Task RunTests(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Message Basics Testing is starting:", ConsoleColor.Blue);
await CreateMessage(openAI);
await CreateMessageWithImage(openAI);
await ListMessages(openAI);
await RetrieveMessage(openAI);
await ModifyMessage(openAI);
Expand All @@ -39,7 +43,62 @@ public static async Task CreateMessage(IOpenAIService openAI)
}

CreatedThreadId = thread.Id;
var result = await openAI.Beta.Messages.CreateMessage(CreatedThreadId, new(StaticValues.AssistantsStatics.MessageStatics.Roles.User, new("How does AI work? Explain it in simple terms.")));
var result = await openAI.Beta.Messages.CreateMessage(CreatedThreadId, new(AssistantsStatics.MessageStatics.Roles.User, new("How does AI work? Explain it in simple terms.")));
if (result.Successful)
{
CreatedMessageId = result.Id;
ConsoleExtensions.WriteLine($"Message Created Successfully with ID: {result.Id}", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteError(result.Error);
}
}

public static async Task CreateMessageWithImage(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Create MessageWithImage Testing is starting:", ConsoleColor.Cyan);

var prompt = "Tell me about this image";
var filename = "image_edit_original.png";
var filePath = $"SampleData/{filename}";

var sampleBytes = await FileExtensions.ReadAllBytesAsync(filePath);

// Upload File
ConsoleExtensions.WriteLine("Upload File Test", ConsoleColor.DarkCyan);

ConsoleExtensions.WriteLine($"Uploading file: {filename}", ConsoleColor.DarkCyan);
var uploadFilesResponse = await openAI.Files.FileUpload(UploadFilePurposes.UploadFilePurpose.Vision, sampleBytes, filename);
if (uploadFilesResponse.Successful)
{
ConsoleExtensions.WriteLine($"{filename} uploaded", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"{filename} failed", ConsoleColor.DarkRed);
return;
}

var uploadFileId = uploadFilesResponse.Id;
ConsoleExtensions.WriteLine($"uploadFileId:{uploadFileId}, purpose:{uploadFilesResponse.Purpose}");


// Message.ImageFileContent
ConsoleExtensions.WriteLine("Message with ImageFileContent Test:", ConsoleColor.DarkCyan);

MessageContentOneOfType content = new([
MessageContent.TextContent(prompt),
MessageContent.ImageFileContent(uploadFileId, ImageStatics.ImageDetailTypes.High)
]);

MessageCreateRequest request = new()
{
Role = AssistantsStatics.MessageStatics.Roles.User,
Content = content
};

var result = await openAI.Beta.Messages.CreateMessage(CreatedThreadId!, request);
if (result.Successful)
{
CreatedMessageId = result.Id;
Expand Down Expand Up @@ -203,6 +262,11 @@ private static async Task Cleanup(IOpenAIService sdk)
{
await sdk.Beta.Threads.ThreadDelete(CreatedThreadId);
}

if (!string.IsNullOrWhiteSpace(CreatedFileId))
{
await sdk.Files.DeleteFile(CreatedFileId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,11 @@ public static async Task CancelVectorStoreFileBatch(IOpenAIService openAI)

private static async Task Cleanup(IOpenAIService sdk)
{

if (!string.IsNullOrWhiteSpace(CreatedVectorFileId))
if (!string.IsNullOrWhiteSpace(CreatedVectorFileId) && !string.IsNullOrWhiteSpace(CreatedVectorId))
{
await sdk.Beta.VectorStoreFiles.DeleteVectorStoreFile(CreatedVectorId, CreatedVectorFileId);
}

if (!string.IsNullOrWhiteSpace(CreatedFileId1))
{
await sdk.Files.DeleteFile(CreatedFileId1);
Expand All @@ -523,4 +523,4 @@ private static async Task Cleanup(IOpenAIService sdk)
}
}
}
}
}
133 changes: 51 additions & 82 deletions OpenAI.Playground/TestHelpers/VisionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,28 @@ internal static class VisionTestHelper
{
public static async Task RunSimpleVisionTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("VIsion Testing is starting:", ConsoleColor.Cyan);
ConsoleExtensions.WriteLine("Vision Testing is starting:", ConsoleColor.Cyan);

try
{
ConsoleExtensions.WriteLine("Vision Test:", ConsoleColor.DarkCyan);

var completionResult = await sdk.ChatCompletion.CreateCompletion(
new ChatCompletionCreateRequest
var completionResult = await sdk.ChatCompletion.CreateCompletion(new()
{
Messages = new List<ChatMessage>
{
Messages = new List<ChatMessage>
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(new List<MessageContent>
{
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(
new List<MessageContent>
{
MessageContent.TextContent("What is on the picture in details?"),
MessageContent.ImageUrlContent(
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
ImageStatics.ImageDetailTypes.High
)
}
),
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
}
);
MessageContent.TextContent("What is on the picture in details?"),
MessageContent.ImageUrlContent("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
ImageStatics.ImageDetailTypes.High)
})
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
});

if (completionResult.Successful)
{
Expand All @@ -47,12 +41,10 @@ public static async Task RunSimpleVisionTest(IOpenAIService sdk)
{
if (completionResult.Error == null)
{
throw new Exception("Unknown Error");
throw new("Unknown Error");
}

Console.WriteLine(
$"{completionResult.Error.Code}: {completionResult.Error.Message}"
);
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
}
}
catch (Exception e)
Expand All @@ -69,28 +61,22 @@ public static async Task RunSimpleVisionStreamTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Vision Stream Test:", ConsoleColor.DarkCyan);

var completionResult = sdk.ChatCompletion.CreateCompletionAsStream(
new ChatCompletionCreateRequest
var completionResult = sdk.ChatCompletion.CreateCompletionAsStream(new()
{
Messages = new List<ChatMessage>
{
Messages = new List<ChatMessage>
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(new List<MessageContent>
{
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(
new List<MessageContent>
{
MessageContent.TextContent("What�s in this image?"),
MessageContent.ImageUrlContent(
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
ImageStatics.ImageDetailTypes.Low
)
}
),
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
}
);
MessageContent.TextContent("What�s in this image?"),
MessageContent.ImageUrlContent("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
ImageStatics.ImageDetailTypes.Low)
})
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
});

await foreach (var completion in completionResult)
{
Expand All @@ -102,12 +88,10 @@ public static async Task RunSimpleVisionStreamTest(IOpenAIService sdk)
{
if (completion.Error == null)
{
throw new Exception("Unknown Error");
throw new("Unknown Error");
}

Console.WriteLine(
$"{completion.Error.Code}: {completion.Error.Message}"
);
Console.WriteLine($"{completion.Error.Code}: {completion.Error.Message}");
}
}

Expand All @@ -127,39 +111,26 @@ public static async Task RunSimpleVisionTestUsingBase64EncodedImage(IOpenAIServi

try
{
ConsoleExtensions.WriteLine(
"Vision with base64 encoded image Test:",
ConsoleColor.DarkCyan
);
ConsoleExtensions.WriteLine("Vision with base64 encoded image Test:", ConsoleColor.DarkCyan);

const string originalFileName = "image_edit_original.png";
var originalFile = await FileExtensions.ReadAllBytesAsync(
$"SampleData/{originalFileName}"
);
var originalFile = await FileExtensions.ReadAllBytesAsync($"SampleData/{originalFileName}");

var completionResult = await sdk.ChatCompletion.CreateCompletion(
new ChatCompletionCreateRequest
var completionResult = await sdk.ChatCompletion.CreateCompletion(new()
{
Messages = new List<ChatMessage>
{
Messages = new List<ChatMessage>
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(new List<MessageContent>
{
ChatMessage.FromSystem("You are an image analyzer assistant."),
ChatMessage.FromUser(
new List<MessageContent>
{
MessageContent.TextContent("What is on the picture in details?"),
MessageContent.ImageBinaryContent(
originalFile,
ImageStatics.ImageFileTypes.Png,
ImageStatics.ImageDetailTypes.High
)
}
),
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
}
);
MessageContent.TextContent("What is on the picture in details?"),
MessageContent.ImageBinaryContent(originalFile, ImageStatics.ImageFileTypes.Png, ImageStatics.ImageDetailTypes.High)
})
},
MaxTokens = 300,
Model = Models.Gpt_4_vision_preview,
N = 1
});

if (completionResult.Successful)
{
Expand All @@ -169,12 +140,10 @@ public static async Task RunSimpleVisionTestUsingBase64EncodedImage(IOpenAIServi
{
if (completionResult.Error == null)
{
throw new Exception("Unknown Error");
throw new("Unknown Error");
}

Console.WriteLine(
$"{completionResult.Error.Code}: {completionResult.Error.Message}"
);
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
}
}
catch (Exception e)
Expand All @@ -183,4 +152,4 @@ public static async Task RunSimpleVisionTestUsingBase64EncodedImage(IOpenAIServi
throw;
}
}
}
}
Loading