mirror of
https://github.com/langgenius/dify.git
synced 2026-03-11 02:07:50 +08:00
fix: code node dose not work as expected
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
from os import environ
|
||||
from typing import Literal
|
||||
from typing import Literal, Optional
|
||||
|
||||
from httpx import post
|
||||
from pydantic import BaseModel
|
||||
@ -16,8 +16,8 @@ class CodeExecutionException(Exception):
|
||||
|
||||
class CodeExecutionResponse(BaseModel):
|
||||
class Data(BaseModel):
|
||||
stdout: str
|
||||
stderr: str
|
||||
stdout: Optional[str]
|
||||
error: Optional[str]
|
||||
|
||||
code: int
|
||||
message: str
|
||||
@ -58,9 +58,9 @@ class CodeExecutor:
|
||||
raise Exception('Failed to execute code')
|
||||
except CodeExecutionException as e:
|
||||
raise e
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
raise CodeExecutionException('Failed to execute code')
|
||||
|
||||
|
||||
try:
|
||||
response = response.json()
|
||||
except:
|
||||
@ -71,7 +71,7 @@ class CodeExecutor:
|
||||
if response.code != 0:
|
||||
raise CodeExecutionException(response.message)
|
||||
|
||||
if response.data.stderr:
|
||||
raise CodeExecutionException(response.data.stderr)
|
||||
if response.data.error:
|
||||
raise CodeExecutionException(response.data.error)
|
||||
|
||||
return template_transformer.transform_response(response.data.stdout)
|
||||
@ -11,11 +11,11 @@ PYTHON_RUNNER = """# declare main function here
|
||||
output = main(**{{inputs}})
|
||||
|
||||
# convert output to json and print
|
||||
result = '''
|
||||
<<RESULT>>
|
||||
output = json.dumps(output, indent=4)
|
||||
|
||||
result = f'''<<RESULT>>
|
||||
{output}
|
||||
<<RESULT>>
|
||||
'''
|
||||
<<RESULT>>'''
|
||||
|
||||
print(result)
|
||||
"""
|
||||
@ -47,11 +47,9 @@ class PythonTemplateTransformer(TemplateTransformer):
|
||||
: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 json.loads(result)
|
||||
|
||||
Reference in New Issue
Block a user