File size: 8,628 Bytes
9c1b824
 
 
 
 
 
 
 
 
379615b
9c1b824
0ee4730
9c1b824
 
 
 
 
 
 
 
0ee4730
 
9c1b824
 
 
0ee4730
b397650
9c1b824
b397650
 
9c1b824
b397650
9c1b824
 
 
0ee4730
 
 
9c1b824
b397650
0ee4730
9c1b824
 
379615b
9c1b824
379615b
b397650
379615b
b397650
379615b
c5cd891
84677b5
c5cd891
b397650
379615b
 
b397650
379615b
 
0ee4730
379615b
 
 
 
 
 
 
0ee4730
379615b
 
3afe501
b397650
0ee4730
379615b
0ee4730
 
379615b
 
 
 
c5cd891
 
379615b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84677b5
379615b
 
84677b5
379615b
 
84677b5
379615b
 
84677b5
379615b
 
 
fc679ee
379615b
0ee4730
379615b
c5cd891
379615b
c5cd891
379615b
c5cd891
0ee4730
194b2d7
0ee4730
 
194b2d7
84677b5
379615b
84677b5
379615b
0ee4730
 
9c1b824
 
0ee4730
379615b
194b2d7
70df3dc
fc679ee
379615b
 
fc679ee
0ee4730
 
 
 
b397650
 
 
379615b
b397650
9c1b824
 
0ee4730
 
9c1b824
 
0ee4730
 
 
 
45afec6
9c1b824
 
0ee4730
 
 
 
9c1b824
0ee4730
9c1b824
 
 
84677b5
379615b
0ee4730
b397650
fc679ee
9c1b824
0ee4730
 
9c1b824
0ee4730
 
 
 
 
b397650
0ee4730
 
 
 
 
 
9c1b824
3afe501
379615b
 
fc679ee
0ee4730
 
fc679ee
b397650
 
3afe501
 
0ee4730
379615b
0ee4730
70df3dc
379615b
0ee4730
 
 
 
 
 
 
 
 
 
 
70df3dc
 
0ee4730
fc679ee
379615b
0ee4730
3afe501
 
0ee4730
 
 
 
9c1b824
 
0ee4730
 
379615b
b397650
379615b
 
0ee4730
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
import os
import torch
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from starlette.middleware.cors import CORSMiddleware

# === Setup FastAPI ===
app = FastAPI(title="Apollo AI Backend - Qwen2-0.5B", version="3.0.0")

# === CORS ===
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# === Configuration ===
API_KEY = os.getenv("API_KEY", "aigenapikey1234567890")
BASE_MODEL = "Qwen/Qwen2-0.5B-Instruct"
ADAPTER_PATH = "adapter"

# === Load Model ===
print("🔧 Loading tokenizer for Qwen2-0.5B...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

print("🧠 Loading Qwen2-0.5B base model...")
base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    trust_remote_code=True,
    torch_dtype=torch.float32,
    device_map="cpu"
)

print("🔗 Applying LoRA adapter to Qwen2-0.5B...")
model = PeftModel.from_pretrained(base_model, ADAPTER_PATH)
model.eval()

print("✅ Qwen2-0.5B model ready!")

def create_conversation_prompt(messages: list, is_force_mode: bool) -> str:
    """
    Create a simple conversation prompt with appropriate system instruction
    """
    if is_force_mode:
        system_prompt = "DIRECT ANSWER MODE: Give immediate, complete solutions. Provide working code and clear explanations. Don't ask questions - just solve the problem directly."
    else:
        system_prompt = "TEACHER MODE: You are a teacher. NEVER give direct answers or solutions. ALWAYS respond with guiding questions. Ask things like 'What do you think this does?' or 'How would you approach this?' Help them discover answers themselves through questions."
    
    # Build conversation
    conversation = f"System: {system_prompt}\n\n"
    
    # Add last 6 messages (3 pairs) for context
    recent_messages = messages[-6:] if len(messages) > 6 else messages
    
    for msg in recent_messages:
        role = msg.get("role", "")
        content = msg.get("content", "")
        if role == "user":
            conversation += f"Student: {content}\n"
        elif role == "assistant":
            conversation += f"Assistant: {content}\n"
    
    conversation += "Assistant:"
    return conversation

