Expand description
§Valyu Rust SDK
Official Rust SDK for the Valyu AI API.
Valyu provides advanced AI-powered search capabilities, combining web search with proprietary data sources to deliver comprehensive, relevant results.
§Quick Start
Add this to your Cargo.toml:
[dependencies]
valyu = "0.1"
tokio = { version = "1", features = ["full"] }§Basic Usage
use valyu::ValyuClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client with your API key
let client = ValyuClient::new("your-api-key");
// Perform a simple search
let response = client.search("quantum computing").await?;
// Process results
if let Some(results) = &response.results {
for result in results {
println!("{}: {}",
result.title.as_deref().unwrap_or("Untitled"),
result.url.as_deref().unwrap_or("No URL")
);
}
}
Ok(())
}§Advanced Usage
Use the builder pattern for more control over search parameters:
use valyu::{ValyuClient, DeepSearchRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ValyuClient::new("your-api-key");
let request = DeepSearchRequest::new("artificial intelligence")
.with_max_results(10)
.with_search_type("web")
.with_fast_mode(true)
.with_response_length("medium")
.with_date_range("2024-01-01", "2024-12-31");
let response = client.deep_search(&request).await?;
println!("Transaction ID: {}", response.tx_id.as_deref().unwrap_or("N/A"));
println!("Cost: ${:.4}", response.total_deduction_dollars.unwrap_or(0.0));
Ok(())
}§Features
- Type-safe API: Full type coverage with serde support
- Async/await: Built on tokio and reqwest for async operations
- Builder pattern: Fluent interface for constructing requests
- Comprehensive error handling: Detailed error types for all failure cases
- Well documented: Extensive documentation and examples
§Error Handling
The SDK uses a custom ValyuError type for all errors:
use valyu::{ValyuClient, ValyuError};
#[tokio::main]
async fn main() {
let client = ValyuClient::new("your-api-key");
match client.search("test").await {
Ok(response) => println!("Success!"),
Err(ValyuError::InvalidApiKey) => eprintln!("Invalid API key"),
Err(ValyuError::RateLimitExceeded) => eprintln!("Rate limit exceeded"),
Err(e) => eprintln!("Error: {}", e),
}
}Structs§
- AiUsage
- AI usage statistics
- Answer
Cost - Cost breakdown for Answer API
- Answer
Request - Request parameters for the Valyu Answer API
- Answer
Response - Response from the Valyu Answer API
- Answer
Search Metadata - Search metadata for Answer API
- Answer
Search Result - Search result included in Answer response
- Content
Result - Individual content result from the Contents API
- Contents
Request - Request parameters for the Valyu Contents API
- Contents
Response - Response from the Valyu Contents API
- Deep
Search Request - Request parameters for the Valyu DeepSearch API
- Deep
Search Response - Response from the Valyu DeepSearch API
- Results
BySource - Breakdown of results by source type
- Search
Result - Individual search result from the Valyu API
- Valyu
Client - Client for interacting with the Valyu API
Enums§
- Response
Length - Response length configuration for Contents API
- Summary
Option - Summary configuration for Contents API
- Valyu
Error - Errors that can occur when using the Valyu SDK
Type Aliases§
- Result
- Result type alias for Valyu SDK operations