openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Assistants/Example05_AssistantsWithVision.cs

78lines · modecode

1using NUnit.Framework;
2using OpenAI.Assistants;
3using OpenAI.Files;
4using System;
5using System.ClientModel;
6using System.IO;
7
8namespace OpenAI.Examples;
9
10// This example uses experimental APIs which are subject to change. To use experimental APIs,
11// please acknowledge their experimental status by suppressing the corresponding warning.
12#pragma warning disable OPENAI001
13
14public partial class AssistantExamples
15{
16 [Test]
17 public void Example05_AssistantsWithVision()
18 {
19 OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
20 OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient();
21 AssistantClient assistantClient = openAIClient.GetAssistantClient();
22
23 OpenAIFile pictureOfAppleFile = fileClient.UploadFile(
24 Path.Combine("Assets", "images_apple.png"),
25 FileUploadPurpose.Vision);
26
27 Uri linkToPictureOfOrange = new("https://raw.githubusercontent.com/openai/openai-dotnet/refs/heads/main/examples/Assets/images_orange.png");
28
29 Assistant assistant = assistantClient.CreateAssistant(
30 "gpt-4o",
31 new AssistantCreationOptions()
32 {
33 Instructions = "When asked a question, attempt to answer very concisely. "
34 + "Prefer one-sentence answers whenever feasible."
35 });
36
37 AssistantThread thread = assistantClient.CreateThread(new ThreadCreationOptions()
38 {
39 InitialMessages =
40 {
41 new ThreadInitializationMessage(
42 MessageRole.User,
43 [
44 "Hello, assistant! Please compare these two images for me:",
45 MessageContent.FromImageFileId(pictureOfAppleFile.Id),
46 MessageContent.FromImageUri(linkToPictureOfOrange),
47 ]),
48 }
49 });
50
51 CollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
52 thread.Id,
53 assistant.Id,
54 new RunCreationOptions()
55 {
56 AdditionalInstructions = "When possible, try to sneak in puns if you're asked to compare things.",
57 });
58
59 foreach (StreamingUpdate streamingUpdate in streamingUpdates)
60 {
61 if (streamingUpdate.UpdateKind == StreamingUpdateReason.RunCreated)
62 {
63 Console.WriteLine($"--- Run started! ---");
64 }
65 if (streamingUpdate is MessageContentUpdate contentUpdate)
66 {
67 Console.Write(contentUpdate.Text);
68 }
69 }
70
71 // Delete temporary resources, if desired
72 _ = fileClient.DeleteFile(pictureOfAppleFile.Id);
73 _ = assistantClient.DeleteThread(thread.Id);
74 _ = assistantClient.DeleteAssistant(assistant.Id);
75 }
76}
77
78#pragma warning restore OPENAI001