Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide an example of using Pydantic for type validation in Claudetools
    claude sonnet


Can you provide an example of using Pydantic for type validation in Claudetools


Sure, here's an example of how you can use Pydantic for type validation in Claudetools:

python
from typing import Literal, Dict
from pydantic import BaseModel, Field
from claudetools.tools.tool import Tool

# Define your function parameters using Pydantic models
class AddTodo(BaseModel):
    text: str = Field(..., description="Text to add for the TODO to remember.")

class MarkCompleted(BaseModel):
    text: str = Field(..., description="Text of the TODO to mark as completed.")

class ReOpen(BaseModel):
    text: str = Field(..., description="Text of the TODO to reopen.")

# Create a list of function definitions with Pydantic schemas
functions = [
    {
        "name": "AddTodo",
        "description": "Add a new TODO",
        "parameters": AddTodo.schema()
    },
    {
        "name": "MarkCompleted",
        "description": "Mark a TODO as completed",
        "parameters": MarkCompleted.schema()
    },
    {
        "name": "ReOpen",
        "description": "Reopen a completed TODO",
        "parameters": ReOpen.schema()
    }
]

# Create a Tool instance with your Anthropic API key
tool = Tool(ANTHROPIC_API_KEY)

# Prepare some sample user messages
user_messages = [
    {
        "role": "user",
        "content": "Add a new TODO to buy milk"
    },
    {
        "role": "user",
        "content": "Mark the 'Clean the house' TODO as completed"
    },
    {
        "role": "user",
        "content": "Reopen the 'Call mom' TODO"
    }
]

# Dependency prompt to provide context to Claude
DEPENDENCY_PROMPT = """You are a helpful assistant that helps a user with their tasks and todos. The user can add a todos, mark todos as completed, or reopen certain todos. The user can provide multiple actions at once so you've to break those down and call the appropriate functions in the correct sequence."""

# Call the tool with the required parameters
output = tool(model="claude-3-sonnet-20240229", messages=user_messages, tools=functions, tool_choice=None, multiple_tools=True, attach_system=DEPENDENCY_PROMPT, max_tokens=3000)

if output:
    print(json.dumps(output, indent=4))
else:
    print("Unable to find a function!")

In this example:

1. We define Pydantic models for the function parameters (`AddTodo`, `MarkCompleted`, `ReOpen`).
2. We create a list of function definitions, including the Pydantic parameter schemas.
3. We create a `Tool` instance with our Anthropic API key.
4. We prepare some sample user messages and a dependency prompt to provide context to Claude.
5. We call the `tool` function, passing in the required parameters, including the user messages, functions, and dependency prompt.

Claudetools uses Pydantic to validate the input data against the specified function parameter schemas. This ensures type safety and consistency in the function calls.

By using Pydantic models, you can define complex data structures, including nested dictionaries, and Claudetools will handle the type validation for you. This makes it easier to integrate your custom functions with the powerful capabilities of the Claude 3 model.

Citations:
[1] https://github.com/vatsalsaglani/claudetools
[2] https://www.reddit.com/r/generativeAI/comments/1bbfdbo/claude_3_function_calling_with_claudetools/
[3] https://discuss.python.org/t/pep-695-type-aliases-not-suitable-as-replacement-for-typing-typealias/43364?page=2
[4] https://bradyim.substack.com/p/how-claude-tools-api-can-make-claude
[5] https://help.sap.com/doc/saphelp_nw73ehp1/7.31.19/en-US/0d/e90c05e3024b309eb539c9a7b923a1/content.htm