Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ImageContent integration test #5586

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -132,6 +133,27 @@ public virtual async Task CompleteStreamingAsync_UsageDataAvailable()
Assert.Equal(usage.Details.InputTokenCount + usage.Details.OutputTokenCount, usage.Details.TotalTokenCount);
}

protected virtual string? GetModel_MultiModal_DescribeImage() => null;

[ConditionalFact]
public virtual async Task MultiModal_DescribeImage()
{
SkipIfNotEnabled();

var response = await _chatClient.CompleteAsync(
[
new(ChatRole.User,
[
new TextContent("What does this logo say?"),
new ImageContent(GetImageDataUri()),
])
],
new() { ModelId = GetModel_MultiModal_DescribeImage() });

Assert.Single(response.Choices);
Assert.True(response.Message.Text?.IndexOf("net", StringComparison.OrdinalIgnoreCase) >= 0, response.Message.Text);
}

[ConditionalFact]
public virtual async Task FunctionInvocation_AutomaticallyInvokeFunction_Parameterless()
{
Expand Down Expand Up @@ -714,6 +736,15 @@ private enum JobType
Unknown,
}

private static Uri GetImageDataUri()
{
using Stream? s = typeof(ChatClientIntegrationTests).Assembly.GetManifestResourceStream("Microsoft.Extensions.AI.dotnet.png");
Assert.NotNull(s);
MemoryStream ms = new();
s.CopyTo(ms);
return new Uri($"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}");
}

[MemberNotNull(nameof(_chatClient))]
protected void SkipIfNotEnabled()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<InjectSharedThrow>true</InjectSharedThrow>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="dotnet.png" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Microsoft.Extensions.AI.Abstractions.Tests\CapturingLogger.cs" />
<Compile Include="..\Microsoft.Extensions.AI.Abstractions.Tests\TestChatClient.cs" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public override Task FunctionInvocation_RequireAny() =>
public override Task FunctionInvocation_RequireSpecific() =>
throw new SkipTestException("Ollama does not currently support requiring function invocation.");

protected override string? GetModel_MultiModal_DescribeImage() => "llava";

[ConditionalFact]
public async Task PromptBasedFunctionCalling_NoArgs()
{
Expand Down
Loading