Skip to main content

Request

Struct Request 

Source
pub struct Request {
Show 16 fields pub provider: Provider, pub api_key: String, pub base_url: String, pub model: String, pub system_message: Option<String>, pub messages: Vec<Message>, pub reminder: Option<String>, pub tools: Vec<ToolDefinition>, pub tool_choice: Option<ToolChoice>, pub temperature: Option<f32>, pub max_tokens: Option<u32>, pub reasoning_effort: Option<ReasoningEffort>, pub response_format: Option<ResponseFormat>, pub extra_body: Map<String, Value>, pub max_retries: u32, pub retry_delay_ms: u64,
}
Expand description

A self-contained, provider-agnostic chat-completion request.

Carries everything needed to hit an LLM API: provider, credentials, model, messages, tools, and tuning parameters.

Call stream() or complete() with a shared reqwest::Client to send the request.

Fields§

§provider: Provider

Which provider to use.

§api_key: String

API key / token.

§base_url: String

Base URL override. If empty, uses Provider::default_base_url.

§model: String

Model identifier (e.g. "deepseek-chat", "gpt-4o").

§system_message: Option<String>

Optional system prompt.

§messages: Vec<Message>

Conversation history.

§reminder: Option<String>

Dynamic runtime context appended after the stable conversation prefix.

§tools: Vec<ToolDefinition>

Tools the model may call.

§tool_choice: Option<ToolChoice>

How the model should select tools.

§temperature: Option<f32>

Sampling temperature.

§max_tokens: Option<u32>

Maximum tokens to generate.

§reasoning_effort: Option<ReasoningEffort>

Reasoning effort hint. None means leave provider default in place; Some(ReasoningEffort::None) explicitly disables thinking on providers that support it.

§response_format: Option<ResponseFormat>

Constrain the output format.

§extra_body: Map<String, Value>

Arbitrary extra top-level JSON fields merged into the provider’s raw request body (e.g. prefix, thinking).

§max_retries: u32

Maximum retries for transient errors. Default: 3.

§retry_delay_ms: u64

Initial retry delay in milliseconds. Default: 1000.

Implementations§

Source§

impl Request

Source

pub fn new(provider: Provider, api_key: impl Into<String>) -> Self

Create a new request for the given provider and API key.

Sets sensible defaults: provider’s default base URL and model, 3 retries with 1 s initial delay, no system prompt.

Source

pub fn deepseek(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::DeepSeek, api_key).

Source

pub fn openai(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::OpenAI, api_key).

Source

pub fn anthropic(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Anthropic, api_key).

Source

pub fn gemini(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Gemini, api_key).

Source

pub fn kimi(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Kimi, api_key).

Source

pub fn glm(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Glm, api_key).

Source

pub fn minimax(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Minimax, api_key).

Source

pub fn mimo(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Mimo, api_key).

Source

pub fn grok(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::Grok, api_key).

Source

pub fn openrouter(api_key: impl Into<String>) -> Self

Shortcut for Request::new(Provider::OpenRouter, api_key).

Source

pub fn base_url(self, url: impl Into<String>) -> Self

Override the base URL.

Source

pub fn model(self, m: impl Into<String>) -> Self

Set the model.

Source

pub fn system_prompt(self, p: impl Into<String>) -> Self

Set the system prompt.

Source

pub fn reminder(self, p: impl Into<String>) -> Self

Set dynamic runtime context for this request only.

Source

pub fn message(self, m: Message) -> Self

Append a message to the conversation.

Source

pub fn user(self, text: impl Into<String>) -> Self

Append a user text message (convenience).

Source

pub fn messages(self, msgs: Vec<Message>) -> Self

Set the full message history.

Source

pub fn tools(self, tools: Vec<ToolDefinition>) -> Self

Set the tool definitions.

Source

pub fn temperature(self, t: f32) -> Self

Set the temperature.

Source

pub fn max_tokens(self, n: u32) -> Self

Set the max tokens.

Source

pub fn reasoning_effort(self, e: ReasoningEffort) -> Self

Set the reasoning-effort hint. ReasoningEffort::None explicitly disables thinking on providers that support the toggle.

Source

pub fn text(self) -> Self

Set the response format to plain text (the default).

Source

pub fn json_schema( self, name: impl Into<String>, schema: Value, strict: bool, ) -> Self

Constrain output to a named JSON Schema (OpenAI json_schema mode).

Use schemars::schema_for!(T) to generate the schema:

let schema = serde_json::to_value(schemars::schema_for!(MyStruct)).unwrap();
let req = Request::openai(key).json_schema("my_struct", schema, true);
Source

pub fn json(self) -> Self

Set the response format to JSON object mode.

The model will be constrained to emit a valid JSON object. You must also instruct the model to produce JSON in your system prompt or user message — the format flag alone is not sufficient for most providers.

Source

pub fn retries(self, max: u32, initial_delay_ms: u64) -> Self

Set retry parameters.

Source

pub fn extra_body(self, extra: Map<String, Value>) -> Self

Merge extra JSON fields into the request body.

Source

pub fn effective_base_url(&self) -> &str

Resolve the effective base URL (custom or provider default).

Source

pub async fn stream( &self, http: &Client, ) -> Result<BoxStream<'static, LlmEvent>, ApiError>

Send a streaming request and return a stream of LlmEvents.

Source

pub async fn complete( &self, http: &Client, ) -> Result<CompleteResponse, ApiError>

Send a non-streaming request and return the complete response.

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more