syncable_cli/bedrock/
completion.rs

1//! All supported models <https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html>
2
3use super::{
4    client::Client,
5    types::{
6        assistant_content::AwsConverseOutput, completion_request::AwsCompletionRequest,
7        converse_output::InternalConverseOutput, errors::AwsSdkConverseError,
8    },
9};
10
11use rig::completion::{self, CompletionError, CompletionRequest};
12use rig::streaming::StreamingCompletionResponse;
13
14/// `ai21.jamba-1-5-large-v1:0`
15pub const AI21_JAMBA_1_5_LARGE: &str = "ai21.jamba-1-5-large-v1:0";
16/// `ai21.jamba-1-5-mini-v1:0`
17pub const AI21_JAMBA_1_5_MINI: &str = "ai21.jamba-1-5-mini-v1:0";
18/// `amazon.nova-canvas-v1:0`
19pub const AMAZON_NOVA_CANVAS: &str = "amazon.nova-canvas-v1:0";
20/// `amazon.nova-lite-v1:0`
21pub const AMAZON_NOVA_LITE: &str = "amazon.nova-lite-v1:0";
22/// `amazon.nova-micro-v1:0`
23pub const AMAZON_NOVA_MICRO: &str = "amazon.nova-micro-v1:0";
24/// `amazon.nova-premier-v1:0`
25pub const AMAZON_NOVA_PREMIER: &str = "amazon.nova-premier-v1:0";
26/// `amazon.nova-pro-v1:0`
27pub const AMAZON_NOVA_PRO: &str = "amazon.nova-pro-v1:0";
28/// `amazon.nova-reel-v1:0`
29pub const AMAZON_NOVA_REEL_V1_0: &str = "amazon.nova-reel-v1:0";
30/// `amazon.nova-reel-v1:1`
31pub const AMAZON_NOVA_REEL_V1_1: &str = "amazon.nova-reel-v1:1";
32/// `amazon.nova-sonic-v1:0`
33pub const AMAZON_NOVA_SONIC: &str = "amazon.nova-sonic-v1:0";
34/// `amazon.rerank-v1:0`
35pub const AMAZON_RERANK_1_0: &str = "amazon.rerank-v1:0";
36/// `amazon.titan-embed-text-v1`
37pub const AMAZON_TITAN_EMBEDDINGS_G1_TEXT: &str = "amazon.titan-embed-text-v1";
38/// `amazon.titan-image-generator-v2:0`
39pub const AMAZON_TITAN_IMAGE_GENERATOR_G1_V2: &str = "amazon.titan-image-generator-v2:0";
40/// `amazon.titan-image-generator-v1`
41pub const AMAZON_TITAN_IMAGE_GENERATOR_G1: &str = "amazon.titan-image-generator-v1";
42/// `amazon.titan-embed-image-v1`
43pub const AMAZON_TITAN_MULTIMODAL_EMBEDDINGS_G1: &str = "amazon.titan-embed-image-v1";
44/// `amazon.titan-embed-text-v2:0`
45pub const AMAZON_TITAN_TEXT_EMBEDDINGS_V2: &str = "amazon.titan-embed-text-v2:0";
46/// `amazon.titan-text-express-v1`
47pub const AMAZON_TITAN_TEXT_EXPRESS_V1: &str = "amazon.titan-text-express-v1";
48/// `amazon.titan-text-lite-v1`
49pub const AMAZON_TITAN_TEXT_LITE_V1: &str = "amazon.titan-text-lite-v1";
50/// `amazon.titan-text-premier-v1:0`
51pub const AMAZON_TITAN_TEXT_PREMIER_V1_0: &str = "amazon.titan-text-premier-v1:0";
52/// `anthropic.claude-3-haiku-20240307-v1:0`
53pub const ANTHROPIC_CLAUDE_3_HAIKU: &str = "anthropic.claude-3-haiku-20240307-v1:0";
54/// `anthropic.claude-3-opus-20240229-v1:0`
55pub const ANTHROPIC_CLAUDE_3_OPUS: &str = "anthropic.claude-3-opus-20240229-v1:0";
56/// `anthropic.claude-3-sonnet-20240229-v1:0`
57pub const ANTHROPIC_CLAUDE_3_SONNET: &str = "anthropic.claude-3-sonnet-20240229-v1:0";
58/// `anthropic.claude-3-5-haiku-20241022-v1:0`
59pub const ANTHROPIC_CLAUDE_3_5_HAIKU: &str = "anthropic.claude-3-5-haiku-20241022-v1:0";
60/// `anthropic.claude-3-5-sonnet-20241022-v2:0`
61pub const ANTHROPIC_CLAUDE_3_5_SONNET_V2: &str = "anthropic.claude-3-5-sonnet-20241022-v2:0";
62/// `anthropic.claude-3-5-sonnet-20240620-v1:0`
63pub const ANTHROPIC_CLAUDE_3_5_SONNET: &str = "anthropic.claude-3-5-sonnet-20240620-v1:0";
64/// `anthropic.claude-3-7-sonnet-20250219-v1:0`
65pub const ANTHROPIC_CLAUDE_3_7_SONNET: &str = "anthropic.claude-3-7-sonnet-20250219-v1:0";
66/// `anthropic.claude-opus-4-20250514-v1:0`
67pub const ANTHROPIC_CLAUDE_OPUS_4: &str = "anthropic.claude-opus-4-20250514-v1:0";
68/// `anthropic.claude-sonnet-4-20250514-v1:0`
69pub const ANTHROPIC_CLAUDE_SONNET_4: &str = "anthropic.claude-sonnet-4-20250514-v1:0";
70/// `cohere.command-light-text-v14`
71pub const COHERE_COMMAND_LIGHT_TEXT: &str = "cohere.command-light-text-v14";
72/// `cohere.command-r-plus-v1:0`
73pub const COHERE_COMMAND_R_PLUS: &str = "cohere.command-r-plus-v1:0";
74/// `cohere.command-r-v1:0`
75pub const COHERE_COMMAND_R: &str = "cohere.command-r-v1:0";
76/// `cohere.command-text-v14`
77pub const COHERE_COMMAND: &str = "cohere.command-text-v14";
78/// `cohere.embed-english-v3`
79pub const COHERE_EMBED_ENGLISH: &str = "cohere.embed-english-v3";
80/// `cohere.embed-multilingual-v3`
81pub const COHERE_EMBED_MULTILINGUAL: &str = "cohere.embed-multilingual-v3";
82/// `cohere.rerank-v3-5:0`
83pub const COHERE_RERANK_V3_5: &str = "cohere.rerank-v3-5:0";
84/// `deepseek.r1-v1:0`
85pub const DEEPSEEK_R1: &str = "deepseek.r1-v1:0";
86/// `luma.ray-v2:0`
87pub const LUMA_RAY_V2_0: &str = "luma.ray-v2:0";
88/// `meta.llama3-8b-instruct-v1:0`
89pub const LLAMA_3_8B_INSTRUCT: &str = "meta.llama3-8b-instruct-v1:0";
90/// `meta.llama3-70b-instruct-v1:0`
91pub const LLAMA_3_70B_INSTRUCT: &str = "meta.llama3-70b-instruct-v1:0";
92/// `meta.llama3-1-8b-instruct-v1:0`
93pub const LLAMA_3_1_8B_INSTRUCT: &str = "meta.llama3-1-8b-instruct-v1:0";
94/// `meta.llama3-1-70b-instruct-v1:0`
95pub const LLAMA_3_1_70B_INSTRUCT: &str = "meta.llama3-1-70b-instruct-v1:0";
96/// `meta.llama3-1-405b-instruct-v1:0`
97pub const LLAMA_3_1_405B_INSTRUCT: &str = "meta.llama3-1-405b-instruct-v1:0";
98/// `meta.llama3-2-1b-instruct-v1:0`
99pub const LLAMA_3_2_1B_INSTRUCT: &str = "meta.llama3-2-1b-instruct-v1:0";
100/// `meta.llama3-2-3b-instruct-v1:0`
101pub const LLAMA_3_2_3B_INSTRUCT: &str = "meta.llama3-2-3b-instruct-v1:0";
102/// `meta.llama3-2-11b-instruct-v1:0`
103pub const LLAMA_3_2_11B_INSTRUCT: &str = "meta.llama3-2-11b-instruct-v1:0";
104/// `meta.llama3-2-90b-instruct-v1:0`
105pub const LLAMA_3_2_90B_INSTRUCT: &str = "meta.llama3-2-90b-instruct-v1:0";
106/// `meta.llama3-3-70b-instruct-v1:0`
107pub const META_LLAMA_3_3_70B_INSTRUCT: &str = "meta.llama3-3-70b-instruct-v1:0";
108/// `meta.llama4-maverick-17b-instruct-v1:0`
109pub const META_LLAMA_4_MAVERICK_17B_INSTRUCT: &str = "meta.llama4-maverick-17b-instruct-v1:0";
110/// `meta.llama4-scout-17b-instruct-v1:0`
111pub const META_LLAMA_4_SCOUT_17B_INSTRUCT: &str = "meta.llama4-scout-17b-instruct-v1:0";
112/// `mistral.mistral-7b-instruct-v0:2`
113pub const MISTRAL_7B_INSTRUCT: &str = "mistral.mistral-7b-instruct-v0:2";
114/// `mistral.mistral-large-2402-v1:0`
115pub const MISTRAL_LARGE_24_02: &str = "mistral.mistral-large-2402-v1:0";
116/// `mistral.mistral-large-2407-v1:0`
117pub const MISTRAL_LARGE_24_07: &str = "mistral.mistral-large-2407-v1:0";
118/// `mistral.mistral-small-2402-v1:0`
119pub const MISTRAL_SMALL_24_02: &str = "mistral.mistral-small-2402-v1:0";
120/// `mistral.mixtral-8x7b-instruct-v0:1`
121pub const MISTRAL_MIXTRAL_8X7B_INSTRUCT_V0: &str = "mistral.mixtral-8x7b-instruct-v0:1";
122/// `mistral.pixtral-large-2502-v1:0`
123pub const MISTRAL_PIXTRAL_LARGE_2502: &str = "mistral.pixtral-large-2502-v1:0";
124/// `stability.sd3-5-large-v1:0`
125pub const STABILITY_SD3_5_LARGE: &str = "stability.sd3-5-large-v1:0";
126/// `stability.stable-image-core-v1:1`
127pub const STABILITY_STABLE_IMAGE_CORE_1_0: &str = "stability.stable-image-core-v1:1";
128/// `stability.stable-image-ultra-v1:1`
129pub const STABILITY_STABLE_IMAGE_ULTRA_1_0: &str = "stability.stable-image-ultra-v1:1";
130/// `twelvelabs.marengo-embed-2-7-v1:0`
131pub const TWELVELABS_MARENGO_EMBED_V2_7: &str = "twelvelabs.marengo-embed-2-7-v1:0";
132/// `twelvelabs.pegasus-1-2-v1:0`
133pub const TWELVELABS_PEGASUS_V1_2: &str = "twelvelabs.pegasus-1-2-v1:0";
134/// `writer.palmyra-x4-v1:0`
135pub const WRITER_PALMYRA_X4: &str = "writer.palmyra-x4-v1:0";
136/// `writer.palmyra-x5-v1:0`
137pub const WRITER_PALMYRA_X5: &str = "writer.palmyra-x5-v1:0";
138/// `ai21.jamba-instruct-v1:0`
139pub const AI21_JAMBA_INSTRUCT: &str = "ai21.jamba-instruct-v1:0";
140/// `anthropic.claude-v2:1`
141pub const ANTHROPIC_CLAUDE_2_1: &str = "anthropic.claude-v2:1";
142/// `anthropic.claude-v2`
143pub const ANTHROPIC_CLAUDE_2: &str = "anthropic.claude-v2";
144/// `anthropic.claude-instant-v1`
145pub const ANTHROPIC_CLAUDE_INSTANT: &str = "anthropic.claude-instant-v1";
146/// `anthropic.claude-instant-v1:2`
147pub const ANTHROPIC_CLAUDE_INSTANT_V1_2: &str = "anthropic.claude-instant-v1:2";
148/// `anthropic.claude-v2:0`
149pub const ANTHROPIC_CLAUDE: &str = "anthropic.claude-v2:0";
150/// `stability.sd3-large-v1:0`
151pub const STABILITY_SD3_LARGE_1_0: &str = "stability.sd3-large-v1:0";
152/// `stability.stable-diffusion-xl-v1`
153pub const STABILITY_SDXL_1_0: &str = "stability.stable-diffusion-xl-v1";
154/// `stability.stable-image-core-v1:0`
155pub const STABILITY_STABLE_IMAGE_CORE_1_0_V1_0: &str = "stability.stable-image-core-v1:0";
156/// `stability.stable-image-ultra-v1:0`
157pub const STABILITY_STABLE_IMAGE_ULTRA_1_0_V1_0: &str = "stability.stable-image-ultra-v1:0";
158
159#[derive(Clone)]
160pub struct CompletionModel {
161    pub(crate) client: Client,
162    pub model: String,
163}
164
165impl CompletionModel {
166    pub fn new(client: Client, model: impl Into<String>) -> Self {
167        Self {
168            client,
169            model: model.into(),
170        }
171    }
172}
173
174impl completion::CompletionModel for CompletionModel {
175    type Response = AwsConverseOutput;
176    type StreamingResponse = super::streaming::BedrockStreamingResponse;
177
178    type Client = Client;
179
180    fn make(client: &Self::Client, model: impl Into<String>) -> Self {
181        Self::new(client.clone(), model)
182    }
183
184    async fn completion(
185        &self,
186        completion_request: completion::CompletionRequest,
187    ) -> Result<completion::CompletionResponse<AwsConverseOutput>, CompletionError> {
188        let request = AwsCompletionRequest(completion_request);
189
190        let mut converse_builder = self
191            .client
192            .get_inner()
193            .await
194            .converse()
195            .model_id(self.model.as_str());
196
197        let tool_config = request.tools_config()?;
198        let messages = request.messages()?;
199        converse_builder = converse_builder
200            .set_additional_model_request_fields(request.additional_params())
201            .set_inference_config(request.inference_config())
202            .set_tool_config(tool_config)
203            .set_system(request.system_prompt())
204            .set_messages(Some(messages));
205
206        let response = converse_builder
207            .send()
208            .await
209            .map_err(|sdk_error| Into::<CompletionError>::into(AwsSdkConverseError(sdk_error)))?;
210
211        let response: InternalConverseOutput = response
212            .try_into()
213            .map_err(|x| CompletionError::ProviderError(format!("Type conversion error: {x}")))?;
214
215        AwsConverseOutput(response).try_into()
216    }
217
218    async fn stream(
219        &self,
220        request: CompletionRequest,
221    ) -> Result<StreamingCompletionResponse<Self::StreamingResponse>, CompletionError> {
222        CompletionModel::stream(self, request).await
223    }
224}