Skip to content

Commit

Permalink
Add ImageContent integration test (#5586)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 30, 2024
1 parent bf0e0a4 commit e18a055
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
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

0 comments on commit e18a055

Please sign in to comment.