Function to Tool Schema
To execute function calling from GPT responses, we need to define the function and its schema as a GPT tool. However, it can be inconvenient to define both, especially if we need to incorporate multiple functions as GPT tools.
This process can be simplified if we utilize type-hinting and annotations on functions supported by Python using libraries such as inspect and ast. Summary of our schema generation process:
Let us start with a well-documented function get_weather_information:
# Define the function to get weather information
from typing import Optional
def get_weather_information(
city: str, # Name of the city
zip_code: Optional[str] = None, # Zip code of the city (optional)
):
"""Get weather information for a city or location based on zip code"""
return {
"city": city,
"zip_code": zip_code,
"temparature": 25,
"humidity": 80,
}get_weather_information
get_weather_information (city:str, zip_code:Optional[str]=None)
Get weather information for a city or location based on zip code
| Type | Default | Details | |
|---|---|---|---|
| city | str | Name of the city | |
| zip_code | Optional | None | Zip code of the city (optional) |
The function is well-documented and its annotations contain almost all necessary information for tool schema. We will build utilities around extracting such information and converting them into appropriate formats for tool schema.
Parameter descriptions
We can extract the descriptions of function parameters with the ast library. In our implementation, we can follow the inline comments for conveniency. The descriptions can be extracted as follows:
extract_parameter_comments
extract_parameter_comments (func:Callable)
Extract comments for function arguments
| Type | Details | |
|---|---|---|
| func | Callable | Function to extract comments from |
| Returns | dict | Dictionary with parameter comments |
Example of extracting comments from the function:
extract_parameter_comments(get_weather_information){'city': 'Name of the city', 'zip_code': 'Zip code of the city (optional)'}
Type converter
Python types cannot be directly transferred into acceptable data types in GPT-compatible tool schema. Therefore, we need an utility to convert these types:
param_converter
param_converter (param_type, description)
Convert Python parameter types to acceptable types for tool schema
| Type | Details | |
|---|---|---|
| param_type | The type of the parameter | |
| description | The description of the parameter | |
| Returns | dict | The converted parameter |
Test with parameters of get_weather_information:
city_param = param_converter(str, "Name of the city")
zip_param = param_converter(Optional[str], "Zip code of the city (optional)")
city_param, zip_param({'type': 'string', 'description': 'Name of the city'},
{'anyOf': [{'type': 'string',
'description': 'Zip code of the city (optional)'},
{'type': 'null',
'description': 'A default value will be automatically used.'}]})
Function to Schema
We can combine the above utilities with other utilities in inspect to extract information from a Python function and generate a tool schema.
function_schema
function_schema (func:Callable, service_name:Optional[str]=None, fixup:Optional[Callable]=None, **kwargs)
Generate a schema from function using its parameters and docstring
| Type | Default | Details | |
|---|---|---|---|
| func | Callable | The function to generate the schema for | |
| service_name | Optional | None | The name of the service |
| fixup | Optional | None | A function to fix up the schema |
| kwargs | VAR_KEYWORD | ||
| Returns | dict | The generated tool schema |
Test with our current function:
tool_schema = function_schema(get_weather_information, service_name="Weather Service")
tool_schema{'type': 'function',
'function': {'name': 'get_weather_information',
'description': 'Get weather information for a city or location based on zip code',
'parameters': {'type': 'object',
'properties': {'city': {'type': 'string',
'description': 'Name of the city'},
'zip_code': {'anyOf': [{'type': 'string',
'description': 'Zip code of the city (optional)'},
{'type': 'null',
'description': 'A default value will be automatically used.'}]}},
'required': ['city']},
'metadata': {'module': '__main__', 'service': 'Weather Service'}}}
Simulated GPT workflow
Test integrating with our current GPT framework:
from llmcam.core.fc import *
tools = [function_schema(get_weather_information, service_name="Weather Service")]
messages = form_msgs([
("system", "You can get weather information for a given location using the `get_weather_information` function"),
("user", "What is the weather in New York?")
])
complete(messages, tools=tools)
print_msgs(messages)>> System:
You can get weather information for a given location using the `get_weather_information` function
>> User:
What is the weather in New York?
>> Assistant:
The current weather in New York is 25°C with 80% humidity.