| 36 | self.find_valid_key() |
| 37 | |
| 38 | def initConfig(self): |
| 39 | if os.path.exists(self.config_file): |
| 40 | with open(self.config_file, 'r') as config_file: |
| 41 | config = json.load(config_file) |
| 42 | openai_config = config['openai'] |
| 43 | proxy_config = config['proxy'] |
| 44 | |
| 45 | openai_app_key = None |
| 46 | openai_url_base = None |
| 47 | openai_app_keys = None |
| 48 | model_type = None |
| 49 | proxy_address = None |
| 50 | proxy_port = None |
| 51 | |
| 52 | if 'app_key' in openai_config.keys(): |
| 53 | openai_app_key = openai_config['app_key'] |
| 54 | if 'url_base' in openai_config.keys(): |
| 55 | openai_url_base = openai_config['url_base'] |
| 56 | if 'app_keys' in openai_config.keys(): |
| 57 | openai_app_keys = openai_config['app_keys'] |
| 58 | if 'model_type' in openai_config.keys(): |
| 59 | model_type = openai_config['model_type'] |
| 60 | |
| 61 | if 'address' in proxy_config.keys(): |
| 62 | proxy_address = proxy_config['address'] |
| 63 | if 'port' in proxy_config.keys(): |
| 64 | proxy_port = proxy_config['port'] |
| 65 | if openai_app_key is not None and openai_app_key != "" and self.OPENAI_API_KEY is None: |
| 66 | logging.info("set openai_app_key as " + openai_app_key) |
| 67 | openai.api_base = openai_app_key |
| 68 | self.OPENAI_API_KEY = openai_app_key |
| 69 | if openai_url_base is not None and openai_url_base != "" and self.OPENAI_API_BASE is None: |
| 70 | logging.info("set openai_url_base as " + openai_url_base) |
| 71 | openai.api_base = openai_url_base |
| 72 | self.OPENAI_API_BASE = openai_url_base |
| 73 | if openai_app_keys is not None and len(openai_app_keys) > 0: |
| 74 | for key in openai_app_keys: |
| 75 | logging.info("add" + key + " to openai_app_keys") |
| 76 | self.app_keys.append(key) |
| 77 | if model_type is not None and model_type != "": |
| 78 | self.MODEL_TYPE = model_type |
| 79 | if (proxy_address is not None and proxy_address != "") and (proxy_port is not None and proxy_port != ""): |
| 80 | logging.info("add proxy to chat.") |
| 81 | os.environ['http_proxy'] = f'http://{proxy_address}:{proxy_port}' |
| 82 | os.environ['https_proxy'] = f'http://{proxy_address}:{proxy_port}' |
| 83 | |
| 84 | def find_valid_key(self): |
| 85 | for key in self.app_keys: |