From 82438a99a2c40560ad404e0df62c2959d5d50daf Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Thu, 26 Dec 2024 17:28:42 +0800 Subject: [PATCH] feat: support parameter auto generate --- python/.vscode/launch.json | 11 +++++++---- python/.vscode/settings.json | 16 +++++++++++++++- python/dify_plugin/entities/agent.py | 6 ++++++ python/dify_plugin/entities/tool.py | 17 ++++++++++++++++- python/examples/agent/main.py | 6 +++++- .../agent/strategies/function_calling.yaml | 18 ++++++++++++++++++ 6 files changed, 67 insertions(+), 7 deletions(-) diff --git a/python/.vscode/launch.json b/python/.vscode/launch.json index 26a090e..205c106 100644 --- a/python/.vscode/launch.json +++ b/python/.vscode/launch.json @@ -1,14 +1,17 @@ { "version": "0.2.0", "configurations": [ - { - "name": "Python Current File", + "name": "Agents", "type": "debugpy", "request": "launch", - "program": "${file}", + "program": "${workspaceFolder}/examples/agent/main.py", + "cwd": "${workspaceFolder}/examples/agent", "console": "integratedTerminal", - "justMyCode": false + "justMyCode": false, + "env": { + "GEVENT_SUPPORT": "True" + }, }, { "name": "Tools", diff --git a/python/.vscode/settings.json b/python/.vscode/settings.json index 9bbbc2d..4605512 100644 --- a/python/.vscode/settings.json +++ b/python/.vscode/settings.json @@ -2,6 +2,7 @@ "cSpell.words": [ "abstractmethods", "Configurate", + "dify", "eventloop", "gevent", "hexlify", @@ -14,5 +15,18 @@ "serpapi", "tiktoken", "unhexlify" - ] + ], + "[python]": { + "editor.defaultFormatter": "charliermarsh.ruff", + "editor.formatOnType": true, + "editor.codeActionsOnSave": { + "source.fixAll": "explicit" + } + }, + "ruff.lint.args": [ + "--config=pyproject.toml" + ], + "ruff.organizeImports": true, + "ruff.fixAll": true, + "editor.formatOnSave": true } \ No newline at end of file diff --git a/python/dify_plugin/entities/agent.py b/python/dify_plugin/entities/agent.py index 15155ed..f128ca1 100644 --- a/python/dify_plugin/entities/agent.py +++ b/python/dify_plugin/entities/agent.py @@ -6,6 +6,8 @@ from dify_plugin.entities import I18nObject from dify_plugin.entities.tool import ( CommonParameterType, + ParameterAutoGenerate, + ParameterTemplate, ToolIdentity, ToolInvokeMessage, ToolParameterOption, @@ -38,6 +40,10 @@ class ToolParameterType(str, Enum): name: str = Field(..., description="The name of the parameter") label: I18nObject = Field(..., description="The label presented to the user") type: ToolParameterType = Field(..., description="The type of the parameter") + auto_generate: Optional[ParameterAutoGenerate] = Field( + default=None, description="The auto generate of the parameter" + ) + template: Optional[ParameterTemplate] = Field(default=None, description="The template of the parameter") scope: str | None = None required: Optional[bool] = False default: Optional[Union[int, float, str]] = None diff --git a/python/dify_plugin/entities/tool.py b/python/dify_plugin/entities/tool.py index f1da9db..2317dcb 100644 --- a/python/dify_plugin/entities/tool.py +++ b/python/dify_plugin/entities/tool.py @@ -1,6 +1,6 @@ import base64 import contextlib -from enum import Enum +from enum import Enum, StrEnum from typing import Any, Mapping, Optional, Union import uuid @@ -171,6 +171,17 @@ def transform_id_to_str(cls, value) -> str: return value +class ParameterAutoGenerate(BaseModel): + class Type(StrEnum): + PROMPT_INSTRUCTION = "prompt_instruction" + + type: Type + + +class ParameterTemplate(BaseModel): + enabled: bool = Field(..., description="Whether the parameter is jinja enabled") + + class ToolParameter(BaseModel): class ToolParameterType(str, Enum): STRING = CommonParameterType.STRING.value @@ -193,6 +204,10 @@ class ToolParameterForm(Enum): label: I18nObject = Field(..., description="The label presented to the user") human_description: I18nObject = Field(..., description="The description presented to the user") type: ToolParameterType = Field(..., description="The type of the parameter") + auto_generate: Optional[ParameterAutoGenerate] = Field( + default=None, description="The auto generate of the parameter" + ) + template: Optional[ParameterTemplate] = Field(default=None, description="The template of the parameter") scope: str | None = None form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm") llm_description: Optional[str] = None diff --git a/python/examples/agent/main.py b/python/examples/agent/main.py index 05256da..5c501b6 100644 --- a/python/examples/agent/main.py +++ b/python/examples/agent/main.py @@ -1,6 +1,10 @@ +import sys + +sys.path.append("../..") + from dify_plugin import Plugin, DifyPluginEnv -plugin = Plugin(DifyPluginEnv(MAX_REQUEST_TIMEOUT=30)) +plugin = Plugin(DifyPluginEnv(MAX_REQUEST_TIMEOUT=240)) if __name__ == "__main__": plugin.run() diff --git a/python/examples/agent/strategies/function_calling.yaml b/python/examples/agent/strategies/function_calling.yaml index 74da144..b472424 100644 --- a/python/examples/agent/strategies/function_calling.yaml +++ b/python/examples/agent/strategies/function_calling.yaml @@ -25,6 +25,24 @@ parameters: en_US: Tools list zh_Hans: 工具列表 pt_BR: Tools list + - name: instruction + type: string + required: true + label: + en_US: Instruction + zh_Hans: 指令 + pt_BR: Instruction + auto_generate: + type: prompt_instruction + template: + enabled: true + - name: query + type: string + required: true + label: + en_US: Query + zh_Hans: 查询 + pt_BR: Query extra: python: source: strategies/function_calling.py