Hi can we use descriptions in Python package with pydantic like this
How we do in the api call
Yes you can!
You will need to add “name” and “Description” … here is a sample from my code:
class OpenAISchema(BaseModel):
class Config:
extra = 'forbid' # Prevents additional properties
class AddLocationToCalendarEvent(OpenAISchema):
user_email: str = Field(..., description="The email address of the calendar owner.")
event_id: str = Field(..., description="The unique identifier of the calendar event.")
new_location: str = Field(..., description="The new location to add to the event.")
calendar_id: Optional[str] = Field('primary', description="The ID of the calendar. Defaults to 'primary'.")
def add_location_to_calendar_event(params: AddLocationToCalendarEvent) -> SimplifiedEvent:
"""
Adds a new location to an existing calendar event.
returns: The updated event object.
"""
body = {"location": params.new_location}
updated_event_data = patch_event(params.user_email, params.calendar_id, params.event_id, body)
return SimplifiedEvent(updated_event_data, user_email=params.user_email, calendar_id=params.calendar_id)
def get_schema(function_name):
func = getattr(module, function_name, None)
description = func.__doc__.strip() if func.__doc__ else ""
signature = inspect.signature(func)
parameter = next(iter(signature.parameters.values()), None)
parameter_class = parameter.annotation # e.g. AddLocationToCalendarEvent
pydantic_schema = parameter_class.schema()
openai_function_schema = {
"name": function_name,
"description": description,
"strict": True,
"parameters": pydantic_schema
}
return openai_function_schema
openai_ready_schema = get_schema("add_location_to_calendar_event",)
If you make sure to create the parameters for the function calls as a Pydantic class you can combine the description from the Python function that handles the function call (+the name of the function) and then automatically add .schema() from Pydantic to generate the correct Schema.
I’ll put some of the further automation about this in my next Medium post!