From 8a7b2c0289a3db03732a9b561c4783806ccb331a Mon Sep 17 00:00:00 2001 From: thsrite Date: Fri, 11 Aug 2023 14:44:50 +0800 Subject: [PATCH] =?UTF-8?q?fix=20chatgpt=20=E4=BD=BF=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/plugins/chatgpt/__init__.py | 23 ++++++++++++++++++++++- app/plugins/chatgpt/openai.py | 4 +++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/plugins/chatgpt/__init__.py b/app/plugins/chatgpt/__init__.py index 1d6eb938..6db0438a 100644 --- a/app/plugins/chatgpt/__init__.py +++ b/app/plugins/chatgpt/__init__.py @@ -1,5 +1,6 @@ from typing import Any, List, Dict, Tuple +from app.core.config import settings from app.core.event import eventmanager from app.plugins import _PluginBase from app.plugins.chatgpt.openai import OpenAi @@ -31,15 +32,18 @@ class ChatGPT(_PluginBase): # 私有属性 openai = None _enabled = False + _proxy = False _openai_url = None _openai_key = None def init_plugin(self, config: dict = None): if config: self._enabled = config.get("enabled") + self._proxy = config.get("proxy") self._openai_url = config.get("openai_url") self._openai_key = config.get("openai_key") - self.openai = OpenAi(api_key=self._openai_key, api_url=self._openai_url) + self.openai = OpenAi(api_key=self._openai_key, api_url=self._openai_url, + proxy=settings.PROXY if self._proxy else None) def get_state(self) -> bool: return self._enabled @@ -77,6 +81,22 @@ class ChatGPT(_PluginBase): } } ] + }, + { + 'component': 'VCol', + 'props': { + 'cols': 12, + 'md': 6 + }, + 'content': [ + { + 'component': 'VSwitch', + 'props': { + 'model': 'proxy', + 'label': '使用代理', + } + } + ] } ] }, @@ -122,6 +142,7 @@ class ChatGPT(_PluginBase): } ], { "enabled": False, + "proxy": False, "openai_url": "https://api.openai.com", "openai_key": "" } diff --git a/app/plugins/chatgpt/openai.py b/app/plugins/chatgpt/openai.py index 4108c43a..3613926a 100644 --- a/app/plugins/chatgpt/openai.py +++ b/app/plugins/chatgpt/openai.py @@ -12,11 +12,13 @@ class OpenAi: _api_key: str = None _api_url: str = None - def __init__(self, api_key: str = None, api_url: str = None): + def __init__(self, api_key: str = None, api_url: str = None, proxy: dict = None): self._api_key = api_key self._api_url = api_url openai.api_base = self._api_url + "/v1" openai.api_key = self._api_key + if proxy and proxy.get("https"): + openai.proxy = proxy.get("https") def get_state(self) -> bool: return True if self._api_key else False