Decorator Usage
The @tracker
decorator provides a simple way to automatically track prompt executions in your existing LLM code. This guide covers how to use the decorator effectively.
Basic Usage
The most basic usage of the decorator requires only a prompt ID:
from promptsite.decorator import tracker
@tracker(prompt_id="my-prompt")
def call_llm(content=None, llm_config=None, variables=None):
# Your LLM call logic here
return response
Full Configuration
The decorator supports several configuration options:
from promptsite.model.variable import ArrayVariable
from pydantic import BaseModel, Field
class Weather(BaseModel):
date: str = Field(description="The date of the weather data")
temperature: float = Field(description="The temperature in Celsius")
condition: str = Field(description="The weather condition (sunny, rainy, etc)")
@tracker(
prompt_id="weather-prompt",
description="Weather prompt with structured data",
tags=["weather", "weather-analysis"],
ps_config={"storage_backend": "git"},
llm_config={"temperature": 0.7},
variables={
"weather": ArrayVariable(Weather) # Pydantic model
}
)
def translate_text(content=None, llm_config=None, variables=None):
# LLM call implementation
pass
Variable Validation
The decorator supports Pydantic models for variable validation.
You can disable validation of variables by setting the disable_validation
flag to True
. It is useful when you have validated the variables in the prompt, it can reduce the run time of the decorated function. See the example below:
@tracker(
prompt_id="my-prompt-with-disable-validation",
variables={
"weather": ArrayVariable(model=Weather, disable_validation=True) # Disable validation of the variable
}
)
def my_function(content=None, **kwargs):
return llm_call(content)
my_function(
content="This is a test",
variables={"weather": [{"date": "2021-01-01", "temperature": 20, "condition": "sunny"}]}
)
Automatic Run Tracking
The decorator automatically tracks: - Execution time - LLM configuration - Input variables - Final prompt - LLM output
Example tracked run data:
```python:tests/test_decorator.py startLine: 58 endLine: 65
## Configuration Options
The `@tracker` decorator accepts the following parameters:
- `prompt_id` (str, required): Unique identifier for the prompt
- `content` (str, optional): Default content for the prompt
- `description` (str, optional): Description of the prompt
- `tags` (List[str], optional): Tags for categorizing the prompt
- `ps_config` (Dict, optional): PromptSite configuration
- `ps` (PromptSite, optional): Existing PromptSite instance
- `llm_config` (Dict, optional): Default LLM configuration
- `variables` (Dict, optional): Variable definitions using Pydantic models
## Working with Variables
The decorator supports both simple and complex variable types:
### Simple Variables
```python
from promptsite.model.variable import StringVariable
@tracker(
prompt_id="simple-prompt",
variables={
"text": StringVariable()
}
)
def process_text(content=None, variables=None):
return llm_call(content)
Complex Variables
from pydantic import BaseModel, Field
class Weather(BaseModel):
date: str = Field(description="The date of the weather data")
temperature: float = Field(description="The temperature in Celsius")
condition: str = Field(description="The weather condition (sunny, rainy, etc)")
@tracker(
prompt_id="complex-prompt",
variables={
"weather": ArrayVariable(Weather)
}
)
def process_weather(content=None, variables=None):
return llm_call(content)
Error Handling
The decorator will raise appropriate exceptions for: - Missing required variables - Invalid variable types - Configuration errors - Storage backend issues
Example error handling:
try:
@tracker(prompt_id="my-prompt")
def my_function():
pass
except ConfigError as e:
print(f"Configuration error: {e}")
except PromptNotFoundError as e:
print(f"Prompt not found: {e}")
Best Practices
-
Variable Validation: Always define Pydantic models for complex variables to ensure proper validation
-
Error Handling: Implement proper error handling for potential exceptions
-
Define PromptSite Instance: Pass the PromptSite instance to the decorator to use the same instance for all decorators, this is useful when you want to use the same PromptSite instance to do other operations (i.e. get prompt, get version, get run, etc.)
Next Steps
- Learn about Variable Definitions
- Explore the Python API
- Check out CLI Usage ```