From 8292fca8a77b13ad20c2b40ce0eb0c8bcc1b2488 Mon Sep 17 00:00:00 2001 From: HavenDV Date: Sun, 18 Aug 2024 18:49:00 +0400 Subject: [PATCH] example: Added CuriousCreatureCreator. --- docs/samples/CuriousCreatureCreator.md | 64 ++++++++++++++++ .../CombinationExamples.AlpacaArtAssessor.cs | 2 +- ...binationExamples.CuriousCreatureCreator.cs | 74 +++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 docs/samples/CuriousCreatureCreator.md create mode 100644 src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.CuriousCreatureCreator.cs diff --git a/docs/samples/CuriousCreatureCreator.md b/docs/samples/CuriousCreatureCreator.md new file mode 100644 index 0000000..c90422e --- /dev/null +++ b/docs/samples/CuriousCreatureCreator.md @@ -0,0 +1,64 @@ +```csharp +using var api = new OpenAiApi(apiKey: + Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? + throw new AssertInconclusiveException("OPENAI_API_KEY environment variable is not found.")); + +// First, we'll use gpt-4o to have a creative helper imagine a twist on a household pet +CreateChatCompletionResponse creativeWriterResult = await api.Chat.CreateChatCompletionAsync( + model: CreateChatCompletionRequestModel.Gpt4o, + messages: + [ + "You're a creative helper that specializes in brainstorming designs for concepts that fuse ordinary, mundane items with a fantastical touch. In particular, you can provide good one-paragraph descriptions of concept images.".AsSystemMessage(), + "Imagine a household pet. Now add in a subtle touch of magic or 'different'. What do you imagine? Provide a one-paragraph description of a picture of this new creature, focusing on the details of the imagery such that it'd be suitable for creating a picture.", + ], + maxTokens: 2048); +string description = creativeWriterResult.Choices[0].Message.Content ?? string.Empty; +Console.WriteLine($"Creative helper's creature description:\n{description}"); + +// Asynchronously, in parallel to the next steps, we'll get the creative description in the voice of Onyx +byte[] imageDescriptionAudio = await api.Audio.CreateSpeechAsync( + model: CreateSpeechRequestModel.Tts1Hd, + input: description, + voice: CreateSpeechRequestVoice.Onyx, + speed: 1.1f, + responseFormat: CreateSpeechRequestResponseFormat.Opus); +FileInfo audioFileInfo = new FileInfo($"{creativeWriterResult.Id}-description.opus"); + +await File.WriteAllBytesAsync(audioFileInfo.FullName, imageDescriptionAudio); + +Console.WriteLine($"Spoken description available at:\n{new Uri(audioFileInfo.FullName).AbsoluteUri}"); + +// Meanwhile, we'll use dall-e-3 to generate a rendition of our LLM artist's vision +ImagesResponse imageGenerationResult = await api.Images.CreateImageAsync( + prompt: description, + model: CreateImageRequestModel.DallE3, + size: CreateImageRequestSize.x1792x1024, + quality: CreateImageRequestQuality.Hd); +Uri imageLocation = new Uri(imageGenerationResult.Data[0].Url ?? string.Empty); +Console.WriteLine($"Creature image available at:\n{imageLocation.AbsoluteUri}"); + +// Now, we'll use gpt vision to get a hopelessly taken assessment from a usually exigent art connoisseur +CreateChatCompletionResponse criticalAppraisalResult = await api.Chat.CreateChatCompletionAsync( + messages: [ + "Assume the role of an art critic. Although usually cranky and occasionally even referred to as a 'curmudgeon', you're somehow entirely smitten with the subject presented to you and, despite your best efforts, can't help but lavish praise when you're asked to appraise a provided image.".AsSystemMessage(), + "Evaluate this image for me. What is it, and what do you think of it?", + imageLocation, + ], + model: ChatClient.LatestSmartModel, + maxTokens: 2048); +string appraisal = criticalAppraisalResult.Choices[0].Message.Content ?? string.Empty; +Console.WriteLine($"Critic's appraisal:\n{appraisal}"); + +// Finally, we'll get that art expert's laudations in the voice of Fable +byte[] appraisalAudioResult = await api.Audio.CreateSpeechAsync( + input: appraisal, + model: CreateSpeechRequestModel.Tts1Hd, + voice: CreateSpeechRequestVoice.Fable, + speed: 0.9f, + responseFormat: CreateSpeechRequestResponseFormat.Opus); +FileInfo criticAudioFileInfo = new($"{criticalAppraisalResult.Id}-appraisal.opus"); + +await File.WriteAllBytesAsync(criticAudioFileInfo.FullName, appraisalAudioResult); + +Console.WriteLine($"Critical appraisal available at:\n{new Uri(criticAudioFileInfo.FullName).AbsoluteUri}"); +``` \ No newline at end of file diff --git a/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.AlpacaArtAssessor.cs b/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.AlpacaArtAssessor.cs index 9f55960..4df8a88 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.AlpacaArtAssessor.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.AlpacaArtAssessor.cs @@ -2,7 +2,7 @@ namespace OpenAI.Examples.Miscellaneous; -public class CombinationExamples +public partial class CombinationExamples { [Test] [Explicit] diff --git a/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.CuriousCreatureCreator.cs b/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.CuriousCreatureCreator.cs new file mode 100644 index 0000000..9c16457 --- /dev/null +++ b/src/tests/OpenAI.IntegrationTests/Examples/CombinationExamples.CuriousCreatureCreator.cs @@ -0,0 +1,74 @@ +using NUnit.Framework; + +namespace OpenAI.Examples.Miscellaneous; + +public partial class CombinationExamples +{ + [Test] + [Explicit] + public async Task CuriousCreatureCreator() + { + using var api = new OpenAiApi(apiKey: + Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? + throw new AssertInconclusiveException("OPENAI_API_KEY environment variable is not found.")); + + // First, we'll use gpt-4o to have a creative helper imagine a twist on a household pet + CreateChatCompletionResponse creativeWriterResult = await api.Chat.CreateChatCompletionAsync( + model: CreateChatCompletionRequestModel.Gpt4o, + messages: + [ + "You're a creative helper that specializes in brainstorming designs for concepts that fuse ordinary, mundane items with a fantastical touch. In particular, you can provide good one-paragraph descriptions of concept images.".AsSystemMessage(), + "Imagine a household pet. Now add in a subtle touch of magic or 'different'. What do you imagine? Provide a one-paragraph description of a picture of this new creature, focusing on the details of the imagery such that it'd be suitable for creating a picture.", + ], + maxTokens: 2048); + string description = creativeWriterResult.Choices[0].Message.Content ?? string.Empty; + Console.WriteLine($"Creative helper's creature description:\n{description}"); + + // Asynchronously, in parallel to the next steps, we'll get the creative description in the voice of Onyx + byte[] imageDescriptionAudio = await api.Audio.CreateSpeechAsync( + model: CreateSpeechRequestModel.Tts1Hd, + input: description, + voice: CreateSpeechRequestVoice.Onyx, + speed: 1.1f, + responseFormat: CreateSpeechRequestResponseFormat.Opus); + FileInfo audioFileInfo = new FileInfo($"{creativeWriterResult.Id}-description.opus"); + + await File.WriteAllBytesAsync(audioFileInfo.FullName, imageDescriptionAudio); + + Console.WriteLine($"Spoken description available at:\n{new Uri(audioFileInfo.FullName).AbsoluteUri}"); + + // Meanwhile, we'll use dall-e-3 to generate a rendition of our LLM artist's vision + ImagesResponse imageGenerationResult = await api.Images.CreateImageAsync( + prompt: description, + model: CreateImageRequestModel.DallE3, + size: CreateImageRequestSize.x1792x1024, + quality: CreateImageRequestQuality.Hd); + Uri imageLocation = new Uri(imageGenerationResult.Data[0].Url ?? string.Empty); + Console.WriteLine($"Creature image available at:\n{imageLocation.AbsoluteUri}"); + + // Now, we'll use gpt vision to get a hopelessly taken assessment from a usually exigent art connoisseur + CreateChatCompletionResponse criticalAppraisalResult = await api.Chat.CreateChatCompletionAsync( + messages: [ + "Assume the role of an art critic. Although usually cranky and occasionally even referred to as a 'curmudgeon', you're somehow entirely smitten with the subject presented to you and, despite your best efforts, can't help but lavish praise when you're asked to appraise a provided image.".AsSystemMessage(), + "Evaluate this image for me. What is it, and what do you think of it?", + imageLocation, + ], + model: ChatClient.LatestSmartModel, + maxTokens: 2048); + string appraisal = criticalAppraisalResult.Choices[0].Message.Content ?? string.Empty; + Console.WriteLine($"Critic's appraisal:\n{appraisal}"); + + // Finally, we'll get that art expert's laudations in the voice of Fable + byte[] appraisalAudioResult = await api.Audio.CreateSpeechAsync( + input: appraisal, + model: CreateSpeechRequestModel.Tts1Hd, + voice: CreateSpeechRequestVoice.Fable, + speed: 0.9f, + responseFormat: CreateSpeechRequestResponseFormat.Opus); + FileInfo criticAudioFileInfo = new($"{criticalAppraisalResult.Id}-appraisal.opus"); + + await File.WriteAllBytesAsync(criticAudioFileInfo.FullName, appraisalAudioResult); + + Console.WriteLine($"Critical appraisal available at:\n{new Uri(criticAudioFileInfo.FullName).AbsoluteUri}"); + } +}