Download src/app/api/feynman/teach/route.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/feynman/teach/route.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/feynman/teach/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/feynman/teach/route.ts
3.51 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { claude, parseClaudeJson } from "@/lib/claude"; | |
| import { withApiError } from "@/lib/api-error"; | |
| import { getGroundedTeaching } from "@/lib/sources/curate"; | |
| import { FEYNMAN_TOPICS } from "@/lib/feynman/topics"; | |
| interface TeachBody { | |
| topic: string; | |
| topicId?: string; | |
| difficulty?: string; | |
| } | |
| export const POST = withApiError(async (request: NextRequest) => { | |
| if (!process.env.ANTHROPIC_API_KEY) { | |
| return NextResponse.json( | |
| { error: "ANTHROPIC_API_KEY is not configured" }, | |
| { status: 503 }, | |
| ); | |
| } | |
| let body: TeachBody; | |
| try { | |
| body = await request.json(); | |
| } catch { | |
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | |
| } | |
| const { topic, topicId, difficulty } = body; | |
| if (!topic || typeof topic !== "string") { | |
| return NextResponse.json({ error: "topic is required" }, { status: 400 }); | |
| } | |
| // Attempt grounded teaching if we have a topicId | |
| if (topicId) { | |
| const knownTopic = FEYNMAN_TOPICS.find((t) => t.id === topicId); | |
| if (knownTopic) { | |
| try { | |
| const grounded = await getGroundedTeaching(topicId, topic, difficulty, knownTopic.domain); | |
| if (grounded) { | |
| return NextResponse.json({ | |
| topic, | |
| explanation: grounded.explanation, | |
| keyTakeaways: grounded.keyTakeaways, | |
| grounded: true, | |
| confidenceScore: grounded.confidenceScore, | |
| sources: grounded.sources.map((s) => ({ | |
| url: s.url, | |
| title: s.title, | |
| domain: s.domain, | |
| license: s.license, | |
| })), | |
| }); | |
| } | |
| } catch { | |
| // Grounded teaching failed — fall through to ungrounded generation | |
| } | |
| } | |
| } | |
| // Fallback: ungrounded Claude generation (original behavior) | |
| const difficultyCtx = difficulty | |
| ? ` The topic is considered ${difficulty}-level.` | |
| : ""; | |
| try { | |
| const message = await claude.messages.create({ | |
| model: "claude-haiku-4-5-20251001", | |
| max_tokens: 1500, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: `You are a brilliant teacher. A student has encountered the topic "${topic}" and doesn't know it yet.${difficultyCtx} | |
| Write a clear, engaging explanation that: | |
| 1. Starts with the core idea in one sentence | |
| 2. Builds understanding step by step (2-3 paragraphs) | |
| 3. Uses a concrete analogy or real-world example | |
| 4. Highlights the 2-3 most important takeaways | |
| Keep it accessible — a motivated beginner should be able to understand this. Be concise but thorough. Do NOT use bullet points for the main explanation — write in flowing prose. You may use a short bullet list only for the key takeaways at the end. | |
| Respond ONLY in valid JSON (no markdown, no code fences): | |
| { "explanation": "...", "keyTakeaways": ["...", "...", "..."] }`, | |
| }, | |
| ], | |
| }); | |
| const text = | |
| message.content[0].type === "text" ? message.content[0].text : ""; | |
| const parsed = parseClaudeJson(text) as { | |
| explanation: string; | |
| keyTakeaways: string[]; | |
| }; | |
| return NextResponse.json({ | |
| topic, | |
| explanation: parsed.explanation, | |
| keyTakeaways: parsed.keyTakeaways, | |
| grounded: false, | |
| sources: [], | |
| }); | |
| } catch (err) { | |
| return NextResponse.json( | |
| { | |
| error: "Failed to generate teaching content", | |
| details: err instanceof Error ? err.message : String(err), | |
| }, | |
| { status: 500 }, | |
| ); | |
| } | |
| }); | |