Skip to main content

1. Setup clients

Initialize Freeplay and Anthropic client SDKs.

2. Fetch prompt from Freeplay

Pull in the formatted prompt with Freeplay. The prompt contains tools schema that’ll we’ll pass down below.

3. Call Anthropic with the tools

When creating a new completion, pass in the tools schema from the prompt we fetched.

4. Handle tool call

When LLM responds back with a tool call, call the external function in your service. As an example here, we are calling get_temperature function

5. Record tool call and schema

Pass in the schema and completion response to capture the tool call and its associated schema.

Examples

import os
import time
import json
from anthropic import Anthropic, NotGiven
from anthropic.types import ToolUseBlock
from freeplay import Freeplay, RecordPayload, ResponseInfo, CallInfo

# A mock function that gets temperature for a location 
def get_temperature(location: str) -> float:
    return 72.5
  
project_id=os.environ['FREEPLAY_PROJECT_ID']
fpclient = Freeplay(
    freeplay_api_key=os.environ['FREEPLAY_API_KEY'],
    api_base=f"{os.environ['FREEPLAY_API_URL']}/api"
)
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))

input_variables = {'location': "Boulder, CO"}
formatted_prompt = fpclient.prompts.get_formatted(
    project_id=project_id,
    template_name='my-anthropic-prompt',
    environment='latest',
    variables=input_variables
)

start = time.time()
completion = client.messages.create(
    system=formatted_prompt.system_content or NotGiven(),
    messages=formatted_prompt.llm_prompt,
    model=formatted_prompt.prompt_info.model,
    tools=formatted_prompt.tool_schema,
    **formatted_prompt.prompt_info.model_parameters
)
end = time.time()

# Get all messages including the completion
messages = formatted_prompt.all_messages({
    'content': completion.content,
    'role': completion.role,
})

# Handle tool calls if present
if isinstance(completion.content, list):
    for block in completion.content:
        if isinstance(block, ToolUseBlock) and block.name == "weather_of_location":
            temperature = get_temperature(block.input["location"])
            tool_response_message = {
                "role": "user", 
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": block.id,
                        "content": str(temperature),
                    }
                ]
            }
            messages.append(tool_response_message)

session = fpclient.sessions.create()
fpclient.recordings.create(
    RecordPayload(
        project_id=project_id,
        all_messages=messages,
        session_info=session.session_info,
        inputs=input_variables,
        prompt_version_info=formatted_prompt.prompt_info,
        call_info=CallInfo.from_prompt_info(formatted_prompt.prompt_info, start, end),
        tool_schema=formatted_prompt.tool_schema,
        response_info=ResponseInfo(is_complete=completion.stop_reason == 'stop_sequence')
    )
)