QChatGPT/pkg/pipeline/preproc/preproc.py

67 lines
1.9 KiB
Python
Raw Normal View History

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
"""请求预处理阶段
签出会话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()
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(
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
)