openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jsquire-patch-1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/CombinationExamples.cs

150lines · modecode

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