/ llama_orchestrator.py
llama_orchestrator.py
1 #!/usr/bin/env python3 2 """ 3 TinyLlama Orchestrator 4 - Uses local TinyLlama to understand/break down tasks 5 - Could delegate to cloud LLMs (commented out for now) 6 - Synthesizes responses 7 """ 8 import subprocess 9 import json 10 import sys 11 import os 12 13 class TinyLlamaOrchestrator: 14 def __init__(self): 15 self.llama_path = os.path.expanduser("~/llama.cpp/build/bin/llama-cli") 16 self.model_path = os.path.expanduser("~/models/tinyllama.gguf") 17 18 def ask_tinyllama(self, prompt, max_tokens=100): 19 """Query local TinyLlama""" 20 cmd = [ 21 self.llama_path, 22 "-m", self.model_path, 23 "-p", prompt, 24 "-n", str(max_tokens), 25 "--temp", "0.7", 26 "-no-cnv" 27 ] 28 29 try: 30 result = subprocess.run(cmd, capture_output=True, text=True, timeout=30) 31 32 # Extract response 33 for line in result.stdout.split('\n'): 34 if prompt in line: 35 response = line[line.find(prompt) + len(prompt):].strip() 36 response = response.lstrip(':').strip() 37 if response: 38 return response 39 40 # Fallback 41 return "I need to think about that more." 42 43 except Exception as e: 44 return f"Error: {str(e)}" 45 46 def decompose_task(self, task): 47 """Use TinyLlama to break down complex tasks""" 48 prompt = f"""Analyze this task and break it into logical parts: {task} 49 50 Return as a simple list of subtasks:""" 51 52 decomposition = self.ask_tinyllama(prompt, max_tokens=150) 53 return decomposition 54 55 def process_subtask(self, subtask): 56 """Process a single subtask (could be with cloud LLM)""" 57 # For now, use TinyLlama 58 prompt = f"""Provide detailed information about: {subtask} 59 60 Be thorough and educational:""" 61 62 return self.ask_tinyllama(prompt, max_tokens=200) 63 64 def synthesize_results(self, task, subtask_results): 65 """Combine all results into coherent answer""" 66 prompt = f"""Combine these insights into a comprehensive answer for: {task} 67 68 Insights: 69 {subtask_results} 70 71 Final comprehensive answer:""" 72 73 return self.ask_tinyllama(prompt, max_tokens=250) 74 75 def orchestrate(self, complex_task): 76 """Main orchestration flow""" 77 print(f"\n{'='*60}") 78 print(f"Task: {complex_task}") 79 print(f"{'='*60}") 80 81 # Step 1: Decompose 82 print("\nš Step 1: Analyzing task structure...") 83 decomposition = self.decompose_task(complex_task) 84 print(f" Decomposition: {decomposition[:100]}...") 85 86 # Step 2: Process (in a real system, these would go to different LLMs) 87 print("\nš§ Step 2: Processing components...") 88 89 # For simplicity, just process the whole task 90 detailed_response = self.process_subtask(complex_task) 91 print(f" Detailed analysis generated") 92 93 # Step 3: Synthesize 94 print("\nš§© Step 3: Synthesizing final answer...") 95 final_answer = self.synthesize_results(complex_task, detailed_response) 96 97 return final_answer 98 99 # Cloud LLM integration (conceptual - would need API keys) 100 class CloudLLMDelegate: 101 """Conceptual class for delegating to cloud LLMs""" 102 103 @staticmethod 104 def delegate_to_openai(prompt): 105 """Example: Delegate to OpenAI""" 106 # Would need: openai.api_key = "your-key" 107 # response = openai.ChatCompletion.create(...) 108 # return response.choices[0].message.content 109 return f"[Would delegate to cloud LLM: {prompt[:50]}...]" 110 111 @staticmethod 112 def delegate_to_anthropic(prompt): 113 """Example: Delegate to Anthropic Claude""" 114 return f"[Would delegate to Claude: {prompt[:50]}...]" 115 116 def main(): 117 orchestrator = TinyLlamaOrchestrator() 118 119 print("š¤ TinyLlama Orchestrator System") 120 print("=" * 60) 121 print("This system demonstrates how a small local LLM can orchestrate") 122 print("complex tasks that could be delegated to larger cloud models.") 123 print("=" * 60) 124 125 if len(sys.argv) > 1: 126 task = " ".join(sys.argv[1:]) 127 else: 128 print("\nEnter a complex topic or task:") 129 task = input("> ").strip() 130 131 if not task: 132 task = "Explain quantum computing and its applications" 133 134 result = orchestrator.orchestrate(task) 135 136 print(f"\n{'='*60}") 137 print("ā FINAL RESULT:") 138 print(f"{'='*60}") 139 print(result) 140 print(f"{'='*60}") 141 142 # Save to file 143 import time 144 timestamp = time.strftime("%Y%m%d_%H%M%S") 145 filename = f"orchestration_{timestamp}.txt" 146 147 with open(filename, 'w') as f: 148 f.write(f"Task: {task}\n\n") 149 f.write(f"Result:\n{result}\n") 150 151 print(f"\nš Saved to: {filename}") 152 153 if __name__ == "__main__": 154 main()