api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
Documentation
//! Platform-Specific Client Extensions
//!
//! Trait implementations for platform-specific features.

use crate::
{
  Client,
  error ::{ Result, OpenAIError },
  environment ::{ OpenaiEnvironment, EnvironmentInterface },
  components ::chat_shared::{ ChatCompletionRequest, CreateChatCompletionResponse },
};

use super::
{
  SearchGroundingConfig, GroundedResponse,
  CodeExecutionConfig, CodeExecutionResult,
  WebBrowsingConfig, BrowsingResult,
  ImageGenerationConfig, ImageResult,
};

/// Extension trait for platform-specific client methods.
#[ async_trait::async_trait ]
pub trait PlatformSpecificClient< E >
where
  E: OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
{
  // Search Grounding Methods

  /// Creates a chat completion with search grounding integration.
  ///
  /// # Errors
  /// Returns error if search grounding fails or chat completion fails.
  async fn create_chat_completion_with_search(
    &self,
    request : ChatCompletionRequest,
    search_config : SearchGroundingConfig
  ) -> Result< CreateChatCompletionResponse >;

  /// Performs search grounding for a query with optional context.
  ///
  /// # Errors
  /// Returns error if search operation fails.
  async fn search_and_ground(
    &self,
    query : &str,
    context : Option< &str >,
    config : SearchGroundingConfig
  ) -> Result< GroundedResponse >;

  // Code Execution Methods

  /// Executes code in a secure environment.
  ///
  /// # Errors
  /// Returns error if code execution fails or times out.
  async fn execute_code(
    &self,
    code : &str,
    config : CodeExecutionConfig
  ) -> Result< CodeExecutionResult >;

  /// Creates a chat completion with code execution capabilities.
  ///
  /// # Errors
  /// Returns error if code execution or chat completion fails.
  async fn create_chat_completion_with_code_execution(
    &self,
    request : ChatCompletionRequest,
    execution_config : CodeExecutionConfig
  ) -> Result< CreateChatCompletionResponse >;

  // Web Browsing Methods

  /// Browses a URL and extracts content.
  ///
  /// # Errors
  /// Returns error if URL cannot be accessed or content extraction fails.
  async fn browse_url(
    &self,
    url : &str,
    config : WebBrowsingConfig
  ) -> Result< BrowsingResult >;

  /// Creates a chat completion with web browsing capabilities.
  ///
  /// # Errors
  /// Returns error if browsing or chat completion fails.
  async fn create_chat_completion_with_browsing(
    &self,
    request : ChatCompletionRequest,
    browsing_config : WebBrowsingConfig
  ) -> Result< CreateChatCompletionResponse >;

  // Image Generation Methods

  /// Generates an image from a prompt.
  ///
  /// # Errors
  /// Returns error if image generation fails.
  async fn generate_image(
    &self,
    prompt : &str,
    config : ImageGenerationConfig
  ) -> Result< ImageResult >;
}

#[ async_trait::async_trait ]
impl< E > PlatformSpecificClient< E > for Client< E >
where
  E: OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
{
  #[ inline ]
  async fn create_chat_completion_with_search(
    &self,
    _request : ChatCompletionRequest,
    _search_config : SearchGroundingConfig
  ) -> Result< CreateChatCompletionResponse >
  {
    Err( OpenAIError::Internal( "Search grounding not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn search_and_ground(
    &self,
    _query : &str,
    _context : Option< &str >,
    _config : SearchGroundingConfig
  ) -> Result< GroundedResponse >
  {
    Err( OpenAIError::Internal( "Search grounding not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn execute_code(
    &self,
    _code : &str,
    _config : CodeExecutionConfig
  ) -> Result< CodeExecutionResult >
  {
    Err( OpenAIError::Internal( "Code execution not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn create_chat_completion_with_code_execution(
    &self,
    _request : ChatCompletionRequest,
    _execution_config : CodeExecutionConfig
  ) -> Result< CreateChatCompletionResponse >
  {
    Err( OpenAIError::Internal( "Code execution integration not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn browse_url(
    &self,
    _url : &str,
    _config : WebBrowsingConfig
  ) -> Result< BrowsingResult >
  {
    Err( OpenAIError::Internal( "Web browsing not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn create_chat_completion_with_browsing(
    &self,
    _request : ChatCompletionRequest,
    _browsing_config : WebBrowsingConfig
  ) -> Result< CreateChatCompletionResponse >
  {
    Err( OpenAIError::Internal( "Browsing integration not yet implemented".to_string() ).into() )
  }

  #[ inline ]
  async fn generate_image(
    &self,
    _prompt : &str,
    _config : ImageGenerationConfig
  ) -> Result< ImageResult >
  {
    Err( OpenAIError::Internal( "Image generation not yet implemented".to_string() ).into() )
  }
}