aws_sdk_bedrockruntime/operation/count_tokens/
builders.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2pub use crate::operation::count_tokens::_count_tokens_output::CountTokensOutputBuilder;
3
4pub use crate::operation::count_tokens::_count_tokens_input::CountTokensInputBuilder;
5
6impl crate::operation::count_tokens::builders::CountTokensInputBuilder {
7    /// Sends a request with this input using the given client.
8    pub async fn send_with(
9        self,
10        client: &crate::Client,
11    ) -> ::std::result::Result<
12        crate::operation::count_tokens::CountTokensOutput,
13        ::aws_smithy_runtime_api::client::result::SdkError<
14            crate::operation::count_tokens::CountTokensError,
15            ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
16        >,
17    > {
18        let mut fluent_builder = client.count_tokens();
19        fluent_builder.inner = self;
20        fluent_builder.send().await
21    }
22}
23/// Fluent builder constructing a request to `CountTokens`.
24///
25/// <p>Returns the token count for a given inference request. This operation helps you estimate token usage before sending requests to foundation models by returning the token count that would be used if the same input were sent to the model in an inference request.</p>
26/// <p>Token counting is model-specific because different models use different tokenization strategies. The token count returned by this operation will match the token count that would be charged if the same input were sent to the model in an <code>InvokeModel</code> or <code>Converse</code> request.</p>
27/// <p>You can use this operation to:</p>
28/// <ul>
29/// <li>
30/// <p>Estimate costs before sending inference requests.</p></li>
31/// <li>
32/// <p>Optimize prompts to fit within token limits.</p></li>
33/// <li>
34/// <p>Plan for token usage in your applications.</p></li>
35/// </ul>
36/// <p>This operation accepts the same input formats as <code>InvokeModel</code> and <code>Converse</code>, allowing you to count tokens for both raw text inputs and structured conversation formats.</p>
37/// <p>The following operations are related to <code>CountTokens</code>:</p>
38/// <ul>
39/// <li>
40/// <p><a href="https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_InvokeModel.html">InvokeModel</a> - Sends inference requests to foundation models</p></li>
41/// <li>
42/// <p><a href="https://docs.aws.amazon.com/bedrock/latest/API/API_runtime_Converse.html">Converse</a> - Sends conversation-based inference requests to foundation models</p></li>
43/// </ul>
44#[derive(::std::clone::Clone, ::std::fmt::Debug)]
45pub struct CountTokensFluentBuilder {
46    handle: ::std::sync::Arc<crate::client::Handle>,
47    inner: crate::operation::count_tokens::builders::CountTokensInputBuilder,
48    config_override: ::std::option::Option<crate::config::Builder>,
49}
50impl
51    crate::client::customize::internal::CustomizableSend<
52        crate::operation::count_tokens::CountTokensOutput,
53        crate::operation::count_tokens::CountTokensError,
54    > for CountTokensFluentBuilder
55{
56    fn send(
57        self,
58        config_override: crate::config::Builder,
59    ) -> crate::client::customize::internal::BoxFuture<
60        crate::client::customize::internal::SendResult<
61            crate::operation::count_tokens::CountTokensOutput,
62            crate::operation::count_tokens::CountTokensError,
63        >,
64    > {
65        ::std::boxed::Box::pin(async move { self.config_override(config_override).send().await })
66    }
67}
68impl CountTokensFluentBuilder {
69    /// Creates a new `CountTokensFluentBuilder`.
70    pub(crate) fn new(handle: ::std::sync::Arc<crate::client::Handle>) -> Self {
71        Self {
72            handle,
73            inner: ::std::default::Default::default(),
74            config_override: ::std::option::Option::None,
75        }
76    }
77    /// Access the CountTokens as a reference.
78    pub fn as_input(&self) -> &crate::operation::count_tokens::builders::CountTokensInputBuilder {
79        &self.inner
80    }
81    /// Sends the request and returns the response.
82    ///
83    /// If an error occurs, an `SdkError` will be returned with additional details that
84    /// can be matched against.
85    ///
86    /// By default, any retryable failures will be retried twice. Retry behavior
87    /// is configurable with the [RetryConfig](aws_smithy_types::retry::RetryConfig), which can be
88    /// set when configuring the client.
89    pub async fn send(
90        self,
91    ) -> ::std::result::Result<
92        crate::operation::count_tokens::CountTokensOutput,
93        ::aws_smithy_runtime_api::client::result::SdkError<
94            crate::operation::count_tokens::CountTokensError,
95            ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
96        >,
97    > {
98        let input = self
99            .inner
100            .build()
101            .map_err(::aws_smithy_runtime_api::client::result::SdkError::construction_failure)?;
102        let runtime_plugins = crate::operation::count_tokens::CountTokens::operation_runtime_plugins(
103            self.handle.runtime_plugins.clone(),
104            &self.handle.conf,
105            self.config_override,
106        );
107        crate::operation::count_tokens::CountTokens::orchestrate(&runtime_plugins, input).await
108    }
109
110    /// Consumes this builder, creating a customizable operation that can be modified before being sent.
111    pub fn customize(
112        self,
113    ) -> crate::client::customize::CustomizableOperation<
114        crate::operation::count_tokens::CountTokensOutput,
115        crate::operation::count_tokens::CountTokensError,
116        Self,
117    > {
118        crate::client::customize::CustomizableOperation::new(self)
119    }
120    pub(crate) fn config_override(mut self, config_override: impl ::std::convert::Into<crate::config::Builder>) -> Self {
121        self.set_config_override(::std::option::Option::Some(config_override.into()));
122        self
123    }
124
125    pub(crate) fn set_config_override(&mut self, config_override: ::std::option::Option<crate::config::Builder>) -> &mut Self {
126        self.config_override = config_override;
127        self
128    }
129    /// <p>The unique identifier or ARN of the foundation model to use for token counting. Each model processes tokens differently, so the token count is specific to the model you specify.</p>
130    pub fn model_id(mut self, input: impl ::std::convert::Into<::std::string::String>) -> Self {
131        self.inner = self.inner.model_id(input.into());
132        self
133    }
134    /// <p>The unique identifier or ARN of the foundation model to use for token counting. Each model processes tokens differently, so the token count is specific to the model you specify.</p>
135    pub fn set_model_id(mut self, input: ::std::option::Option<::std::string::String>) -> Self {
136        self.inner = self.inner.set_model_id(input);
137        self
138    }
139    /// <p>The unique identifier or ARN of the foundation model to use for token counting. Each model processes tokens differently, so the token count is specific to the model you specify.</p>
140    pub fn get_model_id(&self) -> &::std::option::Option<::std::string::String> {
141        self.inner.get_model_id()
142    }
143    /// <p>The input for which to count tokens. The structure of this parameter depends on whether you're counting tokens for an <code>InvokeModel</code> or <code>Converse</code> request:</p>
144    /// <ul>
145    /// <li>
146    /// <p>For <code>InvokeModel</code> requests, provide the request body in the <code>invokeModel</code> field</p></li>
147    /// <li>
148    /// <p>For <code>Converse</code> requests, provide the messages and system content in the <code>converse</code> field</p></li>
149    /// </ul>
150    /// <p>The input format must be compatible with the model specified in the <code>modelId</code> parameter.</p>
151    pub fn input(mut self, input: crate::types::CountTokensInput) -> Self {
152        self.inner = self.inner.input(input);
153        self
154    }
155    /// <p>The input for which to count tokens. The structure of this parameter depends on whether you're counting tokens for an <code>InvokeModel</code> or <code>Converse</code> request:</p>
156    /// <ul>
157    /// <li>
158    /// <p>For <code>InvokeModel</code> requests, provide the request body in the <code>invokeModel</code> field</p></li>
159    /// <li>
160    /// <p>For <code>Converse</code> requests, provide the messages and system content in the <code>converse</code> field</p></li>
161    /// </ul>
162    /// <p>The input format must be compatible with the model specified in the <code>modelId</code> parameter.</p>
163    pub fn set_input(mut self, input: ::std::option::Option<crate::types::CountTokensInput>) -> Self {
164        self.inner = self.inner.set_input(input);
165        self
166    }
167    /// <p>The input for which to count tokens. The structure of this parameter depends on whether you're counting tokens for an <code>InvokeModel</code> or <code>Converse</code> request:</p>
168    /// <ul>
169    /// <li>
170    /// <p>For <code>InvokeModel</code> requests, provide the request body in the <code>invokeModel</code> field</p></li>
171    /// <li>
172    /// <p>For <code>Converse</code> requests, provide the messages and system content in the <code>converse</code> field</p></li>
173    /// </ul>
174    /// <p>The input format must be compatible with the model specified in the <code>modelId</code> parameter.</p>
175    pub fn get_input(&self) -> &::std::option::Option<crate::types::CountTokensInput> {
176        self.inner.get_input()
177    }
178}