plugin refactor: phase 1

This commit is contained in:
binary-husky
2024-05-18 20:23:50 +08:00
parent abf9b5aee5
commit 459c5b2d24
17 changed files with 338 additions and 96 deletions

View File

@@ -0,0 +1,21 @@
from .PDF_Translate import 批量翻译PDF文档
from crazy_functions.plugin_template.plugin_class_template import GptAcademicPluginTemplate, ArgProperty
class PDF_Tran(GptAcademicPluginTemplate):
def __init__(self):
pass
def define_arg_selection_menu(self):
gui_definition = {
"main_input":
ArgProperty(title="PDF文件路径", description="上传文件后,会自动生成路径", default_value="", type="string").model_dump_json(), # 主输入,自动从输入框同步
"advanced_arg":
ArgProperty(title="高级参数输入区", description="", default_value="", type="string").model_dump_json(), # 高级参数输入区,自动同步
"additional_01":
ArgProperty(title="附属参数", description="", default_value="没有附属参数", type="string").model_dump_json(), # 高级参数输入区,自动同步
}
return gui_definition
def execute(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):
print(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request)
yield from 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request)

View File

@@ -0,0 +1,57 @@
import os, json, base64
from pydantic import BaseModel, Field
from textwrap import dedent
class ArgProperty(BaseModel):
title: str = Field(description="The title", default="")
description: str = Field(description="The description", default="")
default_value: str|float = Field(description="The default value", default="")
type: str = Field(description="The type", default="")
class GptAcademicPluginTemplate():
def __init__(self):
# please note that `execute` method may run in different threads,
# thus you should not store any state in the plugin instance,
# which may be accessed by multiple threads
pass
def define_arg_selection_menu(self):
"""
An example as below:
```
def define_arg_selection_menu(self):
gui_definition = {
"main_input":
ArgProperty(title="main input", description="description", default_value="default_value", type="string").model_dump_json(),
"advanced_arg":
ArgProperty(title="advanced arguments", description="description", default_value="default_value", type="string").model_dump_json(),
"additional_arg_01":
ArgProperty(title="additional", description="description", default_value="default_value", type="string").model_dump_json(),
}
return gui_definition
```
"""
raise NotImplementedError("You need to implement this method in your plugin class")
def get_js_code_for_generating_menu(self, btnName):
define_arg_selection = self.define_arg_selection_menu()
if len(define_arg_selection.keys()) > 8:
raise ValueError("You can only have up to 8 arguments in the define_arg_selection")
if "main_input" not in define_arg_selection:
raise ValueError("You must have a 'main_input' in the define_arg_selection")
if "advanced_arg" not in define_arg_selection:
raise ValueError("You must have a 'main_input' in the define_arg_selection")
DEFINE_ARG_INPUT_INTERFACE = json.dumps(define_arg_selection)
return dedent("""
()=>generate_menu("{GUI_JS}", "{BTN_NAME}")
""".format(
GUI_JS=base64.b64encode(DEFINE_ARG_INPUT_INTERFACE.encode('utf-8')).decode('utf-8'),
BTN_NAME=btnName
)
)
def execute(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):
raise NotImplementedError("You need to implement this method in your plugin class")