Spaces:
Sleeping
Sleeping
| def generate_with_claude(text, api_key, course_name="", section_name="", lesson_name=""): | |
| client = Anthropic(api_key=api_key) | |
| segment_analysis_schema = TextSegmentAnalysis.model_json_schema() | |
| tools = [ | |
| { | |
| "name": "build_segment_analysis", | |
| "description": "Build the text segment analysis with quiz questions", | |
| "input_schema": segment_analysis_schema | |
| } | |
| ] | |
| system_prompt = """You are a helpful assistant specialized in text analysis and educational content creation. | |
| You analyze texts to identify distinct segments, create summaries, and generate quiz questions.""" | |
| prompt = f"""Analyze the following text and identify distinct segments within it and do text segmentation: | |
| 1. Segments should be STRICTLY max=15 | |
| 2. For each segment/topic you identify: | |
| - Provide a SPECIFIC and UNIQUE topic name (3-5 words) that clearly differentiates it from other segments | |
| - List 3-5 key concepts discussed in that segment (be precise and avoid repetition between segments) | |
| - Write a brief summary of that segment (3-5 sentences) | |
| - Create 5 high-quality, meaningful quiz questions based DIRECTLY on the content in that segment only | |
| - Questions and answers should be only from the content of the segment | |
| For each quiz question: | |
| - Create cognitively demanding questions that test COMPREHENSION and ANALYSIS, not just recall | |
| - Focus on CORE PRINCIPLES, key insights, and conceptual understanding | |
| - Questions should require readers to think critically about the material | |
| - AVOID superficial questions about statistics, numbers, or trivial details | |
| - AVOID questions about the document structure or presentation | |
| - Create one correct answer that comes DIRECTLY from the text | |
| - Create two plausible but incorrect answers that require discernment | |
| - Ensure all answer options have similar length (± 2 words) | |
| - Ensure the correct answer is clearly indicated with a ✓ symbol | |
| IMPORTANT QUIZ QUESTION CRITERIA: | |
| - Questions should assess understanding of fundamental concepts and principles | |
| - Questions should require application of knowledge, not just memorization | |
| - Questions should focus on important ideas that have educational value | |
| - Questions should be meaningful in an educational context | |
| - Questions should challenge the reader to think about implications and connections | |
| - No need for questions on the introduction, conclusion or the demonstration from the context | |
| ADDITIONAL REQUIREMENT: | |
| - **First, detect the language of the original text.** | |
| - **Generate ALL output (topic names, key concepts, summaries, and quizzes) in the same language as the original text.** | |
| - If the text is in Russian, generate all responses in Russian. | |
| - If the text is in another language, generate responses in that original language. | |
| Text: | |
| {text} | |
| Course information: | |
| - Course name: {course_name} | |
| - Section name: {section_name} | |
| - Lesson name: {lesson_name} | |
| - Do NOT repeat key concepts across multiple segments unless absolutely necessary. | |
| - Ensure the quiz questions challenge the reader and are not easily guessable. | |
| - FOCUS ON CONCEPTUAL UNDERSTANDING, not trivial facts or statistics. | |
| """ | |
| try: | |
| response = client.messages.create( | |
| model="claude-3-5-sonnet-20241022", | |
| max_tokens=8192, | |
| temperature=0.7, | |
| system=system_prompt, | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| tools=tools, | |
| tool_choice={"type": "tool", "name": "build_segment_analysis"} | |
| ) | |
| # Extract the tool call content | |
| if response.content and len(response.content) > 0 and hasattr(response.content[0], 'input'): | |
| function_call = response.content[0].input | |
| return function_call | |
| else: | |
| raise Exception("No valid tool call found in the response") | |
| except Exception as e: | |
| raise Exception(f"Error calling Anthropic API: {str(e)}") | |