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_AssistantsWithVision.cs

71lines · modecode

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