|
| 1 | +//! Live Bedrock Anthropic adaptive-thinking regression tests. |
| 2 | +
|
| 3 | +use futures::StreamExt; |
| 4 | +use rig::agent::AgentBuilder; |
| 5 | +use rig::client::CompletionClient; |
| 6 | +use rig::completion::{CompletionModel as _, Prompt}; |
| 7 | +use rig::streaming::StreamedAssistantContent; |
| 8 | +use serde_json::json; |
| 9 | + |
| 10 | +use super::{ |
| 11 | + anthropic_adaptive_model, anthropic_signature_only_model, client, |
| 12 | + support::{ALPHA_SIGNAL_OUTPUT, AlphaSignal, assert_contains_all_case_insensitive}, |
| 13 | +}; |
| 14 | + |
| 15 | +fn adaptive_thinking_params() -> serde_json::Value { |
| 16 | + json!({ |
| 17 | + "thinking": { |
| 18 | + "type": "adaptive" |
| 19 | + } |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +#[tokio::test] |
| 24 | +#[ignore = "requires AWS credentials and Bedrock Anthropic adaptive-thinking model access"] |
| 25 | +async fn adaptive_thinking_prompt_caching_tool_roundtrip_regression() { |
| 26 | + let model = client() |
| 27 | + .completion_model(anthropic_adaptive_model()) |
| 28 | + .with_prompt_caching(); |
| 29 | + let agent = AgentBuilder::new(model) |
| 30 | + .preamble( |
| 31 | + "You must call tools when the user asks for their result. \ |
| 32 | + After a tool result is available, answer with the exact result.", |
| 33 | + ) |
| 34 | + .max_tokens(2048) |
| 35 | + .additional_params(adaptive_thinking_params()) |
| 36 | + .tool(AlphaSignal) |
| 37 | + .build(); |
| 38 | + |
| 39 | + let response = agent |
| 40 | + .prompt("Call `lookup_harbor_label` exactly once, then answer with the exact tool output.") |
| 41 | + .await |
| 42 | + .expect("adaptive-thinking prompt-caching tool roundtrip should succeed"); |
| 43 | + |
| 44 | + assert_contains_all_case_insensitive(&response, &[ALPHA_SIGNAL_OUTPUT]); |
| 45 | +} |
| 46 | + |
| 47 | +#[tokio::test] |
| 48 | +#[ignore = "requires AWS credentials and Bedrock Anthropic adaptive-thinking model access"] |
| 49 | +async fn streaming_emits_signature_only_adaptive_reasoning_regression() { |
| 50 | + let model = client().completion_model(anthropic_signature_only_model()); |
| 51 | + let request = model |
| 52 | + .completion_request("What is 2 + 2? Answer with only the number.") |
| 53 | + .max_tokens(2048) |
| 54 | + .additional_params(adaptive_thinking_params()) |
| 55 | + .build(); |
| 56 | + let mut stream = model |
| 57 | + .stream(request) |
| 58 | + .await |
| 59 | + .expect("adaptive-thinking Bedrock stream should start"); |
| 60 | + |
| 61 | + let mut reasoning_chunks = 0; |
| 62 | + let mut signature_chunks = 0; |
| 63 | + let mut signature_only_chunks = 0; |
| 64 | + let mut got_final = false; |
| 65 | + |
| 66 | + while let Some(item) = stream.next().await { |
| 67 | + match item.expect("adaptive-thinking Bedrock stream item should succeed") { |
| 68 | + StreamedAssistantContent::Reasoning(reasoning) => { |
| 69 | + reasoning_chunks += 1; |
| 70 | + if reasoning.first_signature().is_some() { |
| 71 | + signature_chunks += 1; |
| 72 | + if reasoning.display_text().is_empty() { |
| 73 | + signature_only_chunks += 1; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + StreamedAssistantContent::Final(_) => got_final = true, |
| 78 | + _ => {} |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + assert!(got_final, "stream should emit a final response"); |
| 83 | + assert!( |
| 84 | + reasoning_chunks > 0, |
| 85 | + "expected at least one adaptive-thinking reasoning chunk" |
| 86 | + ); |
| 87 | + assert!( |
| 88 | + signature_chunks > 0, |
| 89 | + "expected adaptive-thinking reasoning to include a Bedrock signature" |
| 90 | + ); |
| 91 | + assert!( |
| 92 | + signature_only_chunks > 0, |
| 93 | + "expected at least one signature-only reasoning chunk" |
| 94 | + ); |
| 95 | +} |
0 commit comments