Skip to content
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
3 changes: 3 additions & 0 deletions Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<Content Include="appsettings.json.template">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="SummarizePlugin\SummarizeDocuments\skprompt.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
56 changes: 56 additions & 0 deletions Anthropic.SDK.Tests/SemanticKernelInitializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Anthropic.SDK.Constants;
Expand All @@ -9,6 +10,7 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using static Google.Rpc.Context.AttributeContext.Types;
#pragma warning disable SKEXP0001

namespace Anthropic.SDK.Tests
Expand Down Expand Up @@ -53,7 +55,61 @@ public async Task TestSKInit()
Assert.IsTrue(result.Content.Contains("72"));
}

[TestMethod]
public async Task TestSKPDF()
{
string resourceName = "Anthropic.SDK.Tests.Claude3ModelCard.pdf";

Assembly assembly = Assembly.GetExecutingAssembly();

await using Stream stream = assembly.GetManifestResourceStream(resourceName);
//read stream into byte array
using var ms = new MemoryStream();
await stream.CopyToAsync(ms);
byte[] pdfBytes = ms.ToArray();
string base64String = Convert.ToBase64String(pdfBytes);

var file = new File()
{
Name = "Claude3ModelCard.pdf",
DataUri = "data:application/pdf;base64," + base64String
};

AnthropicClient client = new AnthropicClient();
IChatCompletionService skChatService = new ChatClientBuilder(client.Messages)
.ConfigureOptions(opt =>
{
opt.ModelId = AnthropicModels.Claude37Sonnet;
opt.MaxOutputTokens = 1024;
})
.UseFunctionInvocation()
.Build()
.AsChatCompletionService();

IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddKeyedSingleton("test", skChatService);

Kernel kernel = kernelBuilder.Build();

// Add plugins from the `SkPlugins` folder
string pluginDirectoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SummarizePlugin");
kernel.ImportPluginFromPromptDirectory(pluginDirectoryPath);

string? filesSummary = await kernel.InvokeAsync<string>(
"SummarizePlugin",
"SummarizeDocuments",
new() { { "fileName", file.Name }, { "fileDataUri", file.DataUri } }
);

}


}

public class File
{
public string Name { get; set; }
public string DataUri { get; set; }
}

public class SkPlugins
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<message role="system">
You are an AI assistant designed to help document information extraction.
Describe in detail what is in the documents provided to you.
If you do not see any documents, then state that you do not see any documents.
</message>
<message role="user">
<text>{{$fileName}}</text>
<image>{{$fileDataUri}}</image>
</message>
8 changes: 4 additions & 4 deletions Anthropic.SDK/Anthropic.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<PackageTags>Claude, AI, ML, API, Anthropic</PackageTags>
<Title>Claude API</Title>
<PackageReleaseNotes>
Vertex AI Support
Fix for bug in Semantic Kernel integration
</PackageReleaseNotes>
<PackageId>Anthropic.SDK</PackageId>
<Version>5.1.0</Version>
<AssemblyVersion>5.1.0.0</AssemblyVersion>
<FileVersion>5.1.0.0</FileVersion>
<Version>5.1.1</Version>
<AssemblyVersion>5.1.1.0</AssemblyVersion>
<FileVersion>5.1.1.0</FileVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
Expand Down
11 changes: 11 additions & 0 deletions Anthropic.SDK/Messaging/ChatClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ public static MessageParameters CreateMessageParameters(IEnumerable<ChatMessage>
});
break;

case Microsoft.Extensions.AI.DataContent documentContent when documentContent.HasTopLevelMediaType("application"):
m.Content.Add(new DocumentContent()
{
Source = new()
{
Data = Convert.ToBase64String(documentContent.Data.ToArray()),
MediaType = documentContent.MediaType,
}
});
break;

case Microsoft.Extensions.AI.FunctionCallContent fcc:
m.Content.Add(new ToolUseContent()
{
Expand Down
Loading