Skip to content

Commit

Permalink
Ensure the skeleton code does not contain code implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Oct 11, 2023
1 parent b9d690c commit f068de5
Show file tree
Hide file tree
Showing 3 changed files with 824 additions and 44 deletions.
40 changes: 34 additions & 6 deletions faststream_gen/_code_generator/app_skeleton_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/App_Skeleton_Generator.ipynb.

# %% auto 0
__all__ = ['logger', 'generate_app_skeleton']
__all__ = ['logger', 'CODE_CONTAINS_IMPLEMENTATION_ERROR', 'has_function_implementation', 'generate_app_skeleton']

# %% ../../nbs/App_Skeleton_Generator.ipynb 1
from typing import *
import time
import json
import ast
from pathlib import Path
from collections import defaultdict

Expand All @@ -30,16 +31,43 @@
logger = get_logger(__name__)

# %% ../../nbs/App_Skeleton_Generator.ipynb 5
CODE_CONTAINS_IMPLEMENTATION_ERROR = "Error: The response contains code implementation. Rewrite the skeleton code without implementing the business logic for the functions. Ensure the new code has only google styled docstring describing the business logic step by step and raise NotImplementedError()"

# %% ../../nbs/App_Skeleton_Generator.ipynb 6
def has_function_implementation(
node: Union[ast.AsyncFunctionDef, ast.FunctionDef]
) -> bool:
return len(node.body) == 2 and isinstance(node.body[-1], ast.Raise)


def _check_response_for_implementation(response: str) -> List[str]:
parsed = ast.parse(response)
function_nodes = [
node
for node in ast.walk(parsed)
if isinstance(node, ast.AsyncFunctionDef) or isinstance(node, ast.FunctionDef)
]
ret_val = (
[]
if all(has_function_implementation(node) for node in function_nodes)
else [CODE_CONTAINS_IMPLEMENTATION_ERROR]
)
return ret_val

# %% ../../nbs/App_Skeleton_Generator.ipynb 9
def _validate_response(
response: str, output_directory: str, **kwargs: Dict[str, Any]
) -> List[str]:
target_file_name = Path(output_directory) / APPLICATION_FILE_PATH
write_file_contents(str(target_file_name), response)
ret_val = validate_python_code(str(target_file_name))

return ret_val
code_import_errors = validate_python_code(str(target_file_name))

if len(code_import_errors) != 0:
return code_import_errors

return _check_response_for_implementation(response)

# %% ../../nbs/App_Skeleton_Generator.ipynb 8
# %% ../../nbs/App_Skeleton_Generator.ipynb 15
@retry_on_error() # type: ignore
def _generate(
model: str,
Expand Down Expand Up @@ -72,7 +100,7 @@ def _generate(
else validator_result
)

# %% ../../nbs/App_Skeleton_Generator.ipynb 11
# %% ../../nbs/App_Skeleton_Generator.ipynb 18
def generate_app_skeleton(
validated_description: str,
output_directory: str,
Expand Down
8 changes: 6 additions & 2 deletions faststream_gen/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
'faststream_gen/_code_generator/app_and_test_generator.py')},
'faststream_gen._code_generator.app_description_validator': { 'faststream_gen._code_generator.app_description_validator.validate_app_description': ( 'app_description_validator.html#validate_app_description',
'faststream_gen/_code_generator/app_description_validator.py')},
'faststream_gen._code_generator.app_skeleton_generator': { 'faststream_gen._code_generator.app_skeleton_generator._generate': ( 'app_skeleton_generator.html#_generate',
'faststream_gen._code_generator.app_skeleton_generator': { 'faststream_gen._code_generator.app_skeleton_generator._check_response_for_implementation': ( 'app_skeleton_generator.html#_check_response_for_implementation',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator._generate': ( 'app_skeleton_generator.html#_generate',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator._validate_response': ( 'app_skeleton_generator.html#_validate_response',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator.generate_app_skeleton': ( 'app_skeleton_generator.html#generate_app_skeleton',
'faststream_gen/_code_generator/app_skeleton_generator.py')},
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator.has_function_implementation': ( 'app_skeleton_generator.html#has_function_implementation',
'faststream_gen/_code_generator/app_skeleton_generator.py')},
'faststream_gen._code_generator.chat': { 'faststream_gen._code_generator.chat.CustomAIChat': ( 'chat.html#customaichat',
'faststream_gen/_code_generator/chat.py'),
'faststream_gen._code_generator.chat.CustomAIChat.__call__': ( 'chat.html#customaichat.__call__',
Expand Down
Loading

0 comments on commit f068de5

Please sign in to comment.