Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Aug 27, 2024
1 parent ee7d5e7 commit 604864f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
19 changes: 17 additions & 2 deletions api/configs/feature/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from pydantic import AliasChoices, Field, NegativeInt, NonNegativeInt, PositiveInt, computed_field
from pydantic import AliasChoices, Field, HttpUrl, NegativeInt, NonNegativeInt, PositiveInt, computed_field
from pydantic_settings import BaseSettings

from configs.feature.hosted_service import HostedServiceConfig
Expand Down Expand Up @@ -45,7 +45,7 @@ class CodeExecutionSandboxConfig(BaseSettings):
Code Execution Sandbox configs
"""

CODE_EXECUTION_ENDPOINT: str = Field(
CODE_EXECUTION_ENDPOINT: HttpUrl = Field(
description="endpoint URL of code execution servcie",
default="http://sandbox:8194",
)
Expand All @@ -55,6 +55,21 @@ class CodeExecutionSandboxConfig(BaseSettings):
default="dify-sandbox",
)

CODE_EXECUTION_CONNECT_TIMEOUT: Optional[float] = Field(
description="connect timeout in seconds for code execution request, default to 10.0",
default=10.0,
)

CODE_EXECUTION_READ_TIMEOUT: Optional[float] = Field(
description="read timeout in seconds for code execution request, default to 60.0",
default=60.0,
)

CODE_EXECUTION_WRITE_TIMEOUT: Optional[float] = Field(
description="write timeout in seconds for code execution request, default to 10.0",
default=10.0,
)

CODE_MAX_NUMBER: PositiveInt = Field(
description="max depth for code execution",
default=9223372036854775807,
Expand Down
26 changes: 12 additions & 14 deletions api/core/helper/code_executor/code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@

logger = logging.getLogger(__name__)

# Code Executor
CODE_EXECUTION_ENDPOINT = dify_config.CODE_EXECUTION_ENDPOINT
CODE_EXECUTION_API_KEY = dify_config.CODE_EXECUTION_API_KEY

CODE_EXECUTION_TIMEOUT = Timeout(connect=10, write=10, read=60, pool=None)

class CodeExecutionException(Exception):
pass

Expand Down Expand Up @@ -71,10 +65,10 @@ def execute_code(cls,
:param code: code
:return:
"""
url = URL(CODE_EXECUTION_ENDPOINT) / 'v1' / 'sandbox' / 'run'
url = URL(str(dify_config.CODE_EXECUTION_ENDPOINT)) / 'v1' / 'sandbox' / 'run'

headers = {
'X-Api-Key': CODE_EXECUTION_API_KEY
'X-Api-Key': dify_config.CODE_EXECUTION_API_KEY
}

data = {
Expand All @@ -85,7 +79,12 @@ def execute_code(cls,
}

try:
response = post(str(url), json=data, headers=headers, timeout=CODE_EXECUTION_TIMEOUT)
response = post(str(url), json=data, headers=headers,
timeout=Timeout(
connect=dify_config.CODE_EXECUTION_CONNECT_TIMEOUT,
read=dify_config.CODE_EXECUTION_READ_TIMEOUT,
write=dify_config.CODE_EXECUTION_WRITE_TIMEOUT,
pool=None))
if response.status_code == 503:
raise CodeExecutionException('Code execution service is unavailable')
elif response.status_code != 200:
Expand All @@ -96,20 +95,20 @@ def execute_code(cls,
raise CodeExecutionException('Failed to execute code, which is likely a network issue,'
' please check if the sandbox service is running.'
f' ( Error: {str(e)} )')

try:
response = response.json()
except:
raise CodeExecutionException('Failed to parse response')

if (code := response.get('code')) != 0:
raise CodeExecutionException(f"Got error code: {code}. Got error msg: {response.get('message')}")

response = CodeExecutionResponse(**response)

if response.data.error:
raise CodeExecutionException(response.data.error)

return response.data.stdout or ''

@classmethod
Expand All @@ -133,4 +132,3 @@ def execute_workflow_code_template(cls, language: CodeLanguage, code: str, input
raise e

return template_transformer.transform_response(response)

2 changes: 2 additions & 0 deletions api/tests/unit_tests/configs/test_dify_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ def test_flask_configs(example_env_file):
assert config["CONSOLE_WEB_URL"] == "https://example.com"
assert config["CONSOLE_CORS_ALLOW_ORIGINS"] == ["https://example.com"]
assert config["WEB_API_CORS_ALLOW_ORIGINS"] == ["*"]

assert str(config["CODE_EXECUTION_ENDPOINT"]) == "http://sandbox:8194"

0 comments on commit 604864f

Please sign in to comment.