Notes Generator using langchain

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật