Search code examples
c#.net-coreagentamazon-bedrockclaude

Function calling with Claude on Amazon Bedrock in .NET Core


I want to build a basic LLM integration with function calling on Amazon Bedrock using Claude in .NET Core. I use the official AWSSDK.BedrockRuntime (version 3.7.412.3) nuget package by AWS.

After hours of searching, I was unable to find .NET Core example code. The only example I found was this python example: https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use-examples.html

After I converted it to .NET Core, it looked promising.

var messages = new List<Message>
{
    new Message
    {
        Role = ConversationRole.User,
        Content = new List<ContentBlock> { new ContentBlock { Text = "Get the most popular song played on a radio station." } }
    }
};

var response = await bedrock.ConverseAsync(new ConverseRequest
{
    ModelId = "anthropic.claude-3-sonnet-20240229-v1:0",
    Messages = messages,
    ToolConfig = new ToolConfiguration
    {
        //taken from https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use-inference-call.html
        Tools = new List<Tool>
        {
            new Tool
            {
                ToolSpec = new ToolSpecification
                {
                    Name = "GetSong",
                    Description = "Gets the current song on the radio",
                    InputSchema = new ToolInputSchema
                    {
                        Json = Amazon.Runtime.Documents.Document.FromObject(new
                        {
                            Type = "object",
                            Properties = new Dictionary<string, object>
                            {
                                { "sign", new {
                                    Type = "string",
                                    Description = "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP."
                                } }
                            },
                            Required = new string[]
                            {
                                 "sign"
                            },
                        }),
                    },
                }
            }
        }
    }
});

Unfortunately it throws this exception at runtime:

Amazon.BedrockRuntime.Model.ValidationException
  HResult=0x80131500
  Message=The value at toolConfig.tools.0.toolSpec.inputSchema.json.type must be one of the following: object.
  Source=AWSSDK.Core
  StackTrace:
   at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleExceptionStream(IRequestContext requestContext, IWebResponseData httpErrorResponse, HttpErrorResponseException exception, Stream responseStream)
   at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.<HandleExceptionAsync>d__2.MoveNext()
   at Amazon.Runtime.Internal.ExceptionHandler`1.<HandleAsync>d__6.MoveNext()
   at Amazon.Runtime.Internal.ErrorHandler.<ProcessExceptionAsync>d__8.MoveNext()
   at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1.MoveNext()
   at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()
   at Amazon.Runtime.Internal.Signer.<InvokeAsync>d__1`1.MoveNext()
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.<InvokeAsync>d__2`1.MoveNext()
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.<InvokeAsync>d__2`1.MoveNext()
   at Amazon.Runtime.Internal.CredentialsRetriever.<InvokeAsync>d__7`1.MoveNext()
   at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()
   at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()
   at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()
   at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()
   at Amazon.Runtime.Internal.ErrorCallbackHandler.<InvokeAsync>d__5`1.MoveNext()
   at Amazon.Runtime.Internal.MetricsHandler.<InvokeAsync>d__1`1.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in D:\DotnetAgentExperiments\aws.bed\Program.cs:line 22

Does anybody have an idea on how to correctly set up function calling with Amazon Bedrock using .NET Core?


Solution

  • After I changed the property names within ToolInputSchema.Json to lowercase, it worked:

    new Tool
    {
        ToolSpec = new ToolSpecification
        {
            Name = "GetSong",
            Description = "Gets the current song on the radio",
            InputSchema = new ToolInputSchema
            {
                Json = Amazon.Runtime.Documents.Document.FromObject(new
                {
                    type = "object",
                    properties = new Dictionary<string, object>
                    {
                        { "sign", new {
                            type = "string",
                            description = "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP."
                        } }
                    },
                    required = new string[]
                    {
                            "sign"
                    },
                }),
            },
        }
    }