Skip to content

1. Function Format

GitHub Actions edited this page Jun 21, 2023 · 4 revisions

CallingGPT only accepts Google Style docstring for the function parsing.
Also, we suggest you to put functions into a module, check the Module Format section below.

Function Format

a very simple function: adder

def adder(a: float, b: float) -> float:
    """Add up two numbers.

    Args:
        a: The first number.
        b: The second number.

    Returns:
        The sum of the two numbers.
    """
    return a + b
  • Type hints of arguments and return value are required.
  • The docstring must be in the Google Style.
  • The docstring must has Description, Args, and Returns sections, each section must be separated by a blank line.
  • Type hints of arguments in docstring are optional.
  • Currently supported type: int, float, str, bool, list, dict.
  • Description of return value is optional.

More Examples

More examples for func format
  1. int type args

    def adder(a: int, b: int) -> int:
        """Add up two numbers.
    
        Args:
            a: The first number.
            b: The second number.
    
        Returns:
            The sum of the two numbers.
        """
        return a + b
  2. list and bool type args

    def list_to_str(l: list) -> str:
        """Convert a list to a string.
    
        Args:
            l: The list to be converted.
    
        Returns:
            The string converted from the list.
        """
        return ', '.join(l)

    Type hints of list elements are optional, set to str by default.

    def shopping_list(l: list[str], ordered: bool) -> str:
        """Convert a shopping list to string and number the items.
    
        Args:
            l: The shopping list.
            ordered: Whether the list is ordered.
    
        Returns:
            The string converted from the shopping list.
        """
        if ordered:
            return '\n'.join([f'{i+1}. {item}' for i, item in enumerate(l)])
        else:
            return '\n'.join([f'- {item}' for item in l])

Module Format

Attribute __functions__

We strongly suggest you to use __functions__ attribute to specific the functions you want to export for parser, this is required when you import unused functions from other modules.

# examples/random.py
from random import choice

def choose_randomly(l: list[str]) -> str:
    """Choose a random element from a list.

    Args:
        l: The list to choose from.

    Returns:
        The chosen element.
    """
    return choice(l)

__functions__ = [choose_randomly]

In this case, we import choice from random module, while this function's docstring doesn't meet the requirement of parser, and we only want to export choose_randomly function, so we use __functions__ attribute to specific the function we want to export.

  • If you don't specific __functions__ attribute, all functions in the module will be exported and parsed.
Clone this wiki locally