Files
dsProject/dsLightRag/TeacherHelper/T4_ZuoYe.py
2025-09-04 15:41:55 +08:00

96 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import logging
import os
import sys
# 导入现有工具类(不创建新类)
from TeacherHelper.Kit.TeacherHelper import (
LLMClient,
save_lesson_plan
)
# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def generate_gravitation_homework():
"""生成万有引力课后作业的异步函数
Returns:
异步生成器: 流式返回作业内容
"""
# 系统指令 - 符合布鲁姆认知分类和双减政策
system_prompt = "你是命题专家,熟悉布鲁姆认知分类和'双减'政策。客观题采用'四选一'单选,难度比例易:中:难=6:3:1主观题设置2小问第1问'解释现象'对应'理解'第2问'方案设计'对应'创新'。题量控制为20分钟完成。"
# 创建LLM客户端实例使用现有类
llm_client = LLMClient(system_prompt=system_prompt) # 修改为与T3_KeJian.py一致
# 作业生成提示词
prompt = """请输出'万有引力'课后作业满分100分
A. 客观题8题×5分=40分
- 前3题考'史实&概念'识记
- 中间3题考'公式变形&比例'理解
- 后2题考'情境估算'应用
B. 主观题2题30+30分
- 题1结合'天问一号'发射新闻,解释地球与火星之间的引力如何变化
- 题2设计一个实验用智能手机+免费APP估算地球质量写出步骤与所需测量量
C. 评分标准主观题分点给分每点10分
D. 参考答案与解析(客观题给出选项+一句话解析;主观题给出关键公式与评分关键词)
【格式要求】
### A. 客观题
<ul>
<li>1. ……</li>
……
</ul>
### B. 主观题
#### 1. ……
#### 2. ……
### C. 评分标准
……
### D. 参考答案与解析
……"""
# 流式响应生成器
async def get_homework():
async for chunk in llm_client.get_response(prompt, stream=True):
yield chunk
return get_homework()
async def test_generate_homework():
"""测试生成万有引力课后作业并保存"""
print("===== 开始生成万有引力课后作业 =====")
try:
# 修复添加await关键字获取生成器
homework_generator = await generate_gravitation_homework()
output_path = "./markdown/万有引力作业.md"
# 修复正确调用save_lesson_plan函数使用位置参数而非关键字参数
full_content, success = await save_lesson_plan(
homework_generator,
output_path, # 修改为位置参数
stream_print=True
)
if success:
print(f"\n作业生成成功!文件路径: {os.path.abspath(output_path)}")
return full_content
else:
print("\n作业生成失败")
return None
except Exception as e:
print(f"生成过程中发生错误: {str(e)}", file=sys.stderr)
return None
if __name__ == "__main__":
# 运行异步测试函数
asyncio.run(test_generate_homework())