@@ -10,7 +10,7 @@ use tiktoken_rs::{async_openai::get_chat_completion_max_tokens, get_completion_m
1010
1111use crate :: { settings:: OpenAISettings , util:: HTTP_USER_AGENT } ;
1212use async_openai:: {
13- config:: OpenAIConfig ,
13+ config:: { OpenAIConfig , OPENAI_API_BASE } ,
1414 types:: {
1515 ChatCompletionRequestMessageArgs , CreateChatCompletionRequestArgs ,
1616 CreateCompletionRequestArgs , Role ,
@@ -36,25 +36,28 @@ impl Debug for OpenAIClient {
3636
3737impl OpenAIClient {
3838 pub ( crate ) fn new ( settings : OpenAISettings ) -> Result < Self , anyhow:: Error > {
39- let api_base = settings. api_base . unwrap_or_default ( ) ;
40- let openai_config = if api_base. is_empty ( ) {
41- let api_key = settings. api_key . unwrap_or_default ( ) ;
42- if api_key. is_empty ( ) {
43- bail ! ( "No OpenAI API key found. Please provide a valid API key." ) ;
44- }
45- OpenAIConfig :: new ( ) . with_api_key ( api_key)
46- } else {
47- OpenAIConfig :: new ( ) . with_api_base ( & api_base)
48- } ;
39+ let api_base = settings
40+ . api_base
41+ . unwrap_or_else ( || OPENAI_API_BASE . to_string ( ) ) ;
42+ let api_key = settings. api_key . unwrap_or_default ( ) ;
43+
44+ let openai_config = OpenAIConfig :: new ( )
45+ . with_api_base ( & api_base)
46+ . with_api_key ( & api_key) ;
47+
4948 let mut openai_client = Client :: < OpenAIConfig > :: with_config ( openai_config) ;
49+
50+ if api_base == OPENAI_API_BASE && api_key. is_empty ( ) {
51+ bail ! ( "No OpenAI API key found. Please provide a valid API key." ) ;
52+ }
5053 // TODO make configurable
5154 let mut http_client = reqwest:: Client :: builder ( )
5255 . gzip ( true )
5356 . brotli ( true )
5457 . timeout ( Duration :: from_secs ( 60 ) )
5558 . user_agent ( HTTP_USER_AGENT ) ;
5659
57- if api_base. is_empty ( ) {
60+ if api_base == OPENAI_API_BASE {
5861 // Optimized HTTP client
5962 http_client = http_client
6063 . http2_prior_knowledge ( )
@@ -66,7 +69,7 @@ impl OpenAIClient {
6669 . min_tls_version ( tls:: Version :: TLS_1_2 ) ;
6770 }
6871 let model = settings. model . unwrap_or_default ( ) ;
69- if api_base. is_empty ( ) && model. is_empty ( ) {
72+ if api_base == OPENAI_API_BASE && model. is_empty ( ) {
7073 bail ! ( "No OpenAI model configured. Please choose a valid model to use." ) ;
7174 }
7275
0 commit comments