pub struct Client<E>{
pub http_client: Client,
pub environment: E,
pub diagnostics: Option<Arc<DiagnosticsCollector>>,
pub cache: Option<Arc<ApiRequestCache>>,
pub retry_config: Option<EnhancedRetryConfig>,
pub circuit_breaker_config: Option<EnhancedCircuitBreakerConfig>,
pub circuit_breaker: Option<EnhancedCircuitBreaker>,
pub rate_limiting_config: Option<EnhancedRateLimitingConfig>,
pub rate_limiter: Option<EnhancedRateLimiter>,
}Expand description
The main client for interacting with the OpenAI API.
Provides methods for accessing different API categories like assistants, chat, embeddings, etc.
§Governing Principle : “Thin Client, Rich API”
This client follows the “Thin Client, Rich API” principle - it exposes all server-side functionality transparently while maintaining zero client-side intelligence or automatic behaviors. All operations are explicit and developer-controlled, with clear separation between information retrieval and state-changing actions.
§Example
use api_openai::{ Client, environment::{ OpenaiEnvironmentImpl, OpenAIRecommended }, Secret, ClientApiAccessors };
let secret = Secret::load_from_env("OPENAI_API_KEY")?;
let env = OpenaiEnvironmentImpl::build(secret, None, None, OpenAIRecommended::base_url().to_string(), OpenAIRecommended::realtime_base_url().to_string())?;
let client = Client::build(env)?;
// Use the client to access different APIs
let models = client.models().list().await?;Fields§
§http_client: ClientThe HTTP client used for making requests.
environment: EThe OpenAI environment configuration.
diagnostics: Option<Arc<DiagnosticsCollector>>Optional diagnostics collector for monitoring requests.
cache: Option<Arc<ApiRequestCache>>Optional request cache for API responses.
retry_config: Option<EnhancedRetryConfig>Optional retry configuration for reliability.
circuit_breaker_config: Option<EnhancedCircuitBreakerConfig>Optional circuit breaker configuration for fault tolerance.
circuit_breaker: Option<EnhancedCircuitBreaker>Optional circuit breaker instance.
rate_limiting_config: Option<EnhancedRateLimitingConfig>Optional rate limiting configuration.
rate_limiter: Option<EnhancedRateLimiter>Optional rate limiter instance.
Implementations§
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub fn with_cache(self) -> Self
pub fn with_cache(self) -> Self
Enable request caching with default configuration.
Sourcepub fn with_cache_config(self, config: CacheConfig) -> Self
pub fn with_cache_config(self, config: CacheConfig) -> Self
Enable request caching with custom configuration.
Sourcepub fn cache_statistics(&self) -> Option<&CacheStatistics>
pub fn cache_statistics(&self) -> Option<&CacheStatistics>
Get cache statistics if caching is enabled.
Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear the cache if caching is enabled.
Sourcepub fn with_retry(self) -> Self
pub fn with_retry(self) -> Self
Enable enhanced retry logic with default configuration.
Only available when the retry feature is enabled.
Sourcepub fn with_retry_config(self, config: EnhancedRetryConfig) -> Self
pub fn with_retry_config(self, config: EnhancedRetryConfig) -> Self
Enable enhanced retry logic with custom configuration.
Only available when the retry feature is enabled.
Sourcepub fn retry_config(&self) -> Option<&EnhancedRetryConfig>
pub fn retry_config(&self) -> Option<&EnhancedRetryConfig>
Get retry configuration if enabled.
Sourcepub fn with_circuit_breaker(self) -> Self
pub fn with_circuit_breaker(self) -> Self
Enable circuit breaker logic with default configuration.
Only available when the circuit_breaker feature is enabled.
Sourcepub fn with_circuit_breaker_config(
self,
config: EnhancedCircuitBreakerConfig,
) -> Self
pub fn with_circuit_breaker_config( self, config: EnhancedCircuitBreakerConfig, ) -> Self
Enable circuit breaker logic with custom configuration.
Only available when the circuit_breaker feature is enabled.
Sourcepub fn circuit_breaker_config(&self) -> Option<&EnhancedCircuitBreakerConfig>
pub fn circuit_breaker_config(&self) -> Option<&EnhancedCircuitBreakerConfig>
Get circuit breaker configuration if enabled.
Sourcepub fn with_rate_limiting(self) -> Self
pub fn with_rate_limiting(self) -> Self
Enable rate limiting logic with default configuration.
Only available when the rate_limiting feature is enabled.
Sourcepub fn with_rate_limiting_config(
self,
config: EnhancedRateLimitingConfig,
) -> Self
pub fn with_rate_limiting_config( self, config: EnhancedRateLimitingConfig, ) -> Self
Enable rate limiting logic with custom configuration.
Only available when the rate_limiting feature is enabled.
Sourcepub fn rate_limiting_config(&self) -> Option<&EnhancedRateLimitingConfig>
pub fn rate_limiting_config(&self) -> Option<&EnhancedRateLimitingConfig>
Get rate limiting configuration if enabled.
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub fn get_diagnostics(&self) -> Option<Arc<DiagnosticsCollector>>
pub fn get_diagnostics(&self) -> Option<Arc<DiagnosticsCollector>>
Returns the diagnostics collector for monitoring requests.
§Returns
Some(DiagnosticsCollector)if diagnostics are enabledNoneif diagnostics are disabled
Sourcepub fn list_registered_tools(&self) -> Vec<&str>
pub fn list_registered_tools(&self) -> Vec<&str>
Lists all registered custom tools.
§Returns
An empty vector as custom tool registration is not yet implemented.
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub async fn list_organizations(&self) -> Result<Vec<Organization>>
pub async fn list_organizations(&self) -> Result<Vec<Organization>>
List organizations (convenience method)
§Errors
Returns an error if the API request fails or if the response cannot be parsed.
Sourcepub async fn get_organization(&self, org_id: &str) -> Result<Organization>
pub async fn get_organization(&self, org_id: &str) -> Result<Organization>
Get organization details (convenience method)
§Errors
Returns an error if the API request fails, the organization is not found, or if the response cannot be parsed.
Sourcepub async fn list_users(&self, org_id: &str) -> Result<Vec<User>>
pub async fn list_users(&self, org_id: &str) -> Result<Vec<User>>
List users in organization (convenience method)
§Errors
Returns an error if the API request fails, the organization is not found, insufficient permissions, or if the response cannot be parsed.
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub async fn upload_file<P: AsRef<Path>>(
&self,
file_path: P,
purpose: &str,
) -> Result<FileObject>
pub async fn upload_file<P: AsRef<Path>>( &self, file_path: P, purpose: &str, ) -> Result<FileObject>
Sourcepub async fn list_files(&self, purpose: Option<&str>) -> Result<Vec<FileObject>>
pub async fn list_files(&self, purpose: Option<&str>) -> Result<Vec<FileObject>>
Sourcepub async fn get_file(&self, file_id: &str) -> Result<FileObject>
pub async fn get_file(&self, file_id: &str) -> Result<FileObject>
Sourcepub async fn delete_file(&self, file_id: &str) -> Result<DeleteFileResponse>
pub async fn delete_file(&self, file_id: &str) -> Result<DeleteFileResponse>
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub fn enterprise(&self) -> EnterpriseClient<'_, E>
pub fn enterprise(&self) -> EnterpriseClient<'_, E>
Access enterprise features
Source§impl<E> Client<E>
Extension trait for Client to add comparator method
impl<E> Client<E>
Extension trait for Client to add comparator method
Sourcepub fn comparator(&self) -> ModelComparator<'_, E>
pub fn comparator(&self) -> ModelComparator<'_, E>
Create a model comparator for A/B testing
Trait Implementations§
Source§impl<E> ClientApiAccessors<E> for Client<E>
impl<E> ClientApiAccessors<E> for Client<E>
Source§fn assistants(&self) -> Assistants<'_, E>
fn assistants(&self) -> Assistants<'_, E>
Assistants API client.Source§fn embeddings(&self) -> Embeddings<'_, E>
fn embeddings(&self) -> Embeddings<'_, E>
Embeddings API client.Source§fn fine_tuning(&self) -> FineTuning<'_, E>
fn fine_tuning(&self) -> FineTuning<'_, E>
FineTuning API client.Source§fn moderations(&self) -> Moderations<'_, E>
fn moderations(&self) -> Moderations<'_, E>
Moderations API client.Source§fn vector_stores(&self) -> VectorStores<'_, E>
fn vector_stores(&self) -> VectorStores<'_, E>
VectorStores API client.Source§impl<E> PlatformSpecificClient<E> for Client<E>
impl<E> PlatformSpecificClient<E> for Client<E>
Source§fn create_chat_completion_with_search<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_search_config: SearchGroundingConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn create_chat_completion_with_search<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_search_config: SearchGroundingConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn search_and_ground<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_query: &'life1 str,
_context: Option<&'life2 str>,
_config: SearchGroundingConfig,
) -> Pin<Box<dyn Future<Output = Result<GroundedResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn search_and_ground<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_query: &'life1 str,
_context: Option<&'life2 str>,
_config: SearchGroundingConfig,
) -> Pin<Box<dyn Future<Output = Result<GroundedResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Source§fn execute_code<'life0, 'life1, 'async_trait>(
&'life0 self,
_code: &'life1 str,
_config: CodeExecutionConfig,
) -> Pin<Box<dyn Future<Output = Result<CodeExecutionResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn execute_code<'life0, 'life1, 'async_trait>(
&'life0 self,
_code: &'life1 str,
_config: CodeExecutionConfig,
) -> Pin<Box<dyn Future<Output = Result<CodeExecutionResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn create_chat_completion_with_code_execution<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_execution_config: CodeExecutionConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn create_chat_completion_with_code_execution<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_execution_config: CodeExecutionConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn browse_url<'life0, 'life1, 'async_trait>(
&'life0 self,
_url: &'life1 str,
_config: WebBrowsingConfig,
) -> Pin<Box<dyn Future<Output = Result<BrowsingResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn browse_url<'life0, 'life1, 'async_trait>(
&'life0 self,
_url: &'life1 str,
_config: WebBrowsingConfig,
) -> Pin<Box<dyn Future<Output = Result<BrowsingResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn create_chat_completion_with_browsing<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_browsing_config: WebBrowsingConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn create_chat_completion_with_browsing<'life0, 'async_trait>(
&'life0 self,
_request: ChatCompletionRequest,
_browsing_config: WebBrowsingConfig,
) -> Pin<Box<dyn Future<Output = Result<CreateChatCompletionResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn generate_image<'life0, 'life1, 'async_trait>(
&'life0 self,
_prompt: &'life1 str,
_config: ImageGenerationConfig,
) -> Pin<Box<dyn Future<Output = Result<ImageResult>> + 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,
_config: ImageGenerationConfig,
) -> Pin<Box<dyn Future<Output = Result<ImageResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl<E> Freeze for Client<E>where
E: Freeze,
impl<E> !RefUnwindSafe for Client<E>
impl<E> Send for Client<E>
impl<E> Sync for Client<E>
impl<E> Unpin for Client<E>where
E: Unpin,
impl<E> !UnwindSafe for Client<E>
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<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
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> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.
Source§type Entry = <C as CollectionValToEntry<Val>>::Entry
type Entry = <C as CollectionValToEntry<Val>>::Entry
Entry is defined by the Collection trait.