File size: 5,092 Bytes
e18813b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# πŸ“¨ Prompt Management Structure

## Overview
This document explains how AI prompts are organized and managed in the PyCatan AI system.

## Directory Structure

```
examples/ai_testing/my_games/ai_logs/
└── session_YYYYMMDD_HHMMSS/          # Game session folder
    β”œβ”€β”€ NH/                            # Player NH
    β”‚   β”œβ”€β”€ prompts/                   # All prompts for this player
    β”‚   β”‚   β”œβ”€β”€ prompt_1.json          # First prompt
    β”‚   β”‚   β”œβ”€β”€ prompt_1.txt           # Human-readable version
    β”‚   β”‚   β”œβ”€β”€ prompt_2.json          # Second prompt
    β”‚   β”‚   β”œβ”€β”€ prompt_2.txt
    β”‚   β”‚   └── ...                    # More prompts as game progresses
    β”‚   └── responses/                 # AI responses (to be implemented)
    β”‚       β”œβ”€β”€ response_1.json
    β”‚       └── ...
    β”œβ”€β”€ Alex/                          # Player Alex
    β”‚   β”œβ”€β”€ prompts/
    β”‚   β”‚   β”œβ”€β”€ prompt_1.json
    β”‚   β”‚   └── ...
    β”‚   └── responses/
    └── Sarah/                         # Player Sarah
        β”œβ”€β”€ prompts/
        └── responses/
```

## How It Works

### 1. **Prompt Generation**
Every time the game state changes and it's a player's turn, the system generates a new prompt:

```python
from examples.ai_testing.generate_prompts_from_state import main as generate_prompts

# This creates numbered prompts for each player
generate_prompts()
```

### 2. **Prompt Numbering**
- Each new prompt gets a sequential number: `prompt_1.json`, `prompt_2.json`, etc.
- **Always use the highest numbered prompt** - it's the most recent one
- Old prompts are kept for debugging and analysis

### 3. **Prompt Format**
Each `.json` file contains:
- `response_schema`: The expected format for LLM response
- `system_instruction`: Instructions for the LLM
- `prompt`: The actual game state and context

### 4. **Getting the Latest Prompt**

```python
from examples.ai_testing.generate_prompts_from_state import get_latest_prompt

# Get the most recent prompt for a player
prompt_file, prompt_data = get_latest_prompt("NH")

if prompt_file:
    print(f"Latest prompt: {prompt_file}")
    # Use prompt_data to send to LLM
```

Or use the example script:
```bash
python examples/ai_testing/example_get_latest_prompt.py NH
```

## File Types

### JSON Files (`prompt_N.json`)
**Send this to the LLM**
- Complete request ready for API
- Includes response schema
- Machine-readable format

### TXT Files (`prompt_N.txt`)
**For human inspection**
- Formatted for readability
- Includes headers and sections
- Helpful for debugging

## Benefits of This Structure

βœ… **No Overwrites** - Each prompt is saved with unique number
βœ… **Easy to Track** - Can see full history of prompts
βœ… **Simple to Use** - Always use the highest number
βœ… **Player-Specific** - Each player has their own folder
βœ… **Session-Based** - Each game session is separate
βœ… **Debugging** - Can compare prompts across turns

## Example Usage

### Basic Usage
```python
import json
from pathlib import Path
from examples.ai_testing.generate_prompts_from_state import get_latest_prompt

# 1. Get latest prompt for player
prompt_file, prompt_data = get_latest_prompt("NH")

# 2. Extract the prompt
if prompt_data:
    llm_request = prompt_data  # Already in correct format
    
    # 3. Send to LLM (pseudo-code)
    response = llm_client.send(
        system=llm_request["system_instruction"],
        prompt=llm_request["prompt"],
        response_format=llm_request["response_schema"]
    )
```

### Finding All Prompts for a Player
```python
from pathlib import Path

player_name = "NH"
session_dir = Path("examples/ai_testing/my_games/ai_logs/session_20260104_025733")
player_prompts = sorted((session_dir / player_name / "prompts").glob("prompt_*.json"))

print(f"Found {len(player_prompts)} prompts for {player_name}")
for prompt in player_prompts:
    print(f"  - {prompt.name}")
```

### Comparing Two Prompts
```python
import json

# Get two sequential prompts
prompt_1 = session_dir / "NH" / "prompts" / "prompt_1.json"
prompt_2 = session_dir / "NH" / "prompts" / "prompt_2.json"

with open(prompt_1) as f1, open(prompt_2) as f2:
    data1 = json.load(f1)
    data2 = json.load(f2)
    
    # Compare what changed
    turn1 = data1["prompt"]["game_state"]["meta"]["turn"]
    turn2 = data2["prompt"]["game_state"]["meta"]["turn"]
    
    print(f"Prompt 1 was for turn {turn1}")
    print(f"Prompt 2 was for turn {turn2}")
```

## Future Enhancements

- [ ] Add `responses/` directory for AI agent responses
- [ ] Create prompt-response pairs for training
- [ ] Add automatic cleanup of old sessions
- [ ] Add prompt compression for long games
- [ ] Add prompt diff tool to see what changed

## Related Files

- [generate_prompts_from_state.py](generate_prompts_from_state.py) - Main prompt generation logic
- [example_get_latest_prompt.py](example_get_latest_prompt.py) - Example usage
- [play_with_prompts.py](play_with_prompts.py) - Auto-generate prompts during gameplay