How to Retrieve Prompt Templates Without Variable Interpolation
Last updated: December 13, 2024
Traceloop allows you to retrieve prompt templates without variable interpolation. This can be useful when you need to access the raw template structure for analysis or custom processing. Here's how you can do it:
Using the Traceloop API
You can retrieve prompt templates by making a direct API call to Traceloop. Here's a Python script that demonstrates how to fetch and display prompt templates:
import requests
import json
from datetime import datetime
from typing import List, Dict, Any
BASE_URL = "https://api.traceloop.com"
API_KEY = "your-api-key-here" # Replace with your actual API key
def fetch_prompts(base_url: str, api_key: str) -> Dict[str, Any]:
url = f"{base_url}/v1/traceloop/prompts"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
return response.json()
def print_prompt(prompt: Dict[str, Any]):
print(f"Key: {prompt['key']}")
print(f"ID: {prompt['id']}")
print(f"Created at: {prompt['created_at']}")
print(f"Updated at: {prompt['updated_at']}")
print("Versions:")
for version in prompt['versions']:
print(f" - Version: {version['version']}")
print(f" Name: {version.get('name', 'N/A')}")
print(f" Provider: {version['provider']}")
print(f" Templating Engine: {version['templating_engine']}")
print(" Messages:")
for message in version['messages']:
print(f" - Role: {message['role']}")
if isinstance(message['template'], str):
print(f" Template: {message['template']}")
else:
print(" Template:")
for content in message['template']:
print(f" Type: {content['type']}")
if content['type'] == "text":
print(f" Text: {content['text']}")
elif content['type'] == "image_url":
print(f" Image URL: {content['image_url']['url']}")
print(f"Target Version: {prompt['target']['version']}")
print("-" * 40)
def main():
try:
response_data = fetch_prompts(BASE_URL, API_KEY)
prompts = response_data.get("prompts", [])
if not prompts:
print("No prompts found.")
return
print(f"Fetched {len(prompts)} Prompts:")
for prompt in prompts:
print_prompt(prompt)
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()
Understanding the Output
This script will fetch all prompts associated with your account and display their details, including:
Prompt key and ID
Creation and update timestamps
Version information
Provider and templating engine used
Raw template content for each message
The raw template content is displayed without any variable interpolation, allowing you to see the original structure of the prompt.