@@ -36,21 +36,16 @@ 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_key = settings. api_key . unwrap_or_default ( ) ;
40
- if api_key. is_empty ( ) {
41
- bail ! ( "No OpenAI API key found. Please provide a valid API key." ) ;
42
- }
43
- let model = settings. model . unwrap_or_default ( ) ;
44
- if model. is_empty ( ) {
45
- bail ! ( "No OpenAI model configured. Please choose a valid model to use." ) ;
46
- }
47
-
48
- let mut openai_config = OpenAIConfig :: new ( ) . with_api_key ( api_key) ;
49
-
50
39
let api_base = settings. api_base . unwrap_or_default ( ) ;
51
- if !api_base. is_empty ( ) {
52
- openai_config = openai_config. with_api_base ( & api_base) ;
53
- }
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
+ } ;
54
49
let mut openai_client = Client :: < OpenAIConfig > :: with_config ( openai_config) ;
55
50
// TODO make configurable
56
51
let mut http_client = reqwest:: Client :: builder ( )
@@ -70,6 +65,10 @@ impl OpenAIClient {
70
65
. http2_keep_alive_while_idle ( true )
71
66
. min_tls_version ( tls:: Version :: TLS_1_2 ) ;
72
67
}
68
+ let model = settings. model . unwrap_or_default ( ) ;
69
+ if api_base. is_empty ( ) && model. is_empty ( ) {
70
+ bail ! ( "No OpenAI model configured. Please choose a valid model to use." ) ;
71
+ }
73
72
74
73
if let Some ( proxy) = settings. proxy {
75
74
if !proxy. is_empty ( ) {
0 commit comments