Introduction and System Overview
Creating a sophisticated chatbot that can generate complete board games represents one of the most challenging applications of large language models in interactive entertainment. This system must combine natural language processing, game design principles, creative storytelling, and visual rendering capabilities into a cohesive platform that can understand user preferences and translate them into playable board games.
The core challenge lies in bridging the gap between abstract user requirements and concrete game mechanics. When a user specifies they want a "collaborative medieval ghost game for 4 players with medium complexity," the system must interpret these constraints and generate a complete game experience including rules, board layout, story elements, and visual representation.
The chatbot we will design operates as a multi-stage pipeline where each component handles specific aspects of game creation. The input processing stage captures and validates user requirements, the generation engine creates game mechanics and narratives, the validation system ensures logical consistency, and the rendering system produces visual output in either web or console format.
At the heart of this system lies a sophisticated Large Language Model that serves as the central intelligence orchestrating all aspects of board game creation. The LLM provides both analytical and creative capabilities, from understanding user requirements to generating creative content and managing the entire design process. The model must handle multiple types of reasoning simultaneously, including creative writing, logical reasoning, spatial understanding, and domain-specific knowledge about game design principles.
System Architecture and Core Components
The foundation of our board game creation chatbot rests on a modular architecture that separates concerns while maintaining tight integration between components. The system consists of several interconnected modules that work together to transform user input into complete board game experiences.
The Natural Language Understanding module serves as the primary interface between users and the system. This component must parse complex game requirements and extract structured data about game type, theme, complexity, and player constraints. Unlike simple keyword matching, this module needs to understand context and resolve ambiguities in user descriptions.
The Game Knowledge Base contains extensive information about board game mechanics, common patterns, and design principles. This database includes information about different game types such as worker placement, area control, deck building, and resource management. It also maintains relationships between game mechanics and their typical implementations.
The Story Generation Engine creates compelling narratives that align with the specified game theme and mechanics. This component must ensure that the generated story supports the game objectives and provides meaningful context for player actions. The engine draws from narrative templates while maintaining creativity and coherence.
The Board Layout Generator creates spatial representations of the game board based on the mechanics and story requirements. This component must consider player count, game complexity, and mechanical needs when determining board size, layout patterns, and special areas.
The Validation Engine ensures that all generated components work together harmoniously. It checks for logical inconsistencies, balance issues, and mechanical conflicts that could make the game unplayable or unfun.
Core LLM Architecture and Model Selection
The foundation of the board game creation system relies on a sophisticated LLM implementation that can handle multiple types of reasoning simultaneously. The system requires a model with strong capabilities in creative writing, logical reasoning, spatial understanding, and domain-specific knowledge about game design principles.
class BoardGameLLMCore:
def __init__(self, model_name="gpt-4", temperature_settings=None):
self.primary_model = self.initialize_primary_llm(model_name)
self.specialized_models = self.initialize_specialized_models()
self.temperature_settings = temperature_settings or self.get_default_temperature_settings()
self.context_manager = LLMContextManager()
self.prompt_templates = GameDesignPromptTemplates()
def initialize_primary_llm(self, model_name):
# Configure the main LLM with specific parameters for game design
llm_config = {
'model': model_name,
'max_tokens': 4096,
'temperature': 0.7, # Balanced creativity and consistency
'top_p': 0.9,
'frequency_penalty': 0.1,
'presence_penalty': 0.1
}
# Add game design specific system prompts
system_prompt = self.create_game_design_system_prompt()
return LLMInterface(config=llm_config, system_prompt=system_prompt)
def create_game_design_system_prompt(self):
system_prompt = """
You are an expert board game designer with deep knowledge of game mechanics,
player psychology, balance theory, and creative storytelling. Your role is to
create engaging, balanced, and innovative board games based on user specifications.
Core Expertise Areas:
- Game mechanics design and integration
- Player interaction modeling
- Narrative integration with gameplay
- Balance analysis and optimization
- Spatial reasoning for board layouts
- Component design and functionality
Design Principles:
- Every mechanic must serve a clear purpose
- Player agency and meaningful choices are paramount
- Complexity should scale appropriately with target audience
- Theme and mechanics must be tightly integrated
- Games must be balanced across different player counts
- Rules must be clear and unambiguous
When designing games, always consider:
1. Player engagement and fun factor
2. Mechanical consistency and logical flow
3. Scalability across different player counts
4. Accessibility for the target age group
5. Replayability and strategic depth
"""
return system_prompt
def initialize_specialized_models(self):
# Different aspects of game design may benefit from specialized model configurations
specialized_models = {
'creative_writing': self.configure_creative_model(),
'logical_analysis': self.configure_analytical_model(),
'spatial_reasoning': self.configure_spatial_model(),
'balance_optimization': self.configure_optimization_model()
}
return specialized_models
def configure_creative_model(self):
# Higher temperature for creative story and theme generation
creative_config = {
'temperature': 0.9,
'top_p': 0.95,
'frequency_penalty': 0.2,
'presence_penalty': 0.3
}
return LLMInterface(config=creative_config)
def configure_analytical_model(self):
# Lower temperature for logical analysis and validation
analytical_config = {
'temperature': 0.3,
'top_p': 0.8,
'frequency_penalty': 0.0,
'presence_penalty': 0.0
}
return LLMInterface(config=analytical_config)
The LLM core demonstrates how different model configurations serve different purposes within the game creation pipeline. Creative tasks require higher temperature settings to encourage innovation, while analytical tasks need lower temperatures to ensure logical consistency and accuracy.
The system prompt engineering plays a crucial role in establishing the LLM's expertise and behavioral guidelines. The prompt must establish the model as a game design expert while providing clear principles and constraints that guide decision-making throughout the generation process.
Input Processing and Validation System
The input processing system must handle the inherent ambiguity and incompleteness of natural language game descriptions. Users rarely provide complete specifications in their initial requests, so the system must identify missing information and guide users through a structured discovery process.
The natural language understanding component relies heavily on the LLM's ability to interpret complex, ambiguous user input and extract structured game requirements. This involves sophisticated prompt engineering and context management to ensure accurate interpretation of user intentions.
class GameSpecificationProcessor:
def __init__(self, llm_core):
self.llm_core = llm_core
self.required_fields = {
'game_type': ['collaboration', 'competition'],
'mechanics_type': ['luck', 'strategy', 'knowledge', 'action', 'exploration', 'trading', 'quiz', 'simulation'],
'theme_context': str,
'game_goal': str,
'complexity_level': range(1, 11),
'player_count': range(2, 9),
'minimum_age': range(6, 100)
}
self.optional_fields = {
'game_duration': range(15, 300),
'special_requirements': str,
'preferred_mechanics': list
}
self.extraction_prompts = self.create_extraction_prompt_templates()
self.validation_prompts = self.create_validation_prompt_templates()
self.clarification_generator = LLMClarificationGenerator(llm_core)
def process_initial_input(self, user_input, conversation_context=None):
# Use LLM to extract structured information from natural language
extraction_prompt = self.build_extraction_prompt(user_input, conversation_context)
extraction_response = self.llm_core.primary_model.generate(
prompt=extraction_prompt,
response_format="structured_json"
)
# Parse and validate extracted information
extracted_specs = self.parse_extraction_response(extraction_response)
# Use LLM to identify ambiguities and missing information
validation_prompt = self.build_validation_prompt(extracted_specs, user_input)
validation_response = self.llm_core.primary_model.generate(
prompt=validation_prompt,
response_format="structured_json"
)
validation_results = self.parse_validation_response(validation_response)
# Generate clarifying questions using LLM
if validation_results['needs_clarification']:
clarifying_questions = self.generate_clarifying_questions(
extracted_specs, validation_results, user_input
)
return {
'extracted_specifications': extracted_specs,
'needs_clarification': True,
'clarifying_questions': clarifying_questions
}
return {
'extracted_specifications': extracted_specs,
'needs_clarification': False,
'confidence_score': validation_results['confidence_score']
}
def build_extraction_prompt(self, user_input, conversation_context):
context_section = ""
if conversation_context:
context_section = f"""
Previous conversation context:
{self.format_conversation_context(conversation_context)}
"""
extraction_prompt = f"""
{context_section}
User Input: "{user_input}"
Extract board game specifications from the user input above. Analyze the text carefully
to identify explicit and implicit requirements. Consider context clues and common
gaming terminology.
Extract the following information where available:
1. Game Type (collaboration vs competition)
2. Mechanics Type (strategy, luck, knowledge, action, exploration, trading, quiz, simulation)
3. Theme/Setting (medieval, sci-fi, fantasy, modern, historical, etc.)
4. Game Goal/Objective
5. Complexity Level (1-10 scale)
6. Player Count (specific number or range)
7. Minimum Age
8. Estimated Duration
9. Special Requirements or Preferences
10. Preferred Components (dice, cards, boards, tokens, etc.)
For each field, provide:
- extracted_value: The specific value found (or null if not found)
- confidence: How confident you are in this extraction (0.0-1.0)
- source_text: The specific part of the input that led to this extraction
- reasoning: Brief explanation of why you extracted this value
Also identify:
- ambiguous_elements: Parts of the input that could be interpreted multiple ways
- implicit_assumptions: Reasonable assumptions you're making based on context
- missing_critical_info: Essential information not provided by the user
Respond in valid JSON format.
"""
return extraction_prompt
def extract_specifications(self, user_input):
specifications = {}
# Extract game type using pattern matching and context analysis
if any(word in user_input.lower() for word in ['together', 'team', 'cooperative', 'collaborative']):
specifications['game_type'] = 'collaboration'
elif any(word in user_input.lower() for word in ['against', 'compete', 'win', 'beat']):
specifications['game_type'] = 'competition'
# Extract complexity level from numerical indicators
complexity_patterns = re.findall(r'complexity\s*(?:level\s*)?(\d+)', user_input.lower())
if complexity_patterns:
specifications['complexity_level'] = int(complexity_patterns[0])
# Extract player count with flexible parsing
player_patterns = re.findall(r'(\d+)(?:\s*-\s*(\d+))?\s*players?', user_input.lower())
if player_patterns:
min_players, max_players = player_patterns[0]
if max_players:
specifications['player_count'] = (int(min_players), int(max_players))
else:
specifications['player_count'] = int(min_players)
# Extract theme and context using semantic analysis
theme_keywords = self.extract_theme_context(user_input)
if theme_keywords:
specifications['theme_context'] = theme_keywords
return specifications
def generate_clarifying_questions(self, extracted_specs, validation_results, original_input):
clarification_prompt = f"""
Original user input: "{original_input}"
Extracted specifications: {json.dumps(extracted_specs, indent=2)}
Validation issues: {json.dumps(validation_results, indent=2)}
Generate 2-3 clarifying questions that will help resolve ambiguities and gather
missing essential information. The questions should:
1. Be specific and actionable
2. Offer clear options when appropriate
3. Build on what the user has already specified
4. Prioritize the most important missing information
5. Use friendly, conversational language
6. Avoid overwhelming the user with too many options
For each question, explain why this information is needed for game creation.
Format as a JSON array of question objects with 'question' and 'purpose' fields.
"""
response = self.llm_core.primary_model.generate(
prompt=clarification_prompt,
response_format="structured_json"
)
return self.parse_clarification_response(response)
This input processor demonstrates sophisticated natural language understanding capabilities that go beyond simple keyword matching. The system uses pattern recognition, semantic analysis, and contextual understanding to extract meaningful game specifications from user input.
The processor handles various input formats and styles, from casual descriptions like "I want a spooky game for kids" to detailed specifications with specific parameters. When information is missing, the system generates targeted questions that guide users toward providing the necessary details without overwhelming them.
Internet Search Integration for Game Ideas
Integrating internet search capabilities allows the chatbot to access current board game trends, mechanics, and design patterns that can inspire and inform the generation process. This component must balance creativity with existing successful game patterns while avoiding direct copying of copyrighted material.
The search integration system operates on multiple levels, from broad thematic research to specific mechanical pattern discovery. When a user requests a game with particular themes or mechanics, the system can research similar games to understand common patterns and successful implementations.
class GameResearchEngine:
def __init__(self, search_api_key, llm_core):
self.search_client = SearchAPIClient(search_api_key)
self.llm_core = llm_core
self.game_database = BoardGameDatabase()
self.pattern_analyzer = MechanicsPatternAnalyzer()
def research_game_concepts(self, game_specifications):
research_results = {
'thematic_inspiration': self.search_thematic_games(game_specifications['theme_context']),
'mechanical_patterns': self.search_mechanical_implementations(game_specifications['mechanics_type']),
'complexity_examples': self.find_complexity_references(game_specifications['complexity_level']),
'player_count_considerations': self.analyze_player_count_mechanics(game_specifications['player_count'])
}
synthesized_insights = self.synthesize_research_findings(research_results)
return synthesized_insights
def search_thematic_games(self, theme_context):
search_queries = self.generate_thematic_search_queries(theme_context)
thematic_results = []
for query in search_queries:
search_results = self.search_client.search(f"board games {query} mechanics theme")
filtered_results = self.filter_relevant_results(search_results, theme_context)
thematic_results.extend(filtered_results)
# Use LLM to extract common thematic elements and successful patterns
analysis_prompt = f"""
Search results for thematic games related to "{theme_context}":
{json.dumps(thematic_results, indent=2)}
Analyze these search results to identify:
1. Common thematic elements that work well in board games
2. Successful mechanical implementations of this theme
3. Popular variations and interpretations
4. Design patterns that enhance thematic immersion
5. Potential pitfalls or overused tropes to avoid
Provide insights that can inform original game design while avoiding direct copying.
"""
thematic_analysis = self.llm_core.primary_model.generate(
prompt=analysis_prompt,
temperature=0.6
)
return self.parse_thematic_analysis(thematic_analysis)
def synthesize_research_findings(self, research_results):
synthesis_prompt = f"""
Research Results Summary:
{json.dumps(research_results, indent=2)}
Synthesize these research findings to provide actionable insights for game design.
Focus on:
1. RECOMMENDED MECHANICS: Which mechanics work well for this type of game?
2. THEMATIC INTEGRATION STRATEGIES: How can theme and mechanics be effectively combined?
3. COMPLEXITY SCALING APPROACHES: How do successful games handle complexity?
4. PLAYER INTERACTION MODELS: What interaction patterns work well?
5. INNOVATION OPPORTUNITIES: Where are there gaps or opportunities for innovation?
Provide specific, actionable recommendations that can guide original game creation.
"""
synthesis_response = self.llm_core.primary_model.generate(
prompt=synthesis_prompt,
temperature=0.5
)
return self.parse_synthesis_response(synthesis_response)
The research engine demonstrates how the system can leverage external knowledge sources while maintaining focus on the user's specific requirements. The search integration goes beyond simple keyword matching to perform semantic analysis of game mechanics and thematic elements.
This approach allows the system to discover innovative combinations of existing mechanics while ensuring that generated games benefit from proven design patterns. The research component also helps identify potential issues or challenges associated with specific game types or player counts.
Game Logic Generation Engine
The heart of the board game creation system lies in its ability to generate coherent, balanced, and engaging game mechanics that align with user specifications. This engine must understand the relationships between different game elements and ensure that all components work together to create a satisfying play experience.
The generation process begins with establishing the core game loop, which defines how players take turns, make decisions, and progress toward victory conditions. This foundation determines how other game elements will interact and provides the structure for more detailed mechanical development.
class LLMGameMechanicsGenerator:
def __init__(self, llm_core):
self.llm_core = llm_core
self.mechanics_library = GameMechanicsLibrary()
self.balance_analyzer = GameBalanceAnalyzer()
self.interaction_modeler = PlayerInteractionModeler()
def generate_complete_game_logic(self, specifications, research_insights):
# Use LLM to design core game loop
core_loop_prompt = self.build_core_loop_generation_prompt(specifications)
core_loop_response = self.llm_core.primary_model.generate(
prompt=core_loop_prompt,
temperature=0.7
)
core_loop = self.parse_core_loop_response(core_loop_response)
# Generate specific mechanics using iterative LLM calls
mechanics_generation_prompt = self.build_mechanics_generation_prompt(
specifications, research_insights, core_loop
)
mechanics_response = self.llm_core.primary_model.generate(
prompt=mechanics_generation_prompt,
temperature=0.6
)
generated_mechanics = self.parse_mechanics_response(mechanics_response)
# Use LLM to validate and refine mechanics integration
integration_prompt = self.build_integration_validation_prompt(
core_loop, generated_mechanics, specifications
)
integration_response = self.llm_core.analytical_model.generate(
prompt=integration_prompt,
temperature=0.3
)
integration_analysis = self.parse_integration_response(integration_response)
# Refine mechanics based on integration analysis
if integration_analysis['needs_refinement']:
refined_mechanics = self.refine_mechanics_with_llm(
generated_mechanics, integration_analysis, specifications
)
return refined_mechanics
return generated_mechanics
def build_mechanics_generation_prompt(self, specifications, research_insights, core_structure):
research_section = self.format_research_insights(research_insights)
mechanics_prompt = f"""
Game Specifications:
- Type: {specifications['game_type']}
- Mechanics Focus: {specifications['mechanics_type']}
- Theme: {specifications['theme_context']}
- Complexity: {specifications['complexity_level']}/10
- Players: {specifications['player_count']}
- Age: {specifications['minimum_age']}+
- Goal: {specifications['game_goal']}
Core Game Loop:
{json.dumps(core_structure, indent=2)}
Research Insights:
{research_section}
Design comprehensive game mechanics that create an engaging experience matching
the specifications. Consider the following design principles:
1. MECHANICAL COHERENCE: All mechanics must work together seamlessly
2. PLAYER AGENCY: Players should have meaningful choices at each decision point
3. SCALABILITY: Mechanics must work well across the specified player count
4. COMPLEXITY APPROPRIATENESS: Mechanical complexity should match the target level
5. THEMATIC INTEGRATION: Mechanics should reinforce the chosen theme
For each mechanic, provide:
PRIMARY MECHANICS (2-4 core systems):
- Name and brief description
- Detailed implementation rules
- How it integrates with the core loop
- Player interaction points
- Complexity contribution
- Thematic justification
SUPPORTING MECHANICS (1-3 additional systems):
- How they enhance primary mechanics
- When they activate during gameplay
- Their role in achieving game goals
RESOURCE SYSTEMS (if applicable):
- Types of resources
- Acquisition methods
- Usage opportunities
- Scarcity and abundance patterns
PLAYER INTERACTION MECHANISMS:
- Direct interaction opportunities
- Indirect interaction through shared systems
- Conflict and cooperation balance
PROGRESSION SYSTEMS:
- How players advance toward victory
- Milestone markers
- Catch-up mechanisms (if needed)
Ensure all mechanics support the specified game goal and create multiple viable
strategies for achieving victory. Consider how mechanics will feel during actual
play and prioritize player engagement over mechanical complexity.
"""
return mechanics_prompt
def refine_mechanics_with_llm(self, initial_mechanics, integration_analysis, specifications):
refinement_prompt = f"""
Initial Mechanics Design:
{json.dumps(initial_mechanics, indent=2)}
Integration Analysis Issues:
{json.dumps(integration_analysis['issues'], indent=2)}
Refinement Recommendations:
{json.dumps(integration_analysis['recommendations'], indent=2)}
Original Specifications:
{json.dumps(specifications, indent=2)}
Refine the game mechanics to address the identified issues while maintaining
the core design intent. Focus on:
1. Resolving mechanical conflicts
2. Improving player interaction balance
3. Ensuring complexity appropriateness
4. Strengthening thematic integration
5. Optimizing for the target player count
Provide the refined mechanics in the same detailed format, highlighting
what changes were made and why. Ensure that refinements don't introduce
new problems or compromise the overall game experience.
If multiple refinement approaches are possible, choose the one that best
preserves player agency and maintains mechanical elegance.
"""
refinement_response = self.llm_core.primary_model.generate(
prompt=refinement_prompt,
temperature=0.5
)
return self.parse_refinement_response(refinement_response)
The mechanics generation component showcases how the LLM serves as a creative game designer that understands complex relationships between different game systems. The prompts guide the model through systematic design processes while encouraging creative solutions.
The iterative refinement process demonstrates how multiple LLM calls with different temperature settings can improve design quality. Creative generation uses higher temperatures, while analytical validation uses lower temperatures to ensure logical consistency.
Story and Board Creation System
Creating compelling narratives and visual board layouts requires the system to understand how story elements can enhance gameplay while ensuring that the physical game components support the intended mechanics. The story generation must go beyond simple theme application to create meaningful narrative integration.
The board creation system must consider spatial relationships, player accessibility, component placement, and visual clarity while supporting the generated game mechanics. This involves complex spatial reasoning and understanding of how physical constraints affect gameplay.
class LLMStoryAndBoardCreator:
def __init__(self, llm_core):
self.llm_core = llm_core
self.narrative_engine = NarrativeGenerationEngine()
self.spatial_designer = SpatialLayoutDesigner()
self.component_generator = GameComponentGenerator()
self.visual_renderer = VisualRenderingEngine()
def create_integrated_game_experience(self, game_logic, specifications):
# Generate narrative framework that supports mechanics
narrative_framework = self.create_narrative_framework(
game_logic, specifications['theme_context'], specifications['game_goal']
)
# Design board layout that supports both story and mechanics
board_layout = self.design_board_layout(
game_logic, narrative_framework, specifications
)
# Generate game components with thematic integration
game_components = self.generate_thematic_components(
game_logic, narrative_framework, board_layout
)
# Create detailed story content and flavor text
story_content = self.generate_detailed_story_content(
narrative_framework, game_logic, specifications
)
# Integrate all elements and validate coherence
integrated_experience = self.integrate_story_and_mechanics(
narrative_framework, board_layout, game_components, story_content, game_logic
)
return integrated_experience
def create_narrative_framework(self, game_logic, theme_context, game_goal):
framework_prompt = f"""
Game Specifications:
- Theme: {theme_context}
- Goal: {game_goal}
- Type: {game_logic.get('game_type', 'unknown')}
Game Mechanics Summary:
{self.summarize_mechanics_for_narrative(game_logic)}
Create a compelling narrative framework that seamlessly integrates with the
game mechanics. The story should enhance gameplay rather than distract from it.
NARRATIVE FRAMEWORK REQUIREMENTS:
1. SETTING ESTABLISHMENT:
- Rich, immersive world that supports the theme
- Clear geographical or conceptual boundaries
- Atmosphere that matches the intended player experience
- Historical or contextual background that explains current situation
2. CENTRAL CONFLICT:
- Primary tension that drives the game forward
- Multiple stakeholders with different motivations
- Escalating stakes that create urgency
- Connection to the specified game goal
3. CHARACTER ROLES:
- Define what each player represents in the story
- Unique motivations and capabilities for each role
- Relationships between different player characters
- How character goals align with or conflict with game mechanics
4. STORY PROGRESSION:
- How the narrative unfolds during gameplay
- Key story beats that correspond to game phases
- Climactic moments that enhance mechanical tension
- Resolution possibilities based on different outcomes
5. THEMATIC ELEMENTS:
- Recurring motifs that reinforce the theme
- Symbolic elements that connect to mechanics
- Emotional tone appropriate for the target age group
- Cultural or genre elements that enhance immersion
Ensure the narrative framework supports the specified gameplay type
and creates meaningful context for all major game mechanics. The story should
make players feel like their mechanical choices have narrative significance.
"""
framework_response = self.llm_core.creative_writing.generate(
prompt=framework_prompt,
temperature=0.8
)
return self.parse_framework_response(framework_response)
def design_board_layout(self, game_logic, narrative_framework, specifications):
layout_prompt = f"""
Game Logic Summary:
{json.dumps(game_logic, indent=2)}
Narrative Framework:
{json.dumps(narrative_framework, indent=2)}
Specifications:
- Player Count: {specifications['player_count']}
- Complexity Level: {specifications['complexity_level']}
- Age Group: {specifications['minimum_age']}+
Design a board layout that effectively supports both the game mechanics and
the narrative framework. Consider spatial relationships, player accessibility,
and visual clarity.
BOARD LAYOUT DESIGN:
1. BOARD DIMENSIONS AND STRUCTURE:
- Optimal size for the player count and complexity
- Basic geometric structure (grid, radial, modular, etc.)
- How the structure supports the core game mechanics
2. MECHANICAL ZONES:
- Areas dedicated to specific game mechanics
- Spatial relationships between different mechanical elements
- How zones facilitate player interaction and game flow
3. THEMATIC AREAS:
- Regions that represent important story locations
- How thematic areas integrate with mechanical zones
- Visual and functional design of narrative spaces
4. PLAYER ELEMENTS:
- Starting positions and home areas
- Movement paths and accessibility considerations
- Personal player spaces and shared areas
5. CONNECTION SYSTEMS:
- Paths, adjacencies, and movement rules
- How connections support both mechanics and narrative
- Accessibility and flow optimization
6. COMPONENT PLACEMENT:
- Where different game components are positioned
- Storage and organization considerations
- Visual hierarchy and information clarity
Provide detailed specifications for each area including size, position,
connections, and functional requirements. Ensure the layout creates an
engaging visual experience while supporting smooth gameplay.
"""
layout_response = self.llm_core.spatial_reasoning.generate(
prompt=layout_prompt,
temperature=0.6
)
return self.parse_layout_response(layout_response)
The story and board creation system demonstrates the complex integration required between narrative elements and game mechanics. This component must ensure that every story element serves a purpose in gameplay while maintaining thematic coherence and emotional engagement.
The spatial design aspects require sophisticated understanding of how players interact with physical game components and how board layout affects game flow and player experience. The system must balance aesthetic appeal with functional requirements.
Validation and Refinement Mechanisms
Ensuring that generated games are playable, balanced, and enjoyable requires comprehensive validation systems that can identify potential issues before presenting games to users. This validation must cover mechanical consistency, narrative coherence, balance considerations, and practical playability concerns.
The refinement system allows users to modify and improve generated games through natural language feedback, requiring the system to understand modification requests and implement changes while maintaining overall game coherence.
class LLMGameValidator:
def __init__(self, llm_core):
self.llm_core = llm_core
self.mechanical_validator = MechanicalConsistencyValidator()
self.balance_analyzer = GameBalanceAnalyzer()
self.playability_tester = PlayabilitySimulator()
self.narrative_validator = NarrativeCoherenceValidator()
self.refinement_engine = GameRefinementEngine()
def validate_complete_game(self, generated_game):
validation_results = {
'mechanical_consistency': self.validate_mechanical_consistency(generated_game),
'game_balance': self.analyze_game_balance(generated_game),
'playability_issues': self.identify_playability_issues(generated_game),
'narrative_coherence': self.validate_narrative_integration(generated_game),
'component_feasibility': self.validate_component_feasibility(generated_game)
}
# Identify critical issues that prevent gameplay
critical_issues = self.identify_critical_issues(validation_results)
# Generate recommendations for improvements
improvement_recommendations = self.generate_improvement_recommendations(
validation_results, critical_issues
)
# Calculate overall game quality score
quality_score = self.calculate_quality_score(validation_results)
return {
'validation_results': validation_results,
'critical_issues': critical_issues,
'recommendations': improvement_recommendations,
'quality_score': quality_score,
'ready_for_presentation': len(critical_issues) == 0
}
def validate_mechanical_consistency(self, game):
consistency_prompt = f"""
Complete Game Design:
{self.format_game_for_validation(game)}
Perform a comprehensive mechanical consistency analysis. Evaluate whether
all game systems work together logically and create a coherent play experience.
CONSISTENCY ANALYSIS AREAS:
1. RULE COMPLETENESS:
- Are all mechanics fully specified?
- Do rules cover all possible game situations?
- Are there any ambiguous or unclear instructions?
- Do rules handle edge cases appropriately?
2. MECHANICAL INTEGRATION:
- Do all mechanics work together without conflicts?
- Are there any contradictory rules or requirements?
- Do supporting mechanics enhance rather than complicate primary mechanics?
- Is the complexity level consistent across all systems?
3. TURN STRUCTURE COHERENCE:
- Does the turn structure support all mechanics effectively?
- Are there any mechanics that don't fit the turn flow?
- Do players have meaningful choices at each decision point?
- Is the pacing appropriate for the target audience?
4. VICTORY CONDITION ACCESSIBILITY:
- Can players actually achieve the stated victory conditions?
- Are victory paths clear and achievable through gameplay?
- Do all mechanics contribute to victory condition pursuit?
- Are there any impossible or trivial victory scenarios?
5. COMPONENT FUNCTIONALITY:
- Do all game components serve clear mechanical purposes?
- Are there any missing components needed for gameplay?
- Do component interactions make logical sense?
- Is component complexity appropriate for the target age group?
For each area, provide:
- Consistency rating (1-10 scale)
- Specific issues identified
- Severity assessment (critical, moderate, minor)
- Recommended fixes or improvements
Conclude with an overall mechanical consistency score and priority
recommendations for addressing any identified issues.
"""
consistency_response = self.llm_core.analytical_model.generate(
prompt=consistency_prompt,
temperature=0.2
)
return self.parse_consistency_analysis(consistency_response)
def analyze_game_balance(self, game):
balance_prompt = f"""
Game Design for Balance Analysis:
{self.format_game_for_balance_analysis(game)}
Conduct a thorough game balance analysis focusing on fairness, strategic
depth, and player experience quality across different scenarios.
BALANCE ANALYSIS DIMENSIONS:
1. PLAYER ADVANTAGE DISTRIBUTION:
- Do all players have equal opportunities to win?
- Are there any inherent advantages based on turn order or starting position?
- How do player count variations affect balance?
- Are there catch-up mechanisms for players who fall behind?
2. STRATEGY VIABILITY:
- Are multiple viable strategies available to players?
- Is there a single dominant strategy that makes other approaches obsolete?
- Do different strategies require different skill sets or preferences?
- How do strategies interact and counter each other?
3. RESOURCE BALANCE:
- Are resources appropriately scarce or abundant?
- Do resource acquisition methods offer fair opportunities to all players?
- Are there any resources that become overpowered or useless?
- How does resource distribution affect game pacing?
4. LUCK VS SKILL BALANCE:
- Is the luck/skill ratio appropriate for the target audience?
- Do random elements enhance or undermine strategic planning?
- Can skilled players consistently outperform less skilled players?
- Are there ways to mitigate bad luck or capitalize on good luck?
5. GAME LENGTH AND PACING:
- Does the game maintain engagement throughout its duration?
- Are there any phases that drag or feel rushed?
- Do endgame conditions create appropriate tension and excitement?
- Is the game length appropriate for the complexity and target audience?
6. INTERACTION BALANCE:
- Do player interactions feel meaningful and impactful?
- Is there appropriate balance between cooperation and competition?
- Can players affect each other's strategies without being overly disruptive?
- Are there ways to recover from negative player interactions?
For each dimension, provide:
- Balance rating (1-10 scale)
- Specific balance concerns
- Impact assessment on player experience
- Suggested balance adjustments
Conclude with recommendations for improving overall game balance
while maintaining the intended player experience.
"""
balance_response = self.llm_core.analytical_model.generate(
prompt=balance_prompt,
temperature=0.3
)
return self.parse_balance_analysis(balance_response)
def process_user_refinement_request(self, game, user_feedback, conversation_history):
# Interpret user feedback using LLM
feedback_interpretation = self.interpret_user_feedback(
user_feedback, game, conversation_history
)
# Generate specific modification requests
modification_requests = self.generate_modification_requests(
feedback_interpretation, game
)
# Apply modifications using LLM-guided refinement
refined_game = self.apply_llm_guided_refinements(
game, modification_requests
)
# Validate refined game
validation_results = self.validate_complete_game(refined_game)
# Generate response to user
user_response = self.generate_user_response(
refined_game, validation_results, modification_requests
)
return {
'refined_game': refined_game,
'user_response': user_response,
'modification_summary': modification_requests,
'validation_results': validation_results
}
The validation and refinement system represents a critical component that ensures the quality and playability of generated games. This system must understand complex relationships between game elements and identify subtle issues that could affect player experience.
The refinement capabilities allow for iterative improvement based on user feedback, making the system more collaborative and responsive to user preferences. This requires sophisticated natural language understanding and the ability to modify complex game systems while maintaining coherence.
Rendering System for Web and Console Output
The final step in the board game creation process involves presenting the complete game to users in a clear, visually appealing format. The rendering system must support both web-based interfaces with rich graphics and console-based ASCII representations for terminal environments.
The rendering system must translate abstract game concepts into concrete visual representations that help users understand game mechanics, board layout, and component relationships. This requires careful consideration of information hierarchy, visual clarity, and accessibility.
class GameRenderingSystem:
def __init__(self, llm_core):
self.llm_core = llm_core
self.web_renderer = WebGameRenderer()
self.console_renderer = ConsoleGameRenderer()
self.ascii_artist = ASCIIArtGenerator()
self.layout_optimizer = VisualLayoutOptimizer()
def render_complete_game(self, validated_game, output_format='web'):
if output_format == 'web':
return self.render_web_game(validated_game)
elif output_format == 'console':
return self.render_console_game(validated_game)
else:
raise ValueError(f"Unsupported output format: {output_format}")
def render_console_game(self, game):
console_output = []
# Use LLM to generate formatted game description
description_prompt = f"""
Complete Game Design:
{json.dumps(game, indent=2)}
Create a comprehensive, well-formatted console presentation of this board game.
The output should be clear, organized, and easy to read in a terminal environment.
Format the game information in the following sections:
1. GAME TITLE AND OVERVIEW
2. GAME STORY AND SETTING
3. BOARD LAYOUT DESCRIPTION
4. GAME RULES AND MECHANICS
5. COMPONENT DESCRIPTIONS
6. SETUP INSTRUCTIONS
7. VICTORY CONDITIONS
Use ASCII formatting, clear headings, and organized information presentation.
Make it engaging and easy to understand for players.
"""
formatted_description = self.llm_core.primary_model.generate(
prompt=description_prompt,
temperature=0.4
)
console_output.append(formatted_description)
# Render game board using ASCII graphics
board_section = self.render_console_board(game['board_layout'])
console_output.append(board_section)
return '\n\n'.join(console_output)
def render_console_board(self, board_layout):
board_prompt = f"""
Board Layout Specifications:
{json.dumps(board_layout, indent=2)}
Create an ASCII art representation of this game board that clearly shows:
1. Board structure and dimensions
2. Different zones and areas
3. Player starting positions
4. Connection systems and paths
5. Special locations and features
Use ASCII characters to create a clear, readable board diagram. Include a
legend explaining what different symbols represent. Make the board visually
appealing while maintaining clarity and functionality.
The ASCII board should help players understand the spatial relationships
and game flow at a glance.
"""
ascii_board = self.llm_core.primary_model.generate(
prompt=board_prompt,
temperature=0.3
)
return ascii_board
def create_ascii_grid(self, width, height):
# Create a 2D array representing the board grid
grid = []
for row in range(height * 2 + 1): # Double height for better spacing
grid_row = []
for col in range(width * 4 + 1): # Quadruple width for better spacing
if row % 2 == 0: # Horizontal border rows
if col % 4 == 0:
grid_row.append('+')
else:
grid_row.append('-')
else: # Content rows
if col % 4 == 0:
grid_row.append('|')
else:
grid_row.append(' ')
grid.append(grid_row)
return grid
def render_web_game(self, game):
web_generation_prompt = f"""
Complete Game Design:
{json.dumps(game, indent=2)}
Generate a complete HTML page that presents this board game in an attractive,
interactive web format. Include:
1. HTML structure with proper semantic markup
2. CSS styling for visual appeal and readability
3. Interactive elements where appropriate
4. Responsive design considerations
5. Clear information hierarchy
6. Visual board representation
7. Component illustrations
8. Rules presentation
Create a professional, engaging web presentation that makes the game
easy to understand and appealing to play.
"""
web_content = self.llm_core.primary_model.generate(
prompt=web_generation_prompt,
temperature=0.6
)
return self.parse_web_content(web_content)
The rendering system demonstrates the complexity of translating abstract game concepts into concrete visual representations. The console renderer must create clear, informative ASCII representations that convey spatial relationships and game mechanics without relying on color or complex graphics.
The web renderer provides richer visual experiences but must maintain the same clarity and information hierarchy as the console version. Both rendering modes must present complex game information in an organized, accessible manner that helps users understand and play the generated games.
Advanced LLM Techniques for Game Creation
Several advanced LLM techniques enhance the board game creation system's capabilities beyond basic text generation. These techniques leverage the model's reasoning abilities and enable more sophisticated game design processes.
class AdvancedLLMGameDesignTechniques:
def __init__(self, llm_core):
self.llm_core = llm_core
self.chain_of_thought_processor = ChainOfThoughtProcessor()
self.few_shot_examples = self.load_few_shot_examples()
self.self_critique_system = SelfCritiqueSystem(llm_core)
def chain_of_thought_game_design(self, specifications):
"""
Use chain-of-thought reasoning to work through complex game design decisions
"""
cot_prompt = f"""
Game Specifications: {json.dumps(specifications, indent=2)}
Let's work through the game design process step by step, thinking carefully
about each decision and how it affects the overall design.
Step 1: Analyze the core design challenge
What is the fundamental design challenge presented by these specifications?
Let me think about this...
The user wants a {specifications['game_type']} game with {specifications['mechanics_type']}
mechanics for {specifications['player_count']} players. The theme is {specifications['theme_context']}
and the goal is {specifications['game_goal']}. The complexity should be {specifications['complexity_level']}/10.
The main challenge here is...
Step 2: Identify key design constraints
What constraints do these specifications place on the design?
Step 3: Consider player experience goals
What kind of experience should this game create for players?
Step 4: Design core mechanics
Given the constraints and experience goals, what core mechanics would work best?
Step 5: Integrate theme and mechanics
How can the theme be meaningfully integrated with the mechanics?
Step 6: Address potential issues
What problems might arise with this design approach, and how can we address them?
Work through each step systematically, showing your reasoning process.
"""
return self.llm_core.primary_model.generate(prompt=cot_prompt, temperature=0.6)
def self_critique_and_refinement(self, initial_design):
"""
Use the LLM to critique its own design and suggest improvements
"""
critique_prompt = f"""
Initial Game Design:
{json.dumps(initial_design, indent=2)}
Now, critically analyze this game design. What are its strengths and weaknesses?
How could it be improved?
CRITICAL ANALYSIS:
Strengths:
- What aspects of this design work well?
- What makes this game potentially engaging?
- How well does it meet the original specifications?
Weaknesses:
- What problems or concerns do you see?
- Where might players get confused or frustrated?
- What aspects feel underdeveloped or problematic?
Improvement Opportunities:
- What specific changes would address the identified weaknesses?
- How could the strengths be enhanced further?
- What alternative approaches might work better?
Be honest and thorough in your analysis. The goal is to create the best
possible game design.
"""
critique_response = self.llm_core.analytical_model.generate(
prompt=critique_prompt, temperature=0.3
)
# Apply suggested improvements
improvement_prompt = f"""
Original Design: {json.dumps(initial_design, indent=2)}
Critical Analysis: {critique_response}
Based on the critical analysis, create an improved version of the game design
that addresses the identified weaknesses while preserving and enhancing the strengths.
"""
improved_design = self.llm_core.primary_model.generate(
prompt=improvement_prompt, temperature=0.5
)
return {
'critique': critique_response,
'improved_design': improved_design
}
def multi_perspective_design_evaluation(self, game_design):
"""
Evaluate the game design from multiple perspectives using role-playing
"""
perspectives = [
"experienced board gamer",
"casual family player",
"game design expert",
"target age group player"
]
evaluations = {}
for perspective in perspectives:
perspective_prompt = f"""
Game Design: {json.dumps(game_design, indent=2)}
Evaluate this game design from the perspective of a {perspective}.
Consider what this type of player would think about the game.
As a {perspective}, I would think:
Appeal: How appealing would this game be to me?
Accessibility: How easy would it be for me to learn and play?
Engagement: How engaging would the gameplay experience be?
Concerns: What concerns or criticisms would I have?
Suggestions: What changes would make this game better for players like me?
Provide honest feedback from this perspective.
"""
evaluation = self.llm_core.primary_model.generate(
prompt=perspective_prompt, temperature=0.6
)
evaluations[perspective] = evaluation
return evaluations
These advanced techniques demonstrate how sophisticated prompting strategies can enhance the LLM's game design capabilities. Chain-of-thought reasoning helps the model work through complex design decisions systematically, while few-shot learning leverages successful design patterns.
The self-critique and multi-perspective evaluation techniques enable the system to identify and address potential issues before presenting games to users. This multi-layered approach to quality assurance helps ensure that generated games meet high standards for playability and enjoyment.
User Interaction and Iterative Refinement
The user interaction system leverages the LLM's conversational abilities to facilitate iterative game refinement based on user feedback. This requires sophisticated context management and the ability to translate user preferences into specific design modifications.
class LLMUserInteractionManager:
def __init__(self, llm_core):
self.llm_core = llm_core
self.conversation_context = ConversationContextManager()
self.refinement_interpreter = LLMRefinementInterpreter(llm_core)
def process_user_feedback(self, game_design, user_feedback, conversation_history):
# Interpret user feedback using LLM
feedback_interpretation = self.interpret_user_feedback(
user_feedback, game_design, conversation_history
)
# Generate specific modification requests
modification_requests = self.generate_modification_requests(
feedback_interpretation, game_design
)
# Apply modifications using LLM-guided refinement
refined_game = self.apply_llm_guided_refinements(
game_design, modification_requests
)
# Validate refined game
validation_results = self.validate_refined_game(refined_game)
# Generate response to user
user_response = self.generate_user_response(
refined_game, validation_results, modification_requests
)
return {
'refined_game': refined_game,
'user_response': user_response,
'modification_summary': modification_requests,
'validation_results': validation_results
}
def interpret_user_feedback(self, user_feedback, current_game, conversation_history):
interpretation_prompt = f"""
Current Game Design Summary:
{self.summarize_game_for_feedback(current_game)}
Conversation History:
{self.format_conversation_history(conversation_history)}
User Feedback: "{user_feedback}"
Interpret the user's feedback to understand what specific changes they want
made to the game design. Consider both explicit requests and implicit preferences.
FEEDBACK INTERPRETATION ANALYSIS:
1. EXPLICIT CHANGE REQUESTS:
- What specific modifications did the user directly request?
- Which game components or systems do these changes affect?
- How clear and actionable are the requested changes?
2. IMPLICIT PREFERENCES:
- What underlying preferences can be inferred from the feedback?
- Are there unstated concerns about game balance, complexity, or theme?
- What aspects of the current design seem to satisfy the user?
3. PRIORITY ASSESSMENT:
- Which changes are most important to the user?
- Are there any changes that conflict with each other?
- What changes would have the biggest impact on user satisfaction?
4. SCOPE ANALYSIS:
- Do requested changes require minor adjustments or major redesign?
- Which game systems would be affected by the proposed changes?
- Are there any changes that would compromise game integrity?
5. CLARIFICATION NEEDS:
- Are there any ambiguous aspects of the feedback?
- What additional information would help implement the changes effectively?
- Should any proposed changes be discussed further with the user?
Provide a structured interpretation that can guide specific design modifications
while preserving the positive aspects of the current design.
"""
interpretation_response = self.llm_core.primary_model.generate(
prompt=interpretation_prompt,
temperature=0.4
)
return self.parse_feedback_interpretation(interpretation_response)
The user interaction system demonstrates how the LLM can serve as an intelligent intermediary between user preferences and technical game design requirements. The system must understand nuanced feedback and translate it into actionable design modifications.
The iterative refinement process showcases the LLM's ability to maintain design coherence while implementing specific changes. This requires sophisticated understanding of how different game elements interact and how modifications can cascade through interconnected systems.
Technical Challenges and Implementation Considerations
Building a comprehensive board game creation chatbot presents numerous technical challenges that require sophisticated solutions and careful architectural decisions. Understanding these challenges helps developers prepare for the complexity involved in creating such a system.
Natural language understanding represents one of the most significant challenges, as users express game preferences using varied vocabulary, incomplete specifications, and ambiguous descriptions. The LLM must interpret creative and subjective language while extracting concrete, actionable requirements. This requires sophisticated prompt engineering and context management to handle the nuanced ways people describe their gaming preferences.
Game balance validation requires deep understanding of game theory, player psychology, and mathematical modeling. The LLM must identify subtle balance issues that could emerge during actual play, requiring sophisticated simulation capabilities and heuristic analysis. The system must understand how different mechanics interact and how small changes can have cascading effects on game balance.
Spatial reasoning for board layout generation involves complex geometric calculations, accessibility considerations, and aesthetic optimization. The LLM must understand how physical constraints affect gameplay while creating visually appealing and functional board designs. This requires the model to reason about three-dimensional space and player ergonomics.
The integration between story elements and game mechanics requires sophisticated understanding of narrative structure, thematic coherence, and player engagement principles. The LLM must ensure that every story element serves a meaningful purpose in gameplay while maintaining narrative integrity and emotional resonance.
Context management becomes critical when handling long conversations about game refinement. The LLM must maintain awareness of previous design decisions, user preferences, and the evolution of the game design throughout multiple iterations. This requires sophisticated memory management and context compression techniques.
Performance optimization becomes critical when dealing with complex game generation algorithms, extensive search operations, and real-time user interactions. The system must balance generation quality with response time requirements while managing computational resources efficiently.
Quality assurance requires comprehensive testing frameworks that can validate generated games across multiple dimensions including playability, balance, narrative coherence, and technical correctness. The LLM must serve as its own critic while also incorporating external validation mechanisms.
Prompt engineering represents a critical technical challenge, as the quality of LLM outputs depends heavily on well-crafted prompts that guide the model through complex reasoning processes. Different aspects of game design require different prompting strategies, and the system must dynamically adjust its approach based on the specific task at hand.
Temperature and parameter tuning requires careful balance between creativity and consistency. Creative tasks benefit from higher temperatures that encourage innovative solutions, while analytical tasks require lower temperatures to ensure logical consistency and accuracy.
Error handling and recovery mechanisms must account for the probabilistic nature of LLM outputs. The system must detect when the LLM produces invalid or inconsistent results and implement recovery strategies that maintain the overall design process flow.
Future Enhancement Opportunities
The board game creation chatbot represents a foundation for numerous advanced features and capabilities that could enhance the system's utility and user experience. These enhancements could expand the system's creative capabilities while improving the quality and variety of generated games.
Advanced AI integration could incorporate machine learning models trained specifically on successful board games to improve generation quality and identify optimal design patterns. This could include reinforcement learning systems that optimize game balance through simulated play testing, allowing the system to learn from thousands of virtual game sessions.
Collaborative design features could allow multiple users to work together on game creation, with the LLM mediating between different preferences and requirements while maintaining design coherence. The system could facilitate design discussions and help resolve conflicts between different creative visions.
Physical prototyping integration could generate print-ready files for game boards, cards, and components, allowing users to create physical versions of their generated games using 3D printing and traditional printing services. The LLM could optimize designs for different manufacturing constraints and cost considerations.
Playtesting simulation could use AI agents to simulate actual gameplay, identifying balance issues, pacing problems, and strategic depth concerns before presenting games to users. These simulations could test thousands of game scenarios to identify edge cases and potential problems.
Community features could allow users to share generated games, rate designs, and collaborate on improvements, creating a repository of successful game patterns and design innovations. The LLM could learn from community feedback to improve future game generation.
Advanced customization options could support more sophisticated game mechanics, complex victory conditions, and innovative component designs that push the boundaries of traditional board game design. The system could explore entirely new categories of games that leverage unique mechanical interactions.
Integration with existing game platforms could allow generated games to be played digitally, providing immediate playtesting opportunities and broader accessibility. This could include virtual reality implementations that create immersive game experiences.
Conclusion
Creating an LLM chatbot capable of generating complete, playable board games represents a significant technical achievement that combines natural language processing, game design expertise, creative storytelling, and sophisticated validation systems. The system must understand user preferences, research existing game patterns, generate coherent mechanics and narratives, validate game quality, and present results in clear, accessible formats.
The Large Language Model serves as the central intelligence that orchestrates all aspects of this complex process. Through careful prompt engineering, temperature tuning, and specialized model configurations, the LLM can serve as a creative game designer, analytical critic, and collaborative partner in the game creation process.
The modular architecture described in this article provides a foundation for building such a system while maintaining flexibility for future enhancements and modifications. Each component addresses specific challenges while integrating seamlessly with other system elements to create a cohesive game generation pipeline.
The technical challenges involved require sophisticated solutions and careful consideration of user experience, performance, and quality requirements. The LLM must handle multiple types of reasoning simultaneously, from creative writing to logical analysis, while maintaining consistency and coherence throughout the design process.
Success in building such a system requires deep understanding of both technical implementation details and game design principles, along with careful attention to user needs and expectations. The resulting chatbot could democratize board game creation, allowing anyone to generate custom games tailored to their specific preferences and requirements.
The integration of advanced LLM techniques such as chain-of-thought reasoning, self-critique, and multi-perspective evaluation enables the system to produce high-quality games that rival human-designed alternatives. The iterative refinement capabilities allow for collaborative improvement based on user feedback, making the system responsive to individual preferences and creative visions.
The future of AI-assisted creative tools looks promising, and board game generation represents just one example of how sophisticated AI systems can augment human creativity while maintaining the essential human elements that make games engaging and meaningful. The combination of LLM intelligence with human creativity and feedback creates a powerful platform for innovation in game design and interactive entertainment.`
Source Code without warranty
```go
// main.go
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/go-performance-optimizer/internal/analyzer"
"github.com/go-performance-optimizer/internal/backup"
"github.com/go-performance-optimizer/internal/config"
"github.com/go-performance-optimizer/internal/filesystem"
"github.com/go-performance-optimizer/internal/generator"
"github.com/go-performance-optimizer/internal/llm"
"github.com/go-performance-optimizer/internal/parser"
"github.com/go-performance-optimizer/internal/ui"
)
func main() {
var (
path = flag.String("path", "", "Path to Go file, directory, or Git repository")
configPath = flag.String("config", "", "Path to configuration file (optional)")
verbose = flag.Bool("verbose", false, "Enable verbose logging")
llmModel = flag.String("model", "gpt-4", "LLM model to use for analysis")
apiKey = flag.String("api-key", "", "API key for LLM service")
)
flag.Parse()
if *path == "" {
fmt.Println("Usage: go-optimizer -path <file|directory|git-repo> -api-key <key> [-config <config-file>] [-verbose] [-model <model>]")
os.Exit(1)
}
if *apiKey == "" {
fmt.Println("Error: API key is required for LLM service")
os.Exit(1)
}
// Initialize logger
logger := log.New(os.Stdout, "[GO-OPTIMIZER] ", log.LstdFlags)
if *verbose {
logger.SetOutput(os.Stdout)
} else {
logger.SetOutput(os.Stderr)
}
// Load configuration
cfg, err := config.Load(*configPath)
if err != nil {
logger.Fatalf("Failed to load configuration: %v", err)
}
// Initialize LLM client
llmClient, err := llm.NewClient(*llmModel, *apiKey, logger)
if err != nil {
logger.Fatalf("Failed to initialize LLM client: %v", err)
}
// Initialize components
fsHandler := filesystem.NewHandler(logger)
backupManager := backup.NewManager(cfg.BackupDir, logger)
goParser := parser.NewGoParser(logger)
analyzer := analyzer.NewLLMPerformanceAnalyzer(llmClient, cfg.AnalysisRules, logger)
codeGenerator := generator.NewLLMCodeGenerator(llmClient, logger)
userInterface := ui.NewCLI(logger)
// Create the main optimizer
optimizer := &LLMPerformanceOptimizer{
fsHandler: fsHandler,
backupManager: backupManager,
parser: goParser,
analyzer: analyzer,
generator: codeGenerator,
ui: userInterface,
llmClient: llmClient,
config: cfg,
logger: logger,
}
// Run optimization process
if err := optimizer.OptimizeCodebase(*path); err != nil {
logger.Fatalf("Optimization failed: %v", err)
}
logger.Println("LLM-powered optimization process completed successfully")
}
// LLMPerformanceOptimizer orchestrates the LLM-powered optimization process
type LLMPerformanceOptimizer struct {
fsHandler filesystem.Handler
backupManager backup.Manager
parser parser.GoParser
analyzer analyzer.LLMPerformanceAnalyzer
generator generator.LLMCodeGenerator
ui ui.Interface
llmClient llm.Client
config *config.Config
logger *log.Logger
}
// OptimizeCodebase performs the complete LLM-powered optimization workflow
func (lpo *LLMPerformanceOptimizer) OptimizeCodebase(path string) error {
lpo.logger.Printf("Starting LLM-powered optimization for path: %s", path)
// Step 1: Discover Go files
files, err := lpo.fsHandler.DiscoverGoFiles(path)
if err != nil {
return fmt.Errorf("failed to discover Go files: %w", err)
}
lpo.logger.Printf("Found %d Go files to analyze with LLM", len(files))
// Step 2: Create codebase context for LLM
codebaseContext, err := lpo.buildCodebaseContext(files)
if err != nil {
return fmt.Errorf("failed to build codebase context: %w", err)
}
// Step 3: Get LLM analysis of entire codebase
optimizations, err := lpo.analyzer.AnalyzeCodebaseWithLLM(codebaseContext)
if err != nil {
return fmt.Errorf("LLM analysis failed: %w", err)
}
if len(optimizations) == 0 {
lpo.ui.ShowMessage("LLM found no performance optimizations in the codebase.")
return nil
}
lpo.logger.Printf("LLM identified %d potential optimizations", len(optimizations))
// Step 4: Present optimizations to user and apply approved ones
return lpo.processOptimizations(optimizations)
}
// buildCodebaseContext creates a comprehensive context for LLM analysis
func (lpo *LLMPerformanceOptimizer) buildCodebaseContext(files []string) (*llm.CodebaseContext, error) {
context := &llm.CodebaseContext{
Files: make(map[string]string),
Dependencies: make([]string, 0),
Metadata: make(map[string]interface{}),
}
// Read all Go files
for _, file := range files {
content, err := lpo.fsHandler.ReadFile(file)
if err != nil {
lpo.logger.Printf("Warning: failed to read file %s: %v", file, err)
continue
}
context.Files[file] = string(content)
}
// Extract dependencies and metadata
context.Dependencies = lpo.extractDependencies(files)
context.Metadata["total_files"] = len(files)
context.Metadata["analysis_timestamp"] = fmt.Sprintf("%d", lpo.getCurrentTimestamp())
return context, nil
}
// processOptimizations presents LLM optimizations to user and applies approved ones
func (lpo *LLMPerformanceOptimizer) processOptimizations(optimizations []analyzer.LLMOptimization) error {
for i, opt := range optimizations {
lpo.ui.ShowLLMOptimization(i+1, len(optimizations), opt)
if lpo.ui.AskForApproval() {
if err := lpo.applyLLMOptimization(opt); err != nil {
lpo.logger.Printf("Failed to apply LLM optimization: %v", err)
continue
}
lpo.ui.ShowMessage("LLM optimization applied successfully!")
} else {
lpo.ui.ShowMessage("LLM optimization skipped.")
}
}
return nil
}
// applyLLMOptimization applies a single LLM-generated optimization with backup
func (lpo *LLMPerformanceOptimizer) applyLLMOptimization(opt analyzer.LLMOptimization) error {
// Create backup before modification
backupPath, err := lpo.backupManager.CreateBackup(opt.FilePath)
if err != nil {
return fmt.Errorf("failed to create backup: %w", err)
}
lpo.logger.Printf("Created backup: %s", backupPath)
// Generate optimized code using LLM
optimizedCode, err := lpo.generator.GenerateOptimizedCode(opt)
if err != nil {
// Restore from backup on failure
if restoreErr := lpo.backupManager.RestoreBackup(backupPath, opt.FilePath); restoreErr != nil {
lpo.logger.Printf("Failed to restore backup: %v", restoreErr)
}
return fmt.Errorf("LLM failed to generate optimized code: %w", err)
}
// Write LLM-generated optimized code to file
if err := lpo.fsHandler.WriteFile(opt.FilePath, optimizedCode); err != nil {
// Restore from backup on failure
if restoreErr := lpo.backupManager.RestoreBackup(backupPath, opt.FilePath); restoreErr != nil {
lpo.logger.Printf("Failed to restore backup: %v", restoreErr)
}
return fmt.Errorf("failed to write LLM-optimized code: %w", err)
}
return nil
}
// Helper methods
func (lpo *LLMPerformanceOptimizer) extractDependencies(files []string) []string {
// Implementation would extract import statements from Go files
return []string{"sync", "runtime", "fmt", "context"}
}
func (lpo *LLMPerformanceOptimizer) getCurrentTimestamp() int64 {
return 1234567890 // Placeholder
}
```
```go
// internal/llm/client.go
package llm
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
// Client interface for LLM interactions
type Client interface {
AnalyzeCode(prompt string, codeContext *CodebaseContext) (*AnalysisResponse, error)
GenerateOptimizedCode(prompt string, originalCode string) (*CodeGenerationResponse, error)
ExplainOptimization(optimization string) (string, error)
}
// CodebaseContext represents the entire codebase context for LLM analysis
type CodebaseContext struct {
Files map[string]string `json:"files"`
Dependencies []string `json:"dependencies"`
Metadata map[string]interface{} `json:"metadata"`
}
// AnalysisResponse represents LLM's analysis response
type AnalysisResponse struct {
Optimizations []OptimizationSuggestion `json:"optimizations"`
Summary string `json:"summary"`
Confidence float64 `json:"confidence"`
}
// OptimizationSuggestion represents a single optimization suggestion from LLM
type OptimizationSuggestion struct {
Type string `json:"type"`
FilePath string `json:"file_path"`
LineStart int `json:"line_start"`
LineEnd int `json:"line_end"`
Description string `json:"description"`
Rationale string `json:"rationale"`
OriginalCode string `json:"original_code"`
OptimizedCode string `json:"optimized_code"`
EstimatedImpact string `json:"estimated_impact"`
Confidence float64 `json:"confidence"`
}
// CodeGenerationResponse represents LLM's code generation response
type CodeGenerationResponse struct {
OptimizedCode string `json:"optimized_code"`
Explanation string `json:"explanation"`
Confidence float64 `json:"confidence"`
}
// OpenAIClient implements Client for OpenAI GPT models
type OpenAIClient struct {
apiKey string
model string
baseURL string
httpClient *http.Client
logger *log.Logger
}
// NewClient creates a new LLM client
func NewClient(model, apiKey string, logger *log.Logger) (Client, error) {
if apiKey == "" {
return nil, fmt.Errorf("API key is required")
}
return &OpenAIClient{
apiKey: apiKey,
model: model,
baseURL: "https://api.openai.com/v1",
httpClient: &http.Client{
Timeout: 60 * time.Second,
},
logger: logger,
}, nil
}
// AnalyzeCode sends code to LLM for performance analysis
func (c *OpenAIClient) AnalyzeCode(prompt string, codeContext *CodebaseContext) (*AnalysisResponse, error) {
c.logger.Printf("Sending codebase to LLM for analysis...")
// Construct the analysis prompt
analysisPrompt := c.buildAnalysisPrompt(prompt, codeContext)
// Prepare OpenAI API request
requestBody := map[string]interface{}{
"model": c.model,
"messages": []map[string]string{
{
"role": "system",
"content": c.getSystemPrompt(),
},
{
"role": "user",
"content": analysisPrompt,
},
},
"max_tokens": 4000,
"temperature": 0.1,
"response_format": map[string]string{
"type": "json_object",
},
}
// Make API call
response, err := c.makeAPICall("/chat/completions", requestBody)
if err != nil {
return nil, fmt.Errorf("LLM API call failed: %w", err)
}
// Parse response
return c.parseAnalysisResponse(response)
}
// GenerateOptimizedCode asks LLM to generate optimized version of code
func (c *OpenAIClient) GenerateOptimizedCode(prompt string, originalCode string) (*CodeGenerationResponse, error) {
c.logger.Printf("Asking LLM to generate optimized code...")
// Construct the generation prompt
generationPrompt := c.buildGenerationPrompt(prompt, originalCode)
// Prepare OpenAI API request
requestBody := map[string]interface{}{
"model": c.model,
"messages": []map[string]string{
{
"role": "system",
"content": c.getCodeGenerationSystemPrompt(),
},
{
"role": "user",
"content": generationPrompt,
},
},
"max_tokens": 2000,
"temperature": 0.1,
}
// Make API call
response, err := c.makeAPICall("/chat/completions", requestBody)
if err != nil {
return nil, fmt.Errorf("LLM code generation failed: %w", err)
}
// Parse response
return c.parseCodeGenerationResponse(response)
}
// ExplainOptimization asks LLM to explain an optimization
func (c *OpenAIClient) ExplainOptimization(optimization string) (string, error) {
c.logger.Printf("Asking LLM to explain optimization...")
explanationPrompt := fmt.Sprintf(`
Please explain the following Go performance optimization in detail:
%s
Provide:
1. Why this optimization improves performance
2. What specific bottlenecks it addresses
3. Any potential trade-offs or considerations
4. When this optimization should and shouldn't be used
`, optimization)
requestBody := map[string]interface{}{
"model": c.model,
"messages": []map[string]string{
{
"role": "system",
"content": "You are an expert Go performance optimization consultant. Provide clear, detailed explanations of performance optimizations.",
},
{
"role": "user",
"content": explanationPrompt,
},
},
"max_tokens": 1000,
"temperature": 0.2,
}
response, err := c.makeAPICall("/chat/completions", requestBody)
if err != nil {
return "", fmt.Errorf("LLM explanation failed: %w", err)
}
return c.parseExplanationResponse(response)
}
// buildAnalysisPrompt constructs the prompt for code analysis
func (c *OpenAIClient) buildAnalysisPrompt(userPrompt string, context *CodebaseContext) string {
prompt := fmt.Sprintf(`
Analyze the following Go codebase for performance optimization opportunities.
%s
CODEBASE CONTEXT:
Dependencies: %v
Total Files: %d
FILES TO ANALYZE:
`, userPrompt, context.Dependencies, len(context.Files))
// Add file contents (truncated for large codebases)
fileCount := 0
for filePath, content := range context.Files {
if fileCount >= 10 { // Limit to prevent token overflow
prompt += fmt.Sprintf("\n... and %d more files", len(context.Files)-fileCount)
break
}
// Truncate very large files
if len(content) > 5000 {
content = content[:5000] + "\n... [file truncated]"
}
prompt += fmt.Sprintf(`
=== FILE: %s ===
%s
`, filePath, content)
fileCount++
}
prompt += `
ANALYSIS REQUIREMENTS:
1. Identify specific performance optimization opportunities
2. Focus on: caching, concurrency, memory optimization, algorithm improvements
3. Provide exact line numbers and code snippets
4. Explain the rationale for each optimization
5. Estimate the performance impact
6. Return response in JSON format with the following structure:
{
"optimizations": [
{
"type": "caching|concurrency|memory|algorithm",
"file_path": "path/to/file.go",
"line_start": 10,
"line_end": 15,
"description": "Brief description",
"rationale": "Detailed explanation",
"original_code": "original code snippet",
"optimized_code": "optimized code snippet",
"estimated_impact": "Low|Medium|High",
"confidence": 0.85
}
],
"summary": "Overall analysis summary",
"confidence": 0.90
}
`
return prompt
}
// buildGenerationPrompt constructs the prompt for code generation
func (c *OpenAIClient) buildGenerationPrompt(userPrompt string, originalCode string) string {
return fmt.Sprintf(`
Generate an optimized version of the following Go code:
OPTIMIZATION REQUEST:
%s
ORIGINAL CODE:
%s
REQUIREMENTS:
1. Maintain the same functionality
2. Improve performance as requested
3. Keep the code readable and maintainable
4. Add comments explaining the optimization
5. Ensure the code compiles and follows Go best practices
Please provide the complete optimized code.
`, userPrompt, originalCode)
}
// getSystemPrompt returns the system prompt for analysis
func (c *OpenAIClient) getSystemPrompt() string {
return `You are an expert Go performance optimization specialist with deep knowledge of:
- Go runtime and garbage collector behavior
- Concurrency patterns and goroutine optimization
- Memory allocation and management
- Algorithm complexity and data structure selection
- Caching strategies and implementation
- Profiling and benchmarking techniques
Analyze Go code for performance improvements with precision and provide actionable optimizations.
Always respond in valid JSON format when requested.`
}
// getCodeGenerationSystemPrompt returns the system prompt for code generation
func (c *OpenAIClient) getCodeGenerationSystemPrompt() string {
return `You are an expert Go developer specializing in performance optimization.
Generate optimized Go code that:
- Maintains correctness and functionality
- Improves performance significantly
- Follows Go best practices and idioms
- Is well-commented and maintainable
- Compiles without errors`
}
// makeAPICall makes an HTTP request to the OpenAI API
func (c *OpenAIClient) makeAPICall(endpoint string, requestBody map[string]interface{}) (map[string]interface{}, error) {
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
req, err := http.NewRequest("POST", c.baseURL+endpoint, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var response map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return response, nil
}
// parseAnalysisResponse parses the LLM analysis response
func (c *OpenAIClient) parseAnalysisResponse(response map[string]interface{}) (*AnalysisResponse, error) {
choices, ok := response["choices"].([]interface{})
if !ok || len(choices) == 0 {
return nil, fmt.Errorf("invalid response format: no choices")
}
choice := choices[0].(map[string]interface{})
message := choice["message"].(map[string]interface{})
content := message["content"].(string)
var analysisResponse AnalysisResponse
if err := json.Unmarshal([]byte(content), &analysisResponse); err != nil {
return nil, fmt.Errorf("failed to parse analysis response: %w", err)
}
return &analysisResponse, nil
}
// parseCodeGenerationResponse parses the LLM code generation response
func (c *OpenAIClient) parseCodeGenerationResponse(response map[string]interface{}) (*CodeGenerationResponse, error) {
choices, ok := response["choices"].([]interface{})
if !ok || len(choices) == 0 {
return nil, fmt.Errorf("invalid response format: no choices")
}
choice := choices[0].(map[string]interface{})
message := choice["message"].(map[string]interface{})
content := message["content"].(string)
return &CodeGenerationResponse{
OptimizedCode: content,
Explanation: "LLM-generated optimization",
Confidence: 0.85,
}, nil
}
// parseExplanationResponse parses the LLM explanation response
func (c *OpenAIClient) parseExplanationResponse(response map[string]interface{}) (string, error) {
choices, ok := response["choices"].([]interface{})
if !ok || len(choices) == 0 {
return "", fmt.Errorf("invalid response format: no choices")
}
choice := choices[0].(map[string]interface{})
message := choice["message"].(map[string]interface{})
content := message["content"].(string)
return content, nil
}
```
```go
// internal/analyzer/llm_analyzer.go
package analyzer
import (
"fmt"
"log"
"github.com/go-performance-optimizer/internal/config"
"github.com/go-performance-optimizer/internal/llm"
)
// LLMOptimization represents an optimization suggested by the LLM
type LLMOptimization struct {
Type string `json:"type"`
FilePath string `json:"file_path"`
LineStart int `json:"line_start"`
LineEnd int `json:"line_end"`
Description string `json:"description"`
Rationale string `json:"rationale"`
OriginalCode string `json:"original_code"`
OptimizedCode string `json:"optimized_code"`
EstimatedImpact string `json:"estimated_impact"`
Confidence float64 `json:"confidence"`
}
// LLMPerformanceAnalyzer analyzes Go code using LLM
type LLMPerformanceAnalyzer interface {
AnalyzeCodebaseWithLLM(context *llm.CodebaseContext) ([]LLMOptimization, error)
AnalyzeFileWithLLM(filePath string, content string) ([]LLMOptimization, error)
}
// OpenAIPerformanceAnalyzer implements LLMPerformanceAnalyzer using OpenAI
type OpenAIPerformanceAnalyzer struct {
llmClient llm.Client
rules config.AnalysisRules
logger *log.Logger
}
// NewLLMPerformanceAnalyzer creates a new LLM-powered performance analyzer
func NewLLMPerformanceAnalyzer(llmClient llm.Client, rules config.AnalysisRules, logger *log.Logger) LLMPerformanceAnalyzer {
return &OpenAIPerformanceAnalyzer{
llmClient: llmClient,
rules: rules,
logger: logger,
}
}
// AnalyzeCodebaseWithLLM analyzes entire codebase using LLM
func (opa *OpenAIPerformanceAnalyzer) AnalyzeCodebaseWithLLM(context *llm.CodebaseContext) ([]LLMOptimization, error) {
opa.logger.Printf("Starting LLM analysis of codebase with %d files", len(context.Files))
// Construct analysis prompt based on enabled rules
prompt := opa.buildAnalysisPrompt()
// Send to LLM for analysis
response, err := opa.llmClient.AnalyzeCode(prompt, context)
if err != nil {
return nil, fmt.Errorf("LLM analysis failed: %w", err)
}
// Convert LLM response to our optimization format
optimizations := make([]LLMOptimization, 0, len(response.Optimizations))
for _, suggestion := range response.Optimizations {
// Filter based on configuration rules
if opa.shouldIncludeOptimization(suggestion.Type) {
optimization := LLMOptimization{
Type: suggestion.Type,
FilePath: suggestion.FilePath,
LineStart: suggestion.LineStart,
LineEnd: suggestion.LineEnd,
Description: suggestion.Description,
Rationale: suggestion.Rationale,
OriginalCode: suggestion.OriginalCode,
OptimizedCode: suggestion.OptimizedCode,
EstimatedImpact: suggestion.EstimatedImpact,
Confidence: suggestion.Confidence,
}
optimizations = append(optimizations, optimization)
}
}
opa.logger.Printf("LLM identified %d optimizations (filtered from %d suggestions)",
len(optimizations), len(response.Optimizations))
return optimizations, nil
}
// AnalyzeFileWithLLM analyzes a single file using LLM
func (opa *OpenAIPerformanceAnalyzer) AnalyzeFileWithLLM(filePath string, content string) ([]LLMOptimization, error) {
opa.logger.Printf("Starting LLM analysis of file: %s", filePath)
// Create single-file context
context := &llm.CodebaseContext{
Files: map[string]string{
filePath: content,
},
Dependencies: []string{},
Metadata: map[string]interface{}{
"single_file_analysis": true,
},
}
return opa.AnalyzeCodebaseWithLLM(context)
}
// buildAnalysisPrompt constructs the analysis prompt based on configuration
func (opa *OpenAIPerformanceAnalyzer) buildAnalysisPrompt() string {
prompt := "Analyze this Go codebase for performance optimization opportunities. Focus on:\n"
if opa.rules.EnableCaching {
prompt += "- CACHING: Identify repeated expensive operations that could benefit from caching\n"
}
if opa.rules.EnableConcurrency {
prompt += fmt.Sprintf("- CONCURRENCY: Find opportunities for parallel execution (max %d goroutines)\n",
opa.rules.MaxConcurrencyLevel)
}
if opa.rules.EnableMemoryOptimization {
prompt += "- MEMORY: Identify inefficient memory allocations and suggest pre-allocation strategies\n"
}
if opa.rules.EnableAlgorithmOptimization {
prompt += "- ALGORITHMS: Suggest better algorithms or data structures for improved performance\n"
}
prompt += "\nPrioritize optimizations with high impact and confidence. "
prompt += "Provide specific code examples and detailed rationales for each suggestion."
return prompt
}
// shouldIncludeOptimization checks if optimization type is enabled in configuration
func (opa *OpenAIPerformanceAnalyzer) shouldIncludeOptimization(optimizationType string) bool {
switch optimizationType {
case "caching":
return opa.rules.EnableCaching
case "concurrency":
return opa.rules.EnableConcurrency
case "memory":
return opa.rules.EnableMemoryOptimization
case "algorithm":
return opa.rules.EnableAlgorithmOptimization
default:
return false
}
}
```
```go
// internal/generator/llm_generator.go
package generator
import (
"fmt"
"log"
"github.com/go-performance-optimizer/internal/analyzer"
"github.com/go-performance-optimizer/internal/llm"
)
// LLMCodeGenerator generates optimized code using LLM
type LLMCodeGenerator interface {
GenerateOptimizedCode(opt analyzer.LLMOptimization) ([]byte, error)
ValidateGeneratedCode(code string) error
}
// OpenAICodeGenerator implements LLMCodeGenerator using OpenAI
type OpenAICodeGenerator struct {
llmClient llm.Client
logger *log.Logger
}
// NewLLMCodeGenerator creates a new LLM-powered code generator
func NewLLMCodeGenerator(llmClient llm.Client, logger *log.Logger) LLMCodeGenerator {
return &OpenAICodeGenerator{
llmClient: llmClient,
logger: logger,
}
}
// GenerateOptimizedCode uses LLM to generate optimized code
func (ocg *OpenAICodeGenerator) GenerateOptimizedCode(opt analyzer.LLMOptimization) ([]byte, error) {
ocg.logger.Printf("Generating optimized code for %s optimization in %s", opt.Type, opt.FilePath)
// If LLM already provided optimized code in analysis, use it
if opt.OptimizedCode != "" {
ocg.logger.Printf("Using pre-generated optimized code from LLM analysis")
// Validate the code before returning
if err := ocg.ValidateGeneratedCode(opt.OptimizedCode); err != nil {
ocg.logger.Printf("Pre-generated code validation failed, requesting new generation: %v", err)
} else {
return []byte(opt.OptimizedCode), nil
}
}
// Request LLM to generate optimized code
prompt := ocg.buildCodeGenerationPrompt(opt)
response, err := ocg.llmClient.GenerateOptimizedCode(prompt, opt.OriginalCode)
if err != nil {
return nil, fmt.Errorf("LLM code generation failed: %w", err)
}
// Validate generated code
if err := ocg.ValidateGeneratedCode(response.OptimizedCode); err != nil {
return nil, fmt.Errorf("generated code validation failed: %w", err)
}
ocg.logger.Printf("Successfully generated and validated optimized code")
return []byte(response.OptimizedCode), nil
}
// ValidateGeneratedCode performs basic validation on LLM-generated code
func (ocg *OpenAICodeGenerator) ValidateGeneratedCode(code string) error {
// Basic validation checks
if code == "" {
return fmt.Errorf("generated code is empty")
}
// Check for basic Go syntax elements
if !ocg.containsGoSyntax(code) {
return fmt.Errorf("generated code does not appear to be valid Go")
}
// Additional validation could include:
// - AST parsing to ensure syntactic correctness
// - Compilation check
// - Security analysis
// - Performance regression detection
return nil
}
// buildCodeGenerationPrompt constructs prompt for code generation
func (ocg *OpenAICodeGenerator) buildCodeGenerationPrompt(opt analyzer.LLMOptimization) string {
prompt := fmt.Sprintf(`
Generate optimized Go code for the following performance improvement:
OPTIMIZATION TYPE: %s
DESCRIPTION: %s
RATIONALE: %s
ESTIMATED IMPACT: %s
ORIGINAL CODE (lines %d-%d):
%s
REQUIREMENTS:
1. Apply the %s optimization as described
2. Maintain exact same functionality and behavior
3. Ensure code compiles without errors
4. Add clear comments explaining the optimization
5. Follow Go best practices and idioms
6. Make the optimization robust and production-ready
Please provide the complete optimized code that can replace the original code.
`,
opt.Type,
opt.Description,
opt.Rationale,
opt.EstimatedImpact,
opt.LineStart,
opt.LineEnd,
opt.OriginalCode,
opt.Type)
return prompt
}
// containsGoSyntax performs basic check for Go syntax
func (ocg *OpenAICodeGenerator) containsGoSyntax(code string) bool {
// Simple heuristic checks for Go code
goKeywords := []string{"func", "var", "const", "type", "package", "import"}
for _, keyword := range goKeywords {
if contains(code, keyword) {
return true
}
}
return false
}
// Helper function to check if string contains substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr ||
(len(s) > len(substr) && (s[:len(substr)] == substr ||
s[len(s)-len(substr):] == substr ||
indexOf(s, substr) >= 0)))
}
func indexOf(s, substr string) int {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}
```
```go
// internal/ui/cli.go - Updated for LLM optimizations
package ui
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/go-performance-optimizer/internal/analyzer"
)
// Interface defines the user interface contract
type Interface interface {
ShowLLMOptimization(current, total int, opt analyzer.LLMOptimization)
AskForApproval() bool
ShowMessage(message string)
ShowError(err error)
}
// CLI implements Interface for command-line interaction with LLM optimizations
type CLI struct {
reader *bufio.Reader
logger *log.Logger
}
// NewCLI creates a new CLI interface
func NewCLI(logger *log.Logger) Interface {
return &CLI{
reader: bufio.NewReader(os.Stdin),
logger: logger,
}
}
// ShowLLMOptimization displays an LLM-generated optimization opportunity
func (cli *CLI) ShowLLMOptimization(current, total int, opt analyzer.LLMOptimization) {
fmt.Printf("\n" + strings.Repeat("=", 80) + "\n")
fmt.Printf("š¤ LLM OPTIMIZATION %d of %d\n", current, total)
fmt.Printf(strings.Repeat("=", 80) + "\n")
fmt.Printf("Type: %s\n", opt.Type)
fmt.Printf("File: %s (lines %d-%d)\n", opt.FilePath, opt.LineStart, opt.LineEnd)
fmt.Printf("Description: %s\n", opt.Description)
fmt.Printf("LLM Rationale: %s\n", opt.Rationale)
fmt.Printf("Estimated Impact: %s\n", opt.EstimatedImpact)
fmt.Printf("LLM Confidence: %.2f\n", opt.Confidence)
if opt.OriginalCode != "" {
fmt.Printf("\nš Original Code:\n")
fmt.Printf("```go\n%s\n```\n", opt.OriginalCode)
}
if opt.OptimizedCode != "" {
fmt.Printf("\n✨ LLM-Optimized Code:\n")
fmt.Printf("```go\n%s\n```\n", opt.OptimizedCode)
}
fmt.Printf(strings.Repeat("-", 80) + "\n")
}
// AskForApproval asks the user whether to apply the LLM optimization
func (cli *CLI) AskForApproval() bool {
for {
fmt.Print("Apply this LLM optimization? (y/n/s=skip all/e=explain): ")
input, err := cli.reader.ReadString('\n')
if err != nil {
cli.logger.Printf("Error reading input: %v", err)
continue
}
input = strings.TrimSpace(strings.ToLower(input))
switch input {
case "y", "yes":
return true
case "n", "no":
return false
case "s", "skip":
fmt.Println("Skipping all remaining LLM optimizations...")
os.Exit(0)
case "e", "explain":
fmt.Println("Requesting detailed explanation from LLM...")
// This would trigger an explanation request to the LLM
return cli.AskForApproval() // Ask again after explanation
default:
fmt.Println("Please enter 'y' for yes, 'n' for no, 's' to skip all, or 'e' for explanation.")
}
}
}
// ShowMessage displays a message to the user
func (cli *CLI) ShowMessage(message string) {
fmt.Printf("ā¹️ %s\n", message)
}
// ShowError displays an error to the user
func (cli *CLI) ShowError(err error) {
fmt.Printf("❌ ERROR: %s\n", err.Error())
}
```!
No comments:
Post a Comment