pub struct LlmResponse<T> { /* private fields */ }Expand description
LLM-optimized response wrapper with token counting.
This wrapper automatically:
- Serializes to the requested format (JSON or TOON)
- Calculates estimated token counts for both formats
- Adds informative headers about token usage
§Token Estimation
Token counts are estimated using a simple heuristic:
- ~4 characters per token (GPT-3/4 average)
For more accurate counts, use a proper tokenizer.
§Example
ⓘ
use rustapi_rs::prelude::*;
use rustapi_rs::toon::{LlmResponse, AcceptHeader, OutputFormat};
#[derive(Serialize)]
struct ApiData {
items: Vec<Item>,
}
// With content negotiation
async fn get_items(accept: AcceptHeader) -> LlmResponse<ApiData> {
let data = ApiData { items: vec![...] };
LlmResponse::new(data, accept.preferred)
}
// Always TOON format
async fn get_items_toon() -> LlmResponse<ApiData> {
let data = ApiData { items: vec![...] };
LlmResponse::toon(data)
}Implementations§
Source§impl<T> LlmResponse<T>
impl<T> LlmResponse<T>
Sourcepub fn new(data: T, format: OutputFormat) -> Self
pub fn new(data: T, format: OutputFormat) -> Self
Create a new LLM response with the specified format.
Sourcepub fn without_token_headers(self) -> Self
pub fn without_token_headers(self) -> Self
Disable token counting headers.
Sourcepub fn with_token_headers(self) -> Self
pub fn with_token_headers(self) -> Self
Enable token counting headers (default).
Trait Implementations§
Source§impl<T: Clone> Clone for LlmResponse<T>
impl<T: Clone> Clone for LlmResponse<T>
Source§fn clone(&self) -> LlmResponse<T>
fn clone(&self) -> LlmResponse<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for LlmResponse<T>
impl<T: Debug> Debug for LlmResponse<T>
Source§impl<T: Serialize> IntoResponse for LlmResponse<T>
impl<T: Serialize> IntoResponse for LlmResponse<T>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Convert self into a Response
Source§impl<T: Send> OperationModifier for LlmResponse<T>
impl<T: Send> OperationModifier for LlmResponse<T>
Source§fn update_operation(_op: &mut Operation)
fn update_operation(_op: &mut Operation)
Update the operation
Source§impl<T: Serialize> ResponseModifier for LlmResponse<T>
impl<T: Serialize> ResponseModifier for LlmResponse<T>
Source§fn update_response(op: &mut Operation)
fn update_response(op: &mut Operation)
Update the operation with response information
Auto Trait Implementations§
impl<T> Freeze for LlmResponse<T>where
T: Freeze,
impl<T> RefUnwindSafe for LlmResponse<T>where
T: RefUnwindSafe,
impl<T> Send for LlmResponse<T>where
T: Send,
impl<T> Sync for LlmResponse<T>where
T: Sync,
impl<T> Unpin for LlmResponse<T>where
T: Unpin,
impl<T> UnwindSafe for LlmResponse<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more