File size: 2,365 Bytes
3ca2bf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import json
import langchain
import pandas as pd
from openai import OpenAI
from langchain_openai import OpenAI, ChatOpenAI
import os
from dotenv import load_dotenv
from src.mcqgen.utils import read_file, get_table_data
from src.mcqgen.logger import logging

from langchain.llms import openai
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
from langchain.callbacks import get_openai_callback



load_dotenv()

key=os.getenv("OPENAI_API_KEY")


llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7)



Template='''
Text:{text}
You are an expert MCQ maker. Given the above text, it is your job to create a quiz of {number} multiple 
choice questions for {subject} in {tone} tone. Make sure that the questions are not repeated and check all
question to be confirming the text as well. Make sure to format your responses like {response_json} and use
it as a guide     
'''

quiz_generation_prompt=PromptTemplate(
    input_variables=["text", "number", "subject", "tone", "respone_json"],
    template=Template
)

quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key="quiz", verbose=True)


Template2="""
You are an expert english grammarian and a writer. Given a multiple choice Quiz for {subject} students,
you need to evaluate  the complexity of the quiz and give a complete analysis of the quiz. Only use 50 words
at max. If the quiz is not at par the with the cognitive and the analytical ability of the student, update
the quiz questions and change the tone such that it perfectly fits the student's analytical ability.
Quiz_MCQ:
{quiz}
"""

quiz_evaluation_prompt=PromptTemplate(input_variables=['subject','quiz'], template=Template2)
review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True)

generate_evaluate_chain=SequentialChain(chains=[quiz_chain,review_chain], input_variables=["text", "number", "subject", "tone", "response_json"], output_variables=['quiz', 'review'], verbose=True)

# number =6
# subject="Machine Learning"
# tone="simple"

# with get_openai_callback() as cb:
#     response=generate_evaluate_chain(
#         {   
#             "text":text,
#             "number":number,
#             "subject":subject,
#             "tone":tone,
#             "response_json":json.dumps(response_json)
#         }
# )