openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Assistants/Example05_AssistantsWithVision.cs

74lines · modecode

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