use url_preview::{
LLMExtractor, LLMExtractorConfig, OpenAIProvider, Fetcher,
PreviewError, ContentFormat
};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use std::sync::Arc;
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct SimpleArticleInfo {
title: String,
summary: String,
topics: Vec<String>,
}
#[tokio::main]
async fn main() -> Result<(), PreviewError> {
println!("🧪 Claude API Test (Fixed Version)\n");
match reqwest::get("http://localhost:8080/health").await {
Ok(resp) if resp.status().is_success() => {
println!("✅ Connected to claude-code-api\n");
}
_ => {
println!("⚠️ Please start claude-code-api with extended timeout:");
println!(" CLAUDE_CODE__CLAUDE__TIMEOUT_SECONDS=120 \\");
println!(" CLAUDE_CODE__FILE_ACCESS__SKIP_PERMISSIONS=true \\");
println!(" RUST_LOG=info claude-code-api\n");
return Ok(());
}
}
let config = async_openai::config::OpenAIConfig::new()
.with_api_base("http://localhost:8080/v1")
.with_api_key("not-needed");
let provider = Arc::new(
OpenAIProvider::from_config(config, "claude-3-haiku-20240307".to_string())
);
let extractor_config = LLMExtractorConfig {
format: ContentFormat::Text, clean_html: true,
max_content_length: 10_000, model_params: Default::default(),
};
let extractor = LLMExtractor::with_config(provider, extractor_config);
let fetcher = Fetcher::new();
let test_url = "https://www.rust-lang.org/";
println!("Testing with: {}\n", test_url);
println!("⏳ Extracting (this may take 10-30 seconds)...\n");
match extractor.extract::<SimpleArticleInfo>(test_url, &fetcher).await {
Ok(result) => {
println!("✅ Extraction successful!\n");
println!("Title: {}", result.data.title);
println!("Summary: {}", result.data.summary);
println!("\nTopics:");
for topic in &result.data.topics {
println!(" • {}", topic);
}
if let Some(usage) = result.usage {
println!("\nToken Usage: {} + {} = {}",
usage.prompt_tokens, usage.completion_tokens, usage.total_tokens);
}
}
Err(e) => {
println!("❌ Extraction failed: {}", e);
println!("\n💡 Troubleshooting tips:");
println!("1. Increase timeout: CLAUDE_CODE__CLAUDE__TIMEOUT_SECONDS=300");
println!("2. Use simpler schema with fewer fields");
println!("3. Use claude-3-haiku model for faster responses");
println!("4. Reduce max_content_length in extractor config");
println!("5. Check claude-code-api logs for detailed errors");
}
}
Ok(())
}