-
Notifications
You must be signed in to change notification settings - Fork 3
/
ai_api.py
176 lines (149 loc) · 6.56 KB
/
ai_api.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
import winreg
def get_proxy():
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings") as key:
proxy_enable, _ = winreg.QueryValueEx(key, "ProxyEnable")
proxy_server, _ = winreg.QueryValueEx(key, "ProxyServer")
if proxy_enable and proxy_server:
proxy_parts = proxy_server.split(":")
if len(proxy_parts) == 2:
return {"http": f"http://{proxy_server}", "https": f"http://{proxy_server}"}
except WindowsError:
pass
return {"http": None, "https": None}
class ChatSession:
def __init__(self, api_key, base_url, model, system_prompt):
"""
初始化聊天会话
:param api_key: API密钥
:param base_url: API基础URL
:param model: 使用的模型名称
:param system_prompt: 系统提示,用于设定AI角色
"""
base_url = base_url.rstrip('/')
if 'googleapis.com' in base_url.lower():
self.base_url = f"{base_url}/v1beta/chat/completions"
elif 'bigmodel.cn' in base_url.lower():
self.base_url = f"{base_url}/api/paas/v4/chat/completions"
elif 'volces.com' in base_url.lower():
self.base_url = f"{base_url}/api/v3/chat/completions"
else:
if not base_url.startswith(('http://', 'https://')):
base_url = 'https://' + base_url
if not '/chat/completions' in base_url.lower():
if '/v1' in base_url:
base_url = f"{base_url}/chat/completions"
else:
base_url = f"{base_url}/v1/chat/completions"
print(f"已添加标准endpoint -> {base_url}")
self.base_url = base_url
self.api_key = api_key
self.model = model
self.system_prompt = system_prompt
self.message_history = []
def get_full_context(self, user_message):
"""构建完整的消息上下文"""
return [self.system_prompt] + self.message_history + [user_message]
def add_to_history(self, message):
"""添加消息到历史记录"""
self.message_history.append(message)
def clear_history(self):
"""清空历史记录"""
self.message_history = []
def chat(self, user_input, temperature=0.7, max_tokens=2000):
"""
发送消息并获取回复
:param user_input: 用户输入的消息
:param temperature: 温度参数,控制回复的随机性
:param max_tokens: 回复的最大token数量
"""
if self.api_key is None:
print("api_key is None")
return None
user_message = {"role": "user", "content": user_input}
message_context = self.get_full_context(user_message)
print("\n=== 当前对话记录 ===")
if not self.message_history:
print("新对话 或者 未开启记住历史对话")
for i, msg in enumerate(message_context, 1):
print(f"{i}. {msg['role']}: {msg['content']}")
print("==================\n")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}"
}
data = {
"model": self.model,
"messages": message_context,
"temperature": temperature,
"max_tokens": max_tokens,
"stream": False
}
try:
response = requests.post(
self.base_url,
headers=headers,
json=data,
proxies=get_proxy(),
verify=True,
timeout=30
)
if response.status_code != 200:
error_msg = f"API请求错误: HTTP {response.status_code}\n{response.text}"
print(error_msg)
return error_msg
response_data = response.json()
if "choices" in response_data and len(response_data["choices"]) > 0:
ai_response = response_data["choices"][0]["message"]["content"]
if not ai_response.startswith(("\n发生错误", "request error", "API请求错误")):
self.add_to_history(user_message)
self.add_to_history({"role": "assistant", "content": ai_response})
return ai_response
return None
except Exception as e:
error_msg = f"\n发生错误: {str(e)}"
print(error_msg)
return error_msg
def get_models(self):
"""获取可用的模型列表"""
if 'googleapis.com' in self.base_url:
models_url = "https://generativelanguage.googleapis.com/v1beta/models"
try:
response = requests.get(
models_url,
params={'key': self.api_key},
proxies=get_proxy(),
verify=True,
timeout=10
)
if response.status_code == 200:
models = response.json()
return [model['name'].split('models/')[1] for model in models['models']]
return []
except Exception as e:
print(f"获取Gemini模型列表失败: {str(e)}")
return []
elif 'bigmodel.cn' in self.base_url.lower():
return ['GLM-4-Flash', 'GLM-4-Plus', 'GLM-4V-Flash', 'GLM-4V-Plus']
elif 'volces.com' in self.base_url.lower():
models_url = "https://ark.cn-beijing.volces.com/api/v3/models"
return ['请填入格式ep-20241219144352-xxxxxx的接入点ID']
else:
base_url = self.base_url.split('/chat/completions')[0]
models_url = f"{base_url}/models"
try:
response = requests.get(
models_url,
headers={"Authorization": f"Bearer {self.api_key}"},
proxies=get_proxy(),
verify=True,
timeout=10
)
if response.status_code == 200:
models = response.json()
return [model['id'] for model in models['data']]
return []
except Exception as e:
print(f"获取模型列表失败: {str(e)}")
return []