2024-01-26 15:51:49 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import enum
|
|
|
|
|
import typing
|
2024-01-29 21:22:27 +08:00
|
|
|
|
import datetime
|
|
|
|
|
import asyncio
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
import pydantic
|
|
|
|
|
import mirai
|
|
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
|
from ..provider import entities as llm_entities
|
2024-03-12 16:04:11 +08:00
|
|
|
|
from ..provider.modelmgr import entities
|
2024-01-29 21:22:27 +08:00
|
|
|
|
from ..provider.sysprompt import entities as sysprompt_entities
|
|
|
|
|
from ..provider.tools import entities as tools_entities
|
2024-02-11 23:07:38 +08:00
|
|
|
|
from ..platform import adapter as msadapter
|
2024-01-29 21:22:27 +08:00
|
|
|
|
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
class LauncherTypes(enum.Enum):
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""一个请求的发起者类型"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
PERSON = 'person'
|
|
|
|
|
"""私聊"""
|
|
|
|
|
|
|
|
|
|
GROUP = 'group'
|
|
|
|
|
"""群聊"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Query(pydantic.BaseModel):
|
|
|
|
|
"""一次请求的信息封装"""
|
|
|
|
|
|
|
|
|
|
query_id: int
|
2024-02-01 15:48:26 +08:00
|
|
|
|
"""请求ID,添加进请求池时生成"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
launcher_type: LauncherTypes
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""会话类型,platform处理阶段设置"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
launcher_id: int
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""会话ID,platform处理阶段设置"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
sender_id: int
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""发送者ID,platform处理阶段设置"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
message_event: mirai.MessageEvent
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""事件,platform收到的原始事件"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
|
|
|
|
message_chain: mirai.MessageChain
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""消息链,platform收到的原始消息链"""
|
2024-02-01 15:48:26 +08:00
|
|
|
|
|
2024-02-11 23:07:38 +08:00
|
|
|
|
adapter: msadapter.MessageSourceAdapter
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""消息平台适配器对象,单个app中可能启用了多个消息平台适配器,此对象表明发起此query的适配器"""
|
2024-02-11 23:07:38 +08:00
|
|
|
|
|
2024-02-01 15:48:26 +08:00
|
|
|
|
session: typing.Optional[Session] = None
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""会话对象,由前置处理器阶段设置"""
|
2024-02-01 16:35:00 +08:00
|
|
|
|
|
|
|
|
|
messages: typing.Optional[list[llm_entities.Message]] = []
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""历史消息列表,由前置处理器阶段设置"""
|
2024-02-01 16:35:00 +08:00
|
|
|
|
|
|
|
|
|
prompt: typing.Optional[sysprompt_entities.Prompt] = None
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""情景预设内容,由前置处理器阶段设置"""
|
2024-02-01 16:35:00 +08:00
|
|
|
|
|
|
|
|
|
user_message: typing.Optional[llm_entities.Message] = None
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""此次请求的用户消息对象,由前置处理器阶段设置"""
|
2024-02-01 16:35:00 +08:00
|
|
|
|
|
|
|
|
|
use_model: typing.Optional[entities.LLMModelInfo] = None
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""使用的模型,由前置处理器阶段设置"""
|
2024-02-01 16:35:00 +08:00
|
|
|
|
|
|
|
|
|
use_funcs: typing.Optional[list[tools_entities.LLMFunction]] = None
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""使用的函数,由前置处理器阶段设置"""
|
2024-02-01 15:48:26 +08:00
|
|
|
|
|
2024-05-18 20:08:48 +08:00
|
|
|
|
resp_messages: typing.Optional[list[llm_entities.Message]] | typing.Optional[list[mirai.MessageChain]] = []
|
2024-03-22 11:05:58 +08:00
|
|
|
|
"""由Process阶段生成的回复消息对象列表"""
|
2024-01-26 15:51:49 +08:00
|
|
|
|
|
2024-05-14 23:08:49 +08:00
|
|
|
|
resp_message_chain: typing.Optional[list[mirai.MessageChain]] = None
|
2024-02-01 15:48:26 +08:00
|
|
|
|
"""回复消息链,从resp_messages包装而得"""
|
2024-01-29 21:22:27 +08:00
|
|
|
|
|
2024-07-04 13:03:58 +08:00
|
|
|
|
# ======= 内部保留 =======
|
|
|
|
|
current_stage: "pkg.pipeline.stagemgr.StageInstContainer" = None
|
|
|
|
|
|
2024-02-11 23:07:38 +08:00
|
|
|
|
class Config:
|
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
2024-01-29 21:22:27 +08:00
|
|
|
|
|
|
|
|
|
class Conversation(pydantic.BaseModel):
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""对话,包含于 Session 中,一个 Session 可以有多个历史 Conversation,但只有一个当前使用的 Conversation"""
|
2024-01-29 21:22:27 +08:00
|
|
|
|
|
|
|
|
|
prompt: sysprompt_entities.Prompt
|
|
|
|
|
|
|
|
|
|
messages: list[llm_entities.Message]
|
|
|
|
|
|
|
|
|
|
create_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
|
|
|
|
|
|
|
|
|
update_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
|
|
|
|
|
|
|
|
|
use_model: entities.LLMModelInfo
|
|
|
|
|
|
|
|
|
|
use_funcs: typing.Optional[list[tools_entities.LLMFunction]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Session(pydantic.BaseModel):
|
2024-03-23 22:39:42 +08:00
|
|
|
|
"""会话,一个 Session 对应一个 {launcher_type.value}_{launcher_id}"""
|
2024-01-29 21:22:27 +08:00
|
|
|
|
launcher_type: LauncherTypes
|
|
|
|
|
|
|
|
|
|
launcher_id: int
|
|
|
|
|
|
|
|
|
|
sender_id: typing.Optional[int] = 0
|
|
|
|
|
|
|
|
|
|
use_prompt_name: typing.Optional[str] = 'default'
|
|
|
|
|
|
|
|
|
|
using_conversation: typing.Optional[Conversation] = None
|
|
|
|
|
|
|
|
|
|
conversations: typing.Optional[list[Conversation]] = []
|
|
|
|
|
|
|
|
|
|
create_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
|
|
|
|
|
|
|
|
|
update_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
|
|
|
|
|
|
|
|
|
semaphore: typing.Optional[asyncio.Semaphore] = None
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""当前会话的信号量,用于限制并发"""
|
2024-01-29 21:22:27 +08:00
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
arbitrary_types_allowed = True
|