binary-husky commited on
Commit
30d1698
2 Parent(s): 79cd98f 1a796a5

Merge pull request #662 from sperjar/master

Browse files
Files changed (1) hide show
  1. toolbox.py +62 -2
toolbox.py CHANGED
@@ -3,6 +3,7 @@ import importlib
3
  import traceback
4
  import inspect
5
  import re
 
6
  from latex2mathml.converter import convert as tex2mathml
7
  from functools import wraps, lru_cache
8
 
@@ -517,13 +518,72 @@ def select_api_key(keys, llm_model):
517
  api_key = random.choice(avail_key_list) # 随机负载均衡
518
  return api_key
519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
  @lru_cache(maxsize=128)
521
  def read_single_conf_with_lru_cache(arg):
522
  from colorful import print亮红, print亮绿, print亮蓝
523
  try:
524
- r = getattr(importlib.import_module('config_private'), arg)
 
 
525
  except:
526
- r = getattr(importlib.import_module('config'), arg)
 
 
 
 
 
 
527
  # 在读取API_KEY时,检查一下是不是忘了改config
528
  if arg == 'API_KEY':
529
  print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
 
3
  import traceback
4
  import inspect
5
  import re
6
+ import os
7
  from latex2mathml.converter import convert as tex2mathml
8
  from functools import wraps, lru_cache
9
 
 
518
  api_key = random.choice(avail_key_list) # 随机负载均衡
519
  return api_key
520
 
521
+ def read_env_variable(arg, default_value):
522
+ """
523
+ 环境变量可以是 `GPT_ACADEMIC_CONFIG`(优先),也可以直接是`CONFIG`
524
+ 例如在windows cmd中,既可以写:
525
+ set USE_PROXY=True
526
+ set API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx
527
+ set proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",}
528
+ set AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"]
529
+ set AUTHENTICATION=[("username", "password"), ("username2", "password2")]
530
+ 也可以写:
531
+ set GPT_ACADEMIC_USE_PROXY=True
532
+ set GPT_ACADEMIC_API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx
533
+ set GPT_ACADEMIC_proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",}
534
+ set GPT_ACADEMIC_AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"]
535
+ set GPT_ACADEMIC_AUTHENTICATION=[("username", "password"), ("username2", "password2")]
536
+ """
537
+ from colorful import print亮红, print亮绿
538
+ arg_with_prefix = "GPT_ACADEMIC_" + arg
539
+ if arg_with_prefix in os.environ:
540
+ env_arg = os.environ[arg_with_prefix]
541
+ elif arg in os.environ:
542
+ env_arg = os.environ[arg]
543
+ else:
544
+ raise KeyError
545
+ print(f"[ENV_VAR] 尝试加载{arg},默认值:{default_value} --> 修正值:{env_arg}")
546
+ try:
547
+ if isinstance(default_value, bool):
548
+ r = bool(env_arg)
549
+ elif isinstance(default_value, int):
550
+ r = int(env_arg)
551
+ elif isinstance(default_value, float):
552
+ r = float(env_arg)
553
+ elif isinstance(default_value, str):
554
+ r = env_arg
555
+ elif isinstance(default_value, dict):
556
+ r = eval(env_arg)
557
+ elif isinstance(default_value, list):
558
+ r = eval(env_arg)
559
+ elif default_value is None:
560
+ assert arg == "proxies"
561
+ r = eval(env_arg)
562
+ else:
563
+ print亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置! ")
564
+ raise KeyError
565
+ except:
566
+ print亮红(f"[ENV_VAR] 环境变量{arg}加载失败! ")
567
+ raise KeyError(f"[ENV_VAR] 环境变量{arg}加载失败! ")
568
+
569
+ print亮绿(f"[ENV_VAR] 成功读取环境变量{arg}")
570
+ return r
571
+
572
  @lru_cache(maxsize=128)
573
  def read_single_conf_with_lru_cache(arg):
574
  from colorful import print亮红, print亮绿, print亮蓝
575
  try:
576
+ # 优先级1. 获取环境变量作为配置
577
+ default_ref = getattr(importlib.import_module('config'), arg) # 读取默认值作为数据类型转换的参考
578
+ r = read_env_variable(arg, default_ref)
579
  except:
580
+ try:
581
+ # 优先级2. 获取config_private中的配置
582
+ r = getattr(importlib.import_module('config_private'), arg)
583
+ except:
584
+ # 优先级3. 获取config中的配置
585
+ r = getattr(importlib.import_module('config'), arg)
586
+
587
  # 在读取API_KEY时,检查一下是不是忘了改config
588
  if arg == 'API_KEY':
589
  print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")