So basically I have had made a notes generator. First I have had generated the chapters of respective subject and then after that I used those generated chapters to generate notes and mcq questions according to the aspiration of the student and then I am saving it in a text file.
Now I want to convert it into json format instead of text format. I am using OpenAI function calling for that but I am not getting the desired output.
Please can anyone help me with that!!
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
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."}
],
temperature=1,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
def generate_notes_and_mcqs(subject, chapter, aspiration):
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 and MCQ questions on {chapter} of {subject} for {aspiration}."}
],
temperature=1,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
class_to_generate = "8th class"
aspiration = "I want to become a doctor"
syllabus_content = generate_syllabus(class_to_generate)
# print(syllabus_content)
syllabus = {}
current_subject = None
for line in syllabus_content.split('n'):
if not line.strip():
continue
if line.startswith("Subject:"):
current_subject = line.replace("Subject:", "").strip()
syllabus[current_subject] = []
elif current_subject:
syllabus[current_subject].append(line.strip())
output_file = "main2.txt"
with open(output_file, 'a') as file:
for subject, chapters in syllabus.items():
file.write(f"Subject: {subject}n")
print(f"Subject: {subject}n")
for chapter in chapters:
content = generate_notes_and_mcqs(subject, chapter, aspiration)
content_lines = content.split('n')
if content_lines[0].startswith(chapter):
content_lines = content_lines[1:]
content = 'n'.join(content_lines)
file.write(f"{chapter}:n{content}nn")
print(f"{chapter}:n{content}nn")