rig-bedrock 0.43.0

AWS Bedrock model provider for Rig integration.
Documentation
use crate::types::json::AwsDocument;
use crate::types::message::RigMessage;
use aws_sdk_bedrockruntime::types as aws_bedrock;
use aws_sdk_bedrockruntime::types::{
    CachePointBlock, CachePointType, InferenceConfiguration, SystemContentBlock, Tool,
    ToolConfiguration, ToolInputSchema, ToolSpecification,
};
use rig_core::completion::Message;
use rig_core::error::ProviderError;
use rig_core::message::{DocumentMediaType, UserContent};

pub struct AwsCompletionRequest {
    pub inner: rig_core::completion::CompletionRequest,
    /// The issuer whose reasoning the request replays.
    pub issuer: rig_core::message::Issuer,
    pub prompt_caching: bool,
}

fn cache_point_block() -> Result<CachePointBlock, ProviderError> {
    CachePointBlock::builder()
        .r#type(CachePointType::Default)
        .build()
        .map_err(ProviderError::request)
}

impl AwsCompletionRequest {
    /// A Converse request over `inner`, replaying the reasoning `issuer`
    /// issued.
    pub fn new(
        inner: rig_core::completion::CompletionRequest,
        issuer: rig_core::message::Issuer,
        prompt_caching: bool,
    ) -> Self {
        Self {
            inner,
            issuer,
            prompt_caching,
        }
    }

    pub fn additional_params(&self) -> Option<aws_smithy_types::Document> {
        self.inner
            .additional_params
            .clone()
            .map(std::convert::Into::into)
            .map(|doc: AwsDocument| doc.0)
    }

    pub fn inference_config(&self) -> InferenceConfiguration {
        let mut inference_configuration = InferenceConfiguration::builder();

        if let Some(temperature) = &self.inner.temperature {
            inference_configuration =
                inference_configuration.set_temperature(Some(*temperature as f32));
        }

        if let Some(max_tokens) = &self.inner.max_tokens {
            inference_configuration =
                inference_configuration.set_max_tokens(Some(*max_tokens as i32));
        }

        inference_configuration.build()
    }

    pub fn tools_config(&self) -> Result<Option<ToolConfiguration>, ProviderError> {
        if self.inner.tool_choice == Some(rig_core::message::ToolChoice::None) {
            return Ok(None);
        }

        let tools = self
            .inner
            .tools
            .iter()
            .map(|tool_definition| {
                let doc: AwsDocument = tool_definition.parameters.clone().into();
                ToolSpecification::builder()
                    .name(tool_definition.name.clone())
                    .set_description(Some(tool_definition.description.clone()))
                    .set_input_schema(Some(ToolInputSchema::Json(doc.0)))
                    .build()
                    .map(Tool::ToolSpec)
                    .map_err(ProviderError::request)
            })
            .collect::<Result<Vec<_>, _>>()?;

        if !tools.is_empty() {
            use aws_sdk_bedrockruntime::types as aws_bedrock;
            let tool_choice = self
                .inner
                .tool_choice
                .as_ref()
                .map(|choice| match choice {
                    rig_core::message::ToolChoice::Auto => Ok(Some(aws_bedrock::ToolChoice::Auto(
                        aws_bedrock::AutoToolChoice::builder().build(),
                    ))),
                    rig_core::message::ToolChoice::Required => Ok(Some(
                        aws_bedrock::ToolChoice::Any(aws_bedrock::AnyToolChoice::builder().build()),
                    )),
                    rig_core::message::ToolChoice::None => Ok(None),
                    rig_core::message::ToolChoice::Specific { function_names } => function_names
                        .first()
                        .map(|name| {
                            aws_bedrock::SpecificToolChoice::builder()
                                .name(name.clone())
                                .build()
                                .map(aws_bedrock::ToolChoice::Tool)
                                .map(Some)
                                .map_err(ProviderError::request)
                        })
                        .transpose()
                        .map(Option::flatten),
                })
                .transpose()?
                .flatten();

            let config = ToolConfiguration::builder()
                .set_tools(Some(tools))
                .set_tool_choice(tool_choice)
                .build()
                .map_err(ProviderError::request)?;

            Ok(Some(config))
        } else {
            Ok(None)
        }
    }

