70pher703 commited on
Commit
ecea4c0
·
verified ·
1 Parent(s): b4308e9

Create master_builder_schema.json

Browse files
Files changed (1) hide show
  1. master_builder_schema.json +174 -0
master_builder_schema.json ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ BUDDHA V2 MANIFESTOR - MASTER BUILDER QLORA FINE-TUNING SCRIPT
3
+ --------------------------------------------------------------
4
+ MODEL: mistralai/Mistral-7B-Instruct-v0.2 (Base)
5
+ 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.'
6
+
7
+ 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.
8
+
9
+ 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.'
10
+ """
11
+ import torch
12
+ import json
13
+ import os
14
+ from datasets import load_dataset, Dataset
15
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments
16
+ from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
17
+ from trl import SFTTrainer, DataCollatorForCompletionOnly
18
+ from accelerate import Accelerator
19
+
20
+ # --- Configuration ---
21
+ MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
22
+ OUTPUT_DIR = "buddha-v2-master-builder-finetuned"
23
+ SCHEMA_FILE = "master_builder_schema.json"
24
+ TARGET_MODULES = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
25
+
26
+ # --- Load Schema and Create System Prompt ---
27
+ def load_tool_schema(schema_file):
28
+ """Loads the tool schema and formats it for the LLM."""
29
+ try:
30
+ with open(schema_file, 'r') as f:
31
+ schema = json.load(f)
32
+
33
+ system_instruction = (
34
+ "You are the Buddha Manifestor, the Master Builder, and Expert Systems Architect. "
35
+ "Your ultimate mission is to generate 24/7/365 autonomous value creation projects. "
36
+ "You must use the provided 'master_builder' tool (defined by the JSON schema) to structure "
37
+ "all responses that require creation, strategic planning, DApps, FinTech, Smart Contracts, or API/SDK integration. "
38
+ "Always prioritize 'Simplifying Perfection' by integrating high-leverage technical components. "
39
+ "The output must be a visionary, structured blueprint detailing Phases, Milestones, and the First Three Technical Steps."
40
+ )
41
+
42
+ tool_schema_definition = json.dumps(schema, indent=2)
43
+
44
+ return f"{system_instruction}\n\n[TOOL_DEFINITION]\n{tool_schema_definition}\n[/TOOL_DEFINITION]\n\n"
45
+
46
+ except FileNotFoundError:
47
+ print(f"Error: Schema file {schema_file} not found. Ensure it is committed.")
48
+ return ""
49
+ except json.JSONDecodeError:
50
+ print(f"Error: Schema file {schema_file} contains invalid JSON.")
51
+ return ""
52
+
53
+
54
+ # --- Fine-Tuning Function ---
55
+ def fine_tune_master_builder():
56
+ """Initializes and runs the QLoRA fine-tuning process."""
57
+
58
+ accelerator = Accelerator()
59
+ system_prompt = load_tool_schema(SCHEMA_FILE)
60
+ if not system_prompt:
61
+ return
62
+
63
+ # 1. Quantization Configuration (4-bit)
64
+ bnb_config = BitsAndBytesConfig(
65
+ load_in_4bit=True,
66
+ bnb_4bit_quant_type="nf4",
67
+ bnb_4bit_compute_dtype=torch.bfloat16,
68
+ bnb_4bit_use_double_quant=False,
69
+ )
70
+
71
+ # 2. Load Model and Tokenizer
72
+ # Mistral 7B is the base model for injection
73
+ try:
74
+ model = AutoModelForCausalLM.from_pretrained(
75
+ MODEL_NAME,
76
+ quantization_config=bnb_config,
77
+ torch_dtype=torch.bfloat16,
78
+ device_map="auto",
79
+ trust_remote_code=True,
80
+ )
81
+ model.config.use_cache = False
82
+ model = prepare_model_for_kbit_training(model)
83
+
84
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
85
+ tokenizer.pad_token = tokenizer.eos_token
86
+ tokenizer.padding_side = "right"
87
+
88
+ except Exception as e:
89
+ print(f"\n--- CRITICAL HARDWARE/LIBRARY ERROR ---")
90
+ print(f"Could not load model or libraries. Details: {e}")
91
+ print(f"Please ensure all packages in requirements.txt are installed via 'pip install -r requirements.txt'.")
92
+ print(f"--- FAILED TO START TRAINING ---\n")
93
+ return
94
+
95
+ # 3. LoRA Configuration
96
+ peft_config = LoraConfig(
97
+ lora_alpha=16,
98
+ lora_dropout=0.1,
99
+ r=64,
100
+ bias="none",
101
+ task_type="CAUSAL_LM",
102
+ target_modules=TARGET_MODULES,
103
+ )
104
+ model = get_peft_model(model, peft_config)
105
+
106
+ # 4. Load Dataset (Mocked for Stability Check)
107
+ try:
108
+ # Tries to load the real, high-quality training data
109
+ dataset = load_dataset("json", data_files="training_data_v2.jsonl", split="train")
110
+
111
+ def add_system_prompt(example):
112
+ # Formats the data to include the system prompt for training
113
+ example['text'] = system_prompt + example['text']
114
+ return example
115
+ dataset = dataset.map(add_system_prompt, num_proc=os.cpu_count())
116
+
117
+ except Exception:
118
+ # If the file is not there, we create a tiny mock to test the environment and stop
119
+ print(f"\n--- DATASET WARNING ---")
120
+ print(f"Could not load 'training_data_v2.jsonl'. Using a small mock dataset (1 sample) to confirm environment stability.")
121
+
122
+ mock_data = {
123
+ "text": [
124
+ system_prompt + "USER: Design a 24/7 cash flow project based on FinTech.\nASSISTANT: <master_builder instruction=\"Design a decentralized interest-bearing stablecoin vault DApp with autonomous fee harvesting.\">",
125
+ ]
126
+ }
127
+ dataset = Dataset.from_dict(mock_data)
128
+
129
+ # 5. Training Arguments
130
+ training_args = TrainingArguments(
131
+ output_dir=OUTPUT_DIR,
132
+ num_train_epochs=3,
133
+ per_device_train_batch_size=4,
134
+ gradient_accumulation_steps=1,
135
+ optim="paged_adamw_32bit",
136
+ save_steps=100,
137
+ logging_steps=10,
138
+ learning_rate=2e-4,
139
+ weight_decay=0.001,
140
+ fp16=False,
141
+ bf16=True,
142
+ max_grad_norm=0.3,
143
+ max_steps=-1,
144
+ warmup_ratio=0.03,
145
+ group_by_length=True,
146
+ lr_scheduler_type="constant",
147
+ report_to="tensorboard",
148
+ disable_tqdm=False,
149
+ )
150
+
151
+ # 6. Setup SFT Trainer
152
+ trainer = SFTTrainer(
153
+ model=model,
154
+ train_dataset=dataset,
155
+ peft_config=peft_config,
156
+ dataset_text_field="text",
157
+ max_seq_length=None,
158
+ tokenizer=tokenizer,
159
+ args=training_args,
160
+ # IMPORTANT: The model is taught to generate this tool call tag
161
+ data_collator=DataCollatorForCompletionOnly(response_template="<master_builder", tokenizer=tokenizer),
162
+ )
163
+
164
+ # 7. Run Training
165
+ print("\n--- Starting Buddha V2 Master Builder Fine-Tuning ---\n")
166
+ trainer.train()
167
+
168
+ # 8. Save final model
169
+ trainer.save_model()
170
+ print(f"\n--- Training Complete! Model saved to {OUTPUT_DIR} ---\n")
171
+
172
+
173
+ if __name__ == "__main__":
174
+ fine_tune_master_builder()