Created
March 15, 2024 18:40
-
-
Save FanaHOVA/12da1b49d1a436fd3f864b4e058daf10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CohereFunctionModel(BaseModel): | |
name: str | |
description: str | |
parameter_definitions: dict | |
@classmethod | |
def from_pydantic_model(cls, model: BaseModel): | |
return cls( | |
name=model.__name__, | |
description=model.__doc__, | |
parameter_definitions={ | |
field_name: { | |
"description": field.description, | |
"type": field.annotation.__name__, | |
"required": field.is_required() | |
} | |
for field_name, field in model.model_fields.items() | |
} | |
) | |
def function_calling_schema(model): | |
"""Pydantic to JSON schema for function calling""" | |
return CohereFunctionModel.from_pydantic_model(model).model_dump() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment