diff --git a/.config/api_config.json b/.config/api_config.json index e8f23a3..df3a29e 100644 --- a/.config/api_config.json +++ b/.config/api_config.json @@ -1,4 +1,5 @@ { + "api_base": "https://api.openai.com/v1", "api_key": "", "engine": "gpt-3.5-turbo", "max_tokens": 3600, diff --git a/doc/Config.md b/doc/Config.md index 32f9e5e..0ee6657 100644 --- a/doc/Config.md +++ b/doc/Config.md @@ -6,6 +6,8 @@ ```json { + // OpenAI Base URL(must endswith "/v1") + "api_base": "https://api.openai.com/v1", // OpenAI Account API_KEY "api_key": "", // ID of the model to use diff --git a/doc/Config_ZH.md b/doc/Config_ZH.md index 7af4169..0d3cf9a 100644 --- a/doc/Config_ZH.md +++ b/doc/Config_ZH.md @@ -6,6 +6,8 @@ ```json { + // OpenAI Base URL(也可以是任意兼容 OpenAI 的 API 服务地址) + "api_base": "https://api.openai.com/v1", // OpenAI 账户构建的 API_KEY "api_key": "", // 需要的模型名称 diff --git a/services/chat/ChatGPTAPI.py b/services/chat/ChatGPTAPI.py index d2d35d9..0612fb5 100644 --- a/services/chat/ChatGPTAPI.py +++ b/services/chat/ChatGPTAPI.py @@ -38,6 +38,7 @@ def __init__( """ Initialize Chatbot with API key (from https://platform.openai.com/account/api-keys) """ + self.api_base = (config.get("api_base") or "https://api.openai.com/v1").rstrip("/") self.api_key = config["api_key"] self.engine = config["engine"] self.max_tokens = config["max_tokens"] @@ -192,7 +193,7 @@ def ask(self, prompt: str, timeout: float = 120, access_internet=False, access_r try: response = self.session.post( - url="https://api.openai.com/v1/chat/completions", + url=f"{self.api_base}/chat/completions", headers={"Authorization": f"Bearer {self.api_key}"}, json={ "model": self.engine, diff --git a/shared/config.py b/shared/config.py index 14d00cc..402ecbd 100644 --- a/shared/config.py +++ b/shared/config.py @@ -5,7 +5,6 @@ # api model check with open('.config/api_config.json', encoding='utf-8') as f: api_config = json.load(f) -f.close() if api_config['engine'] not in [ 'gpt-3.5-turbo',