2024-02-01 16:35:00 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from .. import stage, entities, stagemgr
|
|
|
|
|
from ...core import entities as core_entities
|
|
|
|
|
from ...provider import entities as llm_entities
|
|
|
|
|
from ...plugin import events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@stage.stage_class("PreProcessor")
|
|
|
|
|
class PreProcessor(stage.PipelineStage):
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""请求预处理阶段
|
2024-05-14 22:20:31 +08:00
|
|
|
|
|
|
|
|
|
签出会话、prompt、上文、模型、内容函数。
|
|
|
|
|
|
|
|
|
|
改写:
|
|
|
|
|
- session
|
|
|
|
|
- prompt
|
|
|
|
|
- messages
|
|
|
|
|
- user_message
|
|
|
|
|
- use_model
|
|
|
|
|
- use_funcs
|
2024-02-01 16:35:00 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
async def process(
|
|
|
|
|
self,
|
|
|
|
|
query: core_entities.Query,
|
|
|
|
|
stage_inst_name: str,
|
|
|
|
|
) -> entities.StageProcessResult:
|
|
|
|
|
"""处理
|
|
|
|
|
"""
|
|
|
|
|
session = await self.ap.sess_mgr.get_session(query)
|
|
|
|
|
|
|
|
|
|
conversation = await self.ap.sess_mgr.get_conversation(session)
|
|
|
|
|
|
|
|
|
|
# 从会话取出消息和情景预设到query
|
|
|
|
|
query.session = session
|
|
|
|
|
query.prompt = conversation.prompt.copy()
|
|
|
|
|
query.messages = conversation.messages.copy()
|
|
|
|
|
|
2024-05-14 22:20:31 +08:00
|
|
|
|
query.user_message = llm_entities.Message( # TODO 适配多模态输入
|
2024-02-01 16:35:00 +08:00
|
|
|
|
role='user',
|
|
|
|
|
content=str(query.message_chain).strip()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
query.use_model = conversation.use_model
|
|
|
|
|
|
|
|
|
|
query.use_funcs = conversation.use_funcs
|
|
|
|
|
|
|
|
|
|
# =========== 触发事件 PromptPreProcessing
|
|
|
|
|
|
|
|
|
|
event_ctx = await self.ap.plugin_mgr.emit_event(
|
|
|
|
|
event=events.PromptPreProcessing(
|
2024-05-14 22:20:31 +08:00
|
|
|
|
session_name=f'{query.session.launcher_type.value}_{query.session.launcher_id}',
|
2024-02-01 16:35:00 +08:00
|
|
|
|
default_prompt=query.prompt.messages,
|
|
|
|
|
prompt=query.messages,
|
|
|
|
|
query=query
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
query.prompt.messages = event_ctx.event.default_prompt
|
|
|
|
|
query.messages = event_ctx.event.prompt
|
|
|
|
|
|
|
|
|
|
return entities.StageProcessResult(
|
|
|
|
|
result_type=entities.ResultType.CONTINUE,
|
|
|
|
|
new_query=query
|
2024-02-01 17:42:51 +08:00
|
|
|
|
)
|