openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1011

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 [
23 new UserChatMessage(
24 ChatMessageContentPart.CreateTextPart("Please describe the following image."),
25 ChatMessageContentPart.CreateImagePart(imageBytes, "image/png")),
26 ];
27
28 ChatCompletion completion = await client.CompleteChatAsync(messages);
29
30 Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");
31 }
32}