Coder Social home page Coder Social logo

rizerphe / anthropic-tools Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 97 KB

Generate Anthropic Claude tool use schemas based on type annotations and function docstrings.

Home Page: https://anthropic-tools.readthedocs.io

License: MIT License

Python 100.00%
anthropic claude-3-haiku claude-3-opus claude-3-sonnet claude-ai claude-api claude3 llm-tools nlp python

anthropic-tools's Introduction

Anthropic tools

The anthropic-tools library simplifies the usage of Anthropics’s tool use feature. It abstracts away the complexity of parsing function signatures and docstrings by providing developers with a clean and intuitive interface. It's a near-clone of my openai-functions library that does the same with OpenAI.

License: MIT PyPI version Documentation Status

Installation

You can install anthropic-tools from PyPI using pip:

pip install anthropic-tools

Usage

  1. Import the necessary modules and provide your API key:
import enum
import anthropic
from anthropic_tools import Conversation

client = anthropic.Anthropic(
    api_key="<YOUR_API_KEY>",
)
  1. Create a Conversation instance:
conversation = Conversation(client)
  1. Define your tools using the @conversation.add_tool decorator:
class Unit(enum.Enum):
    FAHRENHEIT = "fahrenheit"
    CELSIUS = "celsius"

@conversation.add_tool()
def get_current_weather(location: str, unit: Unit = Unit.FAHRENHEIT) -> dict:
    """Get the current weather in a given location.

    Args:
        location (str): The city and state, e.g., San Francisco, CA
        unit (Unit): The unit to use, e.g., fahrenheit or celsius
    """
    return {
        "location": location,
        "temperature": "72",
        "unit": unit.value,
        "forecast": ["sunny", "windy"],
    }
  1. Ask the AI a question:
response = conversation.ask("What's the weather in San Francisco?")
# Should return three messages, the last one's content being something like:
# The current weather in San Francisco is 72 degrees Fahrenheit and it is sunny and windy.

You can read more about how to use Conversation here.

More barebones use - just schema generation and result parsing:

from anthropic_tools import ToolWrapper

wrapper = ToolWrapper(get_current_weather)
schema = wrapper.schema
result = wrapper({"location": "San Francisco, CA"})

Or you could use skills.

How it Works

anthropic-tools takes care of the following tasks:

  • Parsing the function signatures (with type annotations) and docstrings.
  • Sending the conversation and tool descriptions to Anthropic Claude.
  • Deciding whether to call a tool based on the model's response.
  • Calling the appropriate function with the provided arguments.
  • Updating the conversation with the tool response.
  • Repeating the process until the model generates a user-facing message.

This abstraction allows developers to focus on defining their functions and adding user messages without worrying about the details of tool use.

Note

Please note that anthropic-tools is an unofficial project not maintained by Anthropic. Use it at your discretion.

anthropic-tools's People

Contributors

rizerphe avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.