pub struct ExtractBuilder<'a, T> { /* private fields */ }Expand description
Builder for configuring an extraction request.
Created by Client::extract. Call .await to execute the request.
Implementations§
Source§impl<T> ExtractBuilder<'_, T>where
T: DeserializeOwned + JsonSchema,
impl<T> ExtractBuilder<'_, T>where
T: DeserializeOwned + JsonSchema,
Sourcepub fn system(self, system: impl Into<String>) -> Self
pub fn system(self, system: impl Into<String>) -> Self
Override the system prompt for this request.
Sourcepub fn temperature(self, temp: f64) -> Self
pub fn temperature(self, temp: f64) -> Self
Set the temperature (0.0 = deterministic, 1.0 = creative).
Sourcepub fn max_tokens(self, tokens: u32) -> Self
pub fn max_tokens(self, tokens: u32) -> Self
Set the maximum output tokens.
Sourcepub fn max_retries(self, retries: u32) -> Self
pub fn max_retries(self, retries: u32) -> Self
Set the maximum retry attempts on parse/validation failure.
Sourcepub fn messages(self, msgs: Vec<Message>) -> Self
pub fn messages(self, msgs: Vec<Message>) -> Self
Set prior message history for multi-turn conversations.
Messages are prepended before the extraction prompt.
use instructors::Message;
#[derive(Deserialize, JsonSchema)]
struct Summary { text: String }
let client = instructors::Client::openai("sk-...");
let result = client.extract::<Summary>("summarize the above")
.messages(vec![
Message::user("Here is a long document..."),
Message::assistant("I see the document."),
])
.await?;Sourcepub fn on_request<F>(self, f: F) -> Self
pub fn on_request<F>(self, f: F) -> Self
Register a hook called before each API request.
Receives (model, prompt).
Sourcepub fn on_response<F>(self, f: F) -> Self
pub fn on_response<F>(self, f: F) -> Self
Register a hook called after a successful extraction.
Receives the final Usage.
Sourcepub fn validate<F>(self, f: F) -> Self
pub fn validate<F>(self, f: F) -> Self
Add a custom validation function. If validation fails, the error message is fed back to the LLM and the request is retried.
#[derive(Deserialize, JsonSchema)]
struct User { name: String, age: u32 }
let client = instructors::Client::openai("sk-...");
let user: User = client.extract("...")
.validate(|u: &User| {
if u.age > 150 { Err("age must be <= 150".into()) } else { Ok(()) }
})
.await?.value;Source§impl<T> ExtractBuilder<'_, T>
impl<T> ExtractBuilder<'_, T>
Sourcepub fn validated(self) -> Self
pub fn validated(self) -> Self
Enable trait-based validation. Calls T::validate() after deserialization.
If validation fails, the error is fed back to the LLM for retry.
Requires T: Validate.
#[derive(Deserialize, JsonSchema)]
struct Email { address: String }
impl Validate for Email {
fn validate(&self) -> Result<(), ValidationError> {
if self.address.contains('@') { Ok(()) }
else { Err("invalid email".into()) }
}
}
let client = instructors::Client::openai("sk-...");
let email: Email = client.extract("...").validated().await?.value;