aw.func_calling¶
This module provides robust templates for creating (python) code-generating functions that can translate natural language task descriptions into executable Python code.
Functions
|
Extract a function from a string of Python code. |
|
Create a code generation function from a template and schema. |
|
End-to-end: Convert a task description to an executable function. |
- aw.func_calling.extract_function_from_code(code_str_for_func, *, make_picklable=False, strict=True, verbose=True)[source]¶
Extract a function from a string of Python code.
- Parameters:
- Return type:
callable- Returns:
The function object
- Raises:
ValueError – If no function is found or multiple functions exist
Example
>>> code = ''' ... def add(a: int, b: int) -> dict: ... return {"sum": a + b} ... ''' >>> func = extract_function_from_code(code, verbose=False) >>> func(2, 3) {'sum': 5}
- aw.func_calling.make_code_generator(template='\nYou are a Python code generator. Your task is to create a complete, executable Python function.\n\n## TASK DESCRIPTION\n{task}\n\n## OUTPUT SCHEMA\nThe function should return a value that conforms to this JSON schema:\n\n{output_schema}\n\nIf output_schema is null or empty, the function can return any appropriate value.\n\n## REQUIREMENTS\n\n### Function Signature\n- Function name: `{name}`\n- Parameters: Extract from the task description (indicated by {{braces}})\n- All parameters should have type hints where possible\n- Use clear, descriptive parameter names\n\n### Code Quality\n- Write ONLY the function definition (no imports in function body, no examples, no explanations)\n- Include a minimal docstring (one line describing what it does)\n- Use type hints for parameters and return value\n- Handle edge cases appropriately (e.g., empty inputs, None values)\n- Use built-in functions and standard library where possible\n- Keep the code simple and readable\n\n### Output Format\n- If returning a dict matching a schema, ensure all required fields are present\n- If the schema specifies types, ensure they match (e.g., "number" → int or float)\n- Return values directly without unnecessary wrapping\n\n### Code Style\n- Follow PEP 8 conventions\n- Use meaningful variable names\n- Prefer comprehensions over loops where readable\n- No print statements or side effects unless the task requires them\n\n## CRITICAL INSTRUCTIONS\n- Respond with ONLY the function definition\n- Do NOT include: imports, examples, explanations, markdown formatting, or test code\n- Do NOT wrap the code in markdown code blocks\n- The response must be valid Python that can be directly executed with `exec()`\n\n## EXAMPLE FORMAT (for reference only, do not include this in your response)\n```python\ndef example_function(param1: int, param2: str) -> dict:\n """Brief description of what this does."""\n # function body\n return {{"result": param1}}\n```\n\nNOW GENERATE THE FUNCTION.\n', code_schema={'name': 'generated_python_function', 'schema': {'properties': {'code': {'description': 'Complete Python function definition as a string', 'type': 'string'}}, 'required': ['code'], 'type': 'object'}}, prompt_json_function_maker=None)[source]¶
Create a code generation function from a template and schema.
- Parameters:
- Returns:
A function that generates code from task descriptions
Note
The returned function requires all template variables to be provided. See CODE_WRITING_TEMPLATE for required variables (task, output_schema, name).
- aw.func_calling.task_to_function(task, output_schema=None, name='generated_function', code_generator=None, **generator_kwargs)[source]¶
End-to-end: Convert a task description to an executable function.
- Parameters:
task (
str) – Natural language description of the function’s purposeoutput_schema (
dict|str|None) – JSON schema for the return value (optional)name (
str) – Name for the generated functioncode_generator – Code generation function (creates one if None)
**generator_kwargs – Additional arguments for code generator
- Return type:
callable- Returns:
Executable Python function
Note
This function requires an LLM backend (via code_generator or the ‘oa’ package). Cannot be tested in doctests without mocking the LLM calls.