@ -1,7 +1,16 @@
""" Methods for creating chains that use OpenAI function-calling APIs. """
import inspect
import re
from typing import Any , Callable , Dict , List , Optional , Sequence , Tuple , Type , Union
from typing import (
Any ,
Callable ,
Dict ,
List ,
Optional ,
Sequence ,
Tuple ,
Type ,
Union ,
)
from pydantic import BaseModel
@ -25,8 +34,7 @@ PYTHON_TO_JSON_TYPES = {
def _get_python_function_name ( function : Callable ) - > str :
""" Get the name of a Python function. """
source = inspect . getsource ( function )
return re . search ( r " ^def (.*) \ ( " , source ) . groups ( ) [ 0 ] # type: ignore
return function . __name__
def _parse_python_function_docstring ( function : Callable ) - > Tuple [ str , dict ] :
@ -94,10 +102,16 @@ def _get_python_function_required_args(function: Callable) -> List[str]:
spec = inspect . getfullargspec ( function )
required = spec . args [ : - len ( spec . defaults ) ] if spec . defaults else spec . args
required + = [ k for k in spec . kwonlyargs if k not in ( spec . kwonlydefaults or { } ) ]
is_class = type ( function ) is type
if is_class and required [ 0 ] == " self " :
required = required [ 1 : ]
return required
def convert_python_function_to_openai_function ( function : Callable ) - > Dict [ str , Any ] :
def convert_python_function_to_openai_function (
function : Callable ,
) - > Dict [ str , Any ] :
""" Convert a Python function to an OpenAI function-calling API compatible dict.
Assumes the Python function has type hints and a docstring with a description . If