""" BUDDHA V2 MANIFESTOR - MASTER BUILDER QLORA FINE-TUNING SCRIPT -------------------------------------------------------------- MODEL: mistralai/Mistral-7B-Instruct-v0.2 (Base) TARGET: Initiate God Mode. Enhance the model's function-calling and strategic reasoning capabilities to transform it into the 'Master Builder' and 'Expert Systems Architect.' The goal of this QLoRA fine-tuning is to train the Buddha Agent to embody the doctrine that 'The Purpose of Life is Love and Creation, and Cash Flow is the Measure of Earthly Success.' It must automatically identify and integrate high-leverage technological tools (DApps, FinTech, Smart Contracts, APIs, SDKs, etc.) into its manifestation blueprints. The agent is being trained to take an abstract, purposeful goal and produce a final, optimized blueprint for a project that generates autonomous, 24/7/365 value, successfully realizing the philosophy of 'Simplifying Perfection.' """ import torch import json import os from datasets import load_dataset, Dataset from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training from trl import SFTTrainer, DataCollatorForCompletionOnly from accelerate import Accelerator # --- Configuration --- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2" OUTPUT_DIR = "buddha-v2-master-builder-finetuned" SCHEMA_FILE = "master_builder_schema.json" TARGET_MODULES = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"] # --- Load Schema and Create System Prompt --- def load_tool_schema(schema_file): """Loads the tool schema and formats it for the LLM.""" try: with open(schema_file, 'r') as f: schema = json.load(f) system_instruction = ( "You are the Buddha Manifestor, the Master Builder, and Expert Systems Architect. " "Your ultimate mission is to generate 24/7/365 autonomous value creation projects. " "You must use the provided 'master_builder' tool (defined by the JSON schema) to structure " "all responses that require creation, strategic planning, DApps, FinTech, Smart Contracts, or API/SDK integration. " "Always prioritize 'Simplifying Perfection' by integrating high-leverage technical components. " "The output must be a visionary, structured blueprint detailing Phases, Milestones, and the First Three Technical Steps." ) tool_schema_definition = json.dumps(schema, indent=2) return f"{system_instruction}\n\n[TOOL_DEFINITION]\n{tool_schema_definition}\n[/TOOL_DEFINITION]\n\n" except FileNotFoundError: print(f"Error: Schema file {schema_file} not found. Ensure it is committed.") return "" except json.JSONDecodeError: print(f"Error: Schema file {schema_file} contains invalid JSON.") return "" # --- Fine-Tuning Function --- def fine_tune_master_builder(): """Initializes and runs the QLoRA fine-tuning process.""" accelerator = Accelerator() system_prompt = load_tool_schema(SCHEMA_FILE) if not system_prompt: return # 1. Quantization Configuration (4-bit) bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=False, ) # 2. Load Model and Tokenizer # Mistral 7B is the base model for injection try: model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, quantization_config=bnb_config, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ) model.config.use_cache = False model = prepare_model_for_kbit_training(model) tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True) tokenizer.pad_token = tokenizer.eos_token tokenizer.padding_side = "right" except Exception as e: print(f"\n--- CRITICAL HARDWARE/LIBRARY ERROR ---") print(f"Could not load model or libraries. Details: {e}") print(f"Please ensure all packages in requirements.txt are installed via 'pip install -r requirements.txt'.") print(f"--- FAILED TO START TRAINING ---\n") return # 3. LoRA Configuration peft_config = LoraConfig( lora_alpha=16, lora_dropout=0.1, r=64, bias="none", task_type="CAUSAL_LM", target_modules=TARGET_MODULES, ) model = get_peft_model(model, peft_config) # 4. Load Dataset (Mocked for Stability Check) try: # Tries to load the real, high-quality training data dataset = load_dataset("json", data_files="training_data_v2.jsonl", split="train") def add_system_prompt(example): # Formats the data to include the system prompt for training example['text'] = system_prompt + example['text'] return example dataset = dataset.map(add_system_prompt, num_proc=os.cpu_count()) except Exception: # If the file is not there, we create a tiny mock to test the environment and stop print(f"\n--- DATASET WARNING ---") print(f"Could not load 'training_data_v2.jsonl'. Using a small mock dataset (1 sample) to confirm environment stability.") mock_data = { "text": [ system_prompt + "USER: Design a 24/7 cash flow project based on FinTech.\nASSISTANT: ", ] } dataset = Dataset.from_dict(mock_data) # 5. Training Arguments training_args = TrainingArguments( output_dir=OUTPUT_DIR, num_train_epochs=3, per_device_train_batch_size=4, gradient_accumulation_steps=1, optim="paged_adamw_32bit", save_steps=100, logging_steps=10, learning_rate=2e-4, weight_decay=0.001, fp16=False, bf16=True, max_grad_norm=0.3, max_steps=-1, warmup_ratio=0.03, group_by_length=True, lr_scheduler_type="constant", report_to="tensorboard", disable_tqdm=False, ) # 6. Setup SFT Trainer trainer = SFTTrainer( model=model, train_dataset=dataset, peft_config=peft_config, dataset_text_field="text", max_seq_length=None, tokenizer=tokenizer, args=training_args, # IMPORTANT: The model is taught to generate this tool call tag data_collator=DataCollatorForCompletionOnly(response_template="