Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Python Sample Code for Claudetools
    claude sonnet


Python Sample Code for Claudetools


Here is some sample Python code demonstrating how to use the Claudetools library to call functions with the Claude 3 model:

python
import asyncio
from claudetools.tools.tool import Tool
from pydantic import BaseModel, Field
from typing import List, Dict
import json

# create a tool instance with your Anthropic API Key
tool = Tool(ANTHROPIC_API_KEY)

# define your function parameters
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.")

# define your functions
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}"

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

# create some sample todos
todos = ["Buy groceries", "Clean the house", "Call mom"]

# create 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"
    }
]

# dependency prompt to attach to the main system prompt
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!")

This code demonstrates how to:

1. Create a Tool instance with your Anthropic API key
2. Define function parameters using Pydantic models
3. Implement your functions that will be called by Claude
4. Create a list of function definitions with names, descriptions and parameter schemas
5. Prepare some sample user messages to test the tool
6. Attach a dependency prompt to provide context to Claude
7. Call the tool with the required parameters
8. Handle the tool output and print the result

The key steps are defining your functions, creating a list of function definitions, and calling the tool with the appropriate parameters. Claudetools makes it easy to integrate Claude 3's powerful function calling capabilities into your Python applications.

Citations:
[1] https://github.com/vatsalsaglani/claudetools
[2] https://github.com/vatsalsaglani/claudetools-vision-example
[3] https://www.linkedin.com/posts/vatsalsaglani_you-wont-believe-how-this-python-library-activity-7176223810650603520-ZTT7
[4] https://www.w3schools.com/python/python_examples.asp
[5] https://docs.anthropic.com/en/docs/build-with-claude/tool-use