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
460c0da1
Unverified
Commit
460c0da1
authored
Mar 10, 2024
by
Yeuoly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: jinja2
parent
295a2485
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
6 deletions
+64
-6
code_executor.py
api/core/helper/code_executor/code_executor.py
+5
-2
jina2_transformer.py
api/core/helper/code_executor/jina2_transformer.py
+54
-1
template_transform_node.py
...kflow/nodes/template_transform/template_transform_node.py
+4
-2
code_executor.py
.../integration_tests/workflow/nodes/__mock/code_executor.py
+1
-1
No files found.
api/core/helper/code_executor/code_executor.py
View file @
460c0da1
...
@@ -4,6 +4,7 @@ from typing import Literal, Optional
...
@@ -4,6 +4,7 @@ from typing import Literal, Optional
from
httpx
import
post
from
httpx
import
post
from
pydantic
import
BaseModel
from
pydantic
import
BaseModel
from
yarl
import
URL
from
yarl
import
URL
from
core.helper.code_executor.jina2_transformer
import
Jinja2TemplateTransformer
from
core.helper.code_executor.python_transformer
import
PythonTemplateTransformer
from
core.helper.code_executor.python_transformer
import
PythonTemplateTransformer
...
@@ -25,7 +26,7 @@ class CodeExecutionResponse(BaseModel):
...
@@ -25,7 +26,7 @@ class CodeExecutionResponse(BaseModel):
class
CodeExecutor
:
class
CodeExecutor
:
@
classmethod
@
classmethod
def
execute_code
(
cls
,
language
:
Literal
[
'python3'
,
'javascript'
,
'jina2'
],
code
:
str
,
inputs
:
dict
)
->
dict
:
def
execute_code
(
cls
,
language
:
Literal
[
'python3'
,
'javascript'
,
'jin
j
a2'
],
code
:
str
,
inputs
:
dict
)
->
dict
:
"""
"""
Execute code
Execute code
:param language: code language
:param language: code language
...
@@ -36,6 +37,8 @@ class CodeExecutor:
...
@@ -36,6 +37,8 @@ class CodeExecutor:
template_transformer
=
None
template_transformer
=
None
if
language
==
'python3'
:
if
language
==
'python3'
:
template_transformer
=
PythonTemplateTransformer
template_transformer
=
PythonTemplateTransformer
elif
language
==
'jinja2'
:
template_transformer
=
Jinja2TemplateTransformer
else
:
else
:
raise
CodeExecutionException
(
'Unsupported language'
)
raise
CodeExecutionException
(
'Unsupported language'
)
...
@@ -46,7 +49,7 @@ class CodeExecutor:
...
@@ -46,7 +49,7 @@ class CodeExecutor:
'X-Api-Key'
:
CODE_EXECUTION_API_KEY
'X-Api-Key'
:
CODE_EXECUTION_API_KEY
}
}
data
=
{
data
=
{
'language'
:
language
,
'language'
:
language
if
language
!=
'jinja2'
else
'python3'
,
'code'
:
runner
,
'code'
:
runner
,
}
}
...
...
api/core/helper/code_executor/jina2_transformer.py
View file @
460c0da1
# TODO
import
json
\ No newline at end of file
import
re
from
core.helper.code_executor.template_transformer
import
TemplateTransformer
PYTHON_RUNNER
=
"""
import jinja2
template = jinja2.Template('''{{code}}''')
def main(**inputs):
return template.render(**inputs)
# execute main function, and return the result
output = main(**{{inputs}})
result = f'''<<RESULT>>{output}<<RESULT>>'''
print(result)
"""
class
Jinja2TemplateTransformer
(
TemplateTransformer
):
@
classmethod
def
transform_caller
(
cls
,
code
:
str
,
inputs
:
dict
)
->
str
:
"""
Transform code to python runner
:param code: code
:param inputs: inputs
:return:
"""
# transform jinja2 template to python code
runner
=
PYTHON_RUNNER
.
replace
(
'{{code}}'
,
code
)
runner
=
runner
.
replace
(
'{{inputs}}'
,
json
.
dumps
(
inputs
,
indent
=
4
))
return
runner
@
classmethod
def
transform_response
(
cls
,
response
:
str
)
->
dict
:
"""
Transform response to dict
:param response: response
:return:
"""
# extract result
result
=
re
.
search
(
r'<<RESULT>>(.*)<<RESULT>>'
,
response
,
re
.
DOTALL
)
if
not
result
:
raise
ValueError
(
'Failed to parse result'
)
result
=
result
.
group
(
1
)
return
{
'result'
:
result
}
\ No newline at end of file
api/core/workflow/nodes/template_transform/template_transform_node.py
View file @
460c0da1
...
@@ -52,7 +52,7 @@ class TemplateTransformNode(BaseNode):
...
@@ -52,7 +52,7 @@ class TemplateTransformNode(BaseNode):
# Run code
# Run code
try
:
try
:
result
=
CodeExecutor
.
execute_code
(
result
=
CodeExecutor
.
execute_code
(
language
=
'jina2'
,
language
=
'jin
j
a2'
,
code
=
node_data
.
template
,
code
=
node_data
.
template
,
inputs
=
variables
inputs
=
variables
)
)
...
@@ -66,7 +66,9 @@ class TemplateTransformNode(BaseNode):
...
@@ -66,7 +66,9 @@ class TemplateTransformNode(BaseNode):
return
NodeRunResult
(
return
NodeRunResult
(
status
=
WorkflowNodeExecutionStatus
.
SUCCEEDED
,
status
=
WorkflowNodeExecutionStatus
.
SUCCEEDED
,
inputs
=
variables
,
inputs
=
variables
,
outputs
=
result
[
'result'
]
outputs
=
{
'output'
:
result
[
'result'
]
}
)
)
@
classmethod
@
classmethod
...
...
api/tests/integration_tests/workflow/nodes/__mock/code_executor.py
View file @
460c0da1
...
@@ -9,7 +9,7 @@ MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
...
@@ -9,7 +9,7 @@ MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
class
MockedCodeExecutor
:
class
MockedCodeExecutor
:
@
classmethod
@
classmethod
def
invoke
(
cls
,
language
:
Literal
[
'python3'
,
'javascript'
,
'jina2'
],
code
:
str
,
inputs
:
dict
)
->
dict
:
def
invoke
(
cls
,
language
:
Literal
[
'python3'
,
'javascript'
,
'jin
j
a2'
],
code
:
str
,
inputs
:
dict
)
->
dict
:
# invoke directly
# invoke directly
if
language
==
'python3'
:
if
language
==
'python3'
:
return
{
return
{
...
...
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