import json from openai import OpenAI import time # 配置API client = OpenAI( api_key="sk-2b675306a92d4fb389766291ab3f1ec1", base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" ) def analyze_question(question): """分析题目并生成记忆要点""" prompt = f"""分析这道D365考试题目,生成简洁的记忆要点帮助快速记忆。 题目: {question['stem']} 选项: {chr(10).join([f"{opt['label']}. {opt['text']}" for opt in question['options']])} 正确答案:{question['answer']} 请用中文生成以下内容(每项不超过100字): 1. 关键词提示:题目中的关键英文单词或短语 2. 题目特点:这道题的独特之处 3. 记忆技巧:如何快速记住这道题的答案 4. 答案解析:为什么这个答案是对的 请用JSON格式返回: {{ "keywords": "关键词提示", "features": "题目特点", "memory_tips": "记忆技巧", "explanation": "答案解析" }} """ try: response = client.chat.completions.create( model="qwen-plus", messages=[ {"role": "system", "content": "你是一个D365考试专家,擅长总结题目要点和记忆技巧。"}, {"role": "user", "content": prompt} ], temperature=0.7, max_tokens=500 ) result = response.choices[0].message.content.strip() # 提取JSON if '```json' in result: result = result.split('```json')[1].split('```')[0].strip() elif '```' in result: result = result.split('```')[1].split('```')[0].strip() return json.loads(result) except Exception as e: print(f"Error analyzing question {question['topic']}-{question['question_num']}: {e}") return { "keywords": "分析失败", "features": "分析失败", "memory_tips": "分析失败", "explanation": "分析失败" } def main(): # 加载题目 with open('exam_data/questions_translated.json', 'r', encoding='utf-8') as f: questions = json.load(f) # 加载已有的记忆要点 with open('exam_data/question_tips.json', 'r', encoding='utf-8') as f: tips_data = json.load(f) print(f"总共 {len(questions)} 道题目") print(f"已有 {len(tips_data)} 道题目的记忆要点") # 找出需要重新分析的题目(前20道) failed_keys = [k for k, v in tips_data.items() if v.get('keywords') == '分析失败'] print(f"需要重新分析 {len(failed_keys)} 道题目") # 重新分析失败的题目 for i, question in enumerate(questions): key = f"{question['topic']}-{question['question_num']}" if key in failed_keys: print(f"[{i+1}/{len(questions)}] 重新分析题目: {key}") tips = analyze_question(question) tips_data[key] = tips # 每分析5道题保存一次 if (i + 1) % 5 == 0: with open('exam_data/question_tips.json', 'w', encoding='utf-8') as f: json.dump(tips_data, f, ensure_ascii=False, indent=2) print(f"已保存进度") # 避免API限流 time.sleep(0.5) # 最终保存 with open('exam_data/question_tips.json', 'w', encoding='utf-8') as f: json.dump(tips_data, f, ensure_ascii=False, indent=2) print(f"\n完成!共重新分析 {len(failed_keys)} 道题目") if __name__ == "__main__": main()