core

Development of func2llmtool

OpenAI function calling spec: https://platform.openai.com/docs/guides/function-calling#defining-functions

We’re extracting the following information directly from a Python function:

{
    "type": "function",
    "name": "get_weather",
    "description": "Retrieves current weather for the given location.",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "City and country e.g. Bogotá, Colombia"
            },
            "units": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"],
                "description": "Units the temperature will be returned in."
            }
        },
        "required": ["location", "units"],
        "additionalProperties": false
    },
    "strict": true
}
def _test_get_weather(
    city: str  # City name
) -> str:  # Returns the weather in a city, e.g. "rainy"
    """Get the current weather for a city"""

    if city.lower() == "tokyo": weather = "sunny"
    elif city.lower() == "amsterdam": weather = "cloudy"
    else: weather = "rainy"

    return weather

docments test

docments(_test_get_weather, full=True)
{ 'city': { 'anno': <class 'str'>,
            'default': <class 'inspect._empty'>,
            'docment': 'City name'},
  'return': { 'anno': <class 'str'>,
              'default': <class 'inspect._empty'>,
              'docment': 'Returns the weather in a city, e.g. "rainy"'}}

source

FuncInfoPropsParam


def FuncInfoPropsParam(
    type:str, description:str
)->None:

Info related to argument, including automatic conversion


source

FuncInfo


def FuncInfo(
    func_name:str, description:str, properties:dict, required:list, additional_properties:bool=False,
    strict:bool=True
)->None:

Class that stores all data to outputing a OpenAI / LiteLLM tool description and necessary conversion functions.

fi = FuncInfo.from_func(_test_get_weather)
fi
{
    "type": "function",
    "function": {
        "name": "_test_get_weather",
        "description": "Get the current weather for a city Return type: string (Returns the weather in a city, e.g. \"rainy\")",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "City name"
                }
            },
            "required": [
                "city"
            ],
            "additionalProperties": false
        }
    }
}
fi.to_tool_dict(format="openai")
{'type': 'function',
 'name': '_test_get_weather',
 'description': 'Get the current weather for a city Return type: string (Returns the weather in a city, e.g. "rainy")',
 'parameters': {'type': 'object',
  'properties': {'city': {'type': 'string', 'description': 'City name'}},
  'required': ['city'],
  'additionalProperties': False},
 'strict': True}
fi()
{'type': 'function',
 'function': {'name': '_test_get_weather',
  'description': 'Get the current weather for a city Return type: string (Returns the weather in a city, e.g. "rainy")',
  'parameters': {'type': 'object',
   'properties': {'city': {'type': 'string', 'description': 'City name'}},
   'required': ['city'],
   'additionalProperties': False}}}
print(fi)
{
    "type": "function",
    "function": {
        "name": "_test_get_weather",
        "description": "Get the current weather for a city Return type: string (Returns the weather in a city, e.g. \"rainy\")",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "City name"
                }
            },
            "required": [
                "city"
            ],
            "additionalProperties": false
        }
    }
}
FuncInfo.from_func('_test_get_weather')()
{'type': 'function',
 'function': {'name': '_test_get_weather',
  'description': 'Get the current weather for a city Return type: string (Returns the weather in a city, e.g. "rainy")',
  'parameters': {'type': 'object',
   'properties': {'city': {'type': 'string', 'description': 'City name'}},
   'required': ['city'],
   'additionalProperties': False}}}

Numpy style docstring parsing

Proper docstring extraction from _test_get_weather_numpy_style.

# define our tool function with numpy-style docstring
def _test_get_weather_numpy_style(city: str) -> str:
    """
    Get the current weather for a city.

    Parameters
    ----------
    city : str
        City name

    Returns
    -------
    str
        The weather in a city, e.g. "rainy"
    """

    if city.lower() == "tokyo": weather = "sunny"
    elif city.lower() == "amsterdam": weather = "cloudy"
    else: weather = "rainy"

    return weather
parsed = parse_docstring(_test_get_weather_numpy_style.__doc__)
parsed
{ 'Also': '',
  'Attributes': '',
  'Examples': '',
  'Extended': '',
  'Methods': '',
  'Notes': '',
  'Other': '',
  'Parameters': { 'city': Parameter(name='city', type='str', desc=['City name'])},
  'Raises': '',
  'Receives': '',
  'References': '',
  'Returns': Parameter(name='', type='str', desc=['The weather in a city, e.g. "rainy"']),
  'See': '',
  'Summary': 'Get the current weather for a city.',
  'Warnings': '',
  'Warns': '',
  'Yields': ''}
desc = parsed.get('Summary', _test_get_weather_numpy_style.__doc__).strip() if parsed else f.__doc__
desc
'Get the current weather for a city.'
FuncInfo.from_func('_test_get_weather_numpy_style')()
{'type': 'function',
 'function': {'name': '_test_get_weather_numpy_style',
  'description': 'Get the current weather for a city. Return type: string (The weather in a city, e.g. "rainy")',
  'parameters': {'type': 'object',
   'properties': {'city': {'type': 'string', 'description': 'City name'}},
   'required': ['city'],
   'additionalProperties': False}}}