Skip to content

Instantly share code, notes, and snippets.

@FanaHOVA
Created March 15, 2024 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FanaHOVA/12da1b49d1a436fd3f864b4e058daf10 to your computer and use it in GitHub Desktop.
Save FanaHOVA/12da1b49d1a436fd3f864b4e058daf10 to your computer and use it in GitHub Desktop.
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