2024-01-27 00:06:38 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import abc
|
2024-03-12 16:22:07 +00:00
|
|
|
|
import typing
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
|
from ...core import app
|
|
|
|
|
|
from . import entities
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-12 16:22:07 +00:00
|
|
|
|
preregistered_loaders: list[typing.Type[PromptLoader]] = []
|
|
|
|
|
|
|
|
|
|
|
|
def loader_class(name: str):
|
|
|
|
|
|
|
|
|
|
|
|
def decorator(cls: typing.Type[PromptLoader]) -> typing.Type[PromptLoader]:
|
|
|
|
|
|
cls.name = name
|
|
|
|
|
|
preregistered_loaders.append(cls)
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-27 00:06:38 +08:00
|
|
|
|
class PromptLoader(metaclass=abc.ABCMeta):
|
|
|
|
|
|
"""Prompt加载器抽象类
|
|
|
|
|
|
"""
|
2024-03-12 16:22:07 +00:00
|
|
|
|
name: str
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
|
ap: app.Application
|
|
|
|
|
|
|
|
|
|
|
|
prompts: list[entities.Prompt]
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, ap: app.Application):
|
|
|
|
|
|
self.ap = ap
|
|
|
|
|
|
self.prompts = []
|
|
|
|
|
|
|
|
|
|
|
|
async def initialize(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
|
async def load(self):
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""加载Prompt,存放到prompts列表中
|
2024-01-27 00:06:38 +08:00
|
|
|
|
"""
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
def get_prompts(self) -> list[entities.Prompt]:
|
|
|
|
|
|
"""获取Prompt列表
|
|
|
|
|
|
"""
|
|
|
|
|
|
return self.prompts
|