autogen.code_utils
infer_lang
def infer_lang(code)
infer the language for the code. TODO: make it robust.
extract_code
def extract_code(text: str,
pattern: str = CODE_BLOCK_PATTERN) -> List[Tuple[str, str]]
Extract code from a text.
Arguments:
textstr - The text to extract code from.patternOptional, str - The regular expression pattern for finding the code block.
Returns:
list- A list of tuples, each containing the language and the code.
generate_code
def generate_code(pattern: str = CODE_BLOCK_PATTERN,
**config) -> Tuple[str, float]
Generate code.
Arguments:
patternOptional, str - The regular expression pattern for finding the code block. The default pattern is for finding a code block in a markdown file.configOptional, dict - The configuration for the API call.
Returns:
str- The generated code.float- The cost of the generation.
improve_function
def improve_function(file_name, func_name, objective, **config)
(work in progress) Improve the function to achieve the objective.
improve_code
def improve_code(files, objective, suggest_only=True, **config)
Improve the code to achieve a given objective.
Arguments:
fileslist - A list of file names containing the source code.objectivestr - The objective to achieve.suggest_onlybool - Whether to return only the suggestions or the improved code.configOptional, dict - The configuration for the API call.
Returns:
str- The improved code if suggest_only=False; a list of suggestions if suggest_only=True (default).float- The cost of the generation.
execute_code
def execute_code(code: Optional[str] = None,
timeout: Optional[int] = None,
filename: Optional[str] = None,
work_dir: Optional[str] = None,
use_docker: Optional[Union[List[str], str, bool]] = docker
is not None,
lang: Optional[str] = "python") -> Tuple[int, str, str]
Execute code in a docker container. This function is not tested on MacOS.
Arguments:
codeOptional, str - The code to execute. If None, the code from the file specified by filename will be executed. Either code or filename must be provided.timeoutOptional, int - The maximum execution time in seconds. If None, a default timeout will be used. The default timeout is 600 seconds. On Windows, the timeout is not enforced when use_docker=False.filenameOptional, str - The file name to save the code or where the code is stored whencodeis None. If None, a file with a randomly generated name will be created. The randomly generated file will be deleted after execution. The file name must be a relative path. Relative paths are relative to the working directory.work_dirOptional, str - The working directory for the code execution. If None, a default working directory will be used. The default working directory is the "extensions" directory under "path_to_flaml/autogen".use_dockerOptional, list, str or bool - The docker image to use for code execution. If a list or a str of image name(s) is provided, the code will be executed in a docker container with the first image successfully pulled. If None, False or empty, the code will be executed in the current environment. Default is True, which will be converted into a list. If the code is executed in the current environment, the code must be trusted.langOptional, str - The language of the code. Default is "python".
Returns:
int- 0 if the code executes successfully.str- The error message if the code fails to execute; the stdout otherwise.image- The docker image name after container run when docker is used.
generate_assertions
def generate_assertions(definition: str, **config) -> Tuple[str, float]
Generate assertions for a function.
Arguments:
definitionstr - The function definition, including the signature and docstr.configOptional, dict - The configuration for the API call.
Returns:
str- The generated assertions.float- The cost of the generation.
eval_function_completions
def eval_function_completions(responses: List[str],
definition: str,
test: Optional[str] = None,
entry_point: Optional[str] = None,
assertions: Optional[Union[str, Callable[
[str], Tuple[str, float]]]] = None,
timeout: Optional[float] = 3,
use_docker: Optional[bool] = True) -> Dict
Select a response from a list of responses for the function completion task (using generated assertions), and/or evaluate if the task is successful using a gold test.
Arguments:
responseslist - The list of responses.definitionstr - The input definition.testOptional, str - The test code.entry_pointOptional, str - The name of the function.assertionsOptional, str or Callable - The assertion code which serves as a filter of the responses, or an assertion generator. When provided, only the responses that pass the assertions will be considered for the actual test (if provided).timeoutOptional, float - The timeout for executing the code.
Returns:
dict- The success metrics.
PassAssertionFilter Objects
class PassAssertionFilter()
pass_assertions
def pass_assertions(context, response, **_)
Check if the response passes the assertions.
implement
def implement(
definition: str,
configs: Optional[List[Dict]] = None,
assertions: Optional[Union[str,
Callable[[str],
Tuple[str,
float]]]] = generate_assertions
) -> Tuple[str, float]
Implement a function from a definition.
Arguments:
definitionstr - The function definition, including the signature and docstr.configslist - The list of configurations for completion.assertionsOptional, str or Callable - The assertion code which serves as a filter of the responses, or an assertion generator.
Returns:
str- The implementation.float- The cost of the implementation.int- The index of the configuration which generates the implementation.