7777
7878using  json = nlohmann::ordered_json;
7979
80+ // 
81+ //  Environment variable utils
82+ // 
83+ 
84+ template <typename  T>
85+ static  typename  std::enable_if<std::is_same<T, std::string>::value, void >::type
86+ get_env (std::string name, T & target) {
87+     char  * value = std::getenv (name.c_str ());
88+     target = value ? std::string (value) : target;
89+ }
90+ 
91+ template <typename  T>
92+ static  typename  std::enable_if<!std::is_same<T, bool >::value && std::is_integral<T>::value, void >::type
93+ get_env (std::string name, T & target) {
94+     char  * value = std::getenv (name.c_str ());
95+     target = value ? std::stoi (value) : target;
96+ }
97+ 
98+ template <typename  T>
99+ static  typename  std::enable_if<std::is_floating_point<T>::value, void >::type
100+ get_env (std::string name, T & target) {
101+     char  * value = std::getenv (name.c_str ());
102+     target = value ? std::stof (value) : target;
103+ }
104+ 
105+ template <typename  T>
106+ static  typename  std::enable_if<std::is_same<T, bool >::value, void >::type
107+ get_env (std::string name, T & target) {
108+     char  * value = std::getenv (name.c_str ());
109+     if  (value) {
110+         std::string val (value);
111+         target = val == " 1" " true" 
112+     }
113+ }
114+ 
80115// 
81116//  CPU utils
82117// 
@@ -220,12 +255,6 @@ int32_t cpu_get_num_math() {
220255//  CLI argument parsing
221256// 
222257
223- void  gpt_params_handle_hf_token (gpt_params & params) {
224-     if  (params.hf_token .empty () && std::getenv (" HF_TOKEN" 
225-         params.hf_token  = std::getenv (" HF_TOKEN" 
226-     }
227- }
228- 
229258void  gpt_params_handle_model_default (gpt_params & params) {
230259    if  (!params.hf_repo .empty ()) {
231260        //  short-hand to avoid specifying --hf-file -> default it to --model
@@ -273,7 +302,9 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
273302
274303    gpt_params_handle_model_default (params);
275304
276-     gpt_params_handle_hf_token (params);
305+     if  (params.hf_token .empty ()) {
306+         get_env (" HF_TOKEN" hf_token );
307+     }
277308
278309    if  (params.escape ) {
279310        if  (!params.prompt_is_binary ) {
@@ -295,6 +326,25 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
295326    return  true ;
296327}
297328
329+ void  gpt_params_parse_from_env (gpt_params & params) {
330+     //  we only care about server-related params for now
331+     get_env (" LLAMA_ARG_MODEL" model );
332+     get_env (" LLAMA_ARG_THREADS" n_threads );
333+     get_env (" LLAMA_ARG_CTX_SIZE" n_ctx );
334+     get_env (" LLAMA_ARG_N_PARALLEL" n_parallel );
335+     get_env (" LLAMA_ARG_BATCH" n_batch );
336+     get_env (" LLAMA_ARG_UBATCH" n_ubatch );
337+     get_env (" LLAMA_ARG_N_GPU_LAYERS" n_gpu_layers );
338+     get_env (" LLAMA_ARG_THREADS_HTTP" n_threads_http );
339+     get_env (" LLAMA_ARG_CHAT_TEMPLATE" chat_template );
340+     get_env (" LLAMA_ARG_N_PREDICT" n_predict );
341+     get_env (" LLAMA_ARG_ENDPOINT_METRICS" endpoint_metrics );
342+     get_env (" LLAMA_ARG_ENDPOINT_SLOTS" endpoint_slots );
343+     get_env (" LLAMA_ARG_EMBEDDINGS" embedding );
344+     get_env (" LLAMA_ARG_FLASH_ATTN" flash_attn );
345+     get_env (" LLAMA_ARG_DEFRAG_THOLD" defrag_thold );
346+ }
347+ 
298348bool  gpt_params_parse (int  argc, char  ** argv, gpt_params & params) {
299349    const  auto  params_org = params; //  the example can modify the default params
300350
0 commit comments