Skip to main content

1. Setup clients

Initialize Freeplay and OpenAI 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 OpenAI 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 openai import OpenAI

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

fpclient = Freeplay(
    freeplay_api_key=os.environ['FREEPLAY_API_KEY'],
    api_base=f"{os.environ['FREEPLAY_API_URL']}/api"
)
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
project_id=os.environ['FREEPLAY_PROJECT_ID']

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

start = time.time()
completion = client.chat.completions.create(
    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()

# Append the completion to list of messages
messages = formatted_prompt.all_messages(completion.choices[0].message)
if completion.choices[0].message.tool_calls:
    for tool_call in completion.choices[0].message.tool_calls:
        if tool_call.function.name == "weather_of_location":
            args = json.loads(tool_call.function.arguments)
            temperature = get_temperature(args["location"])

            tool_response_message = {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "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.choices[0].finish_reason == 'stop'),
    )
)