|
1 | 1 | """Balatrollm project.""" |
2 | 2 |
|
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from .llm import LLMBot |
| 10 | + |
3 | 11 |
|
4 | 12 | def main() -> None: |
5 | | - print("Hello from balatrollm!") |
| 13 | + """Main CLI entry point for balatrollm.""" |
| 14 | + parser = argparse.ArgumentParser( |
| 15 | + description="LLM-powered Balatro bot using LiteLLM proxy", |
| 16 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 17 | + epilog=""" |
| 18 | +Examples: |
| 19 | + balatrollm --model cerebras-gpt-oss-120b |
| 20 | + balatrollm --model groq-qwen32b --proxy-url http://localhost:4000 |
| 21 | + balatrollm --list-models |
| 22 | + """, |
| 23 | + ) |
| 24 | + |
| 25 | + parser.add_argument( |
| 26 | + "--model", |
| 27 | + default=os.getenv("LITELLM_MODEL", "cerebras-gpt-oss-120b"), |
| 28 | + help="Model name to use from LiteLLM proxy (default: cerebras-gpt-oss-120b)", |
| 29 | + ) |
| 30 | + |
| 31 | + parser.add_argument( |
| 32 | + "--proxy-url", |
| 33 | + default=os.getenv("LITELLM_PROXY_URL", "http://localhost:4000"), |
| 34 | + help="LiteLLM proxy URL (default: http://localhost:4000)", |
| 35 | + ) |
| 36 | + |
| 37 | + parser.add_argument( |
| 38 | + "--api-key", |
| 39 | + default=os.getenv("LITELLM_API_KEY", "sk-balatrollm-proxy-key"), |
| 40 | + help="LiteLLM proxy API key (default: sk-balatrollm-proxy-key)", |
| 41 | + ) |
| 42 | + |
| 43 | + parser.add_argument( |
| 44 | + "--list-models", |
| 45 | + action="store_true", |
| 46 | + help="List available models from the proxy and exit", |
| 47 | + ) |
| 48 | + |
| 49 | + parser.add_argument( |
| 50 | + "--config", |
| 51 | + default="config/litellm.yaml", |
| 52 | + help="Path to LiteLLM configuration file (default: config/litellm.yaml)", |
| 53 | + ) |
| 54 | + |
| 55 | + parser.add_argument( |
| 56 | + "--verbose", "-v", action="store_true", help="Enable verbose logging" |
| 57 | + ) |
| 58 | + |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + # Configure logging |
| 62 | + import logging |
| 63 | + |
| 64 | + level = logging.DEBUG if args.verbose else logging.INFO |
| 65 | + logging.basicConfig( |
| 66 | + level=level, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" |
| 67 | + ) |
| 68 | + logger = logging.getLogger(__name__) |
| 69 | + |
| 70 | + # Check if config file exists |
| 71 | + config_path = Path(args.config) |
| 72 | + if not config_path.exists(): |
| 73 | + logger.error(f"LiteLLM config file not found: {config_path}") |
| 74 | + logger.error(f"Please create the config file or start the proxy manually:") |
| 75 | + logger.error(f" litellm --config {config_path}") |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + # Create and run the bot |
| 79 | + asyncio.run(run_bot(args)) |
| 80 | + |
| 81 | + |
| 82 | +async def run_bot(args) -> None: |
| 83 | + """Run the Balatro bot with the given arguments.""" |
| 84 | + bot = LLMBot(model=args.model, proxy_url=args.proxy_url, api_key=args.api_key) |
| 85 | + |
| 86 | + # List models if requested |
| 87 | + if args.list_models: |
| 88 | + print("Checking available models from LiteLLM proxy...") |
| 89 | + if not await bot.validate_proxy_connection(): |
| 90 | + print(f"❌ Cannot connect to LiteLLM proxy at {args.proxy_url}") |
| 91 | + print(f"Please start the proxy with: litellm --config {args.config}") |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + models = await bot.list_available_models() |
| 95 | + if models: |
| 96 | + print("✅ Available models:") |
| 97 | + for model in models: |
| 98 | + print(f" - {model}") |
| 99 | + else: |
| 100 | + print("❌ No models available or failed to retrieve models") |
| 101 | + return |
| 102 | + |
| 103 | + # Validate proxy connection and model before starting game |
| 104 | + print(f"🤖 Starting Balatro LLM Bot with model: {args.model}") |
| 105 | + |
| 106 | + if not await bot.validate_proxy_connection(): |
| 107 | + print(f"❌ Cannot connect to LiteLLM proxy at {args.proxy_url}") |
| 108 | + print(f"Please start the proxy with: litellm --config {args.config}") |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + if not await bot.validate_model_exists(): |
| 112 | + print(f"❌ Model '{args.model}' not available") |
| 113 | + print("Use --list-models to see available models") |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + print("✅ Proxy connection validated, starting game...") |
| 117 | + |
| 118 | + try: |
| 119 | + with bot: |
| 120 | + await bot.play_game() |
| 121 | + except KeyboardInterrupt: |
| 122 | + print("\n🛑 Game interrupted by user") |
| 123 | + except Exception as e: |
| 124 | + print(f"❌ Game failed: {e}") |
| 125 | + sys.exit(1) |
0 commit comments