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

72lines · 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 MessageRole.User,
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 CollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
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 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 // Delete temporary resources, if desired
68 _ = fileClient.DeleteFile(pictureOfAppleFile.Id);
69 _ = assistantClient.DeleteThread(thread);
70 _ = assistantClient.DeleteAssistant(assistant);
71 }
72}