Skip to content

1. Function Format

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

CallingGPT only accepts Google Style docstring for the function parsing.

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

  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])
Clone this wiki locally