Spaces:
Configuration error
Configuration error
| """ | |
| 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() | |