feat: 为NormalMessageResponded事件添加finish_reason参数

This commit is contained in:
RockChinQ 2023-08-01 14:31:38 +08:00
parent 7930370aa9
commit 8a67fcf40f
5 changed files with 23 additions and 8 deletions

3
.gitignore vendored
View File

@ -26,4 +26,5 @@ bin/
test_*
venv/
hugchat.json
qcapi
qcapi
/*.yaml

View File

@ -194,8 +194,15 @@ class Session:
# 请求回复
# 这个函数是阻塞的
def append(self, text: str=None) -> str:
"""向session中添加一条消息返回接口回复"""
def append(self, text: str=None) -> tuple[str, str]:
"""向session中添加一条消息返回接口回复
Args:
text (str): 用户消息
Returns:
tuple[str, str]: (接口回复, finish_reason)
"""
self.last_interact_timestamp = int(time.time())
@ -209,7 +216,7 @@ class Session:
event = pkg.plugin.host.emit(plugin_models.SessionFirstMessageReceived, **args)
if event.is_prevented_default():
return None
return None, None
config = pkg.utils.context.get_config()
max_length = config.prompt_submit_length
@ -244,7 +251,12 @@ class Session:
total_tokens = 0
finish_reason: str = ""
for resp in pkg.utils.context.get_openai_manager().request_completion(prompts):
finish_reason = resp['choices'][0]['finish_reason']
if resp['choices'][0]['message']['type'] == 'text': # 普通回复
res_text += resp['choices'][0]['message']['content']
@ -305,7 +317,7 @@ class Session:
self.just_switched_to_exist_session = False
self.set_ongoing()
return res_ans if res_ans[0] != '\n' else res_ans[1:]
return res_ans if res_ans[0] != '\n' else res_ans[1:], finish_reason
# 删除上一回合并返回上一回合的问题
def undo(self) -> str:

View File

@ -88,6 +88,7 @@ NormalMessageResponded = "normal_message_responded"
session: pkg.openai.session.Session 会话对象
prefix: str 回复文字消息的前缀
response_text: str 响应文本
finish_reason: str 响应结束原因
returns (optional):
prefix: str 修改后的回复文字消息的前缀

View File

@ -20,7 +20,7 @@ class ContinueCommand(AbstractCommandNode):
session = pkg.openai.session.get_session(session_name)
text = session.append()
text, _ = session.append()
reply = [text]

View File

@ -40,7 +40,7 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str,
try:
prefix = "[GPT]" if config.show_prefix else ""
text = session.append(text_message)
text, finish_reason = session.append(text_message)
# 触发插件事件
args = {
@ -49,7 +49,8 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str,
"sender_id": sender_id,
"session": session,
"prefix": prefix,
"response_text": text
"response_text": text,
"finish_reason": finish_reason,
}
event = pkg.plugin.host.emit(plugin_models.NormalMessageResponded, **args)