Spaces:
Sleeping
Sleeping
Update clare_core.py
Browse files- clare_core.py +23 -3
clare_core.py
CHANGED
|
@@ -69,6 +69,7 @@ MASTERY_KEYWORDS = [
|
|
| 69 |
"清楚了",
|
| 70 |
]
|
| 71 |
|
|
|
|
| 72 |
def update_weaknesses_from_message(message: str, weaknesses: List[str]) -> List[str]:
|
| 73 |
lower_msg = message.lower()
|
| 74 |
if any(k in lower_msg for k in WEAKNESS_KEYWORDS):
|
|
@@ -176,7 +177,8 @@ def detect_language(message: str, preference: str) -> str:
|
|
| 176 |
if re.search(r"[\u4e00-\u9fff]", message):
|
| 177 |
return "中文"
|
| 178 |
return "English"
|
| 179 |
-
|
|
|
|
| 180 |
def get_empty_input_prompt(lang: str) -> str:
|
| 181 |
"""
|
| 182 |
空输入时的友好提示,根据语言返回中/英文。
|
|
@@ -186,6 +188,7 @@ def get_empty_input_prompt(lang: str) -> str:
|
|
| 186 |
# 默认英文
|
| 187 |
return "Please type a question or some text before sending, then hit Enter."
|
| 188 |
|
|
|
|
| 189 |
def build_error_message(
|
| 190 |
e: Exception,
|
| 191 |
lang: str,
|
|
@@ -351,7 +354,6 @@ def find_similar_past_question(
|
|
| 351 |
|
| 352 |
return None
|
| 353 |
|
| 354 |
-
from config import client, DEFAULT_MODEL, EMBEDDING_MODEL # 确认已导入 DEFAULT_MODEL
|
| 355 |
|
| 356 |
def safe_chat_completion(
|
| 357 |
model_name: str,
|
|
@@ -399,6 +401,7 @@ def safe_chat_completion(
|
|
| 399 |
# 两次都失败,返回友好的错误文案
|
| 400 |
return build_error_message(last_error or Exception("unknown error"), lang, op)
|
| 401 |
|
|
|
|
| 402 |
# ---------- 构建 messages ----------
|
| 403 |
def build_messages(
|
| 404 |
user_message: str,
|
|
@@ -409,6 +412,7 @@ def build_messages(
|
|
| 409 |
course_outline: Optional[List[str]],
|
| 410 |
weaknesses: Optional[List[str]],
|
| 411 |
cognitive_state: Optional[Dict[str, int]],
|
|
|
|
| 412 |
) -> List[Dict[str, str]]:
|
| 413 |
messages: List[Dict[str, str]] = [
|
| 414 |
{"role": "system", "content": CLARE_SYSTEM_PROMPT}
|
|
@@ -531,6 +535,21 @@ def build_messages(
|
|
| 531 |
}
|
| 532 |
)
|
| 533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
# 历史对话
|
| 535 |
for user, assistant in history:
|
| 536 |
messages.append({"role": "user", "content": user})
|
|
@@ -552,6 +571,7 @@ def chat_with_clare(
|
|
| 552 |
course_outline: Optional[List[str]],
|
| 553 |
weaknesses: Optional[List[str]],
|
| 554 |
cognitive_state: Optional[Dict[str, int]],
|
|
|
|
| 555 |
) -> Tuple[str, List[Tuple[str, str]]]:
|
| 556 |
# 构建 messages
|
| 557 |
messages = build_messages(
|
|
@@ -563,6 +583,7 @@ def chat_with_clare(
|
|
| 563 |
course_outline=course_outline,
|
| 564 |
weaknesses=weaknesses,
|
| 565 |
cognitive_state=cognitive_state,
|
|
|
|
| 566 |
)
|
| 567 |
|
| 568 |
# 统一安全调用
|
|
@@ -578,7 +599,6 @@ def chat_with_clare(
|
|
| 578 |
return answer, history
|
| 579 |
|
| 580 |
|
| 581 |
-
|
| 582 |
# ---------- 导出对话为 Markdown ----------
|
| 583 |
def export_conversation(
|
| 584 |
history: List[Tuple[str, str]],
|
|
|
|
| 69 |
"清楚了",
|
| 70 |
]
|
| 71 |
|
| 72 |
+
|
| 73 |
def update_weaknesses_from_message(message: str, weaknesses: List[str]) -> List[str]:
|
| 74 |
lower_msg = message.lower()
|
| 75 |
if any(k in lower_msg for k in WEAKNESS_KEYWORDS):
|
|
|
|
| 177 |
if re.search(r"[\u4e00-\u9fff]", message):
|
| 178 |
return "中文"
|
| 179 |
return "English"
|
| 180 |
+
|
| 181 |
+
|
| 182 |
def get_empty_input_prompt(lang: str) -> str:
|
| 183 |
"""
|
| 184 |
空输入时的友好提示,根据语言返回中/英文。
|
|
|
|
| 188 |
# 默认英文
|
| 189 |
return "Please type a question or some text before sending, then hit Enter."
|
| 190 |
|
| 191 |
+
|
| 192 |
def build_error_message(
|
| 193 |
e: Exception,
|
| 194 |
lang: str,
|
|
|
|
| 354 |
|
| 355 |
return None
|
| 356 |
|
|
|
|
| 357 |
|
| 358 |
def safe_chat_completion(
|
| 359 |
model_name: str,
|
|
|
|
| 401 |
# 两次都失败,返回友好的错误文案
|
| 402 |
return build_error_message(last_error or Exception("unknown error"), lang, op)
|
| 403 |
|
| 404 |
+
|
| 405 |
# ---------- 构建 messages ----------
|
| 406 |
def build_messages(
|
| 407 |
user_message: str,
|
|
|
|
| 412 |
course_outline: Optional[List[str]],
|
| 413 |
weaknesses: Optional[List[str]],
|
| 414 |
cognitive_state: Optional[Dict[str, int]],
|
| 415 |
+
rag_context: Optional[str] = None, # 👈 新增:RAG 检索结果
|
| 416 |
) -> List[Dict[str, str]]:
|
| 417 |
messages: List[Dict[str, str]] = [
|
| 418 |
{"role": "system", "content": CLARE_SYSTEM_PROMPT}
|
|
|
|
| 535 |
}
|
| 536 |
)
|
| 537 |
|
| 538 |
+
# ✅ RAG 检索结果(如果有)
|
| 539 |
+
if rag_context:
|
| 540 |
+
messages.append(
|
| 541 |
+
{
|
| 542 |
+
"role": "system",
|
| 543 |
+
"content": (
|
| 544 |
+
"Here are some relevant excerpts from the course materials. "
|
| 545 |
+
"Use them as the primary factual grounding when answering the student's question. "
|
| 546 |
+
"If there is any conflict between these excerpts and your prior knowledge, "
|
| 547 |
+
"prefer the excerpts.\n\n"
|
| 548 |
+
+ rag_context
|
| 549 |
+
),
|
| 550 |
+
}
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
# 历史对话
|
| 554 |
for user, assistant in history:
|
| 555 |
messages.append({"role": "user", "content": user})
|
|
|
|
| 571 |
course_outline: Optional[List[str]],
|
| 572 |
weaknesses: Optional[List[str]],
|
| 573 |
cognitive_state: Optional[Dict[str, int]],
|
| 574 |
+
rag_context: Optional[str] = None, # 👈 新增
|
| 575 |
) -> Tuple[str, List[Tuple[str, str]]]:
|
| 576 |
# 构建 messages
|
| 577 |
messages = build_messages(
|
|
|
|
| 583 |
course_outline=course_outline,
|
| 584 |
weaknesses=weaknesses,
|
| 585 |
cognitive_state=cognitive_state,
|
| 586 |
+
rag_context=rag_context,
|
| 587 |
)
|
| 588 |
|
| 589 |
# 统一安全调用
|
|
|
|
| 599 |
return answer, history
|
| 600 |
|
| 601 |
|
|
|
|
| 602 |
# ---------- 导出对话为 Markdown ----------
|
| 603 |
def export_conversation(
|
| 604 |
history: List[Tuple[str, str]],
|