diff --git a/package.v2.json b/package.v2.json index 76546c2..e731790 100644 --- a/package.v2.json +++ b/package.v2.json @@ -103,11 +103,12 @@ "name": "ChatGPT", "description": "消息交互支持与ChatGPT对话。", "labels": "消息通知,识别", - "version": "2.1.7", + "version": "2.1.8", "icon": "Chatgpt_A.png", "author": "jxxghp", "level": 1, "history": { + "v2.1.8": "修复 OpenAI API >=1.0.0 兼容性问题", "v2.1.7":"独立安装OpenAi SDK依赖", "v2.1.6": "支持自定义辅助识别提示词", "v2.1.5": "兼容一些模型返回json数据信息用markdown语法包裹的情况", diff --git a/plugins/autosubv2/translate/openai_translate.py b/plugins/autosubv2/translate/openai_translate.py index 20249ee..c924e1b 100644 --- a/plugins/autosubv2/translate/openai_translate.py +++ b/plugins/autosubv2/translate/openai_translate.py @@ -12,29 +12,17 @@ class OpenAi: _api_key: str = None _api_url: str = None _model: str = "gpt-3.5-turbo" - _client: openai.OpenAI = None def __init__(self, api_key: str = None, api_url: str = None, proxy: dict = None, model: str = None, compatible: bool = False): self._api_key = api_key self._api_url = api_url + openai.api_base = self._api_url if compatible else self._api_url + "/v1" + openai.api_key = self._api_key + if proxy and proxy.get("https"): + openai.proxy = proxy.get("https") if model: self._model = model - - # 初始化 OpenAI 客户端 - if self._api_key and self._api_url: - base_url = self._api_url if compatible else self._api_url + "/v1" - http_client = None - if proxy and proxy.get("https"): - import httpx - proxy_url = proxy.get("https") - # httpx 支持字符串格式的代理 URL - http_client = httpx.Client(proxies=proxy_url, timeout=60.0) - self._client = openai.OpenAI( - api_key=self._api_key, - base_url=base_url, - http_client=http_client - ) @staticmethod def __save_session(session_id: str, message: str): @@ -85,8 +73,6 @@ class OpenAi: """ 获取模型 """ - if not self._client: - raise ValueError("OpenAI client not initialized. Please check API key and API URL.") if not isinstance(message, list): if prompt: message = [ @@ -106,10 +92,9 @@ class OpenAi: "content": message } ] - # 新版本 API 不支持 user 参数,需要从 kwargs 中移除 - kwargs.pop('user', None) - return self._client.chat.completions.create( + return openai.ChatCompletion.create( model=self._model, + user=user, messages=message, **kwargs ) diff --git a/plugins/chatgpt/openai.py b/plugins/chatgpt/openai.py index 4f6b1b9..f0d3d61 100644 --- a/plugins/chatgpt/openai.py +++ b/plugins/chatgpt/openai.py @@ -12,28 +12,19 @@ class OpenAi: _api_key: str = None _api_url: str = None _model: str = "gpt-3.5-turbo" - _client: openai.OpenAI = None def __init__(self, api_key: str = None, api_url: str = None, proxy: dict = None, model: str = None, compatible: bool = False): self._api_key = api_key self._api_url = api_url + if compatible: + openai.api_base = self._api_url + else: + openai.api_base = self._api_url + "/v1" + openai.api_key = self._api_key + if proxy and proxy.get("https"): + openai.proxy = proxy.get("https") if model: self._model = model - - # 初始化 OpenAI 客户端 - if self._api_key and self._api_url: - base_url = self._api_url if compatible else self._api_url + "/v1" - http_client = None - if proxy and proxy.get("https"): - import httpx - proxy_url = proxy.get("https") - # httpx 支持字符串格式的代理 URL - http_client = httpx.Client(proxies=proxy_url, timeout=60.0) - self._client = openai.OpenAI( - api_key=self._api_key, - base_url=base_url, - http_client=http_client - ) def get_state(self) -> bool: return True if self._api_key else False @@ -87,8 +78,6 @@ class OpenAi: """ 获取模型 """ - if not self._client: - raise ValueError("OpenAI client not initialized. Please check API key and API URL.") if not isinstance(message, list): if prompt: message = [ @@ -108,10 +97,9 @@ class OpenAi: "content": message } ] - # 新版本 API 不支持 user 参数,需要从 kwargs 中移除 - kwargs.pop('user', None) - return self._client.chat.completions.create( + return openai.ChatCompletion.create( model=self._model, + user=user, messages=message, **kwargs ) @@ -170,11 +158,11 @@ class OpenAi: if result: self.__save_session(userid, text) return result - except openai.RateLimitError as e: + except openai.error.RateLimitError as e: return f"请求被ChatGPT拒绝了,{str(e)}" - except openai.APIConnectionError as e: + except openai.error.APIConnectionError as e: return f"ChatGPT网络连接失败:{str(e)}" - except openai.APITimeoutError as e: + except openai.error.Timeout as e: return f"没有接收到ChatGPT的返回消息:{str(e)}" except Exception as e: return f"请求ChatGPT出现错误:{str(e)}"