openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.10

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Assistants/Example05_AssistantsWithVisionAsync.cs

72lines · modecode

1using NUnit.Framework;
2using OpenAI.Assistants;
3using OpenAI.Files;
4using System;
5using System.ClientModel;
6using System.Threading.Tasks;
7
8namespace OpenAI.Examples;
9
10public partial class AssistantExamples
11{
12 [Test]
13 public async Task Example05_AssistantsWithVisionAsync()
14 {
15 // Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning.
16#pragma warning disable OPENAI001
17 OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
18 FileClient fileClient = openAIClient.GetFileClient();
19 AssistantClient assistantClient = openAIClient.GetAssistantClient();
20
21 OpenAIFileInfo pictureOfAppleFile = await fileClient.UploadFileAsync(
22 "picture-of-apple.jpg",
23 FileUploadPurpose.Vision);
24 Uri linkToPictureOfOrange = new("https://platform.openai.com/fictitious-files/picture-of-orange.png");
25
26 Assistant assistant = await assistantClient.CreateAssistantAsync(
27 "gpt-4o",
28 new AssistantCreationOptions()
29 {
30 Instructions = "When asked a question, attempt to answer very concisely. "
31 + "Prefer one-sentence answers whenever feasible."
32 });
33
34 AssistantThread thread = await assistantClient.CreateThreadAsync(new ThreadCreationOptions()
35 {
36 InitialMessages =
37 {
38 new ThreadInitializationMessage(
39 MessageRole.User,
40 [
41 "Hello, assistant! Please compare these two images for me:",
42 MessageContent.FromImageFileId(pictureOfAppleFile.Id),
43 MessageContent.FromImageUrl(linkToPictureOfOrange),
44 ]),
45 }
46 });
47
48 AsyncCollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreamingAsync(
49 thread,
50 assistant,
51 new RunCreationOptions()
52 {
53 AdditionalInstructions = "When possible, try to sneak in puns if you're asked to compare things.",
54 });
55
56 await foreach (StreamingUpdate streamingUpdate in streamingUpdates)
57 {
58 if (streamingUpdate.UpdateKind == StreamingUpdateReason.RunCreated)
59 {
60 Console.WriteLine($"--- Run started! ---");
61 }
62 if (streamingUpdate is MessageContentUpdate contentUpdate)
63 {
64 Console.Write(contentUpdate.Text);
65 }
66 }
67
68 _ = await fileClient.DeleteFileAsync(pictureOfAppleFile.Id);
69 _ = await assistantClient.DeleteThreadAsync(thread);
70 _ = await assistantClient.DeleteAssistantAsync(assistant);
71 }
72}
73