高度な機能(Rust)
コアチャット機能に加え、ai-lib-rust はいくつかの高度な機能を提供します。
ベクトル埋め込みの生成と操作:
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}");ベクトル操作にはコサイン類似度、ユークリッド距離、ドット積が含まれます。
レスポンスキャッシュ
Section titled “レスポンスキャッシュ”コストとレイテンシを削減するためのレスポンスキャッシュ:
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?;
// 最初の呼び出しはプロバイダーにヒットlet r1 = client.chat().user("What is 2+2?").execute().await?;
// 2 回目の同一呼び出しはキャッシュから返却let r2 = client.chat().user("What is 2+2?").execute().await?;複数リクエストを効率的に実行:
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}"), }}トークンカウント
Section titled “トークンカウント”トークン使用量とコストの見積もり:
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}");プラグインシステム
Section titled “プラグインシステム”カスタムプラグインでクライアントを拡張:
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);ガードレール
Section titled “ガードレール”コンテンツフィルタリングとセーフティ:
use ai_lib::guardrails::{GuardrailsConfig, KeywordFilter};
let config = GuardrailsConfig::new() .add_filter(KeywordFilter::new(vec!["unsafe_word"])) .enable_pii_detection();フィーチャーゲート:ルーティング
Section titled “フィーチャーゲート:ルーティング”スマートモデルルーティング(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);フィーチャーゲート:インターセプター
Section titled “フィーチャーゲート:インターセプター”リクエスト/レスポンスのインターセプション(interceptors フィーチャーで有効化):
use ai_lib::interceptors::{InterceptorPipeline, Interceptor};
let pipeline = InterceptorPipeline::new() .add(LoggingInterceptor) .add(MetricsInterceptor) .add(AuditInterceptor);