Skip to content
v2.274.0

Experimental

@aws-cdk/aws-bedrock-alpha is an experimental (alpha) package: its API may change in any release, without a major version bump.

ToolChoice Class ​

Defines how the model should choose which tool to use.

Example ​

ts
const cmk = new kms.Key(this, 'cmk', {});

const variantChat = bedrock.PromptVariant.chat({
  variantName: 'variant1',
  model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
  messages: [
    bedrock.ChatMessage.user('From now on, you speak Japanese!'),
    bedrock.ChatMessage.assistant('Konnichiwa!'),
    bedrock.ChatMessage.user('From now on, you speak {{language}}!'),
  ],
  system: 'You are a helpful assistant that only speaks the language you`re told.',
  promptVariables: ['language'],
  toolConfiguration: {
    toolChoice: bedrock.ToolChoice.AUTO,
    tools: [
      bedrock.Tool.function({
        name: 'top_song',
        description: 'Get the most popular song played on a radio station.',
        inputSchema: {
          type: 'object',
          properties: {
            sign: {
              type: 'string',
              description: 'The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR.',
            },
          },
          required: ['sign'],
        },
      }),
    ],
  },
});

new bedrock.Prompt(this, 'prompt1', {
  promptName: 'prompt-chat',
  description: 'my first chat prompt',
  defaultVariant: variantChat,
  variants: [variantChat],
  kmsKey: cmk,
});
python
cmk = kms.Key(self, "cmk")

variant_chat = bedrock.PromptVariant.chat(
    variant_name="variant1",
    model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
    messages=[
        bedrock.ChatMessage.user("From now on, you speak Japanese!"),
        bedrock.ChatMessage.assistant("Konnichiwa!"),
        bedrock.ChatMessage.user("From now on, you speak {{language}}!")
    ],
    system="You are a helpful assistant that only speaks the language you`re told.",
    prompt_variables=["language"],
    tool_configuration=bedrock.ToolConfiguration(
        tool_choice=bedrock.ToolChoice.AUTO,
        tools=[
            bedrock.Tool.function(
                name="top_song",
                description="Get the most popular song played on a radio station.",
                input_schema={
                    "type": "object",
                    "properties": {
                        "sign": {
                            "type": "string",
                            "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR."
                        }
                    },
                    "required": ["sign"]
                }
            )
        ]
    )
)

bedrock.Prompt(self, "prompt1",
    prompt_name="prompt-chat",
    description="my first chat prompt",
    default_variant=variant_chat,
    variants=[variant_chat],
    kms_key=cmk
)
java
Key cmk = Key.Builder.create(this, "cmk").build();

IPromptVariant variantChat = PromptVariant.chat(ChatPromptVariantProps.builder()
        .variantName("variant1")
        .model(BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0)
        .messages(List.of(ChatMessage.user("From now on, you speak Japanese!"), ChatMessage.assistant("Konnichiwa!"), ChatMessage.user("From now on, you speak {{language}}!")))
        .system("You are a helpful assistant that only speaks the language you`re told.")
        .promptVariables(List.of("language"))
        .toolConfiguration(ToolConfiguration.builder()
                .toolChoice(ToolChoice.AUTO)
                .tools(List.of(Tool.function(FunctionToolProps.builder()
                        .name("top_song")
                        .description("Get the most popular song played on a radio station.")
                        .inputSchema(Map.of(
                                "type", "object",
                                "properties", Map.of(
                                        "sign", Map.of(
                                                "type", "string",
                                                "description", "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR.")),
                                "required", List.of("sign")))
                        .build())))
                .build())
        .build());

Prompt.Builder.create(this, "prompt1")
        .promptName("prompt-chat")
        .description("my first chat prompt")
        .defaultVariant(variantChat)
        .variants(List.of(variantChat))
        .kmsKey(cmk)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.AWS.Bedrock.Alpha
var cmk = new Key(this, "cmk", new KeyProps { });

var variantChat = PromptVariant.Chat(new ChatPromptVariantProps {
    VariantName = "variant1",
    Model = BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
    Messages = new [] { ChatMessage.User("From now on, you speak Japanese!"), ChatMessage.Assistant("Konnichiwa!"), ChatMessage.User("From now on, you speak {{language}}!") },
    System = "You are a helpful assistant that only speaks the language you`re told.",
    PromptVariables = new [] { "language" },
    ToolConfiguration = new ToolConfiguration {
        ToolChoice = ToolChoice.AUTO,
        Tools = new [] { Tool.Function(new FunctionToolProps {
            Name = "top_song",
            Description = "Get the most popular song played on a radio station.",
            InputSchema = new Dictionary<string, object> {
                { "type", "object" },
                { "properties", new Struct {
                    Sign = new Struct {
                        Type = "string",
                        Description = "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR."
                    }
                } },
                { "required", new [] { "sign" } }
            }
        }) }
    }
});

