use langdb_clust::messages::{
CacheControl, CacheTtl, ClaudeModel, ContentBlock, MaxTokens, Message,
MessagesRequestBody, Role, SystemPrompt, TextContentBlock,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let api_key = std::env::var("ANTHROPIC_API_KEY")
.expect("ANTHROPIC_API_KEY environment variable must be set");
let client = langdb_clust::Client::from_api_key(langdb_clust::ApiKey::new(&api_key));
let system_prompt = SystemPrompt::from_text_blocks_with_cache_control(vec![
("You are a helpful assistant.", None), (
"You have access to the following information that should be cached for 1 hour: The weather in New York is currently sunny with a temperature of 72°F.",
Some(CacheControl {
_type: langdb_clust::messages::CacheControlType::Ephemeral,
ttl: Some(CacheTtl::OneHour),
}),
),
]);
let message = Message {
role: Role::User,
content: vec![ContentBlock::Text(
TextContentBlock::new_with_cache_control(
"What's the weather like in New York?",
CacheControl {
_type: langdb_clust::messages::CacheControlType::Ephemeral,
ttl: Some(CacheTtl::OneHour),
},
),
)],
};
let model = ClaudeModel::Claude3Sonnet20240229;
let max_tokens = MaxTokens::new(1024, model)?;
let request_body = MessagesRequestBody {
model,
max_tokens,
messages: vec![message],
system: Some(system_prompt),
..Default::default()
};
println!("Sending request with 1-hour cache control...");
let response = client.create_a_message(request_body).await?;
println!("Response: {}", response.content[0].text());
println!("Usage: {:?}", response.usage);
if let Some(cache_creation) = response.usage.cache_creation {
println!("Cache creation details:");
println!(" 5m cache input tokens: {}", cache_creation.ephemeral_5m_input_tokens);
println!(" 1h cache input tokens: {}", cache_creation.ephemeral_1h_input_tokens);
}
Ok(())
}