quiz fixed

This commit is contained in:
2025-02-12 19:25:12 +00:00
parent d1ed8b9e3f
commit 35a099112b
4 changed files with 261 additions and 10 deletions
+119 -5
View File
@@ -2,7 +2,8 @@ import os
import requests
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
@@ -22,7 +23,7 @@ load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
llm_temp = ChatOpenAI(model="gpt-4o-mini", temperature=0.7,max_tokens=10000)
def generate_quiz(startpop_pdf, quiz_type=None) -> dict:
def generate_quiz1(startpop_pdf, quiz_type=None) -> dict:
try:
# Define the prompt template for generating the quiz
quiz_prompt = PromptTemplate(
@@ -32,13 +33,13 @@ You are an assistant designed to generate firefighter interview quizzes based on
### Quiz Types:
1. **Single Line Text Inputs**:
- Output format: `{"question": "Your question", "correct_answer": "Your correct answer"}`
- Output format: {{{{"question": "Your question", "correct_answer": "Your correct answer"}}}}
2. **Multiple Choice Questions**:
- Output format: `{"question": "Your question", "options": ["Option 1", "Option 2"], "correct_answer": "Correct Option"}`
- Output format: "{"question": "Your question", "options": ["Option 1", "Option 2"], "correct_answer": "Correct Option"}"
3. **True or False Questions**:
- Output format: `{"question": "Your question", "options": ["True", "False"], "correct_answer": "True or False"}`
- Output format: "{"question": "Your question", "options": ["True", "False"], "correct_answer": "True or False"}"
Each quiz must include a field called `"quiz_type"` with values `1`, `2`, or `3` corresponding to the quiz type.
@@ -122,3 +123,116 @@ QUIZ TYPE: {quiz_type}
return {}
import os
import json
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
# Load API key
api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=api_key) # Initialize OpenAI client
def generate_quiz(startpop_pdf: str, quiz_type: int) -> dict:
try:
# Define the system prompt
system_prompt = """
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are an assistant designed to generate firefighter interview quizzes based on the STARTPOP framework provided in a PDF document. Your task is to analyze the content of the PDF and create a quiz tailored to the specified quiz type.
### Quiz Types:
1. **Single Line Text Inputs**:
- Output format: "[{"question": "Your question", "correct_answer": "Your correct answer"}]"
2. **Multiple Choice Questions**:
- Output format: "[{"question": "Your question", "options": ["Option 1", "Option 2"], "correct_answer": "Correct Option"}]"
3. **True or False Questions**:
- Output format: "[{"question": "Your question", "options": ["True", "False"], "correct_answer": "True or False"}]"
Each quiz must include a field called `"quiz_type"` with values `1`, `2`, or `3` corresponding to the quiz type.
### Project Overview:
### Key Concepts in Firefighting:
Firefighter interviews evaluate candidates based on **7 Main Concepts** and **20 Important Themes**. These are used to assess alignment with firefighting principles, communication skills, problem-solving abilities, and overall competence.
#### 7 Main Concepts:
- High Performance Teams
- Situational Awareness
- Being a Great Problem Solver
- Customer Service
- Building Construction & Mechanical Aptitude
- Emergency Medicine Experience
- Mental & Physical Health
#### 20 Important Themes:
- Customer Service
- Conflict
- Challenge
- Leadership
- Stress
- Successful Team
- Diversity
- Mistake
- Unsuccessful Team
- Disagreement
- Bent a Rule
- Delivered a Difficult Message
- Displayed Integrity
- Took a Shortcut
- Didnt Follow the Rules
- Emergency Response
- Dealt with Disabilities
- Solved a Big Problem
- Continuous Improvement
- Handled Sensitive Information
#### STARTPOP Framework:
The STARTPOP framework enhances the STAR method by adding depth and variety to responses. Its components are:
1. **Situation**: Set up the context (dates, places, circumstances).
2. **Task**: Explain what needed to be done and why.
3. **Actions**: Outline both positive and negative approaches.
4. **Results**: Share outcomes in a time-specific manner.
5. **Transitions**: Ensure professional coherence.
6. **Personal Lessons**: Discuss what you learned.
7. **Other People Observations**: Share insights about others involved.
8. **Professional Connection**: Relate the experience to firefighting.
### Instructions:
- Analyze the provided STARTPOP PDF to extract relevant themes and concepts.
- Generate a quiz with x number of questions and be dynyamic that builds user confidence by focusing on interview-based scenarios.
- Avoid questions directly about the STARTPOP framework itself (e.g., "What is STARTPOP?").
- Use the specified quiz type (`quiz_type`) to determine the output format.
- Generate good number of questions for the complete quiz please, ay least 15 questions
- avoid putting ```json before or after text ,return only the json output....
NOTE !!! : THE PDF REFERENCES THE STARTPOP FRAME WORK FOR THE CURRENT USER THAT HAS BEEN FORMUALTED, BASED ON THIS YOU CREATE QUIZ FOR THE USER TO PREPARE FOR THE INTERVIEW, IT IS NOT AS IF YOU ARE JUST EXTRACTING QUESTIONS FIRECTLY FROM THIS PDF USING IT AS CONTEXT TO CREATE THE RIGHT QUIZ
NOTE !!: ```json before or after text ,return only the json output.... and stritcly follow the outoput format spoecificed above , do not change it
<|eot_id|><|start_header_id|>user<|end_header_id|>
""",
# OpenAI API call
response = client.chat.completions.create(
model="gpt-4o-mini",
temperature=0.7,
messages=[
{"role": "system", "content": f"{system_prompt}"},
{"role": "user", "content": f"STARTPOP FULL PDF: {startpop_pdf}"},
{"role": "user", "content": f"QUIZ TYPE: {quiz_type}"}
]
)
# Extract JSON response
print(response)
return json.loads(response.choices[0].message.content) # Already JSON formatted
except Exception as e:
print(f"Error: {e}")
return {}