OpenAIChatModel

Struct OpenAIChatModel 

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

OpenAI chat completion implementation of the language model interface.

This struct provides support for OpenAI’s chat completion API, enabling text generation, streaming, tool calling, and multi-modal capabilities. It handles conversion between the unified SDK format and OpenAI’s API specifications, including model-specific behaviors for reasoning models, search preview models, and flex processing.

§Supported Features

  • Text generation and streaming responses
  • Function calling and tool use
  • Multi-modal inputs (images, audio)
  • JSON schema-based structured outputs
  • Vision capabilities for supported models
  • Reasoning effort customization for o1/o3 models
  • Logit bias and probability tracking
  • Token usage tracking including cached and reasoning tokens

§OpenAI-Specific Behaviors

  • System messages are converted to developer messages for reasoning models (o1, o3)
  • Temperature configuration is ignored for search preview models with a warning
  • Reasoning models use max_completion_tokens instead of max_tokens
  • Flex processing service tier validation for supported models
  • Streaming includes usage information in the final chunk

Implementations§

Source§

impl OpenAIChatModel

Source

pub fn new(model_id: impl Into<String>, config: impl Into<OpenAIConfig>) -> Self

Creates a new OpenAI chat model instance.

Initializes a language model that communicates with OpenAI’s chat completion API. The model will automatically detect capabilities and apply model-specific configurations based on the provided model ID (e.g., reasoning for o1/o3, special handling for vision models).

§Arguments
  • model_id - The OpenAI model identifier (e.g., “gpt-4o”, “gpt-4-turbo”, “gpt-4”, “gpt-3.5-turbo”, “o1”, “o1-mini”, “o3-mini”)
  • config - OpenAI configuration containing API endpoint, authentication headers, and optional customizations
§OpenAI Model Categories
  • GPT-4o Series: Latest high-capability models with vision and reasoning
  • Reasoning Models (o1/o3): Enhanced reasoning with special parameter handling
  • GPT-4 Series: Advanced reasoning and code generation
  • GPT-3.5 Turbo: Balanced performance and cost-effectiveness
  • Search Models: Integration with web search capabilities
§Example
use ai_sdk_openai::{OpenAIConfig, OpenAIChatModel};

let api_key = std::env::var("OPENAI_API_KEY").unwrap();
let config = OpenAIConfig::from_api_key(api_key);
let model = OpenAIChatModel::new("gpt-4o", config);

Trait Implementations§

Source§

impl LanguageModel for OpenAIChatModel

Source§

fn provider(&self) -> &str

Returns the provider identifier for this model. Read more
Source§

fn model_id(&self) -> &str

Returns the specific model identifier. Read more
Source§

fn supported_urls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = HashMap<String, Vec<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns URLs supported by this model for various operations. Read more
Source§

fn do_generate<'life0, 'async_trait>( &'life0 self, options: CallOptions, ) -> Pin<Box<dyn Future<Output = Result<GenerateResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Executes a non-streaming generation request. Read more
Source§

fn do_stream<'life0, 'async_trait>( &'life0 self, options: CallOptions, ) -> Pin<Box<dyn Future<Output = Result<StreamResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Executes a streaming generation request. Read more
Source§

fn specification_version(&self) -> &str

Returns the specification version implemented by this model. Read more
Source§

fn generate<P>(&self, prompt: P) -> GenerateBuilder<'_, Self>
where Self: Sized, P: TryInto<Prompt>, Box<dyn Error + Sync + Send>: From<<P as TryInto<Prompt>>::Error>,

Creates a builder for a non-streaming generation request. Read more
Source§

fn stream<P>(&self, prompt: P) -> StreamBuilder<'_, Self>
where Self: Sized, P: TryInto<Prompt>, Box<dyn Error + Sync + Send>: From<<P as TryInto<Prompt>>::Error>,

Creates a builder for a streaming generation request. 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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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