From d65470775146faa3baeeea3718a53aeb13248d29 Mon Sep 17 00:00:00 2001 From: teslim Date: Tue, 8 Jul 2025 15:20:10 +0100 Subject: [PATCH] multiple area tags --- src/api/routes/chatbot.py | 2 -- src/api/routes/questions.py | 4 ++++ src/models/questions_response.py | 19 ++++--------------- src/prompts/questions.py | 21 +++++++++++++++++++-- src/services/chatbot.py | 2 +- src/services/questions_generator.py | 24 ++++++++++++++++-------- src/utils/auth.py | 2 +- 7 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/api/routes/chatbot.py b/src/api/routes/chatbot.py index 256d431..13add1e 100644 --- a/src/api/routes/chatbot.py +++ b/src/api/routes/chatbot.py @@ -47,8 +47,6 @@ def validate_worker_document(): # Validate the worker's response using the provided document validation_result = chatbot.validate_worker(question, docs) - - print(validation_result) if not validation_result: return jsonify({"error": "Validation failed", "message": "Validation process failed."}), 400 diff --git a/src/api/routes/questions.py b/src/api/routes/questions.py index 17d5664..ca3b63e 100644 --- a/src/api/routes/questions.py +++ b/src/api/routes/questions.py @@ -80,6 +80,10 @@ def generate_questions_from_sop_v3(): "assessment_type": input_data['assessment_type'], "duration": input_data['duration'] } + + # Add area_tags if provided in the payload (optional) + if 'area_tags' in input_data: + generator_input['area_tags'] = input_data['area_tags'] # Generate questions using the QuestionGenerator generator = QuestionsGeneratorV2() diff --git a/src/models/questions_response.py b/src/models/questions_response.py index bd5cf53..d62233d 100644 --- a/src/models/questions_response.py +++ b/src/models/questions_response.py @@ -1,17 +1,3 @@ -'''from pydantic import BaseModel -from typing import List, Dict - -class Question(BaseModel): - assigned_to: str - role: str - questions: str - area_tag:str - -class AssementQuestion(BaseModel): - number: int - questions: List[Question]''' - - from pydantic import BaseModel from typing import List, Dict from typing import Optional @@ -33,5 +19,8 @@ class Questions(BaseModel): class AssessmentQuestions(BaseModel): questions: Questions +class AllQuestionsItems(BaseModel): + items: List[Question] + class AllQuestions(BaseModel): - questions : List[Question] \ No newline at end of file + questions: AllQuestionsItems \ No newline at end of file diff --git a/src/prompts/questions.py b/src/prompts/questions.py index 4f986da..e149a01 100644 --- a/src/prompts/questions.py +++ b/src/prompts/questions.py @@ -236,8 +236,25 @@ def get_questions_prompt_v5(): NOTE: !!! MAKE SURE YOU CORRECTLY ATTACH "assigned_to" AS THE ID OF THE MEMBER OF THE ROLE AS STATED IN THE SOP. CHECK MEMBERS UNDER THE ROLE IN THE PROVIDED SOP AND USE THE CORRECT ID OF THE MEMBER, DO NOT USE MEMBER iD THAT IS NOT PROVIDED AS "assigned_to" pls FOLLOW THIS STRICTLY!!! NOTE: CHECK THE "role_id" UNDER THE PROVIDED SOP AND USE THE CORRECT ID OF THE ROLE, DO NOT USE OR FORMULATE "role_id" THAT IS NOT PROVIDED AS "role" pls FOLLOW THIS STRICTLY!!! - NOTE: IF area tags is not provided for specicfic role SOPS, kindly formulate an area name based on the sop and make the area_tag null but forumlate rea name, only do this if area tags info is not provided for specific role sops - NOTE: Use exactly the area names provided if available unless area tags is missing and you need to forumalate one + NOTE: IF area tags is not provided for specific role SOPS, kindly formulate an area name based on the sop and make the area_tag null but formulate area name, only do this if area tags info is not provided for specific role sops + NOTE: Use exactly the area names provided if available unless area tags is missing and you need to formulate one + + CRITICAL AREA DIVERSIFICATION REQUIREMENT: + When area_tags are not provided or the area_tags array is empty, you MUST generate questions across AT LEAST 2-3 DIFFERENT area_names. Do NOT generate all questions under a single area_name. Analyze the SOPs and create diverse area_names such as: + - "Communication & Coordination" + - "Process Compliance" + - "Quality Assurance" + - "Timeline Management" + - "Technical Execution" + - "Safety Protocols" + - "Documentation & Reporting" + - "Resource Management" + - "Performance Monitoring" + - "Risk Assessment" + - "Training & Development" + - "Customer Relations" + + MANDATORY: Distribute your questions across these different area_names to ensure comprehensive coverage of the organizational processes. Each area_name should have multiple questions assigned to it. Do not concentrate all questions in one area - this defeats the purpose of comprehensive assessment. """ return prompt diff --git a/src/services/chatbot.py b/src/services/chatbot.py index 03e5994..af908d9 100644 --- a/src/services/chatbot.py +++ b/src/services/chatbot.py @@ -77,7 +77,7 @@ dummy_data = generate_dummy_data(num_assessments=100, max_users_per_assessment=5 #SopGeneratorDocument class Chatbot: def __init__(self): - self.api_key = os.getenv("OPENAI_API_KEY") or "sk-svcacct-v2m4jSLxCTLR-WizUhZnkOHEuftWNVy2k7vIGWDGJogaBr5VogTTVT3BlbkFJjCxOowETlz7muR8eAS7ExO1NA7kvcrZ4HVhS66jxK8PpvNce1kAAA" + self.api_key = os.getenv("OPENAI_API_KEY") self.client = OpenAI(api_key=self.api_key) self.model = "gpt-4o-mini" diff --git a/src/services/questions_generator.py b/src/services/questions_generator.py index d80b1dc..ca23dde 100644 --- a/src/services/questions_generator.py +++ b/src/services/questions_generator.py @@ -16,7 +16,6 @@ load_dotenv() class QuestionsGenerator: def __init__(self): self.api_key = os.getenv("OPENAI_API_KEY") - self.client = OpenAI(api_key=self.api_key) self.model = "gpt-4o-mini" def generate_questions(self, input_data: Dict) -> AssessmentQuestions: @@ -204,6 +203,7 @@ class QuestionsGeneratorV2: sops = input_data['sops'] assessment_type = input_data['assessment_type'] total_duration = input_data['duration'] + area_tags = input_data.get('area_tags', []) # Get area_tags if provided, default to empty list # Chunk the SOPs into smaller pieces @@ -211,16 +211,24 @@ class QuestionsGeneratorV2: docs_text = [sops[i:i + chunk_size] for i in range(0, len(sops), chunk_size)] docs = [{"type": "text", "text": text} for text in docs_text] + # Prepare messages for the API call + messages = [ + {"role": "system", "content": get_questions_prompt_v5()}, + {"role": "user", "content": f"The SOPs are provided below."}, + {"role": "user", "content": json.dumps(docs)}, + {"role": "user", "content": f"Assessment Type: {assessment_type}"}, + {"role": "user", "content": f"Duration: {total_duration}"} + ] + + # Add area_tags information if provided + if area_tags: + messages.append({"role": "user", "content": f"Available Area Tags: {json.dumps(area_tags)}"}) + else: + messages.append({"role": "user", "content": "Area Tags: Not provided (empty array) - Please generate questions across multiple diverse area_names as instructed."}) response = self.client.beta.chat.completions.parse( model=self.model, - messages=[ - {"role": "system", "content": get_questions_prompt_v5()}, - {"role": "user", "content": f"The SOPs are provided below."}, - {"role": "user", "content": json.dumps(docs)}, - {"role": "user", "content": f"Assessment Type: {assessment_type}"}, - {"role": "user", "content": f"Duration: {total_duration}"} - ], + messages=messages, temperature=0.1, response_format=AllQuestions, # Ensure you specify the correct format max_tokens=6000 diff --git a/src/utils/auth.py b/src/utils/auth.py index 3738283..043af9b 100644 --- a/src/utils/auth.py +++ b/src/utils/auth.py @@ -4,7 +4,7 @@ from flask import Flask, session, redirect, url_for, request, g, jsonify from dotenv import load_dotenv load_dotenv() -API_KEY = os.getenv("API_KEY") or "erp_3bb0e4be8508494bb23621fc10e76a7d" +API_KEY = os.getenv("API_KEY") def auth_check(func): @wraps(func)