openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/Chat/ChatWithVision.cs

44lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using OpenAI.Tests.Utility;
4using System;
5using System.ClientModel;
6using System.Collections.Generic;
7using System.IO;
8using System.Threading.Tasks;
9using static OpenAI.Tests.TestHelpers;
10
11namespace OpenAI.Tests.Chat;
12
13[TestFixture(true)]
14[TestFixture(false)]
15public partial class ChatWithVision : SyncAsyncTestBase
16{
17 public ChatWithVision(bool isAsync)
18 : base(isAsync)
19 {
20 }
21
22 [Test]
23 public async Task DescribeAnImage()
24 {
25 string mediaType = "image/png";
26 string filePath = Path.Combine("Assets", "images_dog_and_cat.png");
27 using Stream stream = File.OpenRead(filePath);
28 BinaryData imageData = BinaryData.FromStream(stream);
29
30 ChatClient client = GetTestClient<ChatClient>(TestScenario.VisionChat);
31 IEnumerable<ChatMessage> messages = [
32 new UserChatMessage(
33 ChatMessageContentPart.CreateTextMessageContentPart("Describe this image for me."),
34 ChatMessageContentPart.CreateImageMessageContentPart(imageData, mediaType)),
35 ];
36 ChatCompletionOptions options = new() { MaxTokens = 2048 };
37
38 ClientResult<ChatCompletion> result = IsAsync
39 ? await client.CompleteChatAsync(messages, options)
40 : client.CompleteChat(messages, options);
41 Console.WriteLine(result.Value.Content[0].Text);
42 Assert.That(result.Value.Content[0].Text.ToLowerInvariant(), Contains.Substring("dog"));
43 }
44}
45