最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Problems with JSON schema as a string for AWS Bedrock in .NET - Stack Overflow

programmeradmin2浏览0评论

I'm trying to use the .NET AWS Bedrock Client for tools calling. One thing I can't wrap my head around is how to pass the JSON schema of the tool, if I have the schema as a string.

So this is how I configure the tool:

var jsonSchemaString = """
                    {
                        "type":"object",
                        "required":[
                            "data",
                            "something",
                            "responses"
                        ],
                        ...more JSON schema
                    }
                        """;

return new ToolConfiguration
{
    ToolChoice = new ToolChoice
    {
        Tool = new SpecificToolChoice
        {
            Name = "json_processor"
        }
    },
    Tools =
    [
        new Tool
        {
            ToolSpec = new ToolSpecification
            {
                Name = "json_processor",
                Description = "Processes well-structured JSON.",
                InputSchema = new ToolInputSchema
                {
                    Json = jsonSchemaString,
                },
            }
        }
    ]
};

I've tried different ways of configuring the InputSchema. This particular way will throw an exception like:

Amazon.BedrockRuntime.Model.ValidationException
The format of the value at toolConfig.tools.0.toolSpec.inputSchema.json is invalid. Provide a json object for the field and try again.

And if I look at the actual request that is made, it looks like the input schema is double encoded like:

"tools":[{"toolSpec":{"description":"Processes well-structured JSON.","inputSchema":{"json":"{\\n    \\"type\\": \\"object\\",\\n    \\"required\\": [\\n      \\

Not sure what to do here? The only way I've managed to configure it is by creating an object like this:

        var document = Document.FromObject(new
        {
            type = "object",
            required = new List<object>
            {
                "data",
                "something",
                "responses"
            },
            ...
            
        });
        

I'm trying to use the .NET AWS Bedrock Client for tools calling. One thing I can't wrap my head around is how to pass the JSON schema of the tool, if I have the schema as a string.

So this is how I configure the tool:

var jsonSchemaString = """
                    {
                        "type":"object",
                        "required":[
                            "data",
                            "something",
                            "responses"
                        ],
                        ...more JSON schema
                    }
                        """;

return new ToolConfiguration
{
    ToolChoice = new ToolChoice
    {
        Tool = new SpecificToolChoice
        {
            Name = "json_processor"
        }
    },
    Tools =
    [
        new Tool
        {
            ToolSpec = new ToolSpecification
            {
                Name = "json_processor",
                Description = "Processes well-structured JSON.",
                InputSchema = new ToolInputSchema
                {
                    Json = jsonSchemaString,
                },
            }
        }
    ]
};

I've tried different ways of configuring the InputSchema. This particular way will throw an exception like:

Amazon.BedrockRuntime.Model.ValidationException
The format of the value at toolConfig.tools.0.toolSpec.inputSchema.json is invalid. Provide a json object for the field and try again.

And if I look at the actual request that is made, it looks like the input schema is double encoded like:

"tools":[{"toolSpec":{"description":"Processes well-structured JSON.","inputSchema":{"json":"{\\n    \\"type\\": \\"object\\",\\n    \\"required\\": [\\n      \\

Not sure what to do here? The only way I've managed to configure it is by creating an object like this:

        var document = Document.FromObject(new
        {
            type = "object",
            required = new List<object>
            {
                "data",
                "something",
                "responses"
            },
            ...
            
        });
        
Share Improve this question asked Mar 14 at 13:37 JoelJoel 9,00812 gold badges70 silver badges142 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

AWS SDK provides JsonMapper

var jsonSchemaObject = JsonMapper.ToObject(jsonSchema);

new ToolSpecification
{   
    InputSchema = new ToolInputSchema { Json = Document.FromObject(jsonSchemaObject) }
}
发布评论

评论列表(0)

  1. 暂无评论