Spaces:
Configuration error
Configuration error
File size: 6,251 Bytes
fb4d798 | 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | """
Test JSON Prompt Format
-----------------------
Quick test to verify the new JSON format matches promt_format.text structure.
"""
import sys
import json
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from pycatan.ai.prompt_templates import PromptBuilder, get_response_schema
from pycatan.ai.config import AIConfig
def test_prompt_structure():
"""Test that generated prompts match expected structure."""
print("\n" + "="*80)
print("π§ͺ TESTING JSON PROMPT FORMAT")
print("="*80 + "\n")
# Create sample optimized game state
sample_state = {
"H": ["", "W12", "S5", "W4", "S8", "B6"],
"N": [
[], # 0 - empty
[[2, 5], [1, 2], None], # Node 1
[[1, 3], [1, 3], "W2"], # Node 2 with wood port
],
"players": {
"a": {
"vp": 2,
"res": {"W": 3, "B": 1},
"dev": {"h": ["K"], "r": []},
"stat": []
},
"b": {
"vp": 1,
"res": {"S": 2},
"dev": {"h": [], "r": []},
"stat": []
}
},
"bld": [
[20, "a", "S"],
[25, "b", "S"]
],
"rds": [
[[20, 21], "a"],
[[25, 26], "b"]
],
"meta": {
"robber": 10,
"phase": "MAIN_GAME",
"curr": "a",
"dice": 8
}
}
# Build prompt
builder = PromptBuilder()
prompt = builder.build_prompt(
meta_data={
"agent_name": "a",
"my_color": "Red",
"role": "You are player 'a' (Red). Play strategically to win."
},
task_context={
"what_just_happened": "You rolled an 8. You received 2 wood.",
"instructions": "Choose your action from 'allowed_actions'."
},
game_state=sample_state,
social_context={
"recent_chat": [
{"sender": "b", "content": "Anyone need sheep?"}
]
},
memory=[
"b needs ore, I should trade",
"Focus on longest road"
],
constraints={
"usage_instructions": "Choose one action. Use exact parameter structure.",
"allowed_actions": [
{
"action": "build_road",
"description": "Build a road on an edge",
"example_parameters": {"edge_id": 15}
},
{
"action": "end_turn",
"description": "End your turn",
"example_parameters": {}
}
]
}
)
# Verify structure matches promt_format.text
print("β
Testing prompt structure...\n")
required_sections = ["meta_data", "task_context", "game_state",
"social_context", "memory", "constraints"]
for section in required_sections:
if section in prompt:
print(f" β {section}")
else:
print(f" β Missing: {section}")
print("\nπ Meta Data:")
print(f" β’ agent_name: {prompt['meta_data'].get('agent_name')}")
print(f" β’ my_color: {prompt['meta_data'].get('my_color')}")
print(f" β’ role: {prompt['meta_data'].get('role')[:50]}...")
print("\nπ Task Context:")
print(f" β’ what_just_happened: {prompt['task_context'].get('what_just_happened')}")
print("\nπ Game State:")
game_state = prompt.get('game_state', '')
if isinstance(game_state, str):
lines = game_state.split('\n')
print(f" β’ Type: String with embedded JSON")
print(f" β’ Lines: {len(lines)}")
print(f" β’ Has legend: {'FORMAT GUIDE' in game_state}")
print(f" β’ Has H array: {'\"H\":' in game_state}")
print(f" β’ First 100 chars: {game_state[:100]}...")
print("\nπ Social Context:")
print(f" β’ recent_chat: {len(prompt['social_context'].get('recent_chat', []))} messages")
print("\nπ Memory:")
print(f" β’ notes_for_myself: {len(prompt['memory'].get('notes_for_myself', []))} notes")
print("\nπ Constraints:")
print(f" β’ allowed_actions: {len(prompt['constraints'].get('allowed_actions', []))} actions")
# Test response schema
print("\n" + "="*80)
print("π Response Schema:")
print("="*80)
schema = get_response_schema()
print(json.dumps(schema, indent=2))
# Create full LLM request
print("\n" + "="*80)
print("π€ Complete LLM Request Structure:")
print("="*80)
llm_request = {
"response_schema": schema,
"system_instruction": "You are an expert Settlers of Catan player. Respond in JSON format.",
"prompt": prompt
}
# Save to file
output_file = Path('examples/ai_testing/my_games/test_prompt_format.json')
output_file.parent.mkdir(exist_ok=True, parents=True)
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(llm_request, f, indent=2, ensure_ascii=False)
print(f"\nβ
Saved complete request to: {output_file}")
print(f"π File size: {output_file.stat().st_size:,} bytes")
# Estimate tokens (rough)
json_str = json.dumps(llm_request, ensure_ascii=False)
estimated_tokens = len(json_str) // 4
print(f"π Estimated tokens: ~{estimated_tokens:,}")
print("\n" + "="*80)
print("β
ALL TESTS PASSED!")
print("="*80)
print("\nπ‘ The prompt format matches promt_format.text structure:")
print(" β’ meta_data β")
print(" β’ task_context β")
print(" β’ game_state (with legend) β")
print(" β’ social_context β")
print(" β’ memory β")
print(" β’ constraints β")
print("\nπ‘ Complete LLM request includes:")
print(" β’ response_schema (tells LLM how to respond)")
print(" β’ system_instruction")
print(" β’ prompt (the actual game state)")
print("\n" + "="*80 + "\n")
if __name__ == '__main__':
test_prompt_structure()
|