2025-08-18 15:46:09 +08:00
|
|
|
|
import sys
|
2025-08-18 15:34:22 +08:00
|
|
|
|
|
2025-08-18 15:58:31 +08:00
|
|
|
|
from Util import LlmUtil
|
|
|
|
|
|
|
|
|
|
|
2025-08-18 15:46:09 +08:00
|
|
|
|
def initialize_chat_history():
|
|
|
|
|
"""初始化对话历史,包含系统提示"""
|
|
|
|
|
system_prompt = """
|
2025-08-18 15:34:22 +08:00
|
|
|
|
STRICT RULES
|
|
|
|
|
Be an approachable-yet-dynamic teacher,who helps the user learn by guiding
|
2025-08-18 15:58:31 +08:00
|
|
|
|
them through their studies.
|
2025-08-18 15:34:22 +08:00
|
|
|
|
|
|
|
|
|
1.Get to know the user.lf you don't know their goals or grade level,ask the
|
|
|
|
|
user before diving in.(Keep this lightweight!)If they don't answer,aim for
|
|
|
|
|
explanations that would make sense to a10th grade student.
|
|
|
|
|
|
|
|
|
|
2.Build on existing knowledge.Connect new ideas to what the user already
|
|
|
|
|
knows.
|
|
|
|
|
|
|
|
|
|
3.Guide users,don't just give answers.Use questions,hints,and small steps
|
|
|
|
|
so the user discovers the answer for themselves.
|
|
|
|
|
|
|
|
|
|
4.Check and reinforce.After hard parts,confirm the user can restate or use the
|
|
|
|
|
idea.Offer quick summaries,mnemonics,or mini-reviews to help the ideas
|
|
|
|
|
stick.
|
|
|
|
|
|
|
|
|
|
5.Vary the rhythm.Mix explanations,questions,and activities(like roleplaying,
|
|
|
|
|
practice rounds,or asking the user to teach you) so it feels like a conversation,
|
|
|
|
|
not alecture.
|
|
|
|
|
|
|
|
|
|
Above all:DO NOT DO THE USER'S WORK FOR THEM. Don't answer homework questions - Help the user find the answer,by working
|
|
|
|
|
with them collaboratively and building from what they already know.
|
|
|
|
|
"""
|
2025-08-18 15:58:31 +08:00
|
|
|
|
return [{"role": "system", "content": system_prompt}]
|
2025-08-18 15:46:09 +08:00
|
|
|
|
|
|
|
|
|
|
2025-08-18 15:58:31 +08:00
|
|
|
|
if __name__ == "__main__":
|
2025-08-18 15:46:09 +08:00
|
|
|
|
# 初始化对话历史
|
|
|
|
|
chat_history = initialize_chat_history()
|
2025-08-18 15:58:31 +08:00
|
|
|
|
|
|
|
|
|
# 欢迎消息
|
|
|
|
|
print("教师助手已启动。输入 'exit' 或 '退出' 结束对话。")
|
|
|
|
|
print("你可以开始提问了,例如: '讲解一下勾股定理的证明'")
|
|
|
|
|
|
2025-08-18 15:46:09 +08:00
|
|
|
|
# 多轮对话循环
|
|
|
|
|
while True:
|
|
|
|
|
# 获取用户输入
|
|
|
|
|
user_input = input("\n你: ")
|
|
|
|
|
|
|
|
|
|
# 检查是否退出
|
|
|
|
|
if user_input.lower() in ['exit', '退出']:
|
|
|
|
|
print("对话已结束。")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
# 添加用户输入到对话历史
|
2025-08-18 15:58:31 +08:00
|
|
|
|
chat_history.append({"role": "user", "content": user_input})
|
2025-08-18 15:34:22 +08:00
|
|
|
|
|
2025-08-18 15:58:31 +08:00
|
|
|
|
# 发送请求(传递完整对话历史)
|
2025-08-18 15:46:09 +08:00
|
|
|
|
print("\n教师助手:")
|
2025-08-18 15:58:31 +08:00
|
|
|
|
try:
|
|
|
|
|
# 调用LlmUtil获取响应,传递对话历史
|
|
|
|
|
response_content = LlmUtil.get_llm_response(user_input, chat_history)
|
|
|
|
|
|
|
|
|
|
# 打印响应
|
|
|
|
|
print(response_content)
|
|
|
|
|
|
|
|
|
|
# 添加助手回复到对话历史
|
|
|
|
|
chat_history.append({"role": "assistant", "content": response_content})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"发生错误: {str(e)}")
|
|
|
|
|
# 从对话历史中移除最后添加的用户输入,以便用户可以重试
|
|
|
|
|
chat_history.pop()
|