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
8 changes: 4 additions & 4 deletions Anthropic.SDK.Tests/Anthropic.SDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

<ItemGroup>
<PackageReference Include="Google.Cloud.AIPlatform.V1" Version="3.65.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="11.0.0-preview.1.26104.118" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.3.0" />
<PackageReference Include="ModelContextProtocol" Version="0.8.0-preview.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="11.0.0-preview.1.26104.118" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="11.0.0-preview.1.26104.118" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="11.0.0-preview.1.26104.118" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.71.0" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.71.0" />
Expand Down
38 changes: 38 additions & 0 deletions Anthropic.SDK.Tests/CostTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Anthropic.SDK.Constants;
using Anthropic.SDK.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anthropic.SDK.Extensions;

namespace Anthropic.SDK.Tests
{
[TestClass]
public class CostTest
{

[TestMethod]
public async Task TestCostEstimation()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Messages = new List<Message> { new Message(RoleType.User, "Hello!") },
MaxTokens = 1024,
Model = AnthropicModels.Claude46Sonnet,
};
var response = await client.Messages.GetClaudeMessageAsync(parameters);

// Get total estimated cost
var cost = response.CalculateCost();
Console.WriteLine($"Total cost: ${cost.TotalCostUsd:F6}");
Console.WriteLine($" Input tokens: ${cost.InputTokenCost:F6}");
Console.WriteLine($" Output tokens: ${cost.OutputTokenCost:F6}");
Console.WriteLine($" Cache read: ${cost.CacheReadCost:F6}");
Console.WriteLine($" Cache creation: ${cost.CacheCreationCost:F6}");
Console.WriteLine($" Web search: ${cost.WebSearchCost:F6}");
}
}
}
16 changes: 8 additions & 8 deletions Anthropic.SDK.Tests/MCPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public async Task TestMCP()
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude37Sonnet,
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 5000,
Temperature = 1,
MCPServers = new List<MCPServer>()
{
new MCPServer()
{
Url = "https://mcp.deepwiki.com/sse",
Name = "DeepWiki",
Url = "https://learn.microsoft.com/api/mcp",
Name = "MSFT",
}
}
};
Expand All @@ -37,7 +37,7 @@ public async Task TestMCP()
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Tell me about the repo tghamm/Anthropic.SDK" }
new TextContent { Text = "Tell me about the Latest Microsoft.Extensions.AI Library" }
}
}
};
Expand All @@ -55,16 +55,16 @@ public async Task TestMCPExtendedStreaming()
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude37Sonnet,
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 3000,
Temperature = 1,
Stream = true,
MCPServers = new List<MCPServer>()
{
new MCPServer()
{
Url = "https://mcp.deepwiki.com/sse",
Name = "DeepWiki",
Url = "https://learn.microsoft.com/api/mcp",
Name = "MSFT",
}
}
};
Expand All @@ -75,7 +75,7 @@ public async Task TestMCPExtendedStreaming()
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Tell me about the repo tghamm/Anthropic.SDK" }
new TextContent { Text = "Tell me about the latest Microsoft.Extensions.AI Library" }
}
}
};
Expand Down
240 changes: 239 additions & 1 deletion Anthropic.SDK.Tests/WebSearchFunctionality.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anthropic.SDK.Constants;
using Anthropic.SDK.Extensions;
using Anthropic.SDK.Messaging;
using Microsoft.Extensions.AI;
using Tool = Anthropic.SDK.Common.Tool;
using TextContent = Anthropic.SDK.Messaging.TextContent;

namespace Anthropic.SDK.Tests
{
Expand Down Expand Up @@ -158,5 +161,240 @@ public async Task TestWebSearchExtendedStreaming()
Console.WriteLine("Final Result:");
Console.WriteLine(textResult.Last().Text);
}

[TestMethod]
public async Task TestWebSearchDynamicFiltering()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 4096,
Temperature = 1,
Tools = new List<Common.Tool>()
{
ServerTools.GetWebSearchTool()
},
ToolChoice = new ToolChoice()
{
Type = ToolChoiceType.Auto
},
};
var messages = new List<Message>
{
new Message
{
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Search for the current population of Tokyo and give me the number." }
}
}
};
parameters.Messages = messages;
var res = await client.Messages.GetClaudeMessageAsync(parameters);

Assert.IsNotNull(res);
Assert.IsTrue(res.Content.OfType<TextContent>().Any());
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Dynamic Filtering Result:");
Console.WriteLine(res.Content.OfType<TextContent>().Last().Text);
}

