From d6587b406e28567721d593d5fcf6f1a809fa2293 Mon Sep 17 00:00:00 2001 From: tghamm Date: Tue, 1 Apr 2025 17:36:21 -0400 Subject: [PATCH] Fix for SK, Version Bump --- .../Anthropic.SDK.Tests.csproj | 3 + .../SemanticKernelInitializationTests.cs | 56 +++++++++++++++++++ .../SummarizeDocuments/skprompt.txt | 9 +++ Anthropic.SDK/Anthropic.SDK.csproj | 8 +-- Anthropic.SDK/Messaging/ChatClientHelper.cs | 11 ++++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 Anthropic.SDK.Tests/SummarizePlugin/SummarizeDocuments/skprompt.txt diff --git a/Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj b/Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj index c3323b8..f7f8622 100644 --- a/Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj +++ b/Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj @@ -27,6 +27,9 @@ PreserveNewest + + PreserveNewest + diff --git a/Anthropic.SDK.Tests/SemanticKernelInitializationTests.cs b/Anthropic.SDK.Tests/SemanticKernelInitializationTests.cs index 148cf06..d9e9ad7 100644 --- a/Anthropic.SDK.Tests/SemanticKernelInitializationTests.cs +++ b/Anthropic.SDK.Tests/SemanticKernelInitializationTests.cs @@ -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; @@ -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 @@ -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( + "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 diff --git a/Anthropic.SDK.Tests/SummarizePlugin/SummarizeDocuments/skprompt.txt b/Anthropic.SDK.Tests/SummarizePlugin/SummarizeDocuments/skprompt.txt new file mode 100644 index 0000000..3c11826 --- /dev/null +++ b/Anthropic.SDK.Tests/SummarizePlugin/SummarizeDocuments/skprompt.txt @@ -0,0 +1,9 @@ + + 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. + + + {{$fileName}} + {{$fileDataUri}} + \ No newline at end of file diff --git a/Anthropic.SDK/Anthropic.SDK.csproj b/Anthropic.SDK/Anthropic.SDK.csproj index 97245c6..3413852 100644 --- a/Anthropic.SDK/Anthropic.SDK.csproj +++ b/Anthropic.SDK/Anthropic.SDK.csproj @@ -14,12 +14,12 @@ Claude, AI, ML, API, Anthropic Claude API - Vertex AI Support + Fix for bug in Semantic Kernel integration Anthropic.SDK - 5.1.0 - 5.1.0.0 - 5.1.0.0 + 5.1.1 + 5.1.1.0 + 5.1.1.0 True README.md icon.png diff --git a/Anthropic.SDK/Messaging/ChatClientHelper.cs b/Anthropic.SDK/Messaging/ChatClientHelper.cs index ad29628..71551ce 100644 --- a/Anthropic.SDK/Messaging/ChatClientHelper.cs +++ b/Anthropic.SDK/Messaging/ChatClientHelper.cs @@ -134,6 +134,17 @@ public static MessageParameters CreateMessageParameters(IEnumerable }); 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() {