-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
292 lines (253 loc) · 11.6 KB
/
utils.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import configparser
import requests
import re
import json
import os
import openai
import httpx
import asyncio
from openai import AsyncOpenAI
import copy
def read_config(Section, key, file_path = './config.ini'):
config = configparser.ConfigParser()
config.read(file_path, encoding='utf-8')
# 检查指定的 section 和 key 是否存在于配置文件中
if config.has_section(Section) and config.has_option(Section, key):
value = config.get(Section, key)
return value
else:
return None
def load_json(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return data
async def get_openai_completion(client, messages, model="gpt-3.5-turbo",
frequency_penalty=None, presence_penalty=None,
max_tokens=None, temperature=None, top_p=None,
timeout=None):
try:
chat_completion = await client.chat.completions.create(
messages=messages,
model=model,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p
)
return chat_completion
except Exception as e:
print(f"Error: {e}")
return None
def update_token_count(file_name, token_count):
try:
# Read the current token count from the file
with open(file_name, 'r') as file:
current_token_count = int(file.read().strip())
except FileNotFoundError:
current_token_count = 0
# Calculate the new total token count
new_token_count = current_token_count + token_count
# Write the new total token count back to the file (overwrite the file)
with open(file_name, 'w') as file:
file.write(str(new_token_count))
# Print the usage information
print(f"Token usage: {token_count} Total tokens used: {new_token_count}")
def parse_cq_codes(text):
# Regular expression pattern to match CQ codes
cq_pattern = r'\[CQ:[^\]]+\]'
# Find all CQ codes in the text
cq_codes = re.findall(cq_pattern, text)
# Remove CQ codes from the original text
clean_text = re.sub(cq_pattern, '', text)
return cq_codes, clean_text
async def send_msg(message, message_type, target_id, ip='192.168.1.104', port='5700', if_CQ='false'):
# Prepare the data to send in the request
data = {
"message_type": message_type,
"message": message,
"auto_escape": if_CQ # 是否解析CQ码
}
# Depending on the message type, set the target_id in the data
if message_type == "private":
data["user_id"] = target_id
elif message_type == "group":
data["group_id"] = target_id
else:
return {"error": "Invalid message type"}
# Construct the URL
url = f"http://{ip}:{port}/send_msg"
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, params=data)
response_data = response.json()
if response.status_code == 200:
return response_data # Return the response data
else:
return {"error": "Failed to send the message"}
except Exception as e:
return {"error": f"An error occurred: {str(e)}"}
def read_prompt(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
prompt = file.read()
return prompt
"""
except FileNotFoundError:
return f"File not found: {file_path}"
except Exception as e:
return f"An error occurred while reading the file: {str(e)}"
"""
def check_complete_config(directory):
role_names = []
# 遍历目录中的所有文件
for filename in os.listdir(directory):
if filename.endswith(".txt"):
txt_file_path = os.path.join(directory, filename)
json_file_path = os.path.join(directory, filename.replace(".txt", ".json"))
# 如果同时存在相应的txt和json文件,则认为配置完整
if os.path.exists(json_file_path):
role_name = filename.split(".")[0]
role_names.append(role_name)
return role_names
def format_role_list(role_names):
formatted_list = "支持切换的角色:\n"
for i, role_name in enumerate(role_names, start=1):
formatted_list += f"{i}. {role_name}\n"
return formatted_list.strip()
class ConversationManager:
def __init__(self, target_id: str, type: str, conversation_init: list, conversation: list, names: list, context_length: int, max_tokens: int, temperature: float, top_p: float, presence_penalty: float, frequency_penalty: float, model: str):
self.target_id = target_id
self.type = type
self.conversation_init = copy.deepcopy(conversation_init)
self.conversation = copy.deepcopy(conversation)
self.names = copy.deepcopy(names)
self.context_length = context_length
self.max_tokens = max_tokens
self.temperature = temperature
self.top_p = top_p
self.presence_penalty = presence_penalty
self.frequency_penalty = frequency_penalty
self.model = model
def print_parameters(self):
print("目标ID:", self.target_id)
print("目标类型:", self.type)
print("初始化对话:", self.conversation_init)
print("角色名称:", self.names)
print("上下文长度(即最大对话轮数):", self.context_length)
print("最大token数:", self.max_tokens)
print("温度:", self.temperature)
print("Top P:", self.top_p)
print("主题重复度惩罚因子:", self.presence_penalty)
print("重复度惩罚因子:", self.frequency_penalty)
print("模型:", self.model)
class Bot:
def __init__(self, default: ConversationManager, server_ip: str, server_port: int, shamrock_ip: str, shamrock_port: int, QQ: str, Admin_QQ: str, client: AsyncOpenAI, token_count: str):
self.default = default
self.conversation_map = {}
self.private_lock = False
self.ip = server_ip
self.port = server_port
self.shamrock_ip = shamrock_ip
self.shamrock_port = shamrock_port
self.qq = QQ
self.admin = Admin_QQ
self.client = client
self.token_count = token_count
def print_parameters(self):
print("默认对话管理器参数:\n")
self.default.print_parameters()
print("\n")
print("服务器IP:", self.ip)
print("服务器端口:", self.port)
print("Shamrock_IP:", self.shamrock_ip)
print("Shamrock端口:", self.shamrock_port)
print("QQ号:", self.qq)
print("Token计数文件:", self.token_count)
def register_conversation_manager(self, target_id: str, type: str):
# 从self.default中获取其他参数
conversation_init = copy.deepcopy(self.default.conversation_init)
conversation = copy.deepcopy(self.default.conversation)
names = copy.deepcopy(self.default.names)
context_length = self.default.context_length
max_tokens = self.default.max_tokens
temperature = self.default.temperature
top_p = self.default.top_p
presence_penalty = self.default.presence_penalty
frequency_penalty = self.default.frequency_penalty
model = self.default.model
# 创建新的ConversationManager对象并添加到self.conversation_map中
new_conversation_manager = ConversationManager(target_id=target_id, type=type,
conversation_init=conversation_init,
conversation=conversation,
names=names,
context_length=context_length,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
model=model)
self.conversation_map[target_id] = new_conversation_manager
def get_conversation_manager(self, target_id: str) -> ConversationManager:
return self.conversation_map.get(target_id, self.default)
def check_existence(self, target_id: str):
if target_id in self.conversation_map:
return True
else:
return False
def delete_conversation_manager(self, target_id: str) -> bool:
if target_id in self.conversation_map:
del self.conversation_map[target_id]
return True
else:
return False
def load_config():
#服务器设置
SERVER_IP = read_config("Server", "IP")
SERVER_PORT = int(read_config("Server", "PORT"))
MODEL = str(read_config("Model", "MODEL"))
#Prompt设置
ROLE_CONFIG = load_json(read_config("Prompt", "ROLE_CONFIG"))
CONVERSATION = ROLE_CONFIG.get("Conversation")
PROMPT = read_prompt(read_config("Prompt", "ROLE_PROMPT")).strip()
CONVERSATION[0]["content"] = PROMPT
NAMES = ROLE_CONFIG.get("Role")
# 参数设置
CONTEXT_LENGTH = int(ROLE_CONFIG.get("Parameters").get("context_length"))#上下文长度
MAX_TOKENS = int(ROLE_CONFIG.get("Parameters").get("max_tokens"))#最大返回token
TEMPERATURE = float(ROLE_CONFIG.get("Parameters").get("temperature"))
TOP_P = float(ROLE_CONFIG.get("Parameters").get("top_p"))
PRESENCE_PENALTY = float(ROLE_CONFIG.get("Parameters").get("presence_penalty"))
FREQUENCY_PENALTY = float(ROLE_CONFIG.get("Parameters").get("frequency_penalty"))
#OpenAI接口设置
URL = str(read_config("OpenAI", "URL"))
API_KEY = str(read_config("OpenAI", "API_KEY"))
TOKEN_COUNT = read_config("OpenAI", "TOKEN_COUNT")
Client = AsyncOpenAI(api_key=API_KEY, base_url=URL)
#Shamrock框架交互设置
SHAMROCK_IP = str(read_config("Shamrock", "IP"))
SHAMROCK_PORT = read_config("Shamrock", "PORT")
QQ = str(read_config("Shamrock", "QQ")) #这里需要你在配置文件中改成你挂shamrock的QQ号
Admin_QQ = str(read_config("Shamrock", "Admin_QQ")) # 管理员QQ
default_manager = ConversationManager(target_id = "114514",
type = "default",
conversation_init = CONVERSATION,
conversation = CONVERSATION,
names = NAMES,
context_length = CONTEXT_LENGTH,
max_tokens = MAX_TOKENS,
temperature = TEMPERATURE,
top_p = TOP_P,
presence_penalty = PRESENCE_PENALTY,
frequency_penalty = FREQUENCY_PENALTY,
model = MODEL)
bot = Bot(default = default_manager,
server_ip = SERVER_IP,
server_port = SERVER_PORT,
shamrock_ip = SHAMROCK_IP,
shamrock_port = SHAMROCK_PORT,
QQ = QQ,
Admin_QQ = Admin_QQ,
client = Client,
token_count = TOKEN_COUNT)
return bot