Skip to main content

OpenAIChat

Struct OpenAIChat 

Source
pub struct OpenAIChat { /* private fields */ }
Expand description

OpenAI chat client for GPT models.

Implementations§

Source§

impl OpenAIChat

Source

pub fn new(config: OpenAIConfig) -> Self

Creates a new OpenAIChat with the given configuration.

Source

pub fn from_env_result() -> Result<Self, OpenAIError>

Creates an OpenAIChat from environment variables, returning a Result.

Source

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

Binds tool definitions for function calling.

Source

pub fn with_tool_choice(self, choice: impl Into<String>) -> Self

Sets the tool choice strategy.

Source

pub fn with_structured_output<T: DeserializeOwned + JsonSchema>( &self, ) -> StructuredOutputMethod<T>

Enables structured JSON output with schema validation.

Source

pub fn with_json_schema_output<T: DeserializeOwned + JsonSchema>( &self, ) -> StructuredOutputMethod<T>

0.21.0 S3.1: enables engine-constrained structured output via the response_format: { type: "json_schema", ... } request field (OpenAI strict mode).

The schema is generated from T via schemars and normalized for strict mode (additionalProperties: false, all properties required — see crate::openai::response_format::make_strict_schema). The engine guarantees schema-valid JSON, so the returned method parses the message content directly; no tool-binding round trip is involved.

Unlike Self::with_structured_output (tool-based, works on any OpenAI-compatible backend), this path requires provider-side json_schema support; unsupported providers return a 4xx error.

Source§

impl OpenAIChat

OpenAI multimodal extensions.

These methods are on OpenAIChat directly (not through the trait) because they require provider-specific parameters (voice, size, etc.).

Source

pub async fn whisper_transcribe( &self, audio: AudioContent, ) -> Result<String, MultimodalError>

Transcribes audio using Whisper.

Sends audio to the /v1/audio/transcriptions endpoint.

Source

pub async fn tts_generate( &self, text: &str, voice: TtsVoice, ) -> Result<Vec<u8>, MultimodalError>

Generates speech using OpenAI TTS.

Sends text to the /v1/audio/speech endpoint and returns raw audio bytes.

Source

pub async fn dalle_generate( &self, prompt: &str, size: DallEImageSize, ) -> Result<ImageContent, MultimodalError>

Generates an image using DALL-E.

Sends a prompt to the /v1/images/generations endpoint.

Trait Implementations§

Source§

impl BaseChatModel for OpenAIChat

Source§

fn chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Chat with the model. Read more
Source§

fn stream_chat<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream chat with the model. Read more
Source§

fn bind_tools( &self, tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>

Bind tool definitions for function calling. Read more
Source§

fn chat_with_system<'life0, 'async_trait>( &'life0 self, system: String, messages: Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: Sync + 'async_trait,

Chat with system prompt. Read more
Source§

impl BaseLanguageModel<Vec<Message>, LLMResult> for OpenAIChat

Source§

fn model_name(&self) -> &str

Returns the model name.
Source§

fn get_num_tokens(&self, text: &str) -> usize

Calculates token count for text. Read more
Source§

fn temperature(&self) -> Option<f32>

Returns the temperature parameter.
Source§

fn max_tokens(&self) -> Option<usize>

Returns the max tokens limit.
Source§

fn with_temperature(self, temp: f32) -> Self

Sets the temperature parameter.
Source§

fn with_max_tokens(self, max: usize) -> Self

Sets the max tokens limit.
Source§

impl Clone for OpenAIChat

Source§

fn clone(&self) -> OpenAIChat

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 MultimodalModel for OpenAIChat

Implement MultimodalModel trait for OpenAIChat.

Source§

fn transcribe<'life0, 'async_trait>( &'life0 self, audio: AudioContent, ) -> Pin<Box<dyn Future<Output = Result<String, MultimodalError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Transcribes audio to text (Speech-to-Text). Read more
Source§

fn generate_speech<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, MultimodalError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Generates audio from text (Text-to-Speech). Read more
Source§

fn generate_image<'life0, 'life1, 'async_trait>( &'life0 self, prompt: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<ImageContent, MultimodalError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Generates an image from a text prompt. Read more
Source§

impl Runnable<Vec<Message>, LLMResult> for OpenAIChat

Source§

type Error = OpenAIError

Error type.
Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, _config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Transforms single input to output. Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, input: Vec<Message>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<LLMResult, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Streaming output - for real-time responses (LLM, etc). Read more
Source§

fn batch<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Batch processing - transforms multiple inputs to outputs. Read more
Source§

fn batch_as_completed<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Batch processing that returns results in completion order. Read more
Source§

fn transform<'life0, 'async_trait>( &'life0 self, input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + 'life0>>, Self::Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Stream-to-stream transformation - the core of LCEL streaming. 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: Sized + 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: Sized + 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<I, O, R> RunnableExt<I, O> for R
where I: Send + Sync + 'static, O: Send + Sync + 'static, R: Runnable<I, O> + 'static, <R as Runnable<I, O>>::Error: Into<LcelError>,

Source§

fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
where O2: Send + Sync + 'static, R2: Runnable<Output, O2> + Send + Sync + 'static, <R2 as Runnable<Output, O2>>::Error: Into<LcelError>,

Pipe the output of this runnable into another runnable. Read more
Source§

fn into_sequence(self) -> RunnableSequence<Input, Output>

Create a RunnableSequence from this runnable as a single step. Read more
Source§

fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
where Input: Clone, R: Runnable<Input, Output> + Send + Sync + 'static, <R as Runnable<Input, Output>>::Error: Into<LcelError>,

Add fallback runnables that are tried if this one fails. Read more
Source§

fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>
where Input: Clone,

Wrap this runnable with retry logic using exponential backoff. Read more
Source§

fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
where K: Into<String>, R: Runnable<Input, Output> + Send + Sync + 'static, <R as Runnable<Input, Output>>::Error: Into<LcelError>,

Route between a default runnable and named alternatives at invoke time. Read more
Source§

fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>

Override recognized config fields at invoke time from config.configurable (Python’s Runnable.configurable_fields). Read more
Source§

impl<M> StreamingStructuredOutputExt for M
where M: BaseChatModel,

Source§

fn stream_structured_output<'life0, 'life1, 'async_trait, T>( &'life0 self, schema: Value, prompt: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<T, StructuredOutputError>> + Send>>, StructuredOutputError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: DeserializeOwned + Serialize + Clone + PartialEq + Unpin + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait,

Stream structured output from a chat model. Read more
Source§

impl<M> StructuredOutputExt for M
where M: BaseChatModel,

Source§

fn with_structured_output<'life0, 'life1, 'async_trait, T>( &'life0 self, schema: Value, prompt: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<T, StructuredOutputError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait + DeserializeOwned + Serialize + Send + Sync + 'static, Self: Sync + 'async_trait,

Call the LLM with a JSON schema and prompt, returning a parsed result of type T. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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