pub struct OpenAIChat { /* private fields */ }Expand description
OpenAI chat client for GPT models.
Implementations§
Source§impl OpenAIChat
impl OpenAIChat
Sourcepub fn new(config: OpenAIConfig) -> Self
pub fn new(config: OpenAIConfig) -> Self
Creates a new OpenAIChat with the given configuration.
Sourcepub fn from_env_result() -> Result<Self, OpenAIError>
pub fn from_env_result() -> Result<Self, OpenAIError>
Creates an OpenAIChat from environment variables, returning a Result.
Sourcepub fn bind_tools(&self, tools: Vec<ToolDefinition>) -> Self
pub fn bind_tools(&self, tools: Vec<ToolDefinition>) -> Self
Binds tool definitions for function calling.
Sourcepub fn with_tool_choice(self, choice: impl Into<String>) -> Self
pub fn with_tool_choice(self, choice: impl Into<String>) -> Self
Sets the tool choice strategy.
Sourcepub fn with_structured_output<T: DeserializeOwned + JsonSchema>(
&self,
) -> StructuredOutputMethod<T>
pub fn with_structured_output<T: DeserializeOwned + JsonSchema>( &self, ) -> StructuredOutputMethod<T>
Enables structured JSON output with schema validation.
Sourcepub fn with_json_schema_output<T: DeserializeOwned + JsonSchema>(
&self,
) -> StructuredOutputMethod<T>
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.
impl OpenAIChat
OpenAI multimodal extensions.
These methods are on OpenAIChat directly (not through the trait)
because they require provider-specific parameters (voice, size, etc.).
Sourcepub async fn whisper_transcribe(
&self,
audio: AudioContent,
) -> Result<String, MultimodalError>
pub async fn whisper_transcribe( &self, audio: AudioContent, ) -> Result<String, MultimodalError>
Transcribes audio using Whisper.
Sends audio to the /v1/audio/transcriptions endpoint.
Sourcepub async fn tts_generate(
&self,
text: &str,
voice: TtsVoice,
) -> Result<Vec<u8>, MultimodalError>
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.
Sourcepub async fn dalle_generate(
&self,
prompt: &str,
size: DallEImageSize,
) -> Result<ImageContent, MultimodalError>
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
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,
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,
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,
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,
Source§fn bind_tools(
&self,
tools: Vec<ToolDefinition>,
) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>
fn bind_tools( &self, tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>
Source§impl BaseLanguageModel<Vec<Message>, LLMResult> for OpenAIChat
impl BaseLanguageModel<Vec<Message>, LLMResult> for OpenAIChat
Source§fn model_name(&self) -> &str
fn model_name(&self) -> &str
Source§fn temperature(&self) -> Option<f32>
fn temperature(&self) -> Option<f32>
Source§fn max_tokens(&self) -> Option<usize>
fn max_tokens(&self) -> Option<usize>
Source§fn with_temperature(self, temp: f32) -> Self
fn with_temperature(self, temp: f32) -> Self
Source§fn with_max_tokens(self, max: usize) -> Self
fn with_max_tokens(self, max: usize) -> Self
Source§impl Clone for OpenAIChat
impl Clone for OpenAIChat
Source§fn clone(&self) -> OpenAIChat
fn clone(&self) -> OpenAIChat
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl MultimodalModel for OpenAIChat
Implement MultimodalModel trait for OpenAIChat.
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,
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,
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,
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,
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,
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,
Source§impl Runnable<Vec<Message>, LLMResult> for OpenAIChat
impl Runnable<Vec<Message>, LLMResult> for OpenAIChat
Source§type Error = OpenAIError
type Error = OpenAIError
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
Auto Trait Implementations§
impl !RefUnwindSafe for OpenAIChat
impl !UnwindSafe for OpenAIChat
impl Freeze for OpenAIChat
impl Send for OpenAIChat
impl Sync for OpenAIChat
impl Unpin for OpenAIChat
impl UnsafeUnpin for OpenAIChat
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
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<I, O, R> RunnableExt<I, O> for R
impl<I, O, R> RunnableExt<I, O> for R
Source§fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
Source§fn into_sequence(self) -> RunnableSequence<Input, Output>
fn into_sequence(self) -> RunnableSequence<Input, Output>
RunnableSequence from this runnable as a single step. Read moreSource§fn with_fallbacks<R>(
self,
fallbacks: Vec<R>,
) -> RunnableWithFallbacks<Input, Output>
fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
Source§fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>where
Input: Clone,
fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>where
Input: Clone,
Source§fn configurable_alternatives<K, R>(
self,
which: impl Into<String>,
default_key: impl Into<String>,
alternatives: Vec<(K, R)>,
) -> RunnableConfigurable<Input, Output>
fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
Source§fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>
config.configurable (Python’s Runnable.configurable_fields). Read moreSource§impl<M> StreamingStructuredOutputExt for Mwhere
M: BaseChatModel,
impl<M> StreamingStructuredOutputExt for Mwhere
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>>
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>>
Source§impl<M> StructuredOutputExt for Mwhere
M: BaseChatModel,
impl<M> StructuredOutputExt for Mwhere
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,
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,
T. Read more