fix chatgpt 使用代理开关

This commit is contained in:
thsrite
2023-08-11 14:44:50 +08:00
parent 987552558a
commit 8a7b2c0289
2 changed files with 25 additions and 2 deletions

View File

@ -1,5 +1,6 @@
from typing import Any, List, Dict, Tuple from typing import Any, List, Dict, Tuple
from app.core.config import settings
from app.core.event import eventmanager from app.core.event import eventmanager
from app.plugins import _PluginBase from app.plugins import _PluginBase
from app.plugins.chatgpt.openai import OpenAi from app.plugins.chatgpt.openai import OpenAi
@ -31,15 +32,18 @@ class ChatGPT(_PluginBase):
# 私有属性 # 私有属性
openai = None openai = None
_enabled = False _enabled = False
_proxy = False
_openai_url = None _openai_url = None
_openai_key = None _openai_key = None
def init_plugin(self, config: dict = None): def init_plugin(self, config: dict = None):
if config: if config:
self._enabled = config.get("enabled") self._enabled = config.get("enabled")
self._proxy = config.get("proxy")
self._openai_url = config.get("openai_url") self._openai_url = config.get("openai_url")
self._openai_key = config.get("openai_key") 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: def get_state(self) -> bool:
return self._enabled 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, "enabled": False,
"proxy": False,
"openai_url": "https://api.openai.com", "openai_url": "https://api.openai.com",
"openai_key": "" "openai_key": ""
} }

View File

@ -12,11 +12,13 @@ class OpenAi:
_api_key: str = None _api_key: str = None
_api_url: 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_key = api_key
self._api_url = api_url self._api_url = api_url
openai.api_base = self._api_url + "/v1" openai.api_base = self._api_url + "/v1"
openai.api_key = self._api_key openai.api_key = self._api_key
if proxy and proxy.get("https"):
openai.proxy = proxy.get("https")
def get_state(self) -> bool: def get_state(self) -> bool:
return True if self._api_key else False return True if self._api_key else False