Repository navigation
Expand file tree
/
Copy pathplugin_system.py
More file actions
104 lines (76 loc) · 3.02 KB
/
Copy pathplugin_system.py
File metadata and controls
104 lines (76 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, List, Sequence, Tuple
from nonebot.log import logger
from .models import HistoryEntry
@dataclass
class LLMRequestPayload:
"""Payload passed to plugins before calling the language model."""
prompt: str
history: Sequence[HistoryEntry]
sender: str
latest_message: str
images: List[str] = field(default_factory=list)
extra: Dict[str, Any] = field(default_factory=dict)
@dataclass
class LLMResponsePayload:
"""Payload passed to plugins after receiving the language model output."""
content: str
request: LLMRequestPayload
post_messages: List[Any] = field(default_factory=list)
extra: Dict[str, Any] = field(default_factory=dict)
class SimpleGPTPlugin:
"""Base class for pipeline plugins."""
priority: int = 0
async def before_llm_request(self, payload: LLMRequestPayload) -> LLMRequestPayload:
"""Modify the payload before the OpenAI request."""
return payload
async def after_llm_response(
self, payload: LLMResponsePayload
) -> LLMResponsePayload:
"""Modify the payload after receiving the OpenAI response."""
return payload
class PluginManager:
def __init__(self) -> None:
self._plugins: List[Tuple[int, SimpleGPTPlugin]] = []
def register(self, plugin: SimpleGPTPlugin, *, priority: int | None = None) -> None:
plugin_priority = (
priority if priority is not None else getattr(plugin, "priority", 0)
)
self._plugins.append((plugin_priority, plugin))
self._plugins.sort(key=lambda item: item[0], reverse=True)
self._log_plugin_order()
def _log_plugin_order(self) -> None:
if not self._plugins:
logger.info("simple-gpt: 当前未加载任何插件。")
return
order = ", ".join(
f"{plugin.__class__.__name__}(priority={priority})"
for priority, plugin in self._plugins
)
logger.info(f"simple-gpt: 插件加载顺序 -> {order}")
async def run_before_llm_request(
self, payload: LLMRequestPayload
) -> LLMRequestPayload:
for _, plugin in self._plugins:
payload = await plugin.before_llm_request(payload)
return payload
async def run_after_llm_response(
self, payload: LLMResponsePayload
) -> LLMResponsePayload:
for _, plugin in self._plugins:
payload = await plugin.after_llm_response(payload)
return payload
plugin_manager = PluginManager()
def register_simple_gpt_plugin(
plugin: SimpleGPTPlugin, *, priority: int | None = None
) -> None:
plugin_manager.register(plugin, priority=priority)
async def emit_before_llm_request(
payload: LLMRequestPayload,
) -> LLMRequestPayload:
return await plugin_manager.run_before_llm_request(payload)
async def emit_after_llm_response(
payload: LLMResponsePayload,
) -> LLMResponsePayload:
return await plugin_manager.run_after_llm_response(payload)