[TestMethod]
public async Task TestWebFetch()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 4096,
Temperature = 1,
Tools = new List<Common.Tool>()
{
ServerTools.GetWebFetchTool(maxUses: 5, enableCitations: true,
toolVersion: ServerTools.WebFetchVersionLegacy)
},
ToolChoice = new ToolChoice()
{
Type = ToolChoiceType.Auto
},
};
var messages = new List<Message>
{
new Message
{
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Fetch the content at https://www.wikipedia.org and tell me what it says." }
}
}
};
parameters.Messages = messages;
var res = await client.Messages.GetClaudeMessageAsync(parameters);

Assert.IsNotNull(res);
Assert.IsTrue(res.Content.OfType<TextContent>().Any());
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Web Fetch Result:");
Console.WriteLine(res.Content.OfType<TextContent>().Last().Text);
}

[TestMethod]
public async Task TestWebFetchStreaming()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 4096,
Temperature = 1,
Stream = true,
Tools = new List<Common.Tool>()
{
ServerTools.GetWebFetchTool(maxUses: 5,
toolVersion: ServerTools.WebFetchVersionLegacy)
},
ToolChoice = new ToolChoice()
{
Type = ToolChoiceType.Auto
},
};
var messages = new List<Message>
{
new Message
{
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Fetch the content at https://example.com and summarize it." }
}
}
};
parameters.Messages = messages;
var outputs = new List<MessageResponse>();
await foreach (var res in client.Messages.StreamClaudeMessageAsync(parameters))
{
if (res.Delta != null)
{
Debug.Write(res.Delta.Text);
}

outputs.Add(res);
}

var message = new Message(outputs);
Assert.IsNotNull(message);
Assert.IsTrue(message.Content.OfType<TextContent>().Any());
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Web Fetch Streaming Result:");
Console.WriteLine(message.Content.OfType<TextContent>().Last().Text);
}

[TestMethod]
public async Task TestWebSearchWithLegacyVersion()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 3000,
Temperature = 1,
Tools = new List<Common.Tool>()
{
ServerTools.GetWebSearchTool(toolVersion: ServerTools.WebSearchVersionLegacy)
},
ToolChoice = new ToolChoice()
{
Type = ToolChoiceType.Auto
},
};
var messages = new List<Message>
{
new Message
{
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "What is the weather like in San Francisco right now?" }
}
}
};
parameters.Messages = messages;
var res = await client.Messages.GetClaudeMessageAsync(parameters);

Assert.IsNotNull(res);
Assert.IsTrue(res.Content.OfType<TextContent>().Any());
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Legacy Web Search Result:");
Console.WriteLine(res.Content.OfType<TextContent>().Last().Text);
}

[TestMethod]
public async Task TestNonStreamingWebFetchChatClient()
{
IChatClient client = new AnthropicClient().Messages
.AsBuilder()
.UseFunctionInvocation()
.Build();

ChatOptions options = new ChatOptions()
{
ModelId = AnthropicModels.Claude46Sonnet,
MaxOutputTokens = 4096,
}.WithWebFetch(maxUses: 5, enableCitations: true);

var res = await client.GetResponseAsync(
"Fetch the content at https://example.com and tell me what it says.", options);

Assert.IsNotNull(res);
Assert.IsTrue(!string.IsNullOrEmpty(res.Text));
Console.WriteLine("----------------------------------------------");
Console.WriteLine("IChatClient Web Fetch Result:");
Console.WriteLine(res.Text);
}

[TestMethod]
public async Task TestCombinedWebSearchAndFetch()
{
var client = new AnthropicClient();
var parameters = new MessageParameters()
{
Model = AnthropicModels.Claude46Sonnet,
MaxTokens = 4096,
Temperature = 1,
Tools = new List<Common.Tool>()
{
ServerTools.GetWebSearchTool(maxUses: 3,
toolVersion: ServerTools.WebSearchVersionLegacy),
ServerTools.GetWebFetchTool(maxUses: 5, enableCitations: true,
toolVersion: ServerTools.WebFetchVersionLegacy)
},
ToolChoice = new ToolChoice()
{
Type = ToolChoiceType.Auto
},
};
var messages = new List<Message>
{
new Message
{
Role = RoleType.User,
Content = new List<ContentBase>
{
new TextContent { Text = "Search for the Anthropic homepage and then fetch its content to summarize it." }
}
}
};
parameters.Messages = messages;
var res = await client.Messages.GetClaudeMessageAsync(parameters);

Assert.IsNotNull(res);
Assert.IsTrue(res.Content.OfType<TextContent>().Any());
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Combined Search + Fetch Result:");
Console.WriteLine(res.Content.OfType<TextContent>().Last().Text);
}
}
}
Loading
Loading