Basically I am trying to build a notes and mcq generator.
The workflow that I have had followed is :
First I have had generated the syllabus of a class(here class 8th) and then using that generated syllabus I have had generated notes and mcq questions.
So this is the code that I have had written ->
import os
import ast
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
class_to_generate = "8th class"
def generate_syllabus(class_grade):
response = client.chat.completions.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a teacher."},
{"role": "user", "content": f"Generate a list of subjects and chapters for {class_grade} grade in a dictionary format."}
],
temperature=1,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
syllabus_str = generate_syllabus(class_to_generate)
syllabus = ast.literal_eval(syllabus_str)
def generate_notes(subject, chapter):
response = client.chat.completions.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a teacher."},
{"role": "user", "content": f"Write notes (only key points) on {chapter} of {subject}."}
],
temperature=1,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
def generate_mcqs(subject, chapter):
response = client.chat.completions.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "system", "content": "You are a teacher."},
{"role": "user", "content": f"Write MCQ questions on {chapter} of {subject}."}
],
temperature=1,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
note = ""
mcq = ""
for subject, chapters in syllabus.items():
for chapter in chapters:
notes = generate_notes(subject, chapter)
mcqs = generate_mcqs(subject, chapter)
note += f"Subject: {subject}nChapter: {chapter}nNotes:n{notes}nn"
mcq += f"Subject: {subject}nChapter: {chapter}nMCQs:n{mcqs}nn"
def split_text(text, max_tokens=2048):
chunks = []
while len(text) > 0:
if len(text) > max_tokens:
split_index = text[:max_tokens].rfind('n')
if split_index == -1:
split_index = max_tokens
else:
split_index = len(text)
chunks.append(text[:split_index])
text = text[split_index:]
return chunks
def call_openai_function(prompt):
chunk_size = 2000
chunks = [prompt[i:i+chunk_size] for i in range(0, len(prompt), chunk_size)]
notes_outputs = []
mcqs_outputs = []
for chunk in chunks:
completion = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": chunk}],
)
message_content = completion.choices[0].message.content
if "Notes" in prompt:
notes_outputs.append(message_content)
if "MCQs" in prompt:
mcqs_outputs.append(message_content)
return notes_outputs, mcqs_outputs
notes_of_syllabus_prompt = f"Extract notes from the text:nn{note}"
mcqs_of_syllabus_prompt = f"Extract mcq questions from the text:nn{mcq}"
notes_of_syllabus, _ = call_openai_function(notes_of_syllabus_prompt)
_, mcqs_of_syllabus = call_openai_function(mcqs_of_syllabus_prompt)
output_json = {
"Notes": notes_of_syllabus,
"MCQS": mcqs_of_syllabus
}
print(output_json)
formatted_output_json = {
"Notes": {
"content": None,
"role": "assistant",
"function_call": {
"arguments": f'{{n "Notes": "{output_json["Notes"]}"n}}',
"name": "fun"
}
},
"MCQS": {
"content": None,
"role": "assistant",
"function_call": {
"arguments": f'{{n "MCQS": "{output_json["MCQS"]}"n}}',
"name": "fun"
}
}
}
print(formatted_output_json)
My final output is in format ->
{'Notes': {'content': None, 'role': 'assistant', 'function_call': {'arguments': '{n "Notes": "['on method.\n- Quadratic equations involve variables raised .......
But I want my output to be in format ->
Example of expected format :
{
"Mathematics": [
{
"chapter": "Rational Numbers",
"topics": [
{
"topic": "Numbers",
"notes": [
"dcdkfd"
],
"mcqs":[
"dfvfdv"
]
}
]
}
]
}
I have had tried to use function calling but I still wasn’t able to solve it. Please can anyone help me with this question.