    /// Maps rig's `output_schema` to Bedrock's `OutputConfig` for structured output.
    pub fn output_config(&self) -> Result<Option<aws_bedrock::OutputConfig>, ProviderError> {
        let Some(schema) = self.inner.output_schema.as_ref() else {
            return Ok(None);
        };

        let schema_name = self
            .inner
            .output_schema_name()
            .unwrap_or_else(|| "response_schema".to_string());

        let schema_json =
            serde_json::to_string(schema.as_value()).map_err(ProviderError::request)?;

        let json_schema_def = aws_bedrock::JsonSchemaDefinition::builder()
            .schema(schema_json)
            .name(schema_name)
            .build()
            .map_err(ProviderError::request)?;

        let text_format = aws_bedrock::OutputFormat::builder()
            .r#type(aws_bedrock::OutputFormatType::JsonSchema)
            .structure(aws_bedrock::OutputFormatStructure::JsonSchema(
                json_schema_def,
            ))
            .build()
            .map_err(ProviderError::request)?;

        Ok(Some(
            aws_bedrock::OutputConfig::builder()
                .text_format(text_format)
                .build(),
        ))
    }

    pub fn system_prompt(&self) -> Result<Option<Vec<SystemContentBlock>>, ProviderError> {
        let mut system_blocks = Vec::new();

        for message in self.inner.chat_history.iter() {
            if let Message::System { content } = message
                && !content.is_empty()
            {
                system_blocks.push(SystemContentBlock::Text(content.clone()));
            }
        }

        if system_blocks.is_empty() {
            Ok(None)
        } else {
            if self.prompt_caching {
                system_blocks.push(SystemContentBlock::CachePoint(cache_point_block()?));
            }
            Ok(Some(system_blocks))
        }
    }

    /// Consumes the request: this is the one accessor that needs the chat
    /// history by value, so call it after the borrowing accessors.
    pub fn messages(self) -> Result<Vec<aws_bedrock::Message>, ProviderError> {
        let mut full_history: Vec<Message> = Vec::new();

        if !self.inner.documents.is_empty() {
            let messages = self
                .inner
                .documents
                .iter()
                .map(std::string::ToString::to_string)
                .collect::<Vec<_>>()
                .join(" | ");

            let content = vec![UserContent::document(
                messages,
                Some(DocumentMediaType::TXT),
            )];

            full_history.push(Message::User { content });
        }

        let has_reasoning = self.inner.chat_history.iter().any(|message| match message {
            Message::Assistant { content, .. } => content
                .iter()
                .any(|c| matches!(c, rig_core::completion::AssistantContent::Reasoning(_))),
            _ => false,
        });

        full_history.extend(
            self.inner
                .chat_history
                .into_iter()
                .filter(|message| !matches!(message, Message::System { .. })),
        );

        let tool_ids = rig_core::providers::internal::wire_ids::WireIds::new(&full_history);
        let mut messages = Vec::new();
        for (position, message) in full_history.into_iter().enumerate() {
            let mut message = RigMessage(message).into_aws(&self.issuer)?;
            tool_ids
                .apply(
                    position,
                    message.content.iter_mut().filter_map(|part| match part {
                        aws_bedrock::ContentBlock::ToolUse(call) => Some(&mut call.tool_use_id),
                        aws_bedrock::ContentBlock::ToolResult(result) => {
                            Some(&mut result.tool_use_id)
                        }
                        _ => None,
                    }),
                )
                .map_err(ProviderError::request)?;
            messages.push(message);
        }

        crate::types::document::disambiguate_document_names(&mut messages);

        // Reasoning anywhere in history prevents a trailing message checkpoint,
        // even after tool results; the system checkpoint remains valid.
        if self.prompt_caching
            && !has_reasoning
            && let Some(last_msg) = messages.last_mut()
        {
            let mut content = std::mem::take(&mut last_msg.content);
            content.push(aws_bedrock::ContentBlock::CachePoint(cache_point_block()?));
            *last_msg = aws_bedrock::Message::builder()
                .role(last_msg.role.clone())
                .set_content(Some(content))
                .build()
                .map_err(ProviderError::request)?;
        }

        Ok(messages)
    }
}

#[cfg(test)]
mod tests;