def generate_response(messages: list, is_force_mode: bool = False, max_tokens: int = 200, temperature: float = 0.7) -> str:
    """
    Generate response using the actual AI model
    """
    try:
        # Create conversation prompt
        prompt = create_conversation_prompt(messages, is_force_mode)
        
        print(f"🎯 Generating {'FORCE' if is_force_mode else 'MENTOR'} response")
        print(f"🔍 DEBUG: force_mode = {is_force_mode}")
        print(f"📝 System prompt: {prompt.split('Student:')[0][:100]}...")
        
        # Tokenize input
        inputs = tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True)
        
        # Generate response
        with torch.no_grad():
            outputs = model.generate(
                inputs.input_ids,
                max_new_tokens=max_tokens,
                temperature=temperature,
                do_sample=True,
                pad_token_id=tokenizer.eos_token_id,
                eos_token_id=tokenizer.eos_token_id,
                top_p=0.9,
                repetition_penalty=1.1
            )
        
        # Decode response
        full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Extract only the new generated part
        response = full_response[len(prompt):].strip()
        
        # Clean up response
        response = response.replace("Student:", "").replace("Assistant:", "").strip()
        
        # Remove any system mentions
        if response.startswith("System:"):
            response = response.split("\n", 1)[-1].strip()
        
        print(f"✅ Generated response length: {len(response)}")
        
        if not response or len(response) < 10:
            # Stronger fallback responses
            if is_force_mode:
                return "Here's the direct answer: I need you to provide a more specific question so I can give you the exact solution you need."
            else:
                return "What do you think this code is trying to do? Can you trace through it step by step and tell me what you notice?"
        
        return response
        
    except Exception as e:
        print(f"❌ Generation error: {e}")
        if is_force_mode:
            return "I encountered an error. Please try rephrasing your question."
        else:
            return "I had trouble processing that. Can you tell me what you're trying to understand?"

# === Routes ===
@app.get("/")
def root():
    return {
        "message": "🤖 Apollo AI Backend v3.0 - Qwen2-0.5B",
        "model": "Qwen/Qwen2-0.5B-Instruct with LoRA", 
        "status": "ready",
        "modes": {
            "mentor": "Guides learning with questions",
            "force": "Provides direct answers"
        }
    }

@app.get("/health")
def health():
    return {
        "status": "healthy", 
        "model_loaded": True, 
        "model_size": "0.5B"
    }

@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
    # Validate API key
    auth_header = request.headers.get("Authorization", "")
    if not auth_header.startswith("Bearer "):
        return JSONResponse(
            status_code=401, 
            content={"error": "Missing or invalid Authorization header"}
        )

    token = auth_header.replace("Bearer ", "").strip()
    if token != API_KEY:
        return JSONResponse(
            status_code=401, 
            content={"error": "Invalid API key"}
        )

    # Parse request body
    try:
        body = await request.json()
        messages = body.get("messages", [])
        max_tokens = min(body.get("max_tokens", 200), 400)
        temperature = max(0.1, min(body.get("temperature", 0.7), 1.0))
        
        is_force_mode = body.get("force_mode", False)
        
        if not messages or not isinstance(messages, list):
            raise ValueError("Messages field is required and must be a list")
            
    except Exception as e:
        return JSONResponse(
            status_code=400, 
            content={"error": f"Invalid request body: {str(e)}"}
        )

    # Validate messages
    for i, msg in enumerate(messages):
        if not isinstance(msg, dict) or "role" not in msg or "content" not in msg:
            return JSONResponse(
                status_code=400,
                content={"error": f"Invalid message format at index {i}"}
            )

    try:
        print(f"📥 Processing request in {'FORCE' if is_force_mode else 'MENTOR'} mode")
        print(f"📊 Total messages: {len(messages)}")
        
        response_content = generate_response(
            messages=messages,
            is_force_mode=is_force_mode,
            max_tokens=max_tokens,
            temperature=temperature
        )
        
        return {
            "id": f"chatcmpl-apollo-{hash(str(messages)) % 10000}",
            "object": "chat.completion",
            "created": int(torch.tensor(0).item()),
            "model": f"qwen2-0.5b-{'force' if is_force_mode else 'mentor'}",
            "choices": [
                {
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": response_content
                    },
                    "finish_reason": "stop"
                }
            ],
            "usage": {
                "prompt_tokens": len(str(messages)),
                "completion_tokens": len(response_content),
                "total_tokens": len(str(messages)) + len(response_content)
            },
            "apollo_mode": "force" if is_force_mode else "mentor"
        }
        
    except Exception as e:
        print(f"❌ Chat completion error: {e}")
        return JSONResponse(
            status_code=500,
            content={"error": f"Internal server error: {str(e)}"}
        )

if __name__ == "__main__":
    import uvicorn
    print("🚀 Starting Apollo AI Backend v3.0 - Simple & Clean...")
    print("🧠 Model: Qwen/Qwen2-0.5B-Instruct (500M parameters)")
    print("🎯 Mentor Mode: Asks guiding questions")
    print("⚡ Force Mode: Gives direct answers")
    uvicorn.run(app, host="0.0.0.0", port=7860)