Support object type in python function parameters
Overview of Feature
https://github.com/jakecyr/openai-function-calling/blob/5417b8535ea949b0f5ddc703016fb03ecf278103/openai_function_calling/helper_functions.py#L34-L35
object type is not working well in the current version (see the test case below). If we have a specific data type on the function parameters, your lib will give a null in this case. I know it's a little weird, but this is what my team leaders are doing lately on OpenAI function API.
I suggest you might need to look into some dataclass2schema converter libs such as https://pypi.org/project/dataclasses-json/
But that would not be the best solution I guess, if you have a better idea I'd love to hear it.
Why add this feature?
This is quite necessary because most of our company use cases are quite complicated (I'm trying to make the schema prompt more reliable to maintain, while I can't write a new prompt because I'm not allowed to do it under my job.)
I believe most people will need this feature and make general value.
Acceptance Criteria to Meet
- [ ] A simple example:
"""Weather Example.
Uses the function calling wrappers to decide between two functions
and extract entities to pass into the chosen function as arguments.
"""
import json
from dataclasses import dataclass
from openai_function_calling.tool_helpers import ToolHelpers
@dataclass
class Location:
"""Represents a location."""
x: int
y: int
name: str
def get_tomorrows_weather(location: Location, unit: str) -> str:
"""Get tomorrow's weather and return a summary."""
return f"It will be rainy tomorrow in {location} and around 65 degrees {unit}."
function_json = json.dumps(
ToolHelpers.infer_from_function_refs([get_tomorrows_weather]),
indent=2,
)
print(function_json)
[
{
"type": "function",
"function": {
"name": "get_tomorrows_weather",
"description": "Get tomorrow's weather and return a summary.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "null" # <---- not implemented
},
"unit": {
"type": "string"
}
}
}
}
}
]
BTW: I'm doing a similar task at my work, so I believe you can fix it. (sorry I can't paste code from my work, but I'm willing to review if we can make a better design.)
@jakecyr Oh, pydantic has better class2schema method, hope this detail can help you.
import json
from enum import Enum
from pydantic import BaseModel, Field
from openai_function_calling.tool_helpers import ToolHelpers
class LocationType(Enum):
"""The type of location."""
BJ = "北京"
SH = "上海"
class Location(BaseModel):
"""Represents a location."""
type: LocationType = Field(..., description="The type of location.")
x: int
y: int
name: str = Field(..., description="The name of the location.")
print(json.dumps(Location.model_json_schema(), indent=2, ensure_ascii=False))
{
"$defs": {
"LocationType": {
"description": "The type of location.",
"enum": ["北京", "上海"],
"title": "LocationType",
"type": "string",
}
},
"description": "Represents a location.",
"properties": {
"type": {"$ref": "#/$defs/LocationType"},
"x": {"title": "X", "type": "integer"},
"y": {"title": "Y", "type": "integer"},
"name": {
"description": "The name of the location.",
"title": "Name",
"type": "string",
},
},
"required": ["type", "x", "y", "name"],
"title": "Location",
"type": "object",
}
Merged in my changes. Let me know if the updates satisfy your need.