Claudette

repository·main·Indexed 43 days ago

https://github.com/answerdotai/claudette

A high-level Python wrapper for the Anthropic SDK designed to reduce boilerplate and improve ergonomics. It provides stateful chat via the Chat and AsyncChat classes, simplified tool use with automatic handling and toolloop, structured data extraction via Client.structured, and support for prompt caching, image inputs, and extended thinking. It works across Anthropic, AWS Bedrock, and Google Vertex providers.

Tokens
15.9K
Snippets
68
Records
70
Agent score
54%

What's inside Claudette

  1. What is Claudette?

    main

    Claudette is a high-level Python wrapper around Anthropic’s Claude API SDK designed to reduce boilerplate and improve ergonomics.

    Core capabilities:

    • Reduce boilerplate: Automates repetitive low-level SDK tasks.
    • Stateful chat: Uses the Chat class for multi-turn conversations.
    • Tool use: Simplifies providing Claude access to Python functions.
    • Structured output: Extracts typed/structured data from responses.
    • Extra conveniences: Supports prefill, image handling, prompt caching, streaming, and async.
    • Multi-provider support: Works with Anthropic, AWS Bedrock, and Google Vertex.
  2. How Claudette makes tool use more ergonomic

    main

    Claudette simplifies the tool-calling lifecycle through several mechanisms:

    1. Document-based function definitions: Instead of writing manual JSON schemas, you define tools as plain Python functions. Use type annotations and inline comments to describe parameters; Claudette uses these to build the schema.
    2. Automatic tool call handling: When Claude returns a tool_use message, calling chat() again automatically executes the function with the provided arguments.
    3. toolloop method: A single call to chat.toolloop() manages multi-step problems that require sequential tool calls (e.g., calling sums then mults) without manual back-and-forth management.
    4. structured method: For simple cases where you only need the result of a single tool call, Client.structured() handles the entire process in one step and returns the result directly.
    5. tool_choice option: You can force Claude to use a specific tool by passing its name to the tool_choice parameter.
    # Example of a tool defined using documentation-based approach
    def sums(
        a:int,  # First thing to sum
        b:int=1 # Second thing to sum
    ) -> int: # The sum of the inputs
        """Adds a + b."""
        return a + b
  3. Use Anthropic's Text Editor Tool

    main

    Claudette supports Anthropic’s specialized Text Editor Tool, which allows Claude to view and modify files directly using a predefined, schema-less model capability. Unlike standard function-calling tools, it uses specific type identifiers like text_editor_20250124.

    To use it, you must:

    1. Provide the tool configuration (e.g., text_editor_conf['sonnet']).
    2. Implement or provide a dispatcher function to route commands. In Claudette, you can use str_replace_based_edit_tool.
    3. Register the dispatcher in the Chat instance using the ns (namespace) parameter.

    The tool enables Claude to:

    • View file or directory contents
    • Create new files
    • Insert text at specific line numbers
    • Replace text within files
    # Create a chat with the text editor tool
    chat = Chat(model, sp='Be concise in your responses.',
                tools=[text_editor_conf['sonnet']], ns=mk_ns(str_replace_based_edit_tool))
    
    # Now Claude can explore files
    for o in chat.toolloop('Please explain concisely what my _quarto.yml does. Use your tools, and explain before each usage what you are doing.'):
        if not isinstance(o,dict): display(o)
  4. Use tools with the Chat class

    main

    You can provide tools to a Chat instance by passing a list of functions to the tools argument.

    To force Claude to use a specific tool, set the tool_choice parameter to the name of that function (as a string).

    When Claude decides to use a tool, it returns a tool_use message instead of a text answer. You must then call the tool with the provided parameters. Claudette can automate this loop by calling the chat instance again without arguments.

    # Initialize chat with tools
    chat = Chat(model, sp=sp, tools=[sums])
    
    # Force a specific tool
    r = chat(pr, tool_choice='sums')
    
    # Automatically handle the tool call and get the final answer
    chat()
    chat = Chat(model, sp=sp, tools=[sums])
    r = chat(pr, tool_choice='sums')
    chat()
  5. Handle image inputs in Chat

    main

    Claude supports image data. Claudette expects images to be passed as a list of bytes.

    You can include images in a single prompt alongside text, or start a multi-stage conversation by sending an image as the initial prompt.

    # Prepare image bytes
    img = Path('samples/puppy.jpg').read_bytes()
    
    # Option 1: Text and image in one prompt
    chat([img, "In brief, what color flowers are in this image?"])
    
    # Option 2: Multi-stage chat (image first, then follow-up)
    chat = Chat(model)
    chat(img)
    chat("What color are the flowers?")
    img = Path('samples/puppy.jpg').read_bytes()
    chat([img, "In brief, what color flowers are in this image?"])
  6. Use Extended Thinking with Claude models

    main

    Claude 3.7 Sonnet and Opus models support enhanced reasoning through 'extended thinking'. This allows the model to process complex problems step-by-step, making the reasoning process transparent.

    To enable this feature, pass the maxthinktok parameter to a Chat call to specify the maximum number of thinking tokens allowed.

    Important constraints:

    • Only available with select models (check has_extended_thinking_models).
    • Enabling extended thinking automatically sets temperature=1.
    • It is incompatible with prefill.
    • Thinking tokens count toward your total usage.
    • The thinking process is returned as a separate block in the response.
    chat = Chat(model)
    chat('Write a sentence about Python!', maxthinktok=1024)
  7. Enable debug logging for Anthropic requests

    main

    To print every HTTP request and response in full for debugging purposes, set the ANTHROPIC_LOG environment variable to debug before importing Claudette.

    import os
    os.environ['ANTHROPIC_LOG'] = 'debug'
  8. Define tools using docments

    main

    To use Claude's tool-use capabilities, define Python functions using the docments pattern. Each parameter and the return value must have a type hint and a docstring/comment describing it. This allows Claudette to pass the function schema to the model.

    Example function definition:

    def sums(
        a:int,  # First thing to sum
        b:int=1 # Second thing to sum
    ) -> int: # The sum of the inputs
        "Adds a + b."
        print(f"Finding the sum of {a} and {b}")
        return a + b
    def sums(
        a:int,  # First thing to sum
        b:int=1 # Second thing to sum
    ) -> int: # The sum of the inputs
        "Adds a + b."
        print(f"Finding the sum of {a} and {b}")
        return a + b
  9. Use Web Search and Server Tools with Claudette

    main

    Claude supports server-side tools managed by Anthropic, such as the web search tool. Unlike client-side tools that you implement yourself, these are managed by Anthropic's infrastructure. Claudette provides the search_conf() helper function to easily integrate these tools into a Chat instance.

    When Claude uses the web search tool, the response includes citations linking to the source information. Claudette automatically formats these citations as footnotes in the response.

    chat = Chat(model, sp='Be concise in your responses.', tools=[search_conf()])
    pr = 'What is the current weather in San Diego?'
    r = chat(pr)
    r