Skip to content

Advanced Features (Rust)

Beyond core chat functionality, ai-lib-rust provides several advanced capabilities.

Generate and work with vector embeddings:

use ai_lib::embeddings::{EmbeddingClient, cosine_similarity};
let client = EmbeddingClient::builder()
.model("openai/text-embedding-3-small")
.build()
.await?;
let embeddings = client.embed(vec![
"Rust programming language",
"Python programming language",
"Cooking recipes",
]).await?;
let sim = cosine_similarity(&embeddings[0], &embeddings[1]);
println!("Rust vs Python similarity: {sim:.3}");

Vector operations include cosine similarity, Euclidean distance, and dot product.

Cache responses to reduce costs and latency:

use ai_lib::cache::{CacheManager, MemoryCache};
let cache = CacheManager::new(MemoryCache::new())
.with_ttl(Duration::from_secs(3600));
let client = AiClient::builder()
.model("openai/gpt-4o")
.cache(cache)
.build()
.await?;
// First call hits the provider
let r1 = client.chat().user("What is 2+2?").execute().await?;
// Second identical call returns cached response
let r2 = client.chat().user("What is 2+2?").execute().await?;

Execute multiple requests efficiently:

use ai_lib::batch::{BatchCollector, BatchExecutor};
let mut collector = BatchCollector::new();
collector.add(client.chat().user("Question 1"));
collector.add(client.chat().user("Question 2"));
collector.add(client.chat().user("Question 3"));
let executor = BatchExecutor::new()
.concurrency(5)
.timeout(Duration::from_secs(30));
let results = executor.execute(collector).await;
for result in results {
match result {
Ok(response) => println!("{}", response.content),
Err(e) => eprintln!("Error: {e}"),
}
}

Estimate token usage and costs:

use ai_lib::tokens::{TokenCounter, ModelPricing};
let counter = TokenCounter::for_model("gpt-4o");
let count = counter.count("Hello, how are you?");
println!("Tokens: {count}");
let pricing = ModelPricing::from_registry("openai/gpt-4o")?;
let cost = pricing.estimate(prompt_tokens, completion_tokens);
println!("Estimated cost: ${cost:.4}");

Extend the client with custom plugins:

use ai_lib::plugins::{Plugin, PluginRegistry};
struct LoggingPlugin;
impl Plugin for LoggingPlugin {
fn name(&self) -> &str { "logging" }
fn on_request(&self, request: &mut Request) {
tracing::info!("Sending request to {}", request.model);
}
fn on_response(&self, response: &Response) {
tracing::info!("Got {} tokens", response.usage.total_tokens);
}
}
let mut registry = PluginRegistry::new();
registry.register(LoggingPlugin);

Content filtering and safety:

use ai_lib::guardrails::{GuardrailsConfig, KeywordFilter};
let config = GuardrailsConfig::new()
.add_filter(KeywordFilter::new(vec!["unsafe_word"]))
.enable_pii_detection();

Smart model routing (enable with routing_mvp feature):

use ai_lib::routing::{CustomModelManager, ModelArray, ModelSelectionStrategy};
let manager = CustomModelManager::new()
.add_model("openai/gpt-4o", weight: 0.7)
.add_model("anthropic/claude-3-5-sonnet", weight: 0.3)
.strategy(ModelSelectionStrategy::Weighted);

Request/response interception (enable with interceptors feature):

use ai_lib::interceptors::{InterceptorPipeline, Interceptor};
let pipeline = InterceptorPipeline::new()
.add(LoggingInterceptor)
.add(MetricsInterceptor)
.add(AuditInterceptor);