- Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathCombinationExamples.cs
150 lines (140 loc) · 7.82 KB
/
CombinationExamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
usingNUnit.Framework;
usingOpenAI.Audio;
usingOpenAI.Chat;
usingOpenAI.Images;
usingSystem;
usingSystem.ClientModel;
usingSystem.IO;
usingSystem.Threading.Tasks;
namespaceOpenAI.Examples.Miscellaneous;
publicpartialclassCombinationExamples
{
[Test]
publicvoidAlpacaArtAssessor()
{
// First, we create an image using dall-e-3:
ImageClientimageClient=new("dall-e-3",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ClientResult<GeneratedImage>imageResult=imageClient.GenerateImage(
"a majestic alpaca on a mountain ridge, backed by an expansive blue sky accented with sparse clouds",
new()
{
Style=GeneratedImageStyle.Vivid,
Quality=GeneratedImageQuality.High,
Size=GeneratedImageSize.W1792xH1024,
});
GeneratedImageimageGeneration=imageResult.Value;
Console.WriteLine($"Majestic alpaca available at:\n{imageGeneration.ImageUri.AbsoluteUri}");
// Now, we'll ask a cranky art critic to evaluate the image using gpt-4-vision-preview:
ChatClientchatClient=new("gpt-4o-mini",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ChatCompletionchatCompletion=chatClient.CompleteChat(
[
newSystemChatMessage("Assume the role of a cranky art critic. When asked to describe or "
+"evaluate imagery, focus on criticizing elements of subject, composition, and other details."),
newUserChatMessage(
ChatMessageContentPart.CreateTextPart("describe the following image in a few sentences"),
ChatMessageContentPart.CreateImagePart(imageGeneration.ImageUri)),
],
newChatCompletionOptions()
{
MaxOutputTokenCount=2048,
}
);
stringchatResponseText=chatCompletion.Content[0].Text;
Console.WriteLine($"Art critique of majestic alpaca:\n{chatResponseText}");
// Finally, we'll get some text-to-speech for that critical evaluation using tts-1-hd:
AudioClientaudioClient=new("tts-1-hd",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ClientResult<BinaryData>ttsResult=audioClient.GenerateSpeech(
text:chatResponseText,
GeneratedSpeechVoice.Fable,
newSpeechGenerationOptions()
{
SpeedRatio=0.9f,
ResponseFormat=GeneratedSpeechFormat.Opus,
});
FileInfottsFileInfo=new($"{chatCompletion.Id}.opus");
using(FileStreamttsFileStream=ttsFileInfo.Create())
using(BinaryWriterttsFileWriter=new(ttsFileStream))
{
ttsFileWriter.Write(ttsResult.Value);
}
Console.WriteLine($"Alpaca evaluation audio available at:\n{newUri(ttsFileInfo.FullName).AbsoluteUri}");
}
[Test]
publicasyncTaskCuriousCreatureCreator()
{
// First, we'll use gpt-4 to have a creative helper imagine a twist on a household pet
ChatClientcreativeWriterClient=new("gpt-4o-mini",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ClientResult<ChatCompletion>creativeWriterResult=creativeWriterClient.CompleteChat(
[
newSystemChatMessage("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."),
newUserChatMessage("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."),
],
newChatCompletionOptions()
{
MaxOutputTokenCount=2048,
});
stringdescription=creativeWriterResult.Value.Content[0].Text;
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
AudioClientttsClient=new("tts-1-hd",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
Task<ClientResult<BinaryData>>imageDescriptionAudioTask=ttsClient.GenerateSpeechAsync(
description,
GeneratedSpeechVoice.Onyx,
newSpeechGenerationOptions()
{
SpeedRatio=1.1f,
ResponseFormat=GeneratedSpeechFormat.Opus,
});
_=Task.Run(async()=>
{
ClientResult<BinaryData>audioResult=awaitimageDescriptionAudioTask;
FileInfoaudioFileInfo=newFileInfo($"{creativeWriterResult.Value.Id}-description.opus");
usingFileStreamfileStream=audioFileInfo.Create();
usingBinaryWriterfileWriter=new(fileStream);
fileWriter.Write(audioResult.Value);
Console.WriteLine($"Spoken description available at:\n{newUri(audioFileInfo.FullName).AbsoluteUri}");
});
// Meanwhile, we'll use dall-e-3 to generate a rendition of our LLM artist's vision
ImageClientimageGenerationClient=new("dall-e-3",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ClientResult<GeneratedImage>imageGenerationResult=awaitimageGenerationClient.GenerateImageAsync(
description,
newImageGenerationOptions()
{
Size=GeneratedImageSize.W1792xH1024,
Quality=GeneratedImageQuality.High,
});
UriimageLocation=imageGenerationResult.Value.ImageUri;
Console.WriteLine($"Creature image available at:\n{imageLocation.AbsoluteUri}");
// Now, we'll use gpt-4-vision-preview to get a hopelessly taken assessment from a usually exigent art connoisseur
ChatClientimageCriticClient=new("gpt-4o-mini",Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
ClientResult<ChatCompletion>criticalAppraisalResult=awaitimageCriticClient.CompleteChatAsync(
[
newSystemChatMessage("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."),
newUserChatMessage(
ChatMessageContentPart.CreateTextPart("Evaluate this image for me. What is it, and what do you think of it?"),
ChatMessageContentPart.CreateImagePart(imageLocation)),
],
newChatCompletionOptions()
{
MaxOutputTokenCount=2048,
});
stringappraisal=criticalAppraisalResult.Value.Content[0].Text;
Console.WriteLine($"Critic's appraisal:\n{appraisal}");
// Finally, we'll get that art expert's laudations in the voice of Fable
ClientResult<BinaryData>appraisalAudioResult=awaitttsClient.GenerateSpeechAsync(
appraisal,
GeneratedSpeechVoice.Fable,
newSpeechGenerationOptions()
{
SpeedRatio=0.9f,
ResponseFormat=GeneratedSpeechFormat.Opus,
});
FileInfocriticAudioFileInfo=new($"{criticalAppraisalResult.Value.Id}-appraisal.opus");
using(FileStreamcriticStream=criticAudioFileInfo.Create())
using(BinaryWritercriticFileWriter=new(criticStream))
{
criticFileWriter.Write(appraisalAudioResult.Value);
}
Console.WriteLine($"Critical appraisal available at:\n{newUri(criticAudioFileInfo.FullName).AbsoluteUri}");
}
}