File size: 11,863 Bytes
29006c5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
"""
Universal CodeAct Interactive Demo
Supports: CUDA (NVIDIA), MLX (Apple Silicon), CPU
Auto-detects best available backend
"""
import re
import sys
import os
import argparse
from io import StringIO
# ============= BACKEND DETECTION =============
def detect_backend():
"""Auto-detect the best available backend"""
# Check for MLX (Apple Silicon)
try:
import mlx.core as mx
return "mlx"
except ImportError:
pass
# Check for CUDA
try:
import torch
if torch.cuda.is_available():
return "cuda"
except ImportError:
pass
# Check for MPS (Apple Metal via PyTorch)
try:
import torch
if torch.backends.mps.is_available():
return "mps"
except:
pass
# Fallback to CPU
return "cpu"
# ============= MLX BACKEND =============
class MLXBackend:
def __init__(self, model_name, adapter_path=None):
from mlx_lm import load, generate
self.generate_fn = generate
if adapter_path and os.path.exists(adapter_path):
print(f"Loading MLX model with adapter: {adapter_path}")
self.model, self.tokenizer = load(model_name, adapter_path=adapter_path)
else:
print(f"Loading MLX model: {model_name}")
self.model, self.tokenizer = load(model_name)
def generate(self, prompt, max_tokens=400):
return self.generate_fn(
self.model,
self.tokenizer,
prompt=prompt,
max_tokens=max_tokens,
verbose=False
)
# ============= PYTORCH BACKEND (CUDA/MPS/CPU) =============
class PyTorchBackend:
def __init__(self, model_name, device="auto", adapter_path=None):
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# Determine device
if device == "auto":
if torch.cuda.is_available():
self.device = "cuda"
elif torch.backends.mps.is_available():
self.device = "mps"
else:
self.device = "cpu"
else:
self.device = device
print(f"Loading PyTorch model on {self.device}: {model_name}")
self.tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True
)
# Load model with appropriate dtype
dtype = torch.float16 if self.device in ["cuda", "mps"] else torch.float32
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=dtype,
device_map=self.device if self.device == "cuda" else None,
trust_remote_code=True,
low_cpu_mem_usage=True
)
if self.device != "cuda":
self.model = self.model.to(self.device)
# Load LoRA adapter if available
if adapter_path and os.path.exists(adapter_path):
try:
from peft import PeftModel
print(f"Loading LoRA adapter: {adapter_path}")
self.model = PeftModel.from_pretrained(self.model, adapter_path)
except ImportError:
print("Warning: peft not installed, skipping adapter")
def generate(self, prompt, max_tokens=400):
import torch
inputs = self.tokenizer(prompt, return_tensors="pt")
inputs = {k: v.to(self.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=max_tokens,
temperature=0.7,
do_sample=True,
top_p=0.95,
pad_token_id=self.tokenizer.pad_token_id or self.tokenizer.eos_token_id
)
response = self.tokenizer.decode(
outputs[0][len(inputs['input_ids'][0]):],
skip_special_tokens=True
)
return response
# ============= CODE EXECUTION =============
def execute_code(code):
"""Execute Python code and capture output"""
stdout_buffer = StringIO()
stderr_buffer = StringIO()
old_stdout, old_stderr = sys.stdout, sys.stderr
try:
sys.stdout = stdout_buffer
sys.stderr = stderr_buffer
namespace = {}
exec(code, namespace)
output = stdout_buffer.getvalue()
errors = stderr_buffer.getvalue()
return {"success": True, "output": output.strip() or None, "error": errors.strip() or None}
except Exception as e:
return {"success": False, "output": None, "error": str(e)}
finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
# ============= MAIN DEMO CLASS =============
class CodeActDemo:
def __init__(self, backend="auto", model_name=None, adapter_path=None):
# Default model
if model_name is None:
model_name = "Qwen/Qwen2.5-3B"
# Default adapter paths
if adapter_path is None:
adapter_path = "./models/codeact-mlx-qwen2.5-3b"
# Auto-detect or use specified backend
if backend == "auto":
backend = detect_backend()
print(f"\n{'='*60}")
print(f"CodeAct Interactive Demo")
print(f"Backend: {backend.upper()}")
print(f"{'='*60}\n")
self.backend_name = backend
# Initialize backend
if backend == "mlx":
self.backend = MLXBackend(model_name, adapter_path)
else:
self.backend = PyTorchBackend(model_name, device=backend, adapter_path=adapter_path)
self.tokenizer = self.backend.tokenizer if hasattr(self.backend, 'tokenizer') else None
self.conversation_history = []
self.system_prompt = """You are a helpful AI assistant that executes Python code.
Use these tags:
- <thought>reasoning</thought> for thinking
- <execute>code</execute> for code
- <solution>answer</solution> for final answer
- <feedback>assessment</feedback> for self-evaluation"""
print("Model loaded successfully!\n")
def parse_response(self, response):
"""Extract tags from response"""
parts = {'thought': None, 'execute': None, 'solution': None, 'feedback': None}
for tag in parts:
match = re.search(f'<{tag}>(.*?)</{tag}>', response, re.DOTALL)
if match:
parts[tag] = match.group(1).strip()
return parts
def build_prompt(self, user_input, execution_result=None):
"""Build prompt with conversation history"""
messages = [{"role": "system", "content": self.system_prompt}]
messages.extend(self.conversation_history)
if execution_result:
content = f"Previous execution result: {execution_result}\n\nUser: {user_input}"
else:
content = user_input
messages.append({"role": "user", "content": content})
# Apply chat template
if hasattr(self.backend, 'tokenizer') and hasattr(self.backend.tokenizer, 'apply_chat_template'):
return self.backend.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
else:
return "\n".join([f"{m['role']}: {m['content']}" for m in messages]) + "\nassistant:"
def chat(self, user_input, execution_result=None):
"""Generate response"""
prompt = self.build_prompt(user_input, execution_result)
return self.backend.generate(prompt, max_tokens=400)
def run(self):
"""Run interactive loop"""
print("="*60)
print(f"Running on: {self.backend_name.upper()}")
print("="*60)
print("\nCommands:")
print(" - Type your question and press Enter")
print(" - 'clear' - Clear conversation history")
print(" - 'quit' - Exit")
print("="*60 + "\n")
last_execution_result = None
while True:
try:
user_input = input("\nYou: ").strip()
if not user_input:
continue
if user_input.lower() in ['quit', 'exit', 'q']:
print("\nGoodbye!")
break
if user_input.lower() == 'clear':
self.conversation_history = []
last_execution_result = None
print("Conversation cleared")
continue
print("\n[Generating...]", end=" ", flush=True)
response = self.chat(user_input, last_execution_result)
print("Done!\n")
parts = self.parse_response(response)
if parts['thought']:
print(f"Thought:\n{parts['thought']}\n")
if parts['execute']:
print(f"Code:\n```python\n{parts['execute']}\n```\n")
print("Executing...\n")
result = execute_code(parts['execute'])
if result["success"]:
if result["output"]:
print(f"Output:\n{result['output']}")
last_execution_result = f"Output: {result['output']}"
print("\n" + "-"*40)
feedback = input("Is this correct? (y/n/skip): ").strip().lower()
if feedback == 'n':
print("\nMarked as incorrect")
last_execution_result += " [INCORRECT]"
elif feedback == 'y':
print("\nCorrect!")
last_execution_result = None
else:
last_execution_result = None
self.conversation_history.append({"role": "user", "content": user_input})
self.conversation_history.append({"role": "assistant", "content": response})
else:
print("Code executed (no output)")
last_execution_result = None
if result["error"]:
print(f"Warnings: {result['error']}")
else:
print(f"Error: {result['error']}")
last_execution_result = f"Error: {result['error']}"
if parts['solution']:
print(f"\nSolution:\n{parts['solution']}")
if parts['feedback']:
print(f"\nFeedback:\n{parts['feedback']}")
if not any(parts.values()):
print(f"Response:\n{response[:500]}")
# Limit history
if len(self.conversation_history) > 10:
self.conversation_history = self.conversation_history[-10:]
print("\n" + "="*60)
except KeyboardInterrupt:
print("\n\nInterrupted. Goodbye!")
break
except Exception as e:
print(f"\nError: {e}")
import traceback
traceback.print_exc()
def main():
parser = argparse.ArgumentParser(description="CodeAct Interactive Demo")
parser.add_argument("--backend", choices=["auto", "cuda", "mps", "mlx", "cpu"],
default="auto", help="Backend to use (default: auto)")
parser.add_argument("--model", type=str, default="Qwen/Qwen2.5-3B",
help="Model name or path")
parser.add_argument("--adapter", type=str, default=None,
help="Path to LoRA adapter")
args = parser.parse_args()
demo = CodeActDemo(
backend=args.backend,
model_name=args.model,
adapter_path=args.adapter
)
demo.run()
if __name__ == "__main__":
main()
|