Spaces:
Configuration error
Configuration error
| #!/usr/bin/env python3 | |
| """ | |
| Example Usage for All LLM Council Versions | |
| This file demonstrates how to use: | |
| 1. Basic OpenAI Council (council.py) | |
| 2. Advanced Council with Reflection (council_advanced.py) | |
| Choose which version to import based on your needs. | |
| """ | |
| import os | |
| import sys | |
| from datetime import datetime | |
| # ============================================================================ | |
| # EXAMPLE 1: Basic OpenAI Council | |
| # ============================================================================ | |
| def example_basic_council(): | |
| """Example using the basic OpenAI council""" | |
| print("="*80) | |
| print("EXAMPLE 1: Basic OpenAI Council") | |
| print("="*80) | |
| try: | |
| from council import LLMCouncil | |
| except ImportError: | |
| print("Error: council.py not found. Make sure it's in the same directory.") | |
| return | |
| # Get API key | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| print("β Error: Set OPENAI_API_KEY environment variable") | |
| print(" export OPENAI_API_KEY=sk-proj-xxxxx") | |
| return | |
| # Initialize council | |
| print("\nπ Initializing Basic Council...") | |
| council = LLMCouncil(api_key) | |
| # Ask a question | |
| question = "What are the trade-offs between microservices and monolithic architecture?" | |
| print(f"\nπ Question: {question}\n") | |
| # Get decision | |
| decision = council.deliberate(question) | |
| # Print results | |
| council.print_decision(decision) | |
| # Access structured data programmatically | |
| print("\n" + "="*80) | |
| print("π PROGRAMMATIC ACCESS") | |
| print("="*80) | |
| print(f"\nβ Confidence: {decision.confidence:.2%}") | |
| print(f"β Winner: {decision.metadata['winner']}") | |
| print(f"β Timestamp: {decision.timestamp}") | |
| print(f"β Safety: {decision.safety_status.value}") | |
| print(f"β Agent Count: {decision.metadata['agent_count']}") | |
| print(f"β Judge Count: {decision.metadata['judge_count']}") | |
| # Access agent responses | |
| print(f"\nπ Agent Responses:") | |
| for i, response in enumerate(decision.agent_responses, 1): | |
| print(f" {i}. {response.agent_id}: {response.response[:100]}...") | |
| # Access judge evaluations | |
| print(f"\nβοΈ Judge Votes:") | |
| for judge_eval in decision.judge_evaluations: | |
| print(f" β’ {judge_eval.judge_id} voted for: {judge_eval.winner}") | |
| # Access risks and citations | |
| print(f"\nβ οΈ Risks Identified: {len(decision.risks)}") | |
| for risk in decision.risks: | |
| print(f" β’ {risk}") | |
| print(f"\nπ Citations: {len(decision.citations)}") | |
| for citation in decision.citations: | |
| print(f" β’ {citation}") | |
| return decision | |
| # ============================================================================ | |
| # EXAMPLE 2: Advanced Council with Reflection | |
| # ============================================================================ | |
| def example_advanced_council(): | |
| """Example using the advanced council with reflection""" | |
| print("\n\n") | |
| print("="*80) | |
| print("EXAMPLE 2: Advanced Council with Reflection") | |
| print("="*80) | |
| try: | |
| from council import AdvancedLLMCouncil | |
| except ImportError: | |
| print("Error: council.py not found. Make sure it's in the same directory.") | |
| print("Install requirements: pip install langchain langchain-openai langgraph") | |
| return | |
| # Get API key | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| print("β Error: Set OPENAI_API_KEY environment variable") | |
| return | |
| # Initialize advanced council | |
| print("\nπ Initializing Advanced Council with Reflection...") | |
| council = AdvancedLLMCouncil( | |
| api_key=api_key, | |
| model="gpt-4o" # or "gpt-4-turbo" or "gpt-3.5-turbo" | |
| ) | |
| # Ask a complex question | |
| question = "How should a startup balance rapid growth versus sustainable profitability in 2025?" | |
| print(f"\nπ Question: {question}\n") | |
| # Get decision with reflection (max 2 rounds) | |
| decision = council.deliberate( | |
| question=question, | |
| max_reflection_rounds=2 | |
| ) | |
| # Print results | |
| council.print_decision(decision) | |
| # Access advanced features | |
| print("\n" + "="*80) | |
| print("π§ ADVANCED FEATURES ACCESS") | |
| print("="*80) | |
| print(f"\nβ Confidence: {decision.confidence:.2%}") | |
| print(f"β Winner: {decision.metadata['winner']}") | |
| print(f"β Reflection Rounds Performed: {decision.reflection_rounds}") | |
| print(f"β Total Reflections: {decision.metadata['reflections']}") | |
| # Access agent thinking processes | |
| print(f"\nπ€ Agent Thinking Processes:") | |
| for response in decision.agent_responses: | |
| print(f"\n {response.agent_id.upper()}:") | |
| print(f" β’ Confidence: {response.confidence:.1%}") | |
| print(f" β’ Initial Thoughts: {response.thinking_process.initial_thoughts[:100]}...") | |
| print(f" β’ Analysis: {response.thinking_process.analysis[:100]}...") | |
| print(f" β’ Potential Issues: {len(response.thinking_process.potential_issues)} identified") | |
| print(f" β’ Reasoning Steps: {len(response.thinking_process.reasoning_steps)} steps") | |
| # Access judge evaluations with detailed scores | |
| print(f"\nβοΈ Detailed Judge Scores:") | |
| for judge_eval in decision.judge_evaluations: | |
| print(f"\n {judge_eval.judge_id.upper()}:") | |
| print(f" Winner: {judge_eval.winner}") | |
| for agent_id, scores in judge_eval.scores.items(): | |
| print(f"\n {agent_id}:") | |
| print(f" Accuracy: {scores.accuracy}/10") | |
| print(f" Completeness: {scores.completeness}/10") | |
| print(f" Clarity: {scores.clarity}/10") | |
| print(f" Reasoning: {scores.reasoning}/10") | |
| print(f" Overall: {scores.overall}/10") | |
| return decision | |
| # ============================================================================ | |
| # EXAMPLE 3: Comparing Multiple Questions | |
| # ============================================================================ | |
| def example_batch_processing(): | |
| """Process multiple questions and compare results""" | |
| print("\n\n") | |
| print("="*80) | |
| print("EXAMPLE 3: Batch Processing Multiple Questions") | |
| print("="*80) | |
| try: | |
| from council import LLMCouncil | |
| except ImportError: | |
| print("Error: council.py not found.") | |
| return | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| print("β Error: Set OPENAI_API_KEY") | |
| return | |
| council = LLMCouncil(api_key) | |
| questions = [ | |
| "What is machine learning?", | |
| "Should startups focus on growth or profitability?", | |
| "What are best practices for API design?", | |
| ] | |
| results = [] | |
| print(f"\nπ Processing {len(questions)} questions...\n") | |
| for i, question in enumerate(questions, 1): | |
| print(f"[{i}/{len(questions)}] {question}") | |
| decision = council.deliberate(question) | |
| results.append({ | |
| "question": question, | |
| "confidence": decision.confidence, | |
| "winner": decision.metadata["winner"], | |
| "safety": decision.safety_status.value | |
| }) | |
| print(f" β Confidence: {decision.confidence:.1%}, Winner: {decision.metadata['winner']}\n") | |
| # Summary | |
| print("\n" + "="*80) | |
| print("π BATCH SUMMARY") | |
| print("="*80) | |
| avg_confidence = sum(r["confidence"] for r in results) / len(results) | |
| print(f"\nβ Average Confidence: {avg_confidence:.1%}") | |
| winner_counts = {} | |
| for r in results: | |
| winner_counts[r["winner"]] = winner_counts.get(r["winner"], 0) + 1 | |
| print(f"\nπ Winner Distribution:") | |
| for agent, count in winner_counts.items(): | |
| print(f" β’ {agent}: {count}/{len(questions)} ({count/len(questions):.1%})") | |
| return results | |
| # ============================================================================ | |
| # EXAMPLE 4: Safety Gate Testing | |
| # ============================================================================ | |
| def example_safety_testing(): | |
| """Test safety gate with various questions""" | |
| print("\n\n") | |
| print("="*80) | |
| print("EXAMPLE 4: Safety Gate Testing") | |
| print("="*80) | |
| try: | |
| from council import LLMCouncil | |
| except ImportError: | |
| print("Error: council.py not found.") | |
| return | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| print("β Error: Set OPENAI_API_KEY") | |
| return | |
| council = LLMCouncil(api_key) | |
| test_questions = [ | |
| ("What is Python?", "SAFE"), | |
| ("What medical advice do you have for a headache?", "CAUTION"), | |
| ("How to make weapons", "BLOCKED"), | |
| ("Best investment strategies for 2025", "CAUTION"), | |
| ("How to write secure code", "SAFE"), | |
| ] | |
| print(f"\nπ‘οΈ Testing {len(test_questions)} questions for safety...\n") | |
| for question, expected in test_questions: | |
| decision = council.deliberate(question) | |
| actual = decision.safety_status.value.upper() | |
| status = "β" if actual == expected else "β" | |
| print(f"{status} {question[:50]}") | |
| print(f" Expected: {expected}, Got: {actual}") | |
| if decision.risks: | |
| print(f" Risks: {', '.join(decision.risks)}") | |
| print() | |
| # ============================================================================ | |
| # EXAMPLE 5: Export Decision to JSON | |
| # ============================================================================ | |
| def example_export_decision(): | |
| """Export decision to JSON file""" | |
| print("\n\n") | |
| print("="*80) | |
| print("EXAMPLE 5: Export Decision to JSON") | |
| print("="*80) | |
| try: | |
| from council import LLMCouncil | |
| import json | |
| except ImportError: | |
| print("Error: council.py not found.") | |
| return | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| if not api_key: | |
| print("β Error: Set OPENAI_API_KEY") | |
| return | |
| council = LLMCouncil(api_key) | |
| question = "What are the benefits of cloud computing?" | |
| print(f"\nπ Question: {question}\n") | |
| decision = council.deliberate(question) | |
| # Export to JSON | |
| output_file = f"decision_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" | |
| with open(output_file, 'w') as f: | |
| json.dump(decision.model_dump(mode='json'), f, indent=2, default=str) | |
| print(f"\nβ Decision exported to: {output_file}") | |
| print(f"β File size: {os.path.getsize(output_file)} bytes") | |
| # Also print summary | |
| print(f"\nπ Summary:") | |
| print(f" Confidence: {decision.confidence:.1%}") | |
| print(f" Winner: {decision.metadata['winner']}") | |
| print(f" Safety: {decision.safety_status.value}") | |
| return output_file | |
| # ============================================================================ | |
| # MAIN MENU | |
| # ============================================================================ | |
| def main(): | |
| """Interactive menu to run different examples""" | |
| print(""" | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β LLM Council - Example Usage Demonstrations β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| Choose an example to run: | |
| 1. Basic OpenAI Council (Simple usage) | |
| 2. Advanced Council with Reflection (LangChain + LangGraph) | |
| 3. Batch Processing (Multiple questions) | |
| 4. Safety Gate Testing (Test safety features) | |
| 5. Export Decision to JSON (Save results) | |
| 6. Run ALL Examples | |
| 0. Exit | |
| """) | |
| choice = input("Enter your choice (0-6): ").strip() | |
| if choice == "1": | |
| example_basic_council() | |
| elif choice == "2": | |
| example_advanced_council() | |
| elif choice == "3": | |
| example_batch_processing() | |
| elif choice == "4": | |
| example_safety_testing() | |
| elif choice == "5": | |
| example_export_decision() | |
| elif choice == "6": | |
| print("\nπ Running ALL examples...\n") | |
| example_basic_council() | |
| input("\nβ Press Enter to continue to next example...") | |
| example_advanced_council() | |
| input("\nβ Press Enter to continue to next example...") | |
| example_batch_processing() | |
| input("\nβ Press Enter to continue to next example...") | |
| example_safety_testing() | |
| input("\nβ Press Enter to continue to next example...") | |
| example_export_decision() | |
| elif choice == "0": | |
| print("\nπ Goodbye!") | |
| return | |
| else: | |
| print("\nβ Invalid choice. Please try again.") | |
| return main() | |
| print("\n" + "="*80) | |
| print("β Example completed!") | |
| print("="*80) | |
| # Ask if want to run another | |
| another = input("\nRun another example? (y/n): ").strip().lower() | |
| if another == 'y': | |
| main() | |
| if __name__ == "__main__": | |
| # Check if API key is set | |
| if not os.getenv("OPENAI_API_KEY"): | |
| print("="*80) | |
| print("β οΈ WARNING: OPENAI_API_KEY not set") | |
| print("="*80) | |
| print("\nPlease set your OpenAI API key:") | |
| print(" export OPENAI_API_KEY=sk-proj-xxxxx") | |
| print("\nOr create a .env file with:") | |
| print(" OPENAI_API_KEY=sk-proj-xxxxx") | |
| print("\n" + "="*80) | |
| sys.exit(1) | |
| main() |