-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.Net] Support tools for AnthropicClient and AnthropicAgent (#2944)
* Squash commits : support anthropic tools * Support tool_choice * Remove reference from TypeSafeFunctionCallCodeSnippet.cs and add own function in test proj
- Loading branch information
1 parent
e743d4d
commit 80ecbf9
Showing
19 changed files
with
715 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
dotnet/sample/AutoGen.Anthropic.Samples/Create_Anthropic_Agent_With_Tool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Single_Anthropic_Tool.cs | ||
|
||
using AutoGen.Anthropic.DTO; | ||
using AutoGen.Anthropic.Extensions; | ||
using AutoGen.Anthropic.Utils; | ||
using AutoGen.Core; | ||
using FluentAssertions; | ||
|
||
namespace AutoGen.Anthropic.Samples; | ||
|
||
#region WeatherFunction | ||
|
||
public partial class WeatherFunction | ||
{ | ||
/// <summary> | ||
/// Gets the weather based on the location and the unit | ||
/// </summary> | ||
/// <param name="location"></param> | ||
/// <param name="unit"></param> | ||
/// <returns></returns> | ||
[Function] | ||
public async Task<string> GetWeather(string location, string unit) | ||
{ | ||
// dummy implementation | ||
return $"The weather in {location} is currently sunny with a tempature of {unit} (s)"; | ||
} | ||
} | ||
#endregion | ||
public class Create_Anthropic_Agent_With_Tool | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
#region define_tool | ||
var tool = new Tool | ||
{ | ||
Name = "GetWeather", | ||
Description = "Get the current weather in a given location", | ||
InputSchema = new InputSchema | ||
{ | ||
Type = "object", | ||
Properties = new Dictionary<string, SchemaProperty> | ||
{ | ||
{ "location", new SchemaProperty { Type = "string", Description = "The city and state, e.g. San Francisco, CA" } }, | ||
{ "unit", new SchemaProperty { Type = "string", Description = "The unit of temperature, either \"celsius\" or \"fahrenheit\"" } } | ||
}, | ||
Required = new List<string> { "location" } | ||
} | ||
}; | ||
|
||
var weatherFunction = new WeatherFunction(); | ||
var functionMiddleware = new FunctionCallMiddleware( | ||
functions: [ | ||
weatherFunction.GetWeatherFunctionContract, | ||
], | ||
functionMap: new Dictionary<string, Func<string, Task<string>>> | ||
{ | ||
{ weatherFunction.GetWeatherFunctionContract.Name!, weatherFunction.GetWeatherWrapper }, | ||
}); | ||
|
||
#endregion | ||
|
||
#region create_anthropic_agent | ||
|
||
var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") ?? | ||
throw new Exception("Missing ANTHROPIC_API_KEY environment variable."); | ||
|
||
var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey); | ||
var agent = new AnthropicClientAgent(anthropicClient, "assistant", AnthropicConstants.Claude3Haiku, | ||
tools: [tool]); // Define tools for AnthropicClientAgent | ||
#endregion | ||
|
||
#region register_middleware | ||
|
||
var agentWithConnector = agent | ||
.RegisterMessageConnector() | ||
.RegisterPrintMessage() | ||
.RegisterStreamingMiddleware(functionMiddleware); | ||
#endregion register_middleware | ||
|
||
#region single_turn | ||
var question = new TextMessage(Role.Assistant, | ||
"What is the weather like in San Francisco?", | ||
from: "user"); | ||
var functionCallReply = await agentWithConnector.SendAsync(question); | ||
#endregion | ||
|
||
#region Single_turn_verify_reply | ||
functionCallReply.Should().BeOfType<ToolCallAggregateMessage>(); | ||
#endregion Single_turn_verify_reply | ||
|
||
#region Multi_turn | ||
var finalReply = await agentWithConnector.SendAsync(chatHistory: [question, functionCallReply]); | ||
#endregion Multi_turn | ||
|
||
#region Multi_turn_verify_reply | ||
finalReply.Should().BeOfType<TextMessage>(); | ||
#endregion Multi_turn_verify_reply | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// JsonPropertyNameEnumCoverter.cs | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace AutoGen.Anthropic.Converters; | ||
|
||
internal class JsonPropertyNameEnumConverter<T> : JsonConverter<T> where T : struct, Enum | ||
{ | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
string value = reader.GetString() ?? throw new JsonException("Value was null."); | ||
|
||
foreach (var field in typeToConvert.GetFields()) | ||
{ | ||
var attribute = field.GetCustomAttribute<JsonPropertyNameAttribute>(); | ||
if (attribute?.Name == value) | ||
{ | ||
return (T)Enum.Parse(typeToConvert, field.Name); | ||
} | ||
} | ||
|
||
throw new JsonException($"Unable to convert \"{value}\" to enum {typeToConvert}."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
var field = value.GetType().GetField(value.ToString()); | ||
var attribute = field.GetCustomAttribute<JsonPropertyNameAttribute>(); | ||
|
||
if (attribute != null) | ||
{ | ||
writer.WriteStringValue(attribute.Name); | ||
} | ||
else | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.