Skip to content

Commit

Permalink
fix: arguments no longer required for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benhid authored and jfaldanam committed May 16, 2024
1 parent fef1f66 commit e1cf699
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/eidos/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def list_functions_names() -> list[str]:
return [fn["name"] for fn in available_functions()]


def execute(function_name: str, arguments: dict) -> dict[str, Any]:
def execute(function_name: str, arguments: dict | None) -> dict[str, Any]:
"""
Executes an AI function.
Expand All @@ -96,19 +96,20 @@ def execute(function_name: str, arguments: dict) -> dict[str, Any]:
except FileNotFoundError:
raise FileNotFoundError("Error: function module not found.")

try:
arguments = validate_input_schema(
arguments, schema=function_definition["parameters"]
)
except (ValueError, TypeError) as e:
raise ValueError(f"Error: function arguments are malformed.\n{str(e)}")
# Validate input arguments against the function's schema.
if arguments:
try:
arguments = validate_input_schema(
arguments, schema=function_definition["parameters"]
)
except (ValueError, TypeError) as e:
raise ValueError(f"Error: function arguments are malformed.\n{str(e)}")

try:
fn = import_function(function_definition["module"])
result = fn(**arguments)
result = fn(**arguments) if arguments else fn()
except Exception as e:
log.error("Error executing function", e)
raise Exception("Error: function execution failed.")
raise Exception(f"Error: function execution failed.\n{str(e)}")

try:
validated_result = validate_output_schema(
Expand Down
2 changes: 1 addition & 1 deletion src/eidos/routes/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
response_model=dict[str, Any],
)
async def execute_endpoint(
function_name: str, arguments: dict, _: str = Security(query_scheme)
function_name: str, arguments: dict | None = None, _: str = Security(query_scheme)
) -> JSONResponse:
"""Executes an AI function with the given arguments."""
try:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def test_function_execute():
}


def test_function_execute_with_no_arguments():
response = client.post("/api/v1/execution/salute")
assert response.status_code == 500
assert response.json() == {
"data": None,
"status": {
"code": 500,
"message": "Error: function execution failed.\nsalute() missing 1 required positional argument: 'who'",
},
}


def test_function_execute_missing():
response = client.post("/api/v1/execution/nonexistent", json={})
assert response.status_code == 500
Expand Down

0 comments on commit e1cf699

Please sign in to comment.