File size: 4,876 Bytes
3df89a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Example usage of KerdosAgent for training and inference.

"""

from kerdosai.agent import KerdosAgent
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)

def example_basic_usage():
    """Basic usage example."""
    print("=" * 50)
    print("Example 1: Basic Initialization and Generation")
    print("=" * 50)
    
    # Initialize agent with a small model for testing
    agent = KerdosAgent(
        base_model="gpt2",
        training_data=None  # No training data for inference only
    )
    
    # Get model information
    info = agent.get_model_info()
    print(f"\nModel Info:")
    print(f"  Total parameters: {info['total_parameters']:,}")
    print(f"  Trainable parameters: {info['trainable_parameters']:,}")
    print(f"  Device: {info['device']}")
    
    # Generate text
    prompt = "The future of artificial intelligence is"
    print(f"\nPrompt: {prompt}")
    generated = agent.generate(
        prompt=prompt,
        max_length=50,
        temperature=0.7
    )
    print(f"Generated: {generated}")


def example_with_quantization():
    """Example with 4-bit quantization."""
    print("\n" + "=" * 50)
    print("Example 2: Initialization with Quantization")
    print("=" * 50)
    
    # Initialize with 4-bit quantization for memory efficiency
    agent = KerdosAgent(
        base_model="gpt2",
        training_data=None,
        use_4bit=True
    )
    
    info = agent.get_model_info()
    print(f"\nModel loaded with quantization")
    print(f"  Model dtype: {info['model_dtype']}")
    print(f"  Device: {info['device']}")


def example_batch_inference():
    """Example of batch inference."""
    print("\n" + "=" * 50)
    print("Example 3: Batch Inference")
    print("=" * 50)
    
    agent = KerdosAgent(
        base_model="gpt2",
        training_data=None
    )
    
    # Multiple prompts
    prompts = [
        "Once upon a time",
        "In the year 2050",
        "The secret to happiness is"
    ]
    
    print("\nRunning batch inference...")
    results = agent.inference(
        texts=prompts,
        batch_size=2,
        max_length=30
    )
    
    print("\nResults:")
    for prompt, result in zip(prompts, results):
        print(f"  Prompt: {prompt}")
        print(f"  Result: {result}\n")


def example_training_preparation():
    """Example of preparing model for training with LoRA."""
    print("\n" + "=" * 50)
    print("Example 4: Prepare Model for Training with LoRA")
    print("=" * 50)
    
    agent = KerdosAgent(
        base_model="gpt2",
        training_data="data/sample_data.csv"  # You would need actual data
    )
    
    print("\nBefore LoRA:")
    info_before = agent.get_model_info()
    print(f"  Trainable parameters: {info_before['trainable_parameters']:,}")
    print(f"  Trainable %: {info_before['trainable_percentage']:.2f}%")
    
    # Prepare for training with LoRA
    print("\nApplying LoRA...")
    agent.prepare_for_training(
        use_lora=True,
        lora_r=8,
        lora_alpha=32,
        lora_dropout=0.1
    )
    
    print("\nAfter LoRA:")
    info_after = agent.get_model_info()
    print(f"  Trainable parameters: {info_after['trainable_parameters']:,}")
    print(f"  Trainable %: {info_after['trainable_percentage']:.2f}%")


def example_save_and_load():
    """Example of saving and loading a model."""
    print("\n" + "=" * 50)
    print("Example 5: Save and Load Model")
    print("=" * 50)
    
    # Initialize and save
    print("\nInitializing model...")
    agent = KerdosAgent(
        base_model="gpt2",
        training_data=None
    )
    
    # Save model
    output_dir = "models/example_model"
    print(f"\nSaving model to {output_dir}...")
    agent.save(output_dir)
    print("Model saved successfully!")
    
    # Load model
    print(f"\nLoading model from {output_dir}...")
    loaded_agent = KerdosAgent.load(output_dir)
    print("Model loaded successfully!")
    
    # Test loaded model
    result = loaded_agent.generate("Hello", max_length=20)
    print(f"\nGeneration test: {result}")


if __name__ == "__main__":
    print("\n" + "=" * 50)
    print("KerdosAgent Usage Examples")
    print("=" * 50)
    
    try:
        # Run examples
        example_basic_usage()
        
        # Uncomment to run other examples
        # example_with_quantization()
        # example_batch_inference()
        # example_training_preparation()
        # example_save_and_load()
        
        print("\n" + "=" * 50)
        print("Examples completed successfully!")
        print("=" * 50)
        
    except Exception as e:
        print(f"\nError running examples: {str(e)}")
        import traceback
        traceback.print_exc()