File size: 2,626 Bytes
3370983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Prompt Management System

This module provides a centralized way to manage prompts using **PromptLayer** with a local filesystem fallback. It allows you to version prompts, manage environments (dev/staging/prod), and easily switch between local development and cloud-managed prompts.

## πŸš€ Usage

Import the `get_prompt` function to load prompts anywhere in your application.

```python
from src.backend.prompts import get_prompt
```

### 1. Load from PromptLayer (Default)
By default, if `PROMPTLAYER_API_KEY` is set in your environment, it will fetch the prompt from PromptLayer using the configured environment label (default: `production`).

```python
# Fetches 'DB_Executor' tagged with current environment (e.g., 'production')
system_prompt = get_prompt("DB_Executor")
```

### 2. Load Latest Version (Ignore Environment)
Useful for testing or when you want to ensure you have the absolute latest saved version from PromptLayer, ignoring any 'prod' or 'dev' tags.

```python
# Fetches the absolute latest version of the template
system_prompt = get_prompt("DB_Executor", latest_version=True)
```

### 3. Force Load from Local File
You can force loading from a local file, which is useful for local development without an internet connection or for testing new prompts before pushing to PromptLayer.

```python
# Loads from src/prompts/templates/db_executor/v1.txt
# (Assuming 'v1.txt' is the file name in that directory, or provide full path)
system_prompt = get_prompt("db_executor/v1", local_prompt_path="src/prompts/templates")
```

If you don't provide a `local_prompt_path` but also don't have a `PROMPTLAYER_API_KEY` set, it defaults to looking in `src/prompts/templates`.

## πŸ“‚ Directory Structure

Store your local prompt backups in `src/prompts/templates/`.

```
src/prompts/
β”œβ”€β”€ __init__.py          # Exposes get_prompt
β”œβ”€β”€ prompt_layer.py      # Core logic
β”œβ”€β”€ templates/           # Local prompt storage
β”‚   β”œβ”€β”€ db_executor/
β”‚   β”‚   └── v1.txt
β”‚   β”œβ”€β”€ supervisor/
β”‚   β”‚   └── v1.txt
β”‚   └── ...
└── info.md              # This file
```

## βš™οΈ Configuration

- **`PROMPTLAYER_API_KEY`**: Set this env var to enable PromptLayer.
- **`PROMPT_ENVIRONMENT`**: Set to `dev`, `staging`, or `production` (default) to control which tagged version is loaded.

## πŸ” Debugging

The system prints clear logs to stdout so you know where your prompt came from:

- `πŸ“‹ Loaded prompt '...' from PromptLayer (env=production)`
- `πŸ“‹ Loaded prompt '...' from PromptLayer (latest version)`
- `πŸ“„ Loaded prompt '...' from local file: ...`