Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: 移除过时的兼容性处理代码 #667

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions pkg/openai/keymgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@ def get_using_key_md5(self):
return hashlib.md5(self.using_key.encode('utf-8')).hexdigest()

def __init__(self, api_key):

if type(api_key) is dict:
self.api_key = api_key
elif type(api_key) is str:
self.api_key = {
"default": api_key
}
elif type(api_key) is list:
for i in range(len(api_key)):
self.api_key[str(i)] = api_key[i]

assert type(api_key) == dict
self.api_key = api_key
# 从usage中删除未加载的api-key的记录
# 不删了,也许会运行时添加曾经有记录的api-key

Expand Down
58 changes: 12 additions & 46 deletions pkg/openai/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,6 @@ class SessionOfflineStatus:
EXPLICITLY_CLOSED = 'explicitly_closed'


# 重置session.prompt
def reset_session_prompt(session_name, prompt):
# 备份原始数据
bak_path = 'logs/{}-{}.bak'.format(
session_name,
time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
)
f = open(bak_path, 'w+')
f.write(prompt)
f.close()
# 生成新数据
config = context.get_config_manager().data
prompt = [
{
'role': 'system',
'content': config['default_prompt']['default'] if type(config['default_prompt']) == dict else config['default_prompt']
}
]
# 警告
logging.warning(
"""
用户[{}]的数据已被重置,有可能是因为数据版本过旧或存储错误
原始数据将备份在:
{}""".format(session_name, bak_path)
) # 为保证多行文本格式正确故无缩进
return prompt


# 从数据加载session
def load_sessions():
"""从数据库加载sessions"""
Expand All @@ -70,12 +42,10 @@ def load_sessions():
temp_session.name = session_name
temp_session.create_timestamp = session_data[session_name]['create_timestamp']
temp_session.last_interact_timestamp = session_data[session_name]['last_interact_timestamp']
try:
temp_session.prompt = json.loads(session_data[session_name]['prompt'])
temp_session.token_counts = json.loads(session_data[session_name]['token_counts'])
except Exception:
temp_session.prompt = reset_session_prompt(session_name, session_data[session_name]['prompt'])
temp_session.persistence()

temp_session.prompt = json.loads(session_data[session_name]['prompt'])
temp_session.token_counts = json.loads(session_data[session_name]['token_counts'])

temp_session.default_prompt = json.loads(session_data[session_name]['default_prompt']) if \
session_data[session_name]['default_prompt'] else []

Expand Down Expand Up @@ -493,12 +463,10 @@ def last_session(self):

self.create_timestamp = last_one['create_timestamp']
self.last_interact_timestamp = last_one['last_interact_timestamp']
try:
self.prompt = json.loads(last_one['prompt'])
self.token_counts = json.loads(last_one['token_counts'])
except json.decoder.JSONDecodeError:
self.prompt = reset_session_prompt(self.name, last_one['prompt'])
self.persistence()

self.prompt = json.loads(last_one['prompt'])
self.token_counts = json.loads(last_one['token_counts'])

self.default_prompt = json.loads(last_one['default_prompt']) if last_one['default_prompt'] else []

self.just_switched_to_exist_session = True
Expand All @@ -514,12 +482,10 @@ def next_session(self):

self.create_timestamp = next_one['create_timestamp']
self.last_interact_timestamp = next_one['last_interact_timestamp']
try:
self.prompt = json.loads(next_one['prompt'])
self.token_counts = json.loads(next_one['token_counts'])
except json.decoder.JSONDecodeError:
self.prompt = reset_session_prompt(self.name, next_one['prompt'])
self.persistence()

self.prompt = json.loads(next_one['prompt'])
self.token_counts = json.loads(next_one['token_counts'])

self.default_prompt = json.loads(next_one['default_prompt']) if next_one['default_prompt'] else []

self.just_switched_to_exist_session = True
Expand Down
4 changes: 0 additions & 4 deletions pkg/plugin/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,3 @@ def emit(self, event_name: str, **kwargs) -> EventContext:
)

return event_context


if __name__ == "__main__":
pass
3 changes: 0 additions & 3 deletions pkg/qqbot/cmds/plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:

reply = [reply_str]
return True, reply
elif ctx.params[0].startswith("http"):
reply = ["[bot]err: 此命令已弃用,请使用 !plugin get <插件仓库地址> 进行安装"]
return True, reply
else:
return False, []

Expand Down
4 changes: 0 additions & 4 deletions pkg/qqbot/cmds/session/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
reply_str += "\n当前默认情景预设:{}\n".format(dprompt.mode_inst().get_using_name())
reply_str += "请使用 !default set <情景预设名称> 来设置默认情景预设"
reply = [reply_str]
elif params[0] != "set":
reply = ["[bot]err: 已弃用,请使用!default set <情景预设名称> 来设置默认情景预设"]
else:
return False, []

Expand Down Expand Up @@ -69,7 +67,5 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
reply = ["[bot]已设置默认情景预设为:{}".format(full_name)]
except Exception as e:
reply = ["[bot]err: {}".format(e)]
else:
reply = ["[bot]err: 仅管理员可设置默认情景预设"]

return True, reply
9 changes: 3 additions & 6 deletions pkg/qqbot/cmds/session/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
# 时间(使用create_timestamp转换) 序号 部分内容
datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp'])
msg = ""
try:
msg = json.loads(results[i]['prompt'])
except json.decoder.JSONDecodeError:
msg = pkg.openai.session.reset_session_prompt(session_name, results[i]['prompt'])
# 持久化
pkg.openai.session.get_session(session_name).persistence()

msg = json.loads(results[i]['prompt'])

if len(msg) >= 2:
reply_str += "#{} 创建:{} {}\n".format(i + page * 10,
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
Expand Down
12 changes: 4 additions & 8 deletions pkg/qqbot/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ def get_limitation(session_name: str) -> int:
"""获取会话的限制次数"""
config = context.get_config_manager().data

if type(config['rate_limitation']) == dict:
# 如果被指定了
if session_name in config['rate_limitation']:
return config['rate_limitation'][session_name]
else:
return config['rate_limitation']["default"]
elif type(config['rate_limitation']) == int:
return config['rate_limitation']
if session_name in config['rate_limitation']:
return config['rate_limitation'][session_name]
else:
return config['rate_limitation']["default"]


def add_usage(session_name: str):
Expand Down
4 changes: 0 additions & 4 deletions pkg/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def init_runtime_log_file():
if not os.path.exists("logs"):
os.mkdir("logs")

# 检查本目录是否有qchatgpt.log,若有,移动到logs目录
if os.path.exists("qchatgpt.log"):
shutil.move("qchatgpt.log", "logs/qchatgpt.legacy.log")

log_file_name = "logs/qchatgpt-%s.log" % time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())


Expand Down
Loading