Skip to content

[Utilities] Model Schema Function Tools #51

@umar-anzar

Description

@umar-anzar
from pydantic import BaseModel
from typing import Optional, Literal, Union

# Define the base structure for properties
class ToolParameterProperty(BaseModel):
    type: Literal["boolean", "integer", "float", "string", "array", "object"]  # Specifies the allowed types
    description: str  # A description of the property
    enum: Optional[list[Union[str, int, float]]] = None  # Optional enumeration of allowed values for the property
    items: Optional["ToolParameterProperty"] = None  # Recursive definition for arrays, specifying the type of their items
    properties: Optional[dict[str, "ToolParameterProperty"]] = None  # For objects, defines nested properties
    required: Optional[list[str]] = None  # Specifies the list of required fields for objects
    additionalProperties: Optional[bool] = True  # Indicates whether additional fields are allowed

# Update forward references for nested properties
ToolParameterProperty.model_rebuild()  # Ensures recursive references like `items` and `properties` work correctly

# Define the structure for the tool's parameters
class ToolParameters(BaseModel):
    type: Literal["object"]  # The type is always "object" for parameters
    properties: dict[str, ToolParameterProperty]  # A dictionary where keys are property names and values are their definitions
    required: list[str]  # A list of required property keys
    additionalProperties: bool = False  # Specifies whether additional properties are allowed

# Define the overall structure for a tool
class Tool(BaseModel):
    type: Literal["function"]  # The type is always "function" for tools
    name: str  # The name of the tool
    description: str  # A description of what the tool does
    parameters: ToolParameters  # The parameters associated with the tool


TOOLS = [
    {
        "type": "function",
        "name": "<function_name>",
        "description": "<description>",
        "parameters": {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "<parameter_name>": {
                    "type": "<data-type>",
                    "description": "<description>"
                },
            },
            "required": [
                "<parameter_name>",
            ]
        }
    },

]
type LiteralType = "boolean" | "integer" | "float" | "string" | "array" | "object";

interface ToolParameterProperty {
    type: LiteralType; // Defines the type of the property
    description: string; // Description of the property
    enum?: Array<string | number>; // Optional enumeration of allowed values
    items?: ToolParameterProperty; // Recursive definition for nested arrays
    properties?: { [key: string]: ToolParameterProperty }; // Object properties definition for nested structures
    required?: string[]; // List of required fields
    additionalProperties?: boolean; // Allows additional properties
}

interface ToolParameters {
    type: "object"; // Always an object for tool parameters
    properties: { [key: string]: ToolParameterProperty }; // Defines the structure of the properties
    required: string[]; // List of required property keys
    additionalProperties: boolean; // Specifies whether additional properties are allowed
}

interface Tool {
    type: "function"; // Always a function
    name: string; // Name of the tool
    description: string; // Description of the tool
    parameters: ToolParameters; // Parameters for the tool
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions