Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
ac96d192
Unverified
Commit
ac96d192
authored
Feb 27, 2024
by
Yeuoly
Committed by
GitHub
Feb 27, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: parameter type handling in API tool and parser (#2574)
parent
07fbeb6c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
6 deletions
+43
-6
api_tool.py
api/core/tools/tool/api_tool.py
+1
-1
parser.py
api/core/tools/utils/parser.py
+42
-5
No files found.
api/core/tools/tool/api_tool.py
View file @
ac96d192
...
...
@@ -200,7 +200,7 @@ class ApiTool(Tool):
# replace path parameters
for
name
,
value
in
path_params
.
items
():
url
=
url
.
replace
(
f
'{{{name}}}'
,
value
)
url
=
url
.
replace
(
f
'{{{name}}}'
,
f
'{value}'
)
# parse http body data if needed, for GET/HEAD/OPTIONS/TRACE, the body is ignored
if
'Content-Type'
in
headers
:
...
...
api/core/tools/utils/parser.py
View file @
ac96d192
import
re
import
uuid
from
json
import
loads
as
json_loads
from
requests
import
get
...
...
@@ -46,7 +48,7 @@ class ApiBasedToolSchemaParser:
parameters
=
[]
if
'parameters'
in
interface
[
'operation'
]:
for
parameter
in
interface
[
'operation'
][
'parameters'
]:
parameters
.
append
(
ToolParameter
(
tool_parameter
=
ToolParameter
(
name
=
parameter
[
'name'
],
label
=
I18nObject
(
en_US
=
parameter
[
'name'
],
...
...
@@ -61,7 +63,14 @@ class ApiBasedToolSchemaParser:
form
=
ToolParameter
.
ToolParameterForm
.
LLM
,
llm_description
=
parameter
.
get
(
'description'
),
default
=
parameter
[
'schema'
][
'default'
]
if
'schema'
in
parameter
and
'default'
in
parameter
[
'schema'
]
else
None
,
))
)
# check if there is a type
typ
=
ApiBasedToolSchemaParser
.
_get_tool_parameter_type
(
parameter
)
if
typ
:
tool_parameter
.
type
=
typ
parameters
.
append
(
tool_parameter
)
# create tool bundle
# check if there is a request body
if
'requestBody'
in
interface
[
'operation'
]:
...
...
@@ -80,13 +89,14 @@ class ApiBasedToolSchemaParser:
root
=
root
[
ref
]
# overwrite the content
interface
[
'operation'
][
'requestBody'
][
'content'
][
content_type
][
'schema'
]
=
root
# parse body parameters
if
'schema'
in
interface
[
'operation'
][
'requestBody'
][
'content'
][
content_type
]:
body_schema
=
interface
[
'operation'
][
'requestBody'
][
'content'
][
content_type
][
'schema'
]
required
=
body_schema
[
'required'
]
if
'required'
in
body_schema
else
[]
properties
=
body_schema
[
'properties'
]
if
'properties'
in
body_schema
else
{}
for
name
,
property
in
properties
.
items
():
parameters
.
append
(
ToolParameter
(
tool
=
ToolParameter
(
name
=
name
,
label
=
I18nObject
(
en_US
=
name
,
...
...
@@ -101,7 +111,14 @@ class ApiBasedToolSchemaParser:
form
=
ToolParameter
.
ToolParameterForm
.
LLM
,
llm_description
=
property
[
'description'
]
if
'description'
in
property
else
''
,
default
=
property
[
'default'
]
if
'default'
in
property
else
None
,
))
)
# check if there is a type
typ
=
ApiBasedToolSchemaParser
.
_get_tool_parameter_type
(
property
)
if
typ
:
tool
.
type
=
typ
parameters
.
append
(
tool
)
# check if parameters is duplicated
parameters_count
=
{}
...
...
@@ -119,7 +136,11 @@ class ApiBasedToolSchemaParser:
path
=
interface
[
'path'
]
if
interface
[
'path'
]
.
startswith
(
'/'
):
path
=
interface
[
'path'
][
1
:]
path
=
path
.
replace
(
'/'
,
'_'
)
# remove special characters like / to ensure the operation id is valid ^[a-zA-Z0-9_-]{1,64}$
path
=
re
.
sub
(
r'[^a-zA-Z0-9_-]'
,
''
,
path
)
if
not
path
:
path
=
str
(
uuid
.
uuid4
())
interface
[
'operation'
][
'operationId'
]
=
f
'{path}_{interface["method"]}'
bundles
.
append
(
ApiBasedToolBundle
(
...
...
@@ -134,7 +155,23 @@ class ApiBasedToolSchemaParser:
))
return
bundles
@
staticmethod
def
_get_tool_parameter_type
(
parameter
:
dict
)
->
ToolParameter
.
ToolParameterType
:
parameter
=
parameter
or
{}
typ
=
None
if
'type'
in
parameter
:
typ
=
parameter
[
'type'
]
elif
'schema'
in
parameter
and
'type'
in
parameter
[
'schema'
]:
typ
=
parameter
[
'schema'
][
'type'
]
if
typ
==
'integer'
or
typ
==
'number'
:
return
ToolParameter
.
ToolParameterType
.
NUMBER
elif
typ
==
'boolean'
:
return
ToolParameter
.
ToolParameterType
.
BOOLEAN
elif
typ
==
'string'
:
return
ToolParameter
.
ToolParameterType
.
STRING
@
staticmethod
def
parse_openapi_yaml_to_tool_bundle
(
yaml
:
str
,
extra_info
:
dict
=
None
,
warning
:
dict
=
None
)
->
list
[
ApiBasedToolBundle
]:
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment