@@ -10,7 +10,7 @@ use tiktoken_rs::{async_openai::get_chat_completion_max_tokens, get_completion_m
10
10
11
11
use crate :: { settings:: OpenAISettings , util:: HTTP_USER_AGENT } ;
12
12
use async_openai:: {
13
- config:: OpenAIConfig ,
13
+ config:: { OpenAIConfig , OPENAI_API_BASE } ,
14
14
types:: {
15
15
ChatCompletionRequestMessageArgs , CreateChatCompletionRequestArgs ,
16
16
CreateCompletionRequestArgs , Role ,
@@ -36,25 +36,28 @@ impl Debug for OpenAIClient {
36
36
37
37
impl OpenAIClient {
38
38
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
+
49
48
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
+ }
50
53
// TODO make configurable
51
54
let mut http_client = reqwest:: Client :: builder ( )
52
55
. gzip ( true )
53
56
. brotli ( true )
54
57
. timeout ( Duration :: from_secs ( 60 ) )
55
58
. user_agent ( HTTP_USER_AGENT ) ;
56
59
57
- if api_base. is_empty ( ) {
60
+ if api_base == OPENAI_API_BASE {
58
61
// Optimized HTTP client
59
62
http_client = http_client
60
63
. http2_prior_knowledge ( )
@@ -66,7 +69,7 @@ impl OpenAIClient {
66
69
. min_tls_version ( tls:: Version :: TLS_1_2 ) ;
67
70
}
68
71
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 ( ) {
70
73
bail ! ( "No OpenAI model configured. Please choose a valid model to use." ) ;
71
74
}
72
75
0 commit comments