-
Notifications
You must be signed in to change notification settings - Fork 230
/
credential.py
80 lines (67 loc) · 2.18 KB
/
credential.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from urllib.parse import urlparse
import requests
from dotenv import load_dotenv
from loguru import logger
from pydantic import BaseModel
class ProviderError(Exception):
pass
class Credential(BaseModel):
api_key: str
api_endpoint: str
api_model: str
@classmethod
def from_provider(cls, token, provider_url):
"""
使用 token POST 请求 provider_url 获取用户信息
:param token: 用户 token
:param provider_url: provider url
:return: 用户信息
:raises HTTPError: 请求失败
:raises JSONDecodeError: 返回数据解析失败
:raises ProviderError: provider 返回错误信息
"""
response = requests.post(provider_url, data={"token": token})
response.raise_for_status()
user_data = response.json()
if user_data.get("error"):
raise ProviderError(user_data["error"])
return cls(
api_key=user_data["api_key"],
api_endpoint=user_data["api_endpoint"],
api_model=user_data["api_model"],
)
def split_setting_string(input_string):
if not isinstance(input_string, str):
return None
segments = input_string.split("$")
# 检查链接的有效性
def is_valid_url(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
# 开头为链接的情况
if is_valid_url(segments[0]) and len(segments) >= 3:
return segments[:3]
# 第二个元素为链接,第一个元素为字符串的情况
elif (
len(segments) == 2
and not is_valid_url(segments[0])
and is_valid_url(segments[1])
):
return segments
# 其他情况
else:
return None
load_dotenv()
if os.getenv("GLOBAL_OAI_KEY") and os.getenv("GLOBAL_OAI_ENDPOINT"):
logger.warning("\n\n**Using GLOBAL credential**\n\n")
global_credential = Credential(
api_key=os.getenv("GLOBAL_OAI_KEY"),
api_endpoint=os.getenv("GLOBAL_OAI_ENDPOINT"),
api_model=os.getenv("GLOBAL_OAI_MODEL", "gpt-3.5-turbo"),
)
else:
global_credential = None