new Prompt(this, "prompt1", new PromptProps {
    PromptName = "prompt-chat",
    Description = "my first chat prompt",
    DefaultVariant = variantChat,
    Variants = new [] { variantChat },
    KmsKey = cmk
});
go
// Install: go get github.com/aws/aws-cdk-go/awsbedrockalpha/v2
cmk := kms.NewKey(this, jsii.String("cmk"), &KeyProps{
})

variantChat := bedrock.PromptVariant_Chat(&ChatPromptVariantProps{
	VariantName: jsii.String("variant1"),
	Model: bedrock.BedrockFoundationModel_ANTHROPIC_CLAUDE_3_5_SONNET_V1_0(),
	Messages: []ChatMessage{
		bedrock.ChatMessage_User(jsii.String("From now on, you speak Japanese!")),
		bedrock.ChatMessage_Assistant(jsii.String("Konnichiwa!")),
		bedrock.ChatMessage_*User(jsii.String("From now on, you speak {{language}}!")),
	},
	System: jsii.String("You are a helpful assistant that only speaks the language you`re told."),
	PromptVariables: []*string{
		jsii.String("language"),
	},
	ToolConfiguration: &ToolConfiguration{
		ToolChoice: bedrock.ToolChoice_AUTO(),
		Tools: []Tool{
			bedrock.Tool_Function(&FunctionToolProps{
				Name: jsii.String("top_song"),
				Description: jsii.String("Get the most popular song played on a radio station."),
				InputSchema: map[string]interface{}{
					"type": jsii.String("object"),
					"properties": map[string]map[string]*string{
						"sign": map[string]*string{
							"type": jsii.String("string"),
							"description": jsii.String("The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKR."),
						},
					},
					"required": []*string{
						jsii.String("sign"),
					},
				},
			}),
		},
	},
})

bedrock.NewPrompt(this, jsii.String("prompt1"), &PromptProps{
	PromptName: jsii.String("prompt-chat"),
	Description: jsii.String("my first chat prompt"),
	DefaultVariant: variantChat,
	Variants: []IPromptVariant{
		variantChat,
	},
	KmsKey: cmk,
})

Initializer ​

ts
new ToolChoice(any: any, auto: any, tool?: string)
python
ToolChoice(any: Any, auto: Any, tool: typing.Optional[str] = None)
java
new ToolChoice(Object any, Object auto, String tool)
csharp
new ToolChoice(object any, object auto, string? tool = null)
go
NewToolChoice(Any interface{}, Auto interface{}, Tool *string)

Properties ​

anymay be undefinedanyanymay be undefinedAnyanymay be undefinedObjectAnymay be undefinedobjectAnymay be undefinedinterface{}#
Configuration for ANY tool choice.
The model must request at least one tool (no text is generated).
automay be undefinedanyautomay be undefinedAnyautomay be undefinedObjectAutomay be undefinedobjectAutomay be undefinedinterface{}#
Configuration for AUTO tool choice.
AUTOToolChoiceautoToolChoiceAUTOToolChoiceAUTOToolChoiceAUTOToolChoicerequiredstatic#
(Default).

The Model automatically decides if a tool should be called or whether to generate text instead.

toolmay be undefinedstringtoolmay be undefinedstrtoolmay be undefinedStringToolmay be undefinedstringToolmay be undefined*string#
The specific tool name if using specific tool choice.

Methods ​

specificTool(toolName: string): ToolChoice // chainable@staticmethod def specific_tool(tool_name: str) -> ToolChoicepublic static ToolChoice specificTool(String toolName)public static ToolChoice SpecificTool(string toolName)func SpecificTool(ToolName *string) ToolChoicestatic#
The Model must request the specified tool.

Only supported by some models like Anthropic Claude 3 models.

1 parameter · returns

Parameters

  • toolNametool_nametoolNametoolNameToolName — - The name of the specific tool to use.

Returns: A ToolChoice instance configured for the specific tool