add
This commit is contained in:
@@ -124,15 +124,17 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
||||
await event.mark_read()
|
||||
await self.client.set_typing(event.room_id, timeout=99999)
|
||||
platform = self.get_ai_platform()
|
||||
|
||||
if platform.is_streaming_enabled():
|
||||
await self.client.set_typing(event.room_id, timeout=0)
|
||||
await self._handle_streaming(event, platform)
|
||||
return
|
||||
|
||||
chat_completion = await platform.create_chat_completion(self, event)
|
||||
self.log.debug(
|
||||
f"发送结果 {chat_completion.message}, {chat_completion.model}, {chat_completion.finish_reason}")
|
||||
# ai gpt调用
|
||||
# 关闭typing提示
|
||||
await self.client.set_typing(event.room_id, timeout=0)
|
||||
# 打开typing提示
|
||||
if chat_completion.result:
|
||||
# if hasattr(chat_completion.message, 'content'):
|
||||
resp_content = chat_completion.message['content']
|
||||
response = TextMessageEventContent(msgtype=MessageType.TEXT, body=resp_content, format=Format.HTML,
|
||||
formatted_body=markdown.render(resp_content))
|
||||
@@ -150,6 +152,48 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
||||
|
||||
return None
|
||||
|
||||
async def _handle_streaming(self, evt: MessageEvent, platform) -> None:
|
||||
# 发送初始占位消息
|
||||
placeholder = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT, body="▌", format=Format.HTML, formatted_body="▌"
|
||||
)
|
||||
response_event_id = await evt.respond(placeholder, in_thread=self.config['reply_in_thread'])
|
||||
|
||||
accumulated = ""
|
||||
last_edit_len = 0
|
||||
EDIT_THRESHOLD = 50 # 每积累50个字符更新一次消息
|
||||
|
||||
try:
|
||||
async for chunk in platform.create_chat_completion_stream(self, evt):
|
||||
accumulated += chunk
|
||||
if len(accumulated) - last_edit_len >= EDIT_THRESHOLD:
|
||||
display = accumulated + " ▌"
|
||||
new_content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT,
|
||||
body=display,
|
||||
format=Format.HTML,
|
||||
formatted_body=markdown.render(display)
|
||||
)
|
||||
new_content.set_edit(response_event_id)
|
||||
await self.client.send_message(evt.room_id, new_content)
|
||||
last_edit_len = len(accumulated)
|
||||
except Exception as e:
|
||||
self.log.exception(f"Streaming error: {e}")
|
||||
if not accumulated:
|
||||
accumulated = f"Streaming error: {e}"
|
||||
|
||||
# 输出最终完整内容
|
||||
if not accumulated:
|
||||
accumulated = "(无响应)"
|
||||
final_content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT,
|
||||
body=accumulated,
|
||||
format=Format.HTML,
|
||||
formatted_body=markdown.render(accumulated)
|
||||
)
|
||||
final_content.set_edit(response_event_id)
|
||||
await self.client.send_message(evt.room_id, final_content)
|
||||
|
||||
def get_ai_platform(self) -> Platform:
|
||||
use_platform = self.config.cur_platform
|
||||
if use_platform == 'openai':
|
||||
|
||||
Reference in New Issue
Block a user