Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I specify function parameters using Pydantic in Claudetools
    claude sonnet


How do I specify function parameters using Pydantic in Claudetools


To specify function parameters using Pydantic in Claudetools, you can follow these steps:

1. Define your function parameters as Pydantic models:

python
from pydantic import BaseModel, Field

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.")

2. Implement your functions that take the Pydantic models as parameters:

python
def add_todo(params: AddTodo):
    todos.append(params.text)
    return f"Added TODO: {params.text}"

def mark_completed(params: MarkCompleted):
    if params.text in todos:
        todos.remove(params.text)
        return f"Marked TODO as completed: {params.text}"
    else:
        return f"TODO not found: {params.text}"

3. Create a list of function definitions with their names, descriptions, and parameter schemas:

python
functions = [
    {
        "name": "add_todo",
        "description": "Add a new TODO",
        "parameters": AddTodo.schema()
    },
    {
        "name": "mark_completed",
        "description": "Mark a TODO as completed",
        "parameters": MarkCompleted.schema()
    }
]

4. Call the Claudetools Tool with the list of functions and other required parameters:

python
from claudetools.tools.tool import Tool

tool = Tool(ANTHROPIC_API_KEY)
output = tool(model="claude-3-sonnet-20240229", messages=user_messages, tools=functions, ...)

By defining your function parameters using Pydantic models, Claudetools can automatically validate and parse the input data when calling your functions. This ensures type safety and provides a convenient way to specify and document your function parameters[1][2][4].

Citations:
[1] https://github.com/vatsalsaglani/claudetools
[2] https://github.com/pydantic/pydantic/discussions/4741
[3] https://pub.towardsai.net/you-wont-believe-how-this-python-library-unlocks-gpt-4-level-features-with-claude-3-293e675f2bd8?gi=cbb8dee0031f
[4] https://www.reddit.com/r/OpenAI/comments/1baj5u9/function_calling_using_claude_3_family_of_models/
[5] https://docs.pydantic.dev/latest/concepts/fields/