openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Assistants/Example05_AssistantsWithVisionAsync.cs

71lines · 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 [
40 "Hello, assistant! Please compare these two images for me:",
41 MessageContent.FromImageFileId(pictureOfAppleFile.Id),
42 MessageContent.FromImageUrl(linkToPictureOfOrange),
43 ]),
44 }
45 });
46
47 AsyncResultCollection<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreamingAsync(
48 thread,
49 assistant,
50 new RunCreationOptions()
51 {
52 AdditionalInstructions = "When possible, try to sneak in puns if you're asked to compare things.",
53 });
54
55 await foreach (StreamingUpdate streamingUpdate in streamingUpdates)
56 {
57 if (streamingUpdate.UpdateKind == StreamingUpdateReason.RunCreated)
58 {
59 Console.WriteLine($"--- Run started! ---");
60 }
61 if (streamingUpdate is MessageContentUpdate contentUpdate)
62 {
63 Console.Write(contentUpdate.Text);
64 }
65 }
66
67 _ = await fileClient.DeleteFileAsync(pictureOfAppleFile);
68 _ = await assistantClient.DeleteThreadAsync(thread);
69 _ = await assistantClient.DeleteAssistantAsync(assistant);
70 }
71}
72