Update app.py
Browse files
app.py
CHANGED
|
@@ -12,14 +12,10 @@ from langchain_groq import ChatGroq
|
|
| 12 |
from typing_extensions import TypedDict
|
| 13 |
|
| 14 |
# --- Type Definitions ---
|
| 15 |
-
class TaggedReply(TypedDict):
|
| 16 |
-
reply: str
|
| 17 |
-
tags: List[str]
|
| 18 |
-
|
| 19 |
class AssistantState(TypedDict):
|
| 20 |
conversationSummary: str
|
| 21 |
language: str
|
| 22 |
-
|
| 23 |
|
| 24 |
# --- Logging ---
|
| 25 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
@@ -61,14 +57,29 @@ def detect_language_from_text(text: str) -> Optional[str]:
|
|
| 61 |
|
| 62 |
def update_summary(chat_history: List[Dict[str, str]]) -> str:
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
For now, returns last 3 messages concatenated as a simple summary.
|
| 67 |
"""
|
| 68 |
-
recent_msgs = chat_history[-6:]
|
| 69 |
summary = " | ".join(f"{m['role']}: {m['content'][:50].replace('\n',' ')}" for m in recent_msgs)
|
| 70 |
return summary
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# --- Routes ---
|
| 73 |
|
| 74 |
@app.route("/", methods=["GET"])
|
|
@@ -86,7 +97,9 @@ def chat():
|
|
| 86 |
|
| 87 |
conversation_summary = assistant_state.get("conversationSummary", "")
|
| 88 |
language = assistant_state.get("language", "Python")
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Detect language from last user message
|
| 92 |
last_user_msg = ""
|
|
@@ -99,8 +112,8 @@ def chat():
|
|
| 99 |
logger.info(f"Detected new language: {detected_lang}")
|
| 100 |
language = detected_lang
|
| 101 |
|
| 102 |
-
# Build
|
| 103 |
-
system_prompt =
|
| 104 |
messages = [{"role": "system", "content": system_prompt}]
|
| 105 |
messages.extend(chat_history)
|
| 106 |
|
|
@@ -114,7 +127,7 @@ def chat():
|
|
| 114 |
"updated_state": {
|
| 115 |
"conversationSummary": conversation_summary,
|
| 116 |
"language": language,
|
| 117 |
-
"
|
| 118 |
},
|
| 119 |
"chat_history": chat_history,
|
| 120 |
}), 500
|
|
@@ -130,48 +143,15 @@ def chat():
|
|
| 130 |
"updated_state": {
|
| 131 |
"conversationSummary": conversation_summary,
|
| 132 |
"language": language,
|
| 133 |
-
"
|
| 134 |
},
|
| 135 |
"chat_history": chat_history,
|
| 136 |
})
|
| 137 |
|
| 138 |
-
@app.route("/tag_reply", methods=["POST"])
|
| 139 |
-
def tag_reply():
|
| 140 |
-
data = request.get_json(force=True)
|
| 141 |
-
if not isinstance(data, dict):
|
| 142 |
-
return jsonify({"error": "invalid request body"}), 400
|
| 143 |
-
|
| 144 |
-
reply_content = data.get("reply")
|
| 145 |
-
tags = data.get("tags")
|
| 146 |
-
assistant_state: AssistantState = data.get("assistant_state") or {}
|
| 147 |
-
|
| 148 |
-
if not reply_content or not tags:
|
| 149 |
-
return jsonify({"error": "Missing 'reply' or 'tags' in request"}), 400
|
| 150 |
-
|
| 151 |
-
tags = [str(t).strip() for t in tags if str(t).strip()]
|
| 152 |
-
if not tags:
|
| 153 |
-
return jsonify({"error": "Tags list cannot be empty"}), 400
|
| 154 |
-
|
| 155 |
-
tagged_replies = assistant_state.get("taggedReplies", [])
|
| 156 |
-
tagged_replies.append({"reply": reply_content, "tags": tags})
|
| 157 |
-
|
| 158 |
-
updated_state = {
|
| 159 |
-
"conversationSummary": assistant_state.get("conversationSummary", ""),
|
| 160 |
-
"language": assistant_state.get("language", "Python"),
|
| 161 |
-
"taggedReplies": tagged_replies,
|
| 162 |
-
}
|
| 163 |
-
|
| 164 |
-
logger.info(f"Reply tagged with: {tags}")
|
| 165 |
-
|
| 166 |
-
return jsonify({
|
| 167 |
-
"message": "Reply saved and tagged successfully.",
|
| 168 |
-
"updated_state": updated_state,
|
| 169 |
-
}), 200
|
| 170 |
-
|
| 171 |
@app.route("/ping", methods=["GET"])
|
| 172 |
def ping():
|
| 173 |
return jsonify({"status": "ok"})
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
port = int(os.getenv("PORT", 7860))
|
| 177 |
-
app.run(host="0.0.0.0", port=port, debug=True)
|
|
|
|
| 12 |
from typing_extensions import TypedDict
|
| 13 |
|
| 14 |
# --- Type Definitions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
class AssistantState(TypedDict):
|
| 16 |
conversationSummary: str
|
| 17 |
language: str
|
| 18 |
+
mode: str # "teacher" or "student"
|
| 19 |
|
| 20 |
# --- Logging ---
|
| 21 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
|
|
| 57 |
|
| 58 |
def update_summary(chat_history: List[Dict[str, str]]) -> str:
|
| 59 |
"""
|
| 60 |
+
Simple heuristic summary: last 6 messages concatenated.
|
| 61 |
+
Replace with your own summarization chain if desired.
|
|
|
|
| 62 |
"""
|
| 63 |
+
recent_msgs = chat_history[-6:]
|
| 64 |
summary = " | ".join(f"{m['role']}: {m['content'][:50].replace('\n',' ')}" for m in recent_msgs)
|
| 65 |
return summary
|
| 66 |
|
| 67 |
+
def build_system_prompt(language: str, conversation_summary: str, mode: str) -> str:
|
| 68 |
+
"""
|
| 69 |
+
Build system prompt dynamically based on mode.
|
| 70 |
+
"""
|
| 71 |
+
base = f"You are a helpful programming assistant. Current language: {language}. Conversation summary: {conversation_summary}\n\n"
|
| 72 |
+
if mode == "student":
|
| 73 |
+
base += (
|
| 74 |
+
"You are in STUDENT MODE: Guide the user on *how to achieve* their programming tasks. "
|
| 75 |
+
"Provide general explanations and pseudocode examples when appropriate. Avoid full detailed code unless necessary."
|
| 76 |
+
)
|
| 77 |
+
else: # teacher mode default
|
| 78 |
+
base += (
|
| 79 |
+
"You are in TEACHER MODE: Provide detailed suggestions, structured explanations, and full code examples."
|
| 80 |
+
)
|
| 81 |
+
return base
|
| 82 |
+
|
| 83 |
# --- Routes ---
|
| 84 |
|
| 85 |
@app.route("/", methods=["GET"])
|
|
|
|
| 97 |
|
| 98 |
conversation_summary = assistant_state.get("conversationSummary", "")
|
| 99 |
language = assistant_state.get("language", "Python")
|
| 100 |
+
mode = assistant_state.get("mode", "teacher").lower()
|
| 101 |
+
if mode not in ("teacher", "student"):
|
| 102 |
+
mode = "teacher"
|
| 103 |
|
| 104 |
# Detect language from last user message
|
| 105 |
last_user_msg = ""
|
|
|
|
| 112 |
logger.info(f"Detected new language: {detected_lang}")
|
| 113 |
language = detected_lang
|
| 114 |
|
| 115 |
+
# Build system prompt based on mode
|
| 116 |
+
system_prompt = build_system_prompt(language, conversation_summary, mode)
|
| 117 |
messages = [{"role": "system", "content": system_prompt}]
|
| 118 |
messages.extend(chat_history)
|
| 119 |
|
|
|
|
| 127 |
"updated_state": {
|
| 128 |
"conversationSummary": conversation_summary,
|
| 129 |
"language": language,
|
| 130 |
+
"mode": mode,
|
| 131 |
},
|
| 132 |
"chat_history": chat_history,
|
| 133 |
}), 500
|
|
|
|
| 143 |
"updated_state": {
|
| 144 |
"conversationSummary": conversation_summary,
|
| 145 |
"language": language,
|
| 146 |
+
"mode": mode,
|
| 147 |
},
|
| 148 |
"chat_history": chat_history,
|
| 149 |
})
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
@app.route("/ping", methods=["GET"])
|
| 152 |
def ping():
|
| 153 |
return jsonify({"status": "ok"})
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
port = int(os.getenv("PORT", 7860))
|
| 157 |
+
app.run(host="0.0.0.0", port=port, debug=True)
|