|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// snippet-start:[rust.bedrock-runtime.ConverseStream_AnthropicClaude.supporting] |
| 5 | +use aws_config::BehaviorVersion; |
| 6 | +use aws_sdk_bedrockruntime::{ |
| 7 | + error::ProvideErrorMetadata, |
| 8 | + operation::converse_stream::ConverseStreamError, |
| 9 | + types::{ |
| 10 | + error::ConverseStreamOutputError, ContentBlock, ConversationRole, |
| 11 | + ConverseStreamOutput as ConverseStreamOutputType, Message, |
| 12 | + }, |
| 13 | + Client, |
| 14 | +}; |
| 15 | + |
| 16 | +// Set the model ID, e.g., Claude 3 Haiku. |
| 17 | +const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0"; |
| 18 | +const CLAUDE_REGION: &str = "us-east-1"; |
| 19 | + |
| 20 | +// Start a conversation with the user message. |
| 21 | +const USER_MESSAGE: &str = "Describe the purpose of a 'hello world' program in one line."; |
| 22 | + |
| 23 | +#[derive(Debug)] |
| 24 | +struct BedrockConverseStreamError(String); |
| 25 | +impl std::fmt::Display for BedrockConverseStreamError { |
| 26 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 27 | + write!(f, "Can't invoke '{}'. Reason: {}", MODEL_ID, self.0) |
| 28 | + } |
| 29 | +} |
| 30 | +impl std::error::Error for BedrockConverseStreamError {} |
| 31 | +impl From<&str> for BedrockConverseStreamError { |
| 32 | + fn from(value: &str) -> Self { |
| 33 | + BedrockConverseStreamError(value.into()) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl From<&ConverseStreamError> for BedrockConverseStreamError { |
| 38 | + fn from(value: &ConverseStreamError) -> Self { |
| 39 | + BedrockConverseStreamError( |
| 40 | + match value { |
| 41 | + ConverseStreamError::ModelTimeoutException(_) => "Model took too long", |
| 42 | + ConverseStreamError::ModelNotReadyException(_) => "Model is not ready", |
| 43 | + _ => "Unknown", |
| 44 | + } |
| 45 | + .into(), |
| 46 | + ) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl From<&ConverseStreamOutputError> for BedrockConverseStreamError { |
| 51 | + fn from(value: &ConverseStreamOutputError) -> Self { |
| 52 | + match value { |
| 53 | + ConverseStreamOutputError::ValidationException(ve) => BedrockConverseStreamError( |
| 54 | + ve.message().unwrap_or("Unknown ValidationException").into(), |
| 55 | + ), |
| 56 | + ConverseStreamOutputError::ThrottlingException(te) => BedrockConverseStreamError( |
| 57 | + te.message().unwrap_or("Unknown ThrottlingException").into(), |
| 58 | + ), |
| 59 | + value => BedrockConverseStreamError( |
| 60 | + value |
| 61 | + .message() |
| 62 | + .unwrap_or("Unknown StreamOutput exception") |
| 63 | + .into(), |
| 64 | + ), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +// snippet-end:[rust.bedrock-runtime.ConverseStream_AnthropicClaude.supporting] |
| 69 | + |
| 70 | +// snippet-start:[rust.bedrock-runtime.ConverseStream_AnthropicClaude] |
| 71 | +#[tokio::main] |
| 72 | +async fn main() -> Result<(), BedrockConverseStreamError> { |
| 73 | + tracing_subscriber::fmt::init(); |
| 74 | + let sdk_config = aws_config::defaults(BehaviorVersion::latest()) |
| 75 | + .region(CLAUDE_REGION) |
| 76 | + .load() |
| 77 | + .await; |
| 78 | + let client = Client::new(&sdk_config); |
| 79 | + |
| 80 | + let response = client |
| 81 | + .converse_stream() |
| 82 | + .model_id(MODEL_ID) |
| 83 | + .messages( |
| 84 | + Message::builder() |
| 85 | + .role(ConversationRole::User) |
| 86 | + .content(ContentBlock::Text(USER_MESSAGE.to_string())) |
| 87 | + .build() |
| 88 | + .map_err(|_| "failed to build message")?, |
| 89 | + ) |
| 90 | + .send() |
| 91 | + .await; |
| 92 | + |
| 93 | + let mut stream = match response { |
| 94 | + Ok(output) => Ok(output.stream), |
| 95 | + Err(e) => Err(BedrockConverseStreamError::from( |
| 96 | + e.as_service_error().unwrap(), |
| 97 | + )), |
| 98 | + }?; |
| 99 | + |
| 100 | + loop { |
| 101 | + let token = stream.recv().await; |
| 102 | + match token { |
| 103 | + Ok(Some(text)) => { |
| 104 | + let next = get_converse_output_text(text)?; |
| 105 | + print!("{}", next); |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + Ok(None) => break, |
| 109 | + Err(e) => Err(e |
| 110 | + .as_service_error() |
| 111 | + .map(BedrockConverseStreamError::from) |
| 112 | + .unwrap_or(BedrockConverseStreamError( |
| 113 | + "Unknown error receiving stream".into(), |
| 114 | + ))), |
| 115 | + }? |
| 116 | + } |
| 117 | + |
| 118 | + println!(); |
| 119 | + |
| 120 | + Ok(()) |
| 121 | +} |
| 122 | + |
| 123 | +fn get_converse_output_text( |
| 124 | + output: ConverseStreamOutputType, |
| 125 | +) -> Result<String, BedrockConverseStreamError> { |
| 126 | + Ok(match output { |
| 127 | + ConverseStreamOutputType::ContentBlockDelta(event) => match event.delta() { |
| 128 | + Some(delta) => delta |
| 129 | + .as_text() |
| 130 | + .map(|s| s.clone()) |
| 131 | + .unwrap_or_else(|_| "".into()), |
| 132 | + None => "".into(), |
| 133 | + }, |
| 134 | + _ => "".into(), |
| 135 | + }) |
| 136 | +} |
| 137 | + |
| 138 | +// snippet-end:[rust.bedrock-runtime.ConverseStream_AnthropicClaude] |
0 commit comments