Overview
TheLLMClient provides a unified interface for LLM operations with backward compatibility and centralized service architecture. Supports structured output, persona-based generation, and comprehensive error handling.
Module: llm_v2.py
Architecture:
Initialization
from_hydra_config()
Create client from Hydra configuration. Signature:cfg: Hydra config object withllmsectionuse_centralized_service: Use new centralized service (recommended)
Direct Initialization
Signature:Core Methods
populate_entity()
Populate entity with structured LLM output. Signature:entity_schema: Entity schema withentity_idcontext: Context information for generationprevious_knowledge: Previous knowledge state (for evolution)model: Model override
EntityPopulation with:
entity_id: Entity identifierknowledge_state: List of knowledge items (3-8 items)energy_budget: Cognitive resources (0-100)personality_traits: 5 floats between -1 and 1temporal_awareness: Time perception descriptionconfidence: Generation confidence (0-1)
- Attempts with multiple models (70B → 405B → Qwen 72B)
- Temperature adjustment (0.5 → 0.3 → 0.8)
- Fallback defaults if LLM fails
score_relevance()
Score how relevant a knowledge item is to a query. Signature:1.0: Perfectly relevant, directly answers query0.5: Somewhat relevant but not central0.0: Completely irrelevant
generate_structured()
Generate structured output using Pydantic schema. Signature:prompt: Generation promptresponse_model: Pydantic model class for outputmodel: Model override**kwargs: Additional parameters (temperature, max_tokens, etc.)
validate_consistency()
Validate temporal consistency of entities. Signature:ValidationResult with:
is_valid: Boolean (true if no issues)violations: List of problems foundconfidence: Validation confidence (0-1)reasoning: Explanation of result
- Anachronisms (knowledge before event)
- Biological impossibilities (age, health)
- Knowledge contradictions
Dialog Generation
generate_dialog()
Generate structured dialog with turns and metadata. Signature:DialogData with:
turns: List ofDialogTurnobjectstotal_duration: Estimated duration (seconds)information_exchanged: Knowledge items passedrelationship_impacts: How relationships changedatmosphere_evolution: Atmosphere changes over time
generate_single_turn()
Generate one dialog turn with persona-derived parameters. Signature:content: Dialog textemotional_tone: Emotional stateknowledge_references: Referenced knowledge items
Advanced Methods
generate_expectations()
Generate expectations for prospection mechanism (M15). Signature:enrich_animistic_entity()
Enrich animistic entity with LLM-generated background (M16). Signature:animal: Species, biological state, trainingbuilding: Structure, capacity, age, conditionabstract: Concepts, propagation, carriers
generate_scene_atmosphere()
Generate rich narrative description of scene atmosphere. Signature:atmospheric_narrative: Vivid 2-3 paragraph descriptiondominant_mood: Overall emotional tonesensory_details: Key sensory observationssocial_dynamics: Interaction patternshistorical_context: Period-appropriate cultural notes
predict_counterfactual_outcome()
Predict outcomes of counterfactual interventions (M12). Signature:immediate_effects: Direct consequencesripple_effects: Cascading changesentity_state_changes: Predicted entity changesdivergence_significance: Timeline difference magnitude (0-1)timeline_narrative: Divergent timeline descriptionprobability_assessment: Prediction confidence (0-1)key_turning_points: Critical moments of change
Statistics and Monitoring
Token Tracking:Model Management
Default Model:meta-llama/llama-3.1-70b-instruct(70B parameters)- 327K context window (Llama 4 Scout)
meta-llama/llama-3.1-405b-instruct(405B, 100K output)qwen/qwen-2.5-72b-instruct(72B, alternative provider)
Error Handling
Automatic Retry:- Multiple model attempts
- Temperature adjustment
- Exponential backoff
- Empty knowledge_state → Generate defaults
- JSON parse failure → Heuristic scoring
- API timeout → Return safe defaults
Configuration
Hydra Config (config.yaml):Best Practices
- Use centralized service (default, better monitoring)
- Provide context for better entity population
- Set max_tokens appropriately for task
- Use structured_call for Pydantic schemas
- Monitor token usage for cost control
- Handle errors gracefully with fallbacks
- Check confidence scores before using results
- Log all calls for debugging and analysis
Related
- Orchestrator - Scene compilation
- Storage - Entity persistence
- Workflows - Entity training
- Schemas - Data models

