@@ -29,6 +29,9 @@ use std::fs::File;
2929use std:: io:: Write ;
3030use std:: { env, fs} ;
3131use tracing:: { Level , debug, trace} ;
32+ mod built_info {
33+ include ! ( concat!( env!( "OUT_DIR" ) , "/built.rs" ) ) ;
34+ }
3235
3336/// The output format for the commit message
3437#[ derive( Debug ) ]
@@ -67,6 +70,66 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
6770 ) ;
6871 }
6972
73+ // Get the specified model name from environment variable, default to "gpt-4"
74+ let model_name = env:: var ( "OPENAI_MODEL_NAME" ) . unwrap_or_else ( |_| String :: from ( "gpt-5" ) ) ;
75+
76+ // Instantiate OpenAI client, ready to send requests to the OpenAI API
77+ let client = openai:: OpenAI :: new ( ) ;
78+
79+ // Check if the environment variables are set and print the configured values
80+ if cli. check_env {
81+ fn check_and_print_env ( var_name : & str ) {
82+ match env:: var ( var_name) {
83+ Ok ( value) => {
84+ debug ! ( "{} is set to {}" , var_name, value) ;
85+ // Print the value of the environment variable
86+ println ! ( "{:20}\t {}" , var_name, value) ;
87+ }
88+ Err ( _) => {
89+ debug ! ( "{} is not set" , var_name) ;
90+ }
91+ }
92+ }
93+
94+ trace ! ( "check env option is enabled, will check the OpenAI API key and model name" ) ;
95+ debug ! ( "the model name is `{}`" , & model_name) ;
96+
97+ [
98+ "OPENAI_API_BASE" ,
99+ "OPENAI_API_TOKEN" ,
100+ "OPENAI_MODEL_NAME" ,
101+ "OPENAI_API_PROXY" ,
102+ "OPENAI_API_TIMEOUT" ,
103+ "OPENAI_API_MAX_TOKENS" ,
104+ "GIT_AUTO_SIGNOFF" ,
105+ ]
106+ . iter ( )
107+ . for_each ( |v| check_and_print_env ( v) ) ;
108+
109+ return Ok ( ( ) ) ;
110+ }
111+
112+ // Check if the model name is valid
113+ if cli. check_model {
114+ trace ! ( "check option is enabled, will check the OpenAI API key and model name" ) ;
115+ debug ! ( "the model name is `{}`" , & model_name) ;
116+
117+ match client. check_model ( & model_name) . await {
118+ Ok ( ( ) ) => {
119+ println ! (
120+ "the model name `{}` is available, {} is ready for use!" ,
121+ model_name,
122+ built_info:: PKG_NAME
123+ ) ;
124+ }
125+ Err ( e) => {
126+ return Err ( format ! ( "the model name `{model_name}` is not available: {e}" ) . into ( ) ) ;
127+ }
128+ }
129+
130+ return Ok ( ( ) ) ;
131+ }
132+
70133 // Check if the specified path is a valid directory
71134 let repo_dir = fs:: canonicalize ( & cli. repo_path ) ?;
72135
@@ -96,30 +159,9 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
96159 return Err ( "no commit logs found" . into ( ) ) ;
97160 }
98161
99- // Instantiate OpenAI client, ready to send requests to the OpenAI API
100- let client = openai:: OpenAI :: new ( ) ;
101-
102162 // Generate the prompt which will be sent to OpenAI API
103163 let content = OpenAI :: prompt ( & logs, & diffs) ?;
104164
105- // Get the specified model name from environment variable, default to "gpt-4"
106- let model_name = env:: var ( "OPENAI_MODEL_NAME" ) . unwrap_or_else ( |_| String :: from ( "gpt-4" ) ) ;
107-
108- // Check if the model name is valid
109- if cli. check {
110- trace ! ( "check option is enabled, will check the OpenAI API key and model name" ) ;
111- debug ! ( "the model name is `{}`" , & model_name) ;
112-
113- match client. check_model ( & model_name) . await {
114- Ok ( ( ) ) => {
115- debug ! ( "the model name `{}` is available" , model_name) ;
116- }
117- Err ( e) => {
118- return Err ( format ! ( "the model name `{model_name}` is not available: {e}" ) . into ( ) ) ;
119- }
120- }
121- }
122-
123165 // Load the system prompt from the template file
124166 let system_prompt = include_str ! ( "../templates/system.txt" ) ;
125167
0 commit comments