Spaces:
Running
Running
Create generate_with_claude.py
Browse files- generate_with_claude.py +88 -0
generate_with_claude.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_with_claude(text, api_key, course_name="", section_name="", lesson_name=""):
|
| 2 |
+
client = Anthropic(api_key=api_key)
|
| 3 |
+
|
| 4 |
+
segment_analysis_schema = TextSegmentAnalysis.model_json_schema()
|
| 5 |
+
|
| 6 |
+
tools = [
|
| 7 |
+
{
|
| 8 |
+
"name": "build_segment_analysis",
|
| 9 |
+
"description": "Build the text segment analysis with quiz questions",
|
| 10 |
+
"input_schema": segment_analysis_schema
|
| 11 |
+
}
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
system_prompt = """You are a helpful assistant specialized in text analysis and educational content creation.
|
| 15 |
+
You analyze texts to identify distinct segments, create summaries, and generate quiz questions."""
|
| 16 |
+
|
| 17 |
+
prompt = f"""Analyze the following text and identify distinct segments within it and do text segmentation:
|
| 18 |
+
1. Segments should be STRICTLY max=15
|
| 19 |
+
2. For each segment/topic you identify:
|
| 20 |
+
- Provide a SPECIFIC and UNIQUE topic name (3-5 words) that clearly differentiates it from other segments
|
| 21 |
+
- List 3-5 key concepts discussed in that segment (be precise and avoid repetition between segments)
|
| 22 |
+
- Write a brief summary of that segment (3-5 sentences)
|
| 23 |
+
- Create 5 high-quality, meaningful quiz questions based DIRECTLY on the content in that segment only
|
| 24 |
+
- Questions and answers should be only from the content of the segment
|
| 25 |
+
|
| 26 |
+
For each quiz question:
|
| 27 |
+
- Create cognitively demanding questions that test COMPREHENSION and ANALYSIS, not just recall
|
| 28 |
+
- Focus on CORE PRINCIPLES, key insights, and conceptual understanding
|
| 29 |
+
- Questions should require readers to think critically about the material
|
| 30 |
+
- AVOID superficial questions about statistics, numbers, or trivial details
|
| 31 |
+
- AVOID questions about the document structure or presentation
|
| 32 |
+
- Create one correct answer that comes DIRECTLY from the text
|
| 33 |
+
- Create two plausible but incorrect answers that require discernment
|
| 34 |
+
- Ensure all answer options have similar length (± 2 words)
|
| 35 |
+
- Ensure the correct answer is clearly indicated with a ✓ symbol
|
| 36 |
+
|
| 37 |
+
IMPORTANT QUIZ QUESTION CRITERIA:
|
| 38 |
+
- Questions should assess understanding of fundamental concepts and principles
|
| 39 |
+
- Questions should require application of knowledge, not just memorization
|
| 40 |
+
- Questions should focus on important ideas that have educational value
|
| 41 |
+
- Questions should be meaningful in an educational context
|
| 42 |
+
- Questions should challenge the reader to think about implications and connections
|
| 43 |
+
- No need for questions on the introduction, conclusion or the demonstration from the context
|
| 44 |
+
|
| 45 |
+
ADDITIONAL REQUIREMENT:
|
| 46 |
+
- **First, detect the language of the original text.**
|
| 47 |
+
- **Generate ALL output (topic names, key concepts, summaries, and quizzes) in the same language as the original text.**
|
| 48 |
+
- If the text is in Russian, generate all responses in Russian.
|
| 49 |
+
- If the text is in another language, generate responses in that original language.
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
Text:
|
| 53 |
+
{text}
|
| 54 |
+
|
| 55 |
+
Course information:
|
| 56 |
+
- Course name: {course_name}
|
| 57 |
+
- Section name: {section_name}
|
| 58 |
+
- Lesson name: {lesson_name}
|
| 59 |
+
|
| 60 |
+
- Do NOT repeat key concepts across multiple segments unless absolutely necessary.
|
| 61 |
+
- Ensure the quiz questions challenge the reader and are not easily guessable.
|
| 62 |
+
- FOCUS ON CONCEPTUAL UNDERSTANDING, not trivial facts or statistics.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
response = client.messages.create(
|
| 67 |
+
model="claude-3-5-sonnet-20241022",
|
| 68 |
+
max_tokens=8192,
|
| 69 |
+
temperature=0.7,
|
| 70 |
+
system=system_prompt,
|
| 71 |
+
messages=[
|
| 72 |
+
{
|
| 73 |
+
"role": "user",
|
| 74 |
+
"content": prompt
|
| 75 |
+
}
|
| 76 |
+
],
|
| 77 |
+
tools=tools,
|
| 78 |
+
tool_choice={"type": "tool", "name": "build_segment_analysis"}
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Extract the tool call content
|
| 82 |
+
if response.content and len(response.content) > 0 and hasattr(response.content[0], 'input'):
|
| 83 |
+
function_call = response.content[0].input
|
| 84 |
+
return function_call
|
| 85 |
+
else:
|
| 86 |
+
raise Exception("No valid tool call found in the response")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise Exception(f"Error calling Anthropic API: {str(e)}")
|