Expand description
Axum integration for Cognate.
§Features
CognateProvider— an Axum extractor that pulls anArc<dyn Provider>out of shared application state viaaxum::extract::FromRef.UsageLayer/UsageHandle— Tower middleware that accumulates token usage across all requests.into_sse— convert aBoxStream<Result<Chunk>>directly into an AxumSseresponse.
§Wiring up the provider
use axum::{Router, routing::post};
use cognate_axum::CognateProvider;
use cognate_core::{Provider, Request};
use std::sync::Arc;
#[derive(Clone)]
struct AppState {
provider: Arc<dyn Provider>,
}
impl axum::extract::FromRef<AppState> for Arc<dyn Provider> {
fn from_ref(state: &AppState) -> Self {
state.provider.clone()
}
}
async fn chat(
CognateProvider(provider): CognateProvider,
axum::Json(req): axum::Json<Request>,
) -> String {
provider.complete(req).await.unwrap().content().to_string()
}
#[tokio::main]
async fn main() {
let provider: Arc<dyn Provider> = unimplemented!();
let _app: Router = Router::new()
.route("/chat", post(chat))
.with_state(AppState { provider });
}Structs§
- Cognate
Provider - Axum extractor that resolves an
Arc<dyn Provider>from shared application state. - Usage
Handle - A handle for reading token usage accumulated by
UsageLayer. - Usage
Layer - Tower [
Layer] that wraps aProviderand records token usage.