-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (67 loc) · 3.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from openai import OpenAI
# FastAPI app initialization
app = FastAPI(title="Mind Map Generator API")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# OpenAI API configuration
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise HTTPException(status_code=500, detail="OpenAI API key not found in environment variables")
openai_client = OpenAI(api_key=OPENAI_API_KEY)
# Pydantic model for request body
class MindMapRequest(BaseModel):
course_title: str
granularity_level: str
# Function to call OpenAI API
def call_openai_api(prompt: str) -> str:
try:
response = openai_client.chat.completions.create(
model="o1-preview",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
except Exception as e:
raise HTTPException(status_code=500, detail=f"OpenAI API call failed: {str(e)}")
# Function to generate mind map using OpenAI API
def generate_mindmap(request: MindMapRequest) -> str:
prompt = f"""
As an expert educator and curriculum designer, create a comprehensive and well-structured mind map for the course titled '{request.course_title}'. The mind map should be tailored according to the given granularity level ## {request.granularity_level} ##.
Guidelines for mind map creation:
1. Start with the course title as the central node.
2. Create main branches for major units or modules.
3. For each main branch, create sub-branches for topics within that unit.
4. Further break down topics into subtopics based on the desired level of detail ({request.granularity_level}).
5. Ensure that the content aligns with the specified course duration and is appropriate for the target audience.
6. Incorporate the key concepts and themes throughout the mind map.
7. Structure the content to support the achievement of the stated learning objectives.
8. Use concise, clear language for each node.
9. Maintain a logical flow and hierarchy in the mind map structure.
10. Aim for a balanced distribution of content across the main branches.
Present the mind map in Markdown format only, without additional explanations or text. Use proper indentation to represent the hierarchy. Do not use "```markdown" tags.
Ensure the mind map is comprehensive, accurate, and tailored to the specific course requirements provided. Only use the proper markdown format with hashes (#), etc. Don't use "```" anywhere. Start with markdown directly without ```.
"""
return call_openai_api(prompt)
# API endpoint to generate mind map
@app.post("/generate-mindmap/")
async def create_mindmap(request: MindMapRequest):
try:
markdown_content = generate_mindmap(request)
return {"markdown": markdown_content}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
# Root endpoint
@app.get("/")
async def root():
return {"message": "Welcome to the Mind Map Generator API"}