Anthropic are building a series of Large Language Models under the name Claude. In this example we show how to use their approach to function calls and tools (currenly in Beta).
The tool function definitions and execution of the API calls for the selected tool(s) is handled by Superface using the Hub API endpoints. The choice of which tool to use, and other decision making is handled by Claude.
You can download this example as a runnable .ipynb notebook:
superface_hub_api_anthropic_weather_example.ipynb
The Anthropic Python SDK is required for this example.
pip install anthropic
Import the dependencies and configure the required constants. Some of these, such as those relating to the SUPERFACE_USER_ID
are for example purposes. In a production environment you would handle this differenly, as outlined in the Session Management guide.
import anthropic
import json
import requests as r
from IPython.display import display, Markdown
# Set a random number of your choice, but don't change it
# once you have run the notebook, otherwise you will create another user.
SUPERFACE_USER_ID_CONSTANT =
# Use the number to create a unique ID
SUPERFACE_USER_ID = "sfclaudedemo|" + str(SUPERFACE_USER_ID_CONSTANT)
# Default URL for Superface
SUPERFACE_BASE_URL = "<https://pod.superface.ai/api/hub>"
# Set the Superface authentication token
SUPERFACE_API_TOKEN="<your-superface-api-token>"
# Set the OpenAI API Key
ANTHROPIC_API_KEY="<your-anthropic-api-key>"
To initialize the Anthropic SDK, pass in your API key.
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
There are a lot of repetitive tasks when dealing with LLMs so having some helper functions to cut down on code is always the way to go. First we define two functions to use with Superface's Hub API.
# Helper function to return the tool function descriptors
def get_superface_tools():
headers = {"Authorization": "Bearer "+ SUPERFACE_AUTH_TOKEN}
tools = r.get(SUPERFACE_BASE_URL + "/fd", headers=headers)
return tools.json()
# Helper function to perform the action for all the functions.
# This is the only API call required regardless of what the function is.
def perform_action(tool_name=None, tool_body=None):
headers = {"Authorization": "Bearer "+ SUPERFACE_AUTH_TOKEN, "x-superface-user-id": SUPERFACE_USER_ID}
perform = r.post(SUPERFACE_BASE_URL + "/perform/" + tool_name, headers=headers, json=tool_body)
return json.dumps(perform.json())
The first function, get_superface_tools()
, retrieves the function descriptions for any tools that have been added in your Superface account. At the very least, you'll already have the Wttr.in tool that this example uses.
The second function, perform_action()
, is responsible for executing the API request for the tool and specific function that Claude will choose. It also ensures that this request is authenticated as a specific user using the x-superface-user-id
header.