Genie API

pydantic model code_genie.genie.GenieResult[source]

The result of a genie execution

Config:
  • fields: dict = {‘result’: {‘exclude’: True}}

  • frozen: bool = True

Fields:
field cache_dir: str [Required]

The cache directory used by the genie

field code: str [Required]

The code generated by the genie

field id: str [Required]

ID of the genie, this would also be the filename used for storing the generated code in the cache.

field result: Any = None

The result of the execution; None if no result was returned

class code_genie.genie.Genie(data: Optional[Any] = None, client: Optional[Client] = None, cache_dir: Optional[str] = None, copy_data_before_use: bool = True)[source]
__init__(data: Optional[Any] = None, client: Optional[Client] = None, cache_dir: Optional[str] = None, copy_data_before_use: bool = True)[source]

Initialize a genie instance

Parameters:
  • data – a base dataset whose attributes will be used to generate the code. the result will be determined by running this data over the code

  • client – an instance of the client to use for making requests to the api. if not provided, a new instance will be created.

  • cache_dir – if provided, the code generated by the genie will be cached in this directory. if not provided, the global default is used. it is recommended to use set_cache_dir() method to set this.

  • copy_data_before_use – if True, the data will be copied before passing through generated code. this is to prevent the data from being modified inplace by the code. the data passed should have a copy() method implemented. if False, the data will be passed as is. this is faster but can lead to unexpected results

Returns:

A callable which can be used to execute the code generated by the genie.

custom(code: str, additional_inputs: Optional[Dict[str, Any]] = None, update_base_input: bool = False) GenieResult[source]

Define a custom genie with user defined code segment. The first argument of the function should be the base input of the genie. Note that this code should define a stand alone function, ie, it should not depend on any external variables or functions or imports. If any additional packages are required, you need to import them in the code segment itself.

Parameters:
  • code – the code segment defining a single function to be used to process data.

  • additional_inputs – a dictionary of inputs to the function. the keys are the names of the inputs and the values are small description of the inputs.

  • update_base_input – if True, the base data will be replaced by the result of executing the code. this is used if we are making a permanent update to the input and want to use the updated input moving forward.

Returns:

  • result: the result of executing the code

  • id: the id of the genie

  • code: the code generated by the genie

  • cache_dir: the directory where the code is cached. the code will be cached in a file named

    ”cache_dir/<id>.py

Return type:

A GenieResult instance which contains attributes

plz(instructions: Optional[Union[str, List[str]]], additional_inputs: Optional[Dict[str, Any]] = None, override: bool = False, update_base_input: bool = False) GenieResult[source]

Generate code for a new task

Parameters:
  • instructions – text instructions on the task required to be performed. use the keywords in inputs argument to refer to the inputs.

  • additional_inputs – a dictionary of inputs to the function. the keys are the names of the inputs and the values are small description of the inputs.

  • override – if a genie has been generated before with the same args, then it will be loaded from cache be default. set override to True to make a new API call and recreate the genie.

  • update_base_input – if True, the base data will be replaced by the result of executing the code. this is used if we are making a permanent update to the input and want to use the updated input moving forward.

Returns:

  • result: the result of executing the code

  • id: the id of the genie

  • code: the code generated by the genie

  • cache_dir: the directory where the code is cached. the code will be cached in a file named

    ”cache_dir/<id>.py

Return type:

A GenieResult instance which contains attributes

read_cache() Dict[str, str][source]

Read all the code segments in the cache directory set in the current genie instance

Returns:

A dictionary with keys as the genie ids and values as the code segments