File size: 7,926 Bytes
3d3d712 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import os
from dataclasses import dataclass, field
from datetime import timedelta
from typing import Any, Dict, List, Optional, Tuple
from injector import Module, provider
from taskweaver.config.module_config import ModuleConfig
from taskweaver.misc.component_registry import ComponentRegistry
from taskweaver.utils import read_yaml, validate_yaml
@dataclass
class PluginParameter:
"""PluginParameter is the data structure for plugin parameters (including arguments and return values.)"""
name: str = ""
type: str = "None"
required: bool = False
description: Optional[str] = None
@staticmethod
def from_dict(d: Dict[str, Any]):
return PluginParameter(
name=d["name"],
description=d["description"],
required=d["required"] if "required" in d else False,
type=d["type"] if "type" in d else "Any",
)
def format_prompt(self, indent: int = 0) -> str:
lines: List[str] = []
def line(cnt: str):
lines.append(" " * indent + cnt)
line(f"- name: {self.name}")
line(f" type: {self.type}")
line(f" required: {self.required}")
line(f" description: {self.description}")
return "\n".join(lines)
@dataclass
class PluginSpec:
"""PluginSpec is the data structure for plugin specification defined in the yaml files."""
name: str = ""
description: str = ""
args: List[PluginParameter] = field(default_factory=list)
returns: List[PluginParameter] = field(default_factory=list)
embedding: List[float] = field(default_factory=list)
@staticmethod
def from_dict(d: Dict[str, Any]):
return PluginSpec(
name=d["name"],
description=d["description"],
args=[PluginParameter.from_dict(p) for p in d["parameters"]],
returns=[PluginParameter.from_dict(p) for p in d["returns"]],
embedding=[],
)
def format_prompt(self) -> str:
def normalize_type(t: str) -> str:
if t.lower() == "string":
return "str"
if t.lower() == "integer":
return "int"
return t
def normalize_description(d: str) -> str:
d = d.strip().replace("\n", "\n# ")
return d
def normalize_value(v: PluginParameter) -> PluginParameter:
return PluginParameter(
name=v.name,
type=normalize_type(v.type),
required=v.required,
description=normalize_description(v.description or ""),
)
def format_arg_val(val: PluginParameter) -> str:
val = normalize_value(val)
type_val = f"Optional[{val.type}]" if val.type != "Any" and not val.required else "Any"
if val.description is not None:
return f"\n# {val.description}\n{val.name}: {type_val}"
return f"{val.name}: {type_val}"
param_list = ",".join([format_arg_val(p) for p in self.args])
return_type = ""
if len(self.returns) > 1:
def format_return_val(val: PluginParameter) -> str:
val = normalize_value(val)
if val.description is not None:
return f"\n# {val.name}: {val.description}\n{val.type}"
return val.type
return_type = f"Tuple[{','.join([format_return_val(r) for r in self.returns])}]"
elif len(self.returns) == 1:
rv = normalize_value(self.returns[0])
if rv.description is not None:
return_type = f"\\\n# {rv.name}: {rv.description}\n{rv.type}"
return_type = rv.type
else:
return_type = "None"
return f"# {self.description}\ndef {self.name}({param_list}) -> {return_type}:...\n"
@dataclass
class PluginEntry:
name: str
plugin_only: bool
impl: str
spec: PluginSpec
config: Dict[str, Any]
required: bool
enabled: bool = True
@staticmethod
def from_yaml_file(path: str) -> Optional["PluginEntry"]:
content = read_yaml(path)
return PluginEntry.from_yaml_content(content)
@staticmethod
def from_yaml_content(content: Dict) -> Optional["PluginEntry"]:
do_validate = False
valid_state = False
if do_validate:
valid_state = validate_yaml(content, schema="plugin_schema")
if not do_validate or valid_state:
spec: PluginSpec = PluginSpec.from_dict(content)
return PluginEntry(
name=spec.name,
impl=content.get("code", spec.name),
spec=spec,
config=content.get("configurations", {}),
required=content.get("required", False),
enabled=content.get("enabled", True),
plugin_only=content.get("plugin_only", False),
)
return None
def format_prompt(self) -> str:
return self.spec.format_prompt()
def to_dict(self):
return {
"name": self.name,
"impl": self.impl,
"spec": self.spec,
"config": self.config,
"required": self.required,
"enabled": self.enabled,
}
def format_function_calling(self) -> Dict:
assert self.plugin_only is True, "Only `plugin_only` plugins can be called in this way."
def map_type(t: str) -> str:
if t.lower() == "string" or t.lower() == "str" or t.lower() == "text":
return "string"
if t.lower() == "integer" or t.lower() == "int":
return "integer"
if t.lower() == "float" or t.lower() == "double" or t.lower() == "number":
return "number"
if t.lower() == "boolean" or t.lower() == "bool":
return "boolean"
if t.lower() == "null" or t.lower() == "none":
return "null"
raise Exception(f"unknown type {t}")
function = {"type": "function", "function": {}}
required_params = []
function["function"]["name"] = self.name
function["function"]["description"] = self.spec.description
function["function"]["parameters"] = {"type": "object", "properties": {}}
for arg in self.spec.args:
function["function"]["parameters"]["properties"][arg.name] = {
"type": map_type(arg.type),
"description": arg.description,
}
if arg.required:
required_params.append(arg.name)
function["function"]["parameters"]["required"] = required_params
return function
class PluginRegistry(ComponentRegistry[PluginEntry]):
def __init__(
self,
file_glob: str,
ttl: Optional[timedelta] = None,
) -> None:
super().__init__(file_glob, ttl)
def _load_component(self, path: str) -> Tuple[str, PluginEntry]:
entry: Optional[PluginEntry] = PluginEntry.from_yaml_file(path)
if entry is None:
raise Exception(f"failed to loading plugin from {path}")
if not entry.enabled:
raise Exception(f"plugin {entry.name} is disabled")
return entry.name, entry
class PluginModuleConfig(ModuleConfig):
def _configure(self) -> None:
self._set_name("plugin")
app_dir = self.src.app_base_path
self.base_path = self._get_path(
"base_path",
os.path.join(
app_dir,
"plugins",
),
)
class PluginModule(Module):
@provider
def provide_plugin_registry(
self,
config: PluginModuleConfig,
) -> PluginRegistry:
import os
file_glob = os.path.join(config.base_path, "*.yaml")
return PluginRegistry(
file_glob=file_glob,
ttl=timedelta(minutes=10),
)
|