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/Chat/Example05_ChatWithVisionAsync.cs

32lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using System;
4using System.Collections.Generic;
5using System.IO;
6using System.Threading.Tasks;
7
8namespace OpenAI.Examples;
9
10public partial class ChatExamples
11{
12 [Test]
13 public async Task Example05_ChatWithVisionAsync()
14 {
15 ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
16
17 string imageFilePath = Path.Combine("Assets", "images_dog_and_cat.png");
18 using Stream imageStream = File.OpenRead(imageFilePath);
19 BinaryData imageBytes = BinaryData.FromStream(imageStream);
20
21 List<ChatMessage> messages = [
22 new UserChatMessage(
23 ChatMessageContentPart.CreateTextMessageContentPart("Please describe the following image."),
24 ChatMessageContentPart.CreateImageMessageContentPart(imageBytes, "image/png"))
25 ];
26
27 ChatCompletion chatCompletion = await client.CompleteChatAsync(messages);
28
29 Console.WriteLine($"[ASSISTANT]:");
30 Console.WriteLine($"{chatCompletion.Content[0].Text}");
31 }
32}