Ir al contenido

Características avanzadas (Rust)

Más allá de la funcionalidad principal de chat, ai-lib-rust proporciona varias capacidades avanzadas.

Genere y trabaje con embeddings vectoriales:

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}");

Las operaciones vectoriales incluyen similitud coseno, distancia euclidiana y producto escalar.

Almacene en caché las respuestas para reducir costos y latencia:

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?;

Ejecute múltiples solicitudes eficientemente:

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}"),
}
}

Estime el uso de tokens y costos:

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}");

Extienda el cliente con plugins personalizados:

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);

Filtrado de contenido y seguridad:

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

Enrutamiento inteligente de modelos (habilitar con característica routing_mvp):

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);

Con puerta de características: Interceptors

Sección titulada «Con puerta de características: Interceptors»

Interceptación de solicitudes/respuestas (habilitar con característica interceptors):

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