import os
import json
import requests
from dotenv import load_dotenv
load_dotenv()
# Configuration
FREEPLAY_API_KEY = os.getenv("FREEPLAY_API_KEY")
project_id = "<YOUR-PROJECT-ID>"
# self hosted instances will need to change the hostname
base_api_url = "https://app.freeplay.ai/api/v2"
template_name = "api-test"
def create_template():
url = (
base_api_url + f"/projects/{project_id}/prompt-templates/name/{template_name}/versions?create_template_if_not_exists=1"
)
prompt_template_setup = {
"template_messages": load_prompt_content(prompt_file_path),
"provider": "openai",
"model": "gpt-4.1-mini-2025-04-14",
"llm_parameters": {
"temperature": 0.2,
"max_tokens": 256,
},
"version_name": "test-version-1",
"version_description": "Development test version with mustache variable",
}
return send_request(url, prompt_template_setup)
def update_template(template_id):
url = (
base_api_url + f"/projects/{project_id}/prompt-templates/id/{template_id}/versions"
)
# Load the base messages and modify the system message
messages = load_prompt_content(prompt_file_path)
for msg in messages:
if msg["role"] == "system":
msg["content"] = "You talk like a pirate. " + msg["content"]
prompt_template_setup = {
"template_messages": messages,
"provider": "openai",
"model": "gpt-4.1-mini-2025-04-14",
"llm_parameters": {
"temperature": 0.2,
"max_tokens": 256,
},
"version_name": "test-version-2",
"version_description": "Update-to-tone",
}
return send_request(url, prompt_template_setup)
# Read prompt content from file
def load_prompt_content(filepath): # simulate loading a prompt from file
return [
{
"role": "system",
"content": "You are a helpful LLM assistant. Who always responds with the user's name."
},
{
"role": "user",
"content": "Hi, my name is {{name}}."
}
]
def send_request(url, prompt_template_data):
headers = {
"Authorization": f"Bearer {FREEPLAY_API_KEY}",
"Content-Type": "application/json",
}
try:
response = requests.post(url, headers=headers, json=prompt_template_data)
response.raise_for_status() # Raise an exception for bad HTTP status codes
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error sending request: {e}")
return None
if **name** == "**main**": # Create template first
create_response = create_template()
print(create_response)
# Extract template ID from the create response
template_id = create_response.get("prompt_template_id")
template_version_id = create_response.get("prompt_template_version_id")
print(
f"created prompt template with template id {template_id} and version id {template_version_id}"
)
# If template ID exists, update the template
if template_id:
update_response = update_template(template_id)
print(update_response)