|
1 | | -// using System.Diagnostics.CodeAnalysis; |
2 | | -// |
3 | | -// namespace OpenAI.IntegrationTests.Examples; |
4 | | -// |
5 | | -// public partial class Examples |
6 | | -// { |
7 | | -// [Test] |
8 | | -// [Explicit] |
9 | | -// [Experimental("OPENAI_BETA_001")] |
10 | | -// public async Task AssistantsWithVision() |
11 | | -// { |
12 | | -// using var api = GetAuthenticatedClient(); |
13 | | -// |
14 | | -// ImagesResponse appleImage = await api.Images.CreateImageAsync( |
15 | | -// prompt: "picture of apple", |
16 | | -// responseFormat: CreateImageRequestResponseFormat.B64Json); |
17 | | -// byte[] appleBytes = appleImage.Data[0].Bytes; |
18 | | -// |
19 | | -// FileInfo appleFileInfo = new($"{Guid.NewGuid()}.png"); |
20 | | -// |
21 | | -// await File.WriteAllBytesAsync(appleFileInfo.FullName, appleBytes); |
22 | | -// |
23 | | -// Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.FullName).AbsoluteUri}"); |
24 | | -// |
25 | | -// Console.WriteLine($"Orange image available at:\n{new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png")}"); |
26 | | -// |
27 | | -// OpenAIFile pictureOfAppleFile = await api.Files.CreateFileAsync( |
28 | | -// file: appleBytes, |
29 | | -// filename: appleFileInfo.Name, |
30 | | -// purpose: CreateFileRequestPurpose.Vision); |
31 | | -// |
32 | | -// AssistantObject assistant = await api.Assistants.CreateAssistantAsync( |
33 | | -// model: CreateAssistantRequestModel.Gpt4o, |
34 | | -// instructions: "When asked a question, attempt to answer very concisely. " + |
35 | | -// "Prefer one-sentence answers whenever feasible."); |
36 | | -// |
37 | | -// ThreadObject thread = await api.Assistants.CreateThreadAsync(new CreateThreadRequest |
38 | | -// { |
39 | | -// Messages = [ |
40 | | -// "Hello, assistant! Please compare these two images for me:", |
41 | | -// pictureOfAppleFile, |
42 | | -// new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"), |
43 | | -// ] |
44 | | -// }); |
45 | | -// |
46 | | -// var streamingUpdates = api.Assistants.CreateRunAsStreamAsync( |
47 | | -// threadId: thread.Id, |
48 | | -// assistantId: assistant.Id, |
49 | | -// instructions: "When possible, try to sneak in puns if you're asked to compare things."); |
50 | | -// |
51 | | -// await foreach (AssistantStreamEvent streamingUpdate in streamingUpdates) |
52 | | -// { |
53 | | -// if (streamingUpdate.Error is {} error) |
54 | | -// { |
55 | | -// Console.WriteLine("--- Error ---"); |
56 | | -// Console.WriteLine($"Message: {error.Data.Message}"); |
57 | | -// Console.WriteLine($"Code: {error.Data.Code}"); |
58 | | -// Console.WriteLine($"Type: {error.Data.Type}"); |
59 | | -// } |
60 | | -// if (streamingUpdate.Run is {} run) |
61 | | -// { |
62 | | -// if (run.Value1 is { Event: RunStreamEventVariant1Event.ThreadRunCreated }) |
63 | | -// { |
64 | | -// Console.WriteLine("--- Run created! ---"); |
65 | | -// } |
66 | | -// } |
67 | | -// if (streamingUpdate.Message is {} message) |
68 | | -// { |
69 | | -// if (message.Value3 is |
70 | | -// { |
71 | | -// Event: MessageStreamEventVariant3Event.ThreadMessageDelta |
72 | | -// } delta) |
73 | | -// { |
74 | | -// foreach (var deltaVariation in delta.Data.Delta.Content ?? []) |
75 | | -// { |
76 | | -// if (deltaVariation.Value1 is {} imageFile) |
77 | | -// { |
78 | | -// Console.WriteLine(); |
79 | | -// Console.WriteLine(imageFile.ImageFile?.FileId); |
80 | | -// } |
81 | | -// if (deltaVariation.Value2 is {} text) |
82 | | -// { |
83 | | -// Console.Write(text.Text?.Value); |
84 | | -// } |
85 | | -// if (deltaVariation.Value3 is {} refusal) |
86 | | -// { |
87 | | -// Console.WriteLine(); |
88 | | -// Console.WriteLine(refusal.Refusal); |
89 | | -// } |
90 | | -// if (deltaVariation.Value4 is {} imageUrl) |
91 | | -// { |
92 | | -// Console.WriteLine(); |
93 | | -// Console.WriteLine(imageUrl.ImageUrl?.Url); |
94 | | -// } |
95 | | -// } |
96 | | -// } |
97 | | -// } |
98 | | -// } |
99 | | -// |
100 | | -// _ = await api.Files.DeleteFileAsync(pictureOfAppleFile.Id); |
101 | | -// _ = await api.Assistants.DeleteThreadAsync(thread.Id); |
102 | | -// _ = await api.Assistants.DeleteAssistantAsync(assistant.Id); |
103 | | -// } |
104 | | -// } |
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | + |
| 3 | +namespace OpenAI.IntegrationTests.Examples; |
| 4 | + |
| 5 | +public partial class Examples |
| 6 | +{ |
| 7 | + [Test] |
| 8 | + [Explicit] |
| 9 | + [Experimental("OPENAI_BETA_001")] |
| 10 | + public async Task AssistantsWithVision() |
| 11 | + { |
| 12 | + using var api = GetAuthenticatedClient(); |
| 13 | + |
| 14 | + ImagesResponse appleImage = await api.Images.CreateImageAsync( |
| 15 | + prompt: "picture of apple", |
| 16 | + responseFormat: CreateImageRequestResponseFormat.B64Json); |
| 17 | + byte[] appleBytes = appleImage.Data[0].Bytes; |
| 18 | + |
| 19 | + FileInfo appleFileInfo = new($"{Guid.NewGuid()}.png"); |
| 20 | + |
| 21 | + await File.WriteAllBytesAsync(appleFileInfo.FullName, appleBytes); |
| 22 | + |
| 23 | + Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.FullName).AbsoluteUri}"); |
| 24 | + |
| 25 | + Console.WriteLine($"Orange image available at:\n{new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png")}"); |
| 26 | + |
| 27 | + OpenAIFile pictureOfAppleFile = await api.Files.CreateFileAsync( |
| 28 | + file: appleBytes, |
| 29 | + filename: appleFileInfo.Name, |
| 30 | + purpose: CreateFileRequestPurpose.Vision); |
| 31 | + |
| 32 | + AssistantObject assistant = await api.Assistants.CreateAssistantAsync( |
| 33 | + model: CreateAssistantRequestModel.Gpt4o, |
| 34 | + instructions: "When asked a question, attempt to answer very concisely. " + |
| 35 | + "Prefer one-sentence answers whenever feasible."); |
| 36 | + |
| 37 | + ThreadObject thread = await api.Assistants.CreateThreadAsync(new CreateThreadRequest |
| 38 | + { |
| 39 | + Messages = [ |
| 40 | + "Hello, assistant! Please compare these two images for me:", |
| 41 | + pictureOfAppleFile, |
| 42 | + new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"), |
| 43 | + ] |
| 44 | + }); |
| 45 | + |
| 46 | + var streamingUpdates = api.Assistants.CreateRunAsStreamAsync( |
| 47 | + threadId: thread.Id, |
| 48 | + assistantId: assistant.Id, |
| 49 | + instructions: "When possible, try to sneak in puns if you're asked to compare things."); |
| 50 | + |
| 51 | + await foreach (AssistantStreamEvent streamingUpdate in streamingUpdates) |
| 52 | + { |
| 53 | + if (streamingUpdate.Error is {} error) |
| 54 | + { |
| 55 | + Console.WriteLine("--- Error ---"); |
| 56 | + Console.WriteLine($"Message: {error.Data.Message}"); |
| 57 | + Console.WriteLine($"Code: {error.Data.Code}"); |
| 58 | + Console.WriteLine($"Type: {error.Data.Type}"); |
| 59 | + } |
| 60 | + if (streamingUpdate.ThreadRunCreated is not null) |
| 61 | + { |
| 62 | + Console.WriteLine("--- Run created! ---"); |
| 63 | + } |
| 64 | + if (streamingUpdate.ThreadMessageDelta is {} delta) |
| 65 | + { |
| 66 | + foreach (var deltaVariation in delta.Data.Delta.Content ?? []) |
| 67 | + { |
| 68 | + if (deltaVariation.ImageFile is {} imageFile) |
| 69 | + { |
| 70 | + Console.WriteLine(); |
| 71 | + Console.WriteLine(imageFile.ImageFile?.FileId); |
| 72 | + } |
| 73 | + if (deltaVariation.Text is {} text) |
| 74 | + { |
| 75 | + Console.Write(text.Text?.Value); |
| 76 | + } |
| 77 | + if (deltaVariation.Refusal is {} refusal) |
| 78 | + { |
| 79 | + Console.WriteLine(); |
| 80 | + Console.WriteLine(refusal.Refusal); |
| 81 | + } |
| 82 | + if (deltaVariation.ImageUrl is {} imageUrl) |
| 83 | + { |
| 84 | + Console.WriteLine(); |
| 85 | + Console.WriteLine(imageUrl.ImageUrl?.Url); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + _ = await api.Files.DeleteFileAsync(pictureOfAppleFile.Id); |
| 92 | + _ = await api.Assistants.DeleteThreadAsync(thread.Id); |
| 93 | + _ = await api.Assistants.DeleteAssistantAsync(assistant.Id); |
| 94 | + } |
| 95 | +} |
0 commit comments