Skip to content

Commit

Permalink
feat: support parameter auto generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Dec 26, 2024
1 parent 463f97a commit 82438a9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
11 changes: 7 additions & 4 deletions python/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 15 additions & 1 deletion python/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"abstractmethods",
"Configurate",
"dify",
"eventloop",
"gevent",
"hexlify",
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions python/dify_plugin/entities/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from dify_plugin.entities import I18nObject
from dify_plugin.entities.tool import (
CommonParameterType,
ParameterAutoGenerate,
ParameterTemplate,
ToolIdentity,
ToolInvokeMessage,
ToolParameterOption,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion python/dify_plugin/entities/tool.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion python/examples/agent/main.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 18 additions & 0 deletions python/examples/agent/strategies/function_calling.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 82438a9

Please sign in to comment.