Spaces:
Sleeping
Sleeping
Create teacher.py
Browse files- teacher.py +125 -0
teacher.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Teacher Model - Calls OpenRouter API for knowledge distillation"""
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
from typing import Optional, Dict, Any, List
|
| 6 |
+
from config import (
|
| 7 |
+
OPENROUTER_API_KEY,
|
| 8 |
+
OPENROUTER_BASE_URL,
|
| 9 |
+
TEACHER_MODEL,
|
| 10 |
+
TEACHER_TEMPERATURE,
|
| 11 |
+
TEACHER_MAX_TOKENS,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TeacherModel:
|
| 16 |
+
"""Wrapper for the OpenRouter teacher model (Dolphin Mistral)"""
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.api_key = OPENROUTER_API_KEY
|
| 20 |
+
self.base_url = OPENROUTER_BASE_URL
|
| 21 |
+
self.model = TEACHER_MODEL
|
| 22 |
+
self.headers = {
|
| 23 |
+
"Content-Type": "application/json",
|
| 24 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 25 |
+
"HTTP-Referer": "https://huggingface.co/spaces/vedaco/veda-programming",
|
| 26 |
+
"X-Title": "Veda Programming Assistant",
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
def ask(
|
| 30 |
+
self,
|
| 31 |
+
user_message: str,
|
| 32 |
+
conversation_history: List[Dict[str, str]] = None,
|
| 33 |
+
temperature: float = None,
|
| 34 |
+
max_tokens: int = None,
|
| 35 |
+
) -> Optional[str]:
|
| 36 |
+
"""
|
| 37 |
+
Ask the teacher model a question.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
user_message: The user's question
|
| 41 |
+
conversation_history: Previous messages [{"role": "user/assistant", "content": "..."}]
|
| 42 |
+
temperature: Sampling temperature
|
| 43 |
+
max_tokens: Maximum tokens to generate
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
Teacher's response as string, or None if error
|
| 47 |
+
"""
|
| 48 |
+
if not self.api_key or self.api_key == "":
|
| 49 |
+
print("Warning: No OpenRouter API key configured")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
temperature = temperature or TEACHER_TEMPERATURE
|
| 53 |
+
max_tokens = max_tokens or TEACHER_MAX_TOKENS
|
| 54 |
+
|
| 55 |
+
# Build messages
|
| 56 |
+
messages = []
|
| 57 |
+
|
| 58 |
+
# System prompt to make teacher respond like a programming assistant
|
| 59 |
+
messages.append({
|
| 60 |
+
"role": "system",
|
| 61 |
+
"content": """You are a helpful programming assistant. You help users with:
|
| 62 |
+
- Writing Python code
|
| 63 |
+
- Explaining programming concepts
|
| 64 |
+
- Debugging code
|
| 65 |
+
- Answering questions about algorithms and data structures
|
| 66 |
+
|
| 67 |
+
Be clear, concise, and provide code examples when helpful.
|
| 68 |
+
Format code blocks with ```python and ```.
|
| 69 |
+
"""
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
# Add conversation history
|
| 73 |
+
if conversation_history:
|
| 74 |
+
for msg in conversation_history[-6:]: # Last 6 messages for context
|
| 75 |
+
messages.append({
|
| 76 |
+
"role": msg.get("role", "user"),
|
| 77 |
+
"content": msg.get("content", "")
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
# Add current user message
|
| 81 |
+
messages.append({
|
| 82 |
+
"role": "user",
|
| 83 |
+
"content": user_message
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
payload = {
|
| 87 |
+
"model": self.model,
|
| 88 |
+
"messages": messages,
|
| 89 |
+
"temperature": temperature,
|
| 90 |
+
"max_tokens": max_tokens,
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
response = requests.post(
|
| 95 |
+
self.base_url,
|
| 96 |
+
headers=self.headers,
|
| 97 |
+
json=payload,
|
| 98 |
+
timeout=60
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if response.status_code == 200:
|
| 102 |
+
data = response.json()
|
| 103 |
+
if "choices" in data and len(data["choices"]) > 0:
|
| 104 |
+
return data["choices"][0]["message"]["content"]
|
| 105 |
+
else:
|
| 106 |
+
print(f"Teacher API unexpected response: {data}")
|
| 107 |
+
return None
|
| 108 |
+
else:
|
| 109 |
+
print(f"Teacher API error: {response.status_code} - {response.text}")
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
+
except requests.exceptions.Timeout:
|
| 113 |
+
print("Teacher API timeout")
|
| 114 |
+
return None
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Teacher API exception: {e}")
|
| 117 |
+
return None
|
| 118 |
+
|
| 119 |
+
def is_available(self) -> bool:
|
| 120 |
+
"""Check if teacher API is available"""
|
| 121 |
+
return bool(self.api_key and self.api_key != "")
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# Global teacher instance
|
| 125 |
+
teacher = TeacherModel()
|