DEV Community

Cover image for AI-Powered MCQs in a Flash! Generate Perfect Questions with Lyzr and OpenAI
harshit-lyzr
harshit-lyzr

Posted on

AI-Powered MCQs in a Flash! Generate Perfect Questions with Lyzr and OpenAI

In today's dynamic learning landscape, educators constantly seek innovative ways to streamline assessment creation. While crafting high-quality multiple-choice questions (MCQs) is essential, it can be a time-consuming burden. This is where the Lyzr SDK emerges as a game-changer, harnessing the power of Generative AI to revolutionize MCQ generation.

Lyzr SDK: Designed for Ease of Use
Lyzr stands out among Generative AI frameworks for its agentic approach. Unlike competitors that require deep programming knowledge, Lyzr's SDK empowers users to leverage Generative AI with minimal coding expertise. This is thanks to Lyzr's agentic approach, which simplifies complex functionalities behind the scenes.

Benefits of Utilizing Lyzr SDK for Effortless MCQ Creation
Boost Educator Productivity: By automating MCQ creation with minimal setup, Lyzr frees up valuable educator time, allowing them to focus on lesson planning, personalized feedback, and other strategic aspects.
Enhance Assessment Quality: Lyzr ensures MCQs target the most crucial concepts, leading to assessments that accurately measure learning outcomes.
Promote Active Learning: Well-designed MCQs generated by Lyzr can stimulate critical thinking and deeper understanding of the subject matter.
Scalability and Efficiency: The Lyzr SDK streamlines MCQ creation for large-scale courses or educational institutions, ensuring consistency and efficiency.

Setting Up the Environment

from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from dotenv import load_dotenv
import os
Enter fullscreen mode Exit fullscreen mode

The code utilizes the following libraries:
lyzr_automata: Provides functionalities to interact with Lyzr's SDK
OpenAIModel: This class from lyzr_automata interacts with OpenAI's GPT-4 model.
Agent: This class from lyzr_automata defines the role of the AI agent.
Task: This class from lyzr_automata defines the specific task for the agent (e.g., generating MCQs).
LinearSyncPipeline: This class from lyzr_automata orchestrates the communication between the Agent and the Model.

OpenAI Configuration:
load_dotenv loads the OpenAI API key stored in a separate .env file (for security reasons).
api stores the retrieved API key.
open_ai_text_completion_model is an instance of OpenAIModel that defines the AI model to be used (GPT-4 in this case) along with its parameters like temperature and maximum tokens.

load_dotenv()
api = os.getenv("OPENAI_API_KEY")

open_ai_text_completion_model = OpenAIModel(
    api_key=api,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
Enter fullscreen mode Exit fullscreen mode

Defining the Lyzr Agent:
ielts_agent is an instance of the Agent class. It defines the role of the AI agent and sets the prompt persona that guides the agent on its task. The prompt persona instructs the agent to develop MCQs with answers on the entered topic.

topic = "Organic Chemistry"
mcq_agent = Agent(
    role="Ielts expert",
    prompt_persona=f"Your task is to DEVELOP a MULTIPLE-CHOICE QUESTION (MCQ) about {topic} and also give its answers"
)
Enter fullscreen mode Exit fullscreen mode

Defining the Lyzr Task:
mcq_task is an instance of the Task class. It specifies the task details:
name: A name for the task ("get MCQ").
model: The AI model to be used (referencing the open_ai_text_completion_model instance).
agent: The AI agent to interact with the model (referencing the ielts_agent instance).
instructions: Specific instructions for the agent ("Give MCQ Questions with answers").

mcq_task  =  Task(
   name="get MCQ",
   model=open_ai_text_completion_model,
   agent=mcq_agent,
   instructions="Give MCQ Questions with answers",
 )
Enter fullscreen mode Exit fullscreen mode

Running the Pipeline:
LinearSyncPipeline is instantiated, orchestrating the communication between the agent and the model.
name: A name for the pipeline ("MCQ details").
completion_message: A message displayed after the pipeline finishes ("pipeline completed").
tasks: A list containing the task to be executed (referencing the mcq_task instance).
output = pipeline.run() executes the pipeline.

output = LinearSyncPipeline(
        name="MCQ details",
            # completion message after pipeline completes
        completion_message="pipeline completed",
        tasks=[
                    # tasks are instance of Task class
             mcq_task # Task C
        ],
    ).run()
Enter fullscreen mode Exit fullscreen mode
print(output[0]['task_output'])
Enter fullscreen mode Exit fullscreen mode

This code demonstrates how to leverage Lyzr SDK and Streamlit to create a user-friendly MCQ generator. By entering a topic, users can obtain a set of MCQs, making the process of assessment creation efficient and effortless. Remember, this is a simplified example. Lyzr offers more advanced functionalities that can be explored for further customization.

Lyzr.ai – Medium

Read writing from Lyzr.ai on Medium. Every day, Lyzr.ai and thousands of other voices read, write, and share important stories on Medium.

favicon lyzr.medium.com



For more information explore the website: Lyzr

Generate Perfect Questions with Lyzr and OpenAI - Github

Top comments (0)