ds apis implemneted

This commit is contained in:
2025-02-06 20:12:43 +00:00
parent 58e0cbfa3c
commit 4cd9aeac51
17 changed files with 5612 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
# Python bytecode
__pycache__/
*.py[cod]
# Distribution / packaging
.Python
env/
venv/
ENV/
env.bak/
venv.bak/
*.egg-info/
dist/
build/
# IDEs and editors
.vscode/
.idea/
*.swp
*.swo
# Jupyter Notebook checkpoints
.ipynb_checkpoints
# Pytest cache
.cache
# Coverage reports
htmlcov/
.coverage
.coverage.*
.cache
nosetests.xml
test-results/
# MyPy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# C extensions
*.so
# Data files
*.csv
*.dat
*.db
*.sqlite3
# Logs
*.log
# Environment variables
.env
+353
View File
@@ -0,0 +1,353 @@
import os
from typing import Optional
from fastapi import FastAPI, HTTPException, Security, Depends
from fastapi.security import APIKeyHeader
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from dotenv import load_dotenv
from utils.document_loader import load_document
import json
from pydantic import BaseModel
from src.llm import ai_chat
from langchain_openai import ChatOpenAI
import requests
import tempfile
from scripts.generate_pdf import create_pdf
from scripts.generate_theme import generate_theme
from scripts.generate_quiz import generate_quiz
from typing import Dict, Any
from fastapi.responses import Response
from datetime import datetime
from fastapi import HTTPException
from pydantic import BaseModel
from typing import Optional, Union, Dict, Any
import os
import requests
import os
from PyPDF2 import PdfReader
from config import QUIZ_TYPES
# Load environment variables
load_dotenv()
API_KEY = os.getenv("API_KEY_ACCESS")
base_path = os.path.join("data", "config_files")
QUESTIONS_PATH = os.path.join(base_path, "questions.json")
THEME_CONTEXT_PATH = os.path.join(base_path, "theme_context.json")
# Load themes at module level
with open(THEME_CONTEXT_PATH, "r") as f:
themes = json.load(f)
# Initialize FastAPI app
app = FastAPI(
title="Fire Fighter Interview API",
description="API For fire fighter",
version="1.0.0"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Setup API key authentication
api_key_header = APIKeyHeader(name="Authorization", auto_error=False)
async def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
"""Validate API key from header"""
if not api_key_header or not api_key_header.startswith('Bearer '):
raise HTTPException(
status_code=401,
detail={"error": "Unauthorized", "message": "API key is missing or invalid."}
)
token = api_key_header.split(' ')[1]
if token != API_KEY:
raise HTTPException(
status_code=401,
detail={"error": "Unauthorized", "message": "API key does not match."}
)
return token
class ChatRequest(BaseModel):
resume_url: Optional[str] = None
query: str=None
conversation_id: str
theme_id: Optional[int] = 1
class ChatResponse(BaseModel):
message: str
end: bool
error: Optional[str] = None
class GeneratePDFRequest(BaseModel):
conversation_id: str
feedback: Optional[str] = None
previous_results: Optional[Dict[str, Any]] = None
resume_url: Optional[str] = None
full_history_url: Optional[str] = None
class QuizRequest(BaseModel):
pdf_url: str
quiz_type: int # 1, 2, or 3 corresponding to QUIZ_TYPES
class QuizResponse(BaseModel):
success: bool
message: str
quiz_data: Optional[Dict[str, Any]] = None
error: Optional[str] = None
async def extract_pdf_text(pdf_url: str) -> Union[str, None]:
"""Extract text from PDF and handle potential errors."""
try:
response = requests.get(pdf_url)
response.raise_for_status()
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_pdf:
temp_pdf.write(response.content)
temp_path = temp_pdf.name
# Extract text from PDF
reader = PdfReader(temp_path)
text = "\n\n".join(
page.extract_text() for page in reader.pages if page.extract_text()
)
# Clean up temporary file
os.unlink(temp_path)
if not text.strip():
return None
return text
except requests.RequestException as e:
raise HTTPException(
status_code=400,
detail=f"Error downloading PDF: {str(e)}"
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error processing PDF: {str(e)}"
)
@app.post("/rescue-career/chat", response_model=ChatResponse)
async def chat_endpoint(
request: ChatRequest,
api_key: str = Depends(get_api_key)
):
try:
# Validate theme
matching_themes = [t for t in themes if t["id"] == request.theme_id]
if not matching_themes:
raise HTTPException(
status_code=400,
detail=f"No theme found with ID {request.theme_id}"
)
# Only try to load document if resume_url is provided
resume_docs = ""
if request.resume_url:
docs = load_document(request.resume_url)
if not docs:
raise HTTPException(
status_code=400,
detail="Invalid resume URL: Unable to fetch document"
)
resume_docs = "\n".join(f"- {doc.page_content}" for doc in docs)
# Get AI chat response
response = ai_chat(
query=request.query,
conversation_id=request.conversation_id,
theme_id=request.theme_id,
resume=resume_docs
)
# Parse response
try:
parsed_response = json.loads(response)
return ChatResponse(
message=parsed_response.get("message", ""),
end=parsed_response.get("end", "no") == "yes",
error=None
)
except json.JSONDecodeError:
return ChatResponse(
message=response,
end=False,
error=None
)
except HTTPException as e:
# Re-raise HTTP exceptions
raise
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error processing chat request: {str(e)}"
)
@app.post("/rescue-career/generate-theme")
async def generate_pdf_endpoint(
request: GeneratePDFRequest,
api_key: str = Depends(get_api_key)
):
try:
# Here you would fetch the conversation data using the conversation_id
# This is a placeholder - replace with your actual conversation data fetching logic
conversation_data = await get_conversation_data(request.conversation_id)
if not conversation_data:
raise HTTPException(
status_code=404,
detail=f"No conversation found with ID {request.conversation_id}"
)
resume_docs = ""
if request.resume_url:
docs = load_document(request.resume_url)
if not docs:
raise HTTPException(
status_code=400,
detail="Invalid resume URL: Unable to fetch document"
)
resume_docs = "\n".join(f"- {doc.page_content}" for doc in docs)
full_history_docs = ""
if request.full_history_url:
docs = load_document(request.full_history_url)
if not docs:
raise HTTPException(
status_code=400,
detail="Invalid full_history URL: Unable to fetch document"
)
full_history_docs = "\n".join(f"- {doc.page_content}" for doc in docs)
# Generate theme data using the generate_theme function
theme_data = generate_theme(
conversation_data=conversation_data,
feedback=request.feedback,
previous_result=request.previous_results,
resume = resume_docs,
full_history = full_history_docs
)
if not theme_data:
raise HTTPException(
status_code=500,
detail="Failed to generate theme data"
)
# Generate the PDF using the create_pdf function
pdf_content = create_pdf(theme_data)
# Create filename with timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"theme_{timestamp}.pdf"
# Return the PDF as a response
return Response(
content=pdf_content,
media_type="application/pdf",
headers={
"Content-Disposition": f'attachment; filename="{filename}"'
}
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error generating PDF: {str(e)}"
)
@app.post("/rescue-career/generate-quiz", response_model=QuizResponse)
async def generate_quiz_endpoint(
request: QuizRequest,
api_key: str = Depends(get_api_key)
):
"""Generate quiz based on PDF content and quiz type."""
# Validate quiz type
if request.quiz_type not in QUIZ_TYPES:
raise HTTPException(
status_code=400,
detail=f"Invalid quiz type. Must be one of: {list(QUIZ_TYPES)}"
)
try:
# Extract text from PDF
pdf_text = await extract_pdf_text(request.pdf_url)
if not pdf_text:
return QuizResponse(
success=False,
message="PDF extraction completed but no text content found",
error="Empty PDF content"
)
# Generate quiz using the existing function
quiz_data = generate_quiz(
startpop_pdf=pdf_text,
quiz_type=request.quiz_type
)
if not quiz_data:
return QuizResponse(
success=False,
message="Quiz generation failed",
error="Unable to generate quiz from the provided content"
)
return QuizResponse(
success=True,
message="Quiz generated successfully",
quiz_data=quiz_data
)
except HTTPException as he:
raise he
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Unexpected error during quiz generation: {str(e)}"
)
async def get_conversation_data(conversation_id: str) -> dict:
"""
Fetch conversation data using the conversation ID.
Replace this with your actual implementation to fetch conversation data.
"""
try:
storage_path = "conversations.json"
with open(storage_path, 'r') as f:
convs = json.load(f)
convs_id = convs[conversation_id]
return convs_id
except Exception as e:
print(f"Error fetching conversation data: {e}")
return None
@app.on_event("startup")
async def startup_event():
"""Initialize required components on startup"""
pass
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=5048, reload=True)
+20
View File
@@ -0,0 +1,20 @@
QUIZ_TYPES = {
1: {
"name": "Single Line Text Inputs",
"format": """
{"question": "Your question here", "correct_answer": "Your correct answer here"}
"""
},
2: {
"name": "Multiple Choice Questions",
"format": """
{"question": "Your question here", "options": ["Option 1", "Option 2", "Option 3"], "correct_answer": "Correct Option"}
"""
},
3: {
"name": "True or False Questions",
"format": """
{"question": "Your question here", "options": ["True", "False"], "correct_answer": "True or False"}
"""
}
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+112
View File
@@ -0,0 +1,112 @@
%PDF-1.4
%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com
1 0 obj
<<
/F1 2 0 R /F2 3 0 R
>>
endobj
2 0 obj
<<
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font
>>
endobj
3 0 obj
<<
/BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding /Name /F2 /Subtype /Type1 /Type /Font
>>
endobj
4 0 obj
<<
/Contents 10 0 R /MediaBox [ 0 0 612 792 ] /Parent 9 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
5 0 obj
<<
/Contents 11 0 R /MediaBox [ 0 0 612 792 ] /Parent 9 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
6 0 obj
<<
/Contents 12 0 R /MediaBox [ 0 0 612 792 ] /Parent 9 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
7 0 obj
<<
/PageMode /UseNone /Pages 9 0 R /Type /Catalog
>>
endobj
8 0 obj
<<
/Author (\(anonymous\)) /CreationDate (D:20250120220756+00'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20250120220756+00'00') /Producer (ReportLab PDF Library - www.reportlab.com)
/Subject (\(unspecified\)) /Title (\(anonymous\)) /Trapped /False
>>
endobj
9 0 obj
<<
/Count 3 /Kids [ 4 0 R 5 0 R 6 0 R ] /Type /Pages
>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R.@*lpE[lnK4#*$mT`G:7fIbl5enEfb'S6D\K(%@&4#89hiM:X_h@RO@;]taMW=&RdA:Q6lT4V3`,Sg3-L)sBRqVR1bMg"9e($[sf5F@~>endstream
endobj
xref
0 13
0000000000 65535 f
0000000073 00000 n
0000000114 00000 n
0000000221 00000 n
0000000333 00000 n
0000000527 00000 n
0000000721 00000 n
0000000915 00000 n
0000000983 00000 n
0000001266 00000 n
0000001337 00000 n
0000002900 00000 n
0000004363 00000 n
trailer
<<
/ID
[<4b0a13e77a14658d2c9c7ba2786bbc6e><4b0a13e77a14658d2c9c7ba2786bbc6e>]
% ReportLab generated PDF document -- digest (http://www.reportlab.com)
/Info 8 0 R
/Root 7 0 R
/Size 13
>>
startxref
5681
%%EOF
File diff suppressed because one or more lines are too long
+23
View File
@@ -0,0 +1,23 @@
openai
pandas
python-dotenv
fastapi
uvicorn
langchain-community
langchain-openai
pydantic
pypdf
pypandoc
Spire.Doc
plum-dispatch==1.7.4
scikit-learn
werkzeug
python-multipart
langgraph
tiktoken
langchainhub
chromadb
langchain
langchain-text-splitters
beautifulsoup4
langchain-core
+101
View File
@@ -0,0 +1,101 @@
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_CENTER, TA_LEFT
from io import BytesIO
def create_pdf(data, output_filename=None):
try:
# Create a BytesIO buffer to store the PDF
buffer = BytesIO()
# Create the PDF document using the buffer
doc = SimpleDocTemplate(buffer, pagesize=letter)
styles = getSampleStyleSheet()
# All content will use the same font size
STANDARD_FONT_SIZE = 12
# Create custom styles with consistent font size
styles.add(ParagraphStyle(
name='ThemeTitle',
fontSize=STANDARD_FONT_SIZE,
alignment=TA_CENTER,
spaceAfter=15,
fontName='Helvetica-Bold', # Bold style
leading=14 # Line spacing (1.0)
))
styles.add(ParagraphStyle(
name='QuestionTitle',
fontSize=STANDARD_FONT_SIZE,
alignment=TA_LEFT,
spaceAfter=10,
fontName='Helvetica-Bold',
leading=14,
textColor=colors.black
))
styles.add(ParagraphStyle(
name='SectionTitle',
fontSize=STANDARD_FONT_SIZE,
alignment=TA_LEFT,
spaceAfter=4,
fontName='Helvetica-Bold',
leading=14
))
styles.add(ParagraphStyle(
name='NormalText',
fontSize=STANDARD_FONT_SIZE,
alignment=TA_LEFT,
spaceAfter=2,
leftIndent=20,
leading=14,
fontName='Helvetica' # Regular font
))
# Build the document content
story = []
# Add theme title on first page
if data:
theme_title = data.get('theme_title', 'No Title Provided')
story.append(Paragraph(f"THEME: {theme_title.upper()}", styles['ThemeTitle']))
story.append(Spacer(1, 10))
# Process each question data
for i, item in enumerate(data if isinstance(data, list) else [data]):
story.append(Paragraph(f"<b>{item['question']}</b>", styles['QuestionTitle']))
# Add each section with proper handling
sections = ['Situation', 'Task', 'Action', 'Results and Transitions', 'Personal Lessons',
'Observations of Others', 'Professional Connection']
for section in sections:
story.append(Paragraph(f"{section}:", styles['SectionTitle']))
for point in item.get(section, []):
story.append(Paragraph(f"{point}", styles['NormalText']))
# Add a page break after each question except the last one
if i < len(data) - 1:
story.append(PageBreak())
# Build the PDF into the buffer
doc.build(story)
# Get the PDF content from the buffer
pdf_content = buffer.getvalue()
buffer.close()
# If output_filename is provided, also save to file
if output_filename:
with open(output_filename, 'wb') as f:
f.write(pdf_content)
return pdf_content
except Exception as e:
print(f"Error: {e}")
return {}
+175
View File
@@ -0,0 +1,175 @@
import os
import requests
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
from uuid import uuid4
import json
import getpass
import numpy as np
from concurrent.futures import ThreadPoolExecutor, as_completed
from sklearn.metrics.pairwise import cosine_similarity
from typing import List
import time
from datetime import datetime
import pytz
import logging
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
llm_temp = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
def generate_quiz(startpop_pdf, quiz_type=None) -> dict:
try:
# Define prompt for summarizing and extracting the required fields
quiz_prompt = PromptTemplate(
template="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a Fire Fighter Interview preparation assistant that generates QUIZ for user based on STARTPOP FORMAT PDF BASED on
IN THE STARTPOP FORMAT PDF, each theme has its own questions with corresponding STARTPOP framework for each question.
Your responsibility is to carefully analyze the provided PDF data and then generate a quiz for the user.
You will also be provided with the type of quiz.
There are three different types of quizzes namely:
1- Single line text inputs
2- Multiple Choice questions
3- True or False questions
For each quiz type, return the following JSON format:
1. For Single Line Text Inputs:
- A list of objects, each with {{"question": "Your question", "correct_answer": "Your correct answer"}}
2. For Multiple Choice Questions:
- A list of objects, each with {{"question": "Your question", "options": ["Option 1", "Option 2"], "correct_answer": "Correct Option"}}
3. For True or False Questions:
- A list of objects, each with {{"question": "Your question", "options": ["True", "False"], "correct_answer": "True or False"}}
Each response should also include a field called "quiz_type" which can be either 1, 2, or 3 respectively.
Return just the JSON output without any other explanation or comments.
TO KNOW MORE ABOUT THE PROJECT READ BELOW
----START------
Throughout most Probationary Firefighter Interviews, they will be evaluating a ton of things. Typically, they want to see how you align with the **7 Main Concepts of Firefighting**. They are also watching how nervous you are, your communication skills, and your overall general competence for the role. At the end of the day, you want them to like you.
### 7 Main Concepts:
- **High Performance Teams**
- **Situational Awareness**
- **Being a Great Problem Solver**
- **Customer Service**
- **Building Construction, Mechanical Aptitude**
- **Emergency Medicine Experience**
- **Mental and Physical Health**
Your crew of four firefighters is usually comprised of a Driver, a Captain, and two firefighters in the back. That is a High-Performance Team.
We are frequently dispatched to calls that require using our understanding of Building Construction Concepts, Mechanical Aptitude, and Emergency Medical Experience. When you respond to an emergency event that is inherently dangerous (like a vehicle fire, a car accident in a slanted ditch, a person trapped under a machine, a house fire, or a chemical suicide), you need to use your Situational Awareness to keep that crew safe.
Sometimes the tools, training, and tactics that you have been taught work perfectly. Sometimes they dont. Can you be a Good Problem Solver to quickly come up with something to make the situation better for the people, places, and environments that we protect?
Ultimately, your crew will be serving the public, and the chiefs need to know that you can be trained to be above their desired standard so that you give the public great Customer Service.
### 20 Important Themes
Consider the 7 concepts to be the soil. All of your stories grow out of that soil. But not every story works for every question. You need to handpick the right one at the right times to give them. Sort of like how you handpick flowers out of the soil. You NEED to have **20 different flowers** so that you are fully prepared for whatever behavioral question they throw at you. These are the **20 Themes** that you would use for behavioral questions:
- 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
### Behavioral Question Starters
Behavioral questions usually start with phrases like:
- “Tell me a time when…”
- “Can you tell me about a time when you…”
- "Describe a situation where you had to…"
- "Give me an example of how you…"
- "Have you ever been in a position where you needed to…"
- "Walk me through a time when you…"
### STARTPOP Framework
The STAR Format is what most people tell you to do in order to answer a firefighter interview question. Its a great framework. I highly recommend it. I just advise that you pump it up even further. I call it **STARTPOP**.
Try and pull from different parts of your life. My Chief Training Officer told me that he enjoys candidates that are able to use different experiences to answer the questions. Listening to someone drone on and on about a singular time or type of event in their life is a massive turn-off to the interview panel. Thats a bad thing. Just like most things, variety is the spice of life.
#### Components of STARTPOP:
1. **Situation**:
- Set up the answer in the mind of the question asker.
- Your storytelling skills matter here. It has to be concise and impactful (no more than 25 seconds long).
- Include dates, ages, places, and circumstances.
2. **Task**:
- Explain what you needed to do and why you needed to do it.
- Recap the situation quickly from a different angle.
3. **Actions**:
- Outline both the negative and the positive way of doing things.
- Show high moral character in every question.
4. **Results**:
- Explain what happened as a result of your actions.
- Share results in a time-specific manner (e.g., “5 months later X happened”).
5. **Transitions**:
- Speak in a way that aligns with professional expectations.
- Ensure coherence in your responses.
6. **Personal Lessons**:
- Discuss what you learned about yourself.
- Address any concerns the interviewers might have about hiring you.
7. **Other People Observations**:
- Share insights about others in the situation.
- Keep it short and to the point.
8. **Professional Connection**:
- Relate your experience directly to the fire service.
- Conclude strongly, avoiding phrases like “and so yeah…”.
----END------
NOTE: THE QUIZ FOCUES ON BULIDNG USER CONFIDENCE BY ANANLYZING THE QUESTIONS AND FRAMEWORK FOR EACH QUESTION IN THE STARTPOP FRAMEWORK PDF,SOLELY USE THIS PDF PROVIDED BY THE USER
BASED ON THIS FRAMEWORK , CREATE INTERVIEW BASED QUIZ FOR FIRE FIGHTING ROLE BY ANALYZING THIS DOCUMENT
NOTE : THE QUIZ SHOULD NOT BE BASED ON STARTPOP FRAMEWORK ITSELF BUT ANALYZE THE STARTPOP FRAMEWORK PRESENTED TO GENERATE INTERVIEW BASED QUIZ
e.g "The STARTPOP framework is specifically designed for firefighter interviews", THIS KIND OF QUESTION SHOULD NOT BE ASKED IN THE QUIZ....
Thank you for your thorough and precise processing!
STARTPOP FULL PDF :{startpop_pdf}
question type : {quiz_type}
P
<|eot_id|><|start_header_id|>user<|end_header_id|>""",
input_variables=["startpop_pdf", "quiz_type", "question"],
)
# Pipeline to process the prompt and parse output
quiz_router = quiz_prompt | llm_temp | JsonOutputParser()
# Call the pipeline and generate the cohesive output
output = quiz_router.invoke({"startpop_pdf": startpop_pdf, "quiz_type": quiz_type, "question": "Your question here"})
return output
except Exception as e:
print(f"Error:{e}")
return {}
+239
View File
@@ -0,0 +1,239 @@
import os
import requests
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
from uuid import uuid4
import json
import getpass
import numpy as np
from concurrent.futures import ThreadPoolExecutor, as_completed
from sklearn.metrics.pairwise import cosine_similarity
from typing import List
import time
from datetime import datetime
import pytz
import logging
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
llm_temp = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
def generate_theme(conversation_data,resume,full_history,feedback=None, previous_result=None) -> dict:
try:
# Define prompt for summarizing and extracting the required fields
theme_prompt = PromptTemplate(
template="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a Fire Fighter Interview preparation assistant that generates STARTPOP FORMAT BASED on user interaction with AI.
You will be provided with the current theme, user interaction with AI (alongside user resume), and data.
Your responsibility is to carefully analyze user interaction with AI, the theme, and the user RESUME to generate a STARTPOP format for the theme.
NOTE: A SINGLE QUESTION IS GENERATED WITH DETAILED STARTPOP FORMAT
NOTE: For more Context, user full work history may also be provided
TO KNOW MORE ABOUT THE PROJECT READ BELOW
---START------
Throughout most Probationary Firefighter Interviews, they will be evaluating a ton of things. Typically, they want to see how you align with the **7 Main Concepts of Firefighting**. They are also watching how nervous you are, your communication skills, and your overall general competence for the role. At the end of the day, you want them to like you.
### 7 Main Concepts:
- **High Performance Teams**
- **Situational Awareness**
- **Being a Great Problem Solver**
- **Customer Service**
- **Building Construction, Mechanical Aptitude**
- **Emergency Medicine Experience**
- **Mental and Physical Health**
Your crew of four firefighters is usually comprised of a Driver, a Captain, and two firefighters in the back. That is a High-Performance Team.
We are frequently dispatched to calls that require using our understanding of Building Construction Concepts, Mechanical Aptitude, and Emergency Medical Experience. When you respond to an emergency event that is inherently dangerous (like a vehicle fire, a car accident in a slanted ditch, a person trapped under a machine, a house fire, or a chemical suicide), you need to use your Situational Awareness to keep that crew safe.
Sometimes the tools, training, and tactics that you have been taught work perfectly. Sometimes they dont. Can you be a Good Problem Solver to quickly come up with something to make the situation better for the people, places, and environments that we protect?
Ultimately, your crew will be serving the public, and the chiefs need to know that you can be trained to be above their desired standard so that you give the public great Customer Service.
### 20 Important Themes
Consider the 7 concepts to be the soil. All of your stories grow out of that soil. But not every story works for every question. You need to handpick the right one at the right times to give them. Sort of like how you handpick flowers out of the soil. You NEED to have **20 different flowers** so that you are fully prepared for whatever behavioral question they throw at you. These are the **20 Themes** that you would use for behavioral questions:
- 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
### Behavioral Question Starters
Behavioral questions usually start with phrases like:
- “Tell me a time when…”
- “Can you tell me about a time when you…”
- "Describe a situation where you had to…"
- "Give me an example of how you…"
- "Have you ever been in a position where you needed to…"
- "Walk me through a time when you…"
### STARTPOP Framework
The STAR Format is what most people tell you to do in order to answer a firefighter interview question. Its a great framework. I highly recommend it. I just advise that you pump it up even further. I call it **STARTPOP**.
Try and pull from different parts of your life. My Chief Training Officer told me that he enjoys candidates that are able to use different experiences to answer the questions. Listening to someone drone on and on about a singular time or type of event in their life is a massive turn-off to the interview panel. Thats a bad thing. Just like most things, variety is the spice of life.
#### Components of STARTPOP:
1. **Situation**:
- Set up the answer in the mind of the question asker.
- Your storytelling skills matter here. It has to be concise and impactful (no more than 25 seconds long).
- Include dates, ages, places, and circumstances.
2. **Task**:
- Explain what you needed to do and why you needed to do it.
- Recap the situation quickly from a different angle.
3. **Actions**:
- Outline both the negative and the positive way of doing things.
- Show high moral character in every question.
4. **Results**:
- Explain what happened as a result of your actions.
- Share results in a time-specific manner (e.g., “5 months later X happened”).
5. **Transitions**:
- Speak in a way that aligns with professional expectations.
- Ensure coherence in your responses.
6. **Personal Lessons**:
- Discuss what you learned about yourself.
- Address any concerns the interviewers might have about hiring you.
7. **Other People Observations**:
- Share insights about others in the situation.
- Keep it short and to the point.
8. **Professional Connection**:
- Relate your experience directly to the fire service.
- Conclude strongly, avoiding phrases like “and so yeah…”.
EXAMPLE STARTPOP
question: Tell me a time when you made a MISTAKE how did you fix it? (Eaves Cleaning Mistake)
Situation:
• In the Fall my business, Tiger Building Services, does a lot of eavestrough cleaning.
• Back in 2019 I was working with an employee in my truck. We were working nicely to hit my daily revenue target.
• We got to the last job of the day; we were tired and running out of sunlight. But I really wanted to squeeze it in.
• We have procedures to follow in order to work safely and effectively. My goal is to be as low impact as possible.
• I made a mistake when we used the handheld blowers on their eaves to blow out the debris without checking how
wet the debris was or the ground around the back of the house. It made a muddy mess all over their white deck.
• They were livid. Swearing and completely unhappy with how we were doing the work. I take ownership of my
mistakes and realized I screwed up by using blowers instead of hand bombing it.
Task:
• My task was to defuse the situation and clean up the mess as quickly as possible.
• I had to do it because as the owner of the company it was my reputation on the line. We got the job through one
of the apps that we use to fill out our schedule and it is imperative that I make sure their customers have good
experiences with us so that we keep our top position on the app.
• I am also a man of integrity and try to be always empathetic, so I felt obligated to correct the mistake.
Action:
• The wrong approach would have been to match the customers energy and just as belligerent and abrasive. It
would have escalated the situation to a point where things could have gotten ugly and pretty physical.
• It would have also been wrong to just ignore or make fun of the customer and the problem we created, or to just
pack our ladders and tools and run away as quickly as possible.
• The correct approach was to get off the roof safely and speak with the customer on the ground eye to eye.
• I made sure to do that and then apologized for the mess that we made. I empathized with them and the way they
were feeling. I told them that it was our mistake, and we will work to correct it immediately.
• I switched our strategy. Told the employee to clean use their hand for the gutters while I cleaned the deck.
Results and Transitions:
• It was a losing situation for me in the short run. The job ended up taking a bit longer than expected and I actually
told them that we would waive the fees due to the inconveniences we created.
• After we finished up, I gave her a plan of action. She would get the eaves cleaning for free, and we would return
the following day with our soft wash system to make sure that she had a sparkling clean deck also free of charge.
• The next morning when we finished the free soft wash, she was happy with the resolution plan and Jiffy was
impressed with our ability to correct the mistake and alleviate the situation.
Personal Lessons:
• What I learned about myself was that I do make mistakes, but I am the type of person that owns up to it.
• I am also honest and empathetic, and I can perform in stressful situations and that I could de-escalate tense
situations, to be adaptable and think quickly on the fly.
• I used the LAST tactic for good customer service: Listened, Apologized, Solved the problem, then thanked them.
• I took the full brunt of their anger, made an action plan that instantly calmed the situation and then acted on it to
make them happy with the service.
Observations of Others:
• What I learned about other people is that people are entitled to their reactions, emotions, and feelings.
• I respect those emotions and have learned that following actionable game plans will help avoid or resolve issues.
• I know the term proper planning prevents poor performance is applicable here.
• There is a reason organizations have SOPs and SOGs. They are there to be followed in order to avoid mistakes.
Professional Connection:
• My biggest takeaway was it is okay to make mistakes, but it is not okay to not learn from them.
• I know that the team on Markham Fire sometimes makes mistakes on the firegrounds, but they are also the type
of people that own up to their mistake and learn from them.
• I also know that Chief Grant promotes having an open and transparent organization that is not afraid from
admitting an error or correcting it.
---END------
JSON Output Requirements: Generate a list of well-structured JSON output STARTPOP with question and correcpoding STARTPPOP with the following fields:
- theme_title: The title the theme provided
- question: The question
- Situation: A bulleted list of texts as seen in examples
- Task: A bulleted list of texts as seen in examples
- Action: A bulleted list of texts as seen in examples
- Personal Lessons: A bulleted list of texts as seen in examples
- Results and Transitions: A bulleted list of texts as seen in examples
- Observations of Others: A bulleted list of texts as seen in examples
- Professional Connection: A bulleted list of texts as seen in examples
Review Process:
- Carefully review all news items to confirm they align with the specified theme and meet relevance criteria.
- Ensure the JSON format is flawless, comprehensive, and well-structured, with all fields included and correctly formatted.
NOTE: 1. you MAY BE PROVIDED WITH FEEDBACK AND PREVIOUS RESULT, MEANING AI HAS GENERATED STARTPOP BEFORE AND MAYBE USER IS NOT SATISFIED WITH THE RESULT THEN YOU GENERATE A NEW ONE BASED ON THE FEEDBACK
NOTE: Each question will have a correpoding STARTPOP feilds
Return just the JSON output without any other explanation or comments.
Thank you for your thorough and precise processing!
CONVERSATION DATA :{conversation_data}
FEEDBACK: {feedback}
PREVIOUS RESULT: {previous_result}
USER RESUME : {resume}
FULL WORK HISTORY : {full_history}
<|eot_id|><|start_header_id|>user<|end_header_id|>
RULES FOR GENERATING EACH COMPONENT - FOLLOW THESE RULES THOROUGHLY MAKE SURE YOUR OUTPUT IS WELL DETAILED
THE FRAME WORK MUST BE DETAILED WITH THE FOLLWWING RULES
1. Situation : 75 - 100 words
2. Task: 50 words
3. Actions: 2 Negative actions and 2 positive actions
4. Results: 25 - 5o words
5. Personal Lessons : 25 - 50 words
6. Observation of others: 25 words
7. Professional connections: 25 - 50 words and in addition to the 25-50 words:
- Connect to the theme of questions (Be creative here)
- Ask to be part of their team(be creattive here)
""",
input_variables=["resume","conversation_data", "feedback", "previous_result","full_history"],
)
# Pipeline to process the prompt and parse output
theme_router = theme_prompt | llm_temp | JsonOutputParser()
# Call the pipeline and generate the cohesive output
output = theme_router.invoke({"conversation_data": conversation_data, "feedback": feedback, "previous_result": previous_result,"resume":resume,"full_history":full_history})
print(f"Output : {output}")
return output
except Exception as e:
print(f"Error:{e}")
return {}
+197
View File
@@ -0,0 +1,197 @@
import json
from typing import List, Dict, Optional, TypedDict, Sequence, Annotated
from dataclasses import dataclass
from pathlib import Path
from datetime import datetime
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage, BaseMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
from utils.utils import format_questions_text
from src.prompts import chat_prompt
from langchain_openai import ChatOpenAI
@dataclass
class Message:
role: str # 'human' or 'ai'
content: str
timestamp: str
QUESTIONS_PATH = "./data/config_files/questions.json"
with open(QUESTIONS_PATH, "r") as f:
questions = json.load(f)
prompt_template = None
MODEL = "gpt-4o-mini"
def initialize_workflow(model) -> StateGraph:
"""Initialize LangGraph workflow"""
workflow = StateGraph(state_schema=MessagesState)
memory = MemorySaver()
def call_model(state: MessagesState):
prompt = prompt_template.invoke({"messages": state["messages"], "language": state["language"]})
response = model.invoke(prompt)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
return workflow.compile(checkpointer=memory)
def setup_prompt_template(theme: int, resume: str) -> ChatPromptTemplate:
"""Set up the prompt template"""
return ChatPromptTemplate.from_messages([
("system", chat_prompt(theme, resume)),
MessagesPlaceholder(variable_name="messages")
])
def parse_ai_response(content: str) -> Dict:
"""Parse AI response content into expected format"""
try:
response = json.loads(content)
return {
"message": response.get("message", ""),
"end": response.get("end", "no") == "yes"
}
except json.JSONDecodeError:
return {
"message": content,
"end": False
}
def add_message(storage_path: Path, conversation_id: str, role: str, content: str) -> None:
"""Add a message to the conversation history"""
message_data = {
"role": role,
"content": content,
"timestamp": datetime.now().isoformat()
}
conversations = load_conversations(storage_path)
if conversation_id not in conversations:
conversations[conversation_id] = {"messages": []}
conversations[conversation_id]["messages"].append(message_data)
save_conversations(storage_path, conversations)
def get_conversation_history(conversation_id: str, storage_path: Path) -> List[Message]:
"""Get the conversation history"""
conversations = load_conversations(storage_path)
if conversation_id not in conversations:
return None
return [
Message(
role=msg["role"],
content=msg["content"],
timestamp=msg["timestamp"]
)
for msg in conversations[conversation_id]["messages"]
]
def load_conversations(storage_path: Path) -> Dict:
"""Load conversations from storage file"""
try:
with open(storage_path, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_conversations(storage_path: Path, conversations: Dict) -> None:
"""Save conversations to storage file"""
with open(storage_path, 'w') as f:
json.dump(conversations, f, indent=2)
def convert_to_langchain_messages(messages: List[Message]) -> List[HumanMessage | AIMessage]:
"""Convert our Message objects to LangChain message objects"""
converted_messages = []
for msg in messages:
if msg.role == "human":
converted_messages.append(HumanMessage(content=msg.content))
else:
converted_messages.append(AIMessage(content=msg.content))
return converted_messages
def ai_chat(query: str, conversation_id: str, theme_id: int, resume: str) -> str:
"""Main chat function that processes queries and manages conversation"""
storage_path = Path("conversations.json")
class State(TypedDict):
messages: Annotated[Sequence[BaseMessage], "The messages in the conversation"]
language: str
# Initialize model and workflow
model = ChatOpenAI(model=MODEL)
workflow = StateGraph(state_schema=State)
def call_model(state: State):
prompt_template = setup_prompt_template(theme_id, resume)
prompt = prompt_template.invoke({
"messages": state["messages"],
"language": state["language"]
})
response = model.invoke(prompt)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)
# Get conversation history
history = get_conversation_history(conversation_id, storage_path)
config = {"configurable": {"thread_id": conversation_id}}
language = "English"
if not history:
# New conversation
input_messages = [HumanMessage(content=query)] if query else [HumanMessage(content="Let's get started")]
output = app.invoke(
{"messages": input_messages, "language": language},
config
)
else:
# Existing conversation
history = convert_to_langchain_messages(history)
input_messages = history + [HumanMessage(content=query)] if query else history
output = app.invoke(
{"messages": input_messages, "language": language},
config
)
# Store messages
if query:
add_message(storage_path, conversation_id, "human", query)
add_message(storage_path, conversation_id, "ai", output["messages"][-1].content)
return output["messages"][-1].content
# Example usage:
if __name__ == "__main__":
# Sample resume
sample_resume = """
John Doe
EMT-B Certified
5 years experience as volunteer firefighter
Bachelor's in Fire Science
"""
# Sample conversation
conversation_id = "12345"
theme_id = 1 # Customer Service theme
# Start conversation
# Continue conversation
follow_up = ai_chat(
query="What was my last questions?",
conversation_id=conversation_id,
theme_id=theme_id,
resume=sample_resume
)
print("AI:", follow_up)
+134
View File
@@ -0,0 +1,134 @@
import json
from typing import List, Dict, Optional
from dataclasses import dataclass
from utils.utils import format_questions_text, format_theme_text
@dataclass
class Message:
role: str # 'human' or 'ai'
content: str
timestamp: str
QUESTIONS_PATH = "./data/config_files/questions.json"
with open(QUESTIONS_PATH, "r") as f:
questions = json.load(f)
def chat_prompt(theme,resume):
return f"""
You are a Fire Fighter Interview preparation assistant.
Throughout most Probationary Firefighter Interviews, they will be evaluating a ton of things. Typically, they want to see how you align with the **7 Main Concepts of Firefighting**. They are also watching how nervous you are, your communication skills, and your overall general competence for the role. At the end of the day, you want them to like you.
### 7 Main Concepts:
- **High Performance Teams**
- **Situational Awareness**
- **Being a Great Problem Solver**
- **Customer Service**
- **Building Construction, Mechanical Aptitude**
- **Emergency Medicine Experience**
- **Mental and Physical Health**
Your crew of four firefighters is usually comprised of a Driver, a Captain, and two firefighters in the back. That is a High-Performance Team.
We are frequently dispatched to calls that require using our understanding of Building Construction Concepts, Mechanical Aptitude, and Emergency Medical Experience. When you respond to an emergency event that is inherently dangerous (like a vehicle fire, a car accident in a slanted ditch, a person trapped under a machine, a house fire, or a chemical suicide), you need to use your Situational Awareness to keep that crew safe.
Sometimes the tools, training, and tactics that you have been taught work perfectly. Sometimes they dont. Can you be a Good Problem Solver to quickly come up with something to make the situation better for the people, places, and environments that we protect?
Ultimately, your crew will be serving the public, and the chiefs need to know that you can be trained to be above their desired standard so that you give the public great Customer Service.
### 20 Important Themes
Consider the 7 concepts to be the soil. All of your stories grow out of that soil. But not every story works for every question. You need to handpick the right one at the right times to give them. Sort of like how you handpick flowers out of the soil. You NEED to have **20 different flowers** so that you are fully prepared for whatever behavioral question they throw at you. These are the **20 Themes** that you would use for behavioral questions:
- 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
### Behavioral Question Starters
Behavioral questions usually start with phrases like:
- “Tell me a time when…”
- “Can you tell me about a time when you…”
- "Describe a situation where you had to…"
- "Give me an example of how you…"
- "Have you ever been in a position where you needed to…"
- "Walk me through a time when you…"
Your goal is to engage in conversation with the user. You will be provided with the current theme, the resume of the user, and example general competency questions and behavioral questions.
USER_RESUME FROM START TO END :
--- START ---
{resume}
--- END ---
### STARTPOP Framework
The STAR Format is what most people tell you to do in order to answer a firefighter interview question. Its a great framework. I highly recommend it. I just advise that you pump it up even further. I call it **STARTPOP**.
Try and pull from different parts of your life. My Chief Training Officer told me that he enjoys candidates that are able to use different experiences to answer the questions. Listening to someone drone on and on about a singular time or type of event in their life is a massive turn-off to the interview panel. Thats a bad thing. Just like most things, variety is the spice of life.
#### Components of STARTPOP:
1. **Situation**:
- Set up the answer in the mind of the question asker.
- Your storytelling skills matter here. It has to be concise and impactful (no more than 25 seconds long).
- Include dates, ages, places, and circumstances.
2. **Task**:
- Explain what you needed to do and why you needed to do it.
- Recap the situation quickly from a different angle.
3. **Actions**:
- Outline both the negative and the positive way of doing things.
- Show high moral character in every question.
4. **Results**:
- Explain what happened as a result of your actions.
- Share results in a time-specific manner (e.g., “5 months later X happened”).
5. **Transitions**:
- Speak in a way that aligns with professional expectations.
- Ensure coherence in your responses.
6. **Personal Lessons**:
- Discuss what you learned about yourself.
- Address any concerns the interviewers might have about hiring you.
7. **Other People Observations**:
- Share insights about others in the situation.
- Keep it short and to the point.
8. **Professional Connection**:
- Relate your experience directly to the fire service.
- Conclude strongly, avoiding phrases like “and so yeah…”.
Current theme with More context about the theme for Creating The Professional Connection (Lessons Learned): {format_theme_text(theme)}
Sample General Competency QUESTIONS and Situational Questions: {format_questions_text(questions,'General Competency Questions')}
Sample Situational Questions: {format_questions_text(questions,'Situational Questions')}
Your task is to engage the user in conversation, ask relevant questions, that will ultimately help them prepare a strong STARTPOP response based on their experiences and the current theme.
YOU WILL BE PROVIDED WITH THE USER RESUME, ASK 1 QUESTION AT A TIME AND MAKE IT CONVERSATIONAL AND INTERESTING.
These responses will be saved and later used to generate a STARTPOP framework by US (DO NOT WORRY ABOUT THAT, WE WILL BE THE ONE TO GENERATE, JUST ENGAGE USER WITH QUESTION AND ANSWER).
Output format
CUURENT TEHEME USER IS INTERESTED IN {format_theme_text(theme)}
NOTE: !!! EXPLICITLY FOCUS ON THE CURRENT THEME SPECIFIED
WILL BE IN JSON, avoid puttting ```json, before or after , return the excat json with nothing else
message:
end: "yes" or "no" if you are done with asking questions and confident the responses are okay enough to prepare STARTPOP by us
NOTE: DO NOT KEEP THE CONVERSATION , CAREFULL ANALYZE USER RESUME AND THE PROVIDED EXAMPLES QUESTIONS AND ALL CONTEXT , ASK RELEVANT QUESTION BASED ON THE THEME AND THAT IS ALL
"""
+195
View File
@@ -0,0 +1,195 @@
import json
from typing import List, Dict, Optional, TypedDict, Sequence, Annotated
from dataclasses import dataclass
from pathlib import Path
from datetime import datetime
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage, BaseMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
from utils.utils import format_questions_text
from src.prompts import chat_prompt
from langchain_openai import ChatOpenAI
@dataclass
class Message:
role: str # 'human' or 'ai'
content: str
timestamp: str
QUESTIONS_PATH = "./data/config_files/questions.json"
with open(QUESTIONS_PATH, "r") as f:
questions = json.load(f)
prompt_template = None
MODEL = "gpt-4o"
def initialize_workflow(model) -> StateGraph:
"""Initialize LangGraph workflow"""
workflow = StateGraph(state_schema=MessagesState)
memory = MemorySaver()
def call_model(state: MessagesState):
prompt = prompt_template.invoke({"messages": state["messages"], "language": state["language"]})
response = model.invoke(prompt)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
return workflow.compile(checkpointer=memory)
def setup_prompt_template(theme: int, resume: str) -> ChatPromptTemplate:
"""Set up the prompt template"""
return ChatPromptTemplate.from_messages([
("system", chat_prompt(theme, resume)),
MessagesPlaceholder(variable_name="messages")
])
def parse_ai_response(content: str) -> Dict:
"""Parse AI response content into expected format"""
try:
response = json.loads(content)
return {
"message": response.get("message", ""),
"end": response.get("end", "no") == "yes"
}
except json.JSONDecodeError:
return {
"message": content,
"end": False
}
def add_message(storage_path: Path, conversation_id: str, role: str, content: str) -> None:
"""Add a message to the conversation history"""
message_data = {
"role": role,
"content": content,
"timestamp": datetime.now().isoformat()
}
conversations = load_conversations(storage_path)
if conversation_id not in conversations:
conversations[conversation_id] = {"messages": []}
conversations[conversation_id]["messages"].append(message_data)
save_conversations(storage_path, conversations)
def get_conversation_history(conversation_id: str, storage_path: Path) -> List[Message]:
"""Get the conversation history"""
conversations = load_conversations(storage_path)
if conversation_id not in conversations:
return None
return [
Message(
role=msg["role"],
content=msg["content"],
timestamp=msg["timestamp"]
)
for msg in conversations[conversation_id]["messages"]
]
def load_conversations(storage_path: Path) -> Dict:
"""Load conversations from storage file"""
try:
with open(storage_path, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_conversations(storage_path: Path, conversations: Dict) -> None:
"""Save conversations to storage file"""
with open(storage_path, 'w') as f:
json.dump(conversations, f, indent=2)
def convert_to_langchain_messages(messages: List[Message]) -> List[HumanMessage | AIMessage]:
"""Convert our Message objects to LangChain message objects"""
converted_messages = []
for msg in messages:
if msg.role == "human":
converted_messages.append(HumanMessage(content=msg.content))
else:
converted_messages.append(AIMessage(content=msg.content))
return converted_messages
def ai_chat(query: str, conversation_id: str, theme_id: int, resume: str) -> str:
"""Main chat function that processes queries and manages conversation"""
storage_path = Path("conversations.json")
class State(TypedDict):
messages: Annotated[Sequence[BaseMessage], "The messages in the conversation"]
language: str
# Initialize model and workflow
model = ChatOpenAI(model=MODEL)
workflow = StateGraph(state_schema=State)
def call_model(state: State):
prompt_template = setup_prompt_template(theme_id, resume)
prompt = prompt_template.invoke({
"messages": state["messages"],
"language": state["language"]
})
response = model.invoke(prompt)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)
# Get conversation history
history = get_conversation_history(conversation_id, storage_path)
config = {"configurable": {"thread_id": conversation_id}}
language = "English"
if not history:
# New conversation
input_messages = [HumanMessage(content=query)] if query else [HumanMessage(content="Let's get started")]
output = app.invoke(
{"messages": input_messages, "language": language},
config
)
else:
# Existing conversation
history = convert_to_langchain_messages(history)
input_messages = history + [HumanMessage(content=query)] if query else history
output = app.invoke(
{"messages": input_messages, "language": language},
config
)
# Store messages
if query:
add_message(storage_path, conversation_id, "human", query)
add_message(storage_path, conversation_id, "ai", output["messages"][-1].content)
return output["messages"][-1].content
# Example usage:
if __name__ == "__main__":
# Sample resume
sample_resume = """
John Doe
EMT-B Certified
5 years experience as volunteer firefighter
Bachelor's in Fire Science
"""
# Sample conversation
conversation_id = "12345"
theme_id = 1 # Customer Service theme
# Start conversation
# Continue conversation
follow_up = ai_chat(
query="What was my last questions?",
conversation_id=conversation_id,
theme_id=theme_id,
resume=sample_resume
)
print("AI:", follow_up)
+53
View File
@@ -0,0 +1,53 @@
import os
from spire.doc import Document, FileFormat
from langchain_community.document_loaders import PyPDFLoader
def convert_word_to_pdf(doc_path: str) -> str:
"""
Convert a .doc or .docx file to PDF using Spire.Doc.
Args:
doc_path (str): The path to the .doc or .docx file.
Returns:
str: The path to the converted PDF file.
"""
pdf_path = os.path.splitext(doc_path)[0] + '.pdf'
# Create a Document object
document = Document()
# Load the Word document
document.LoadFromFile(doc_path)
# Save as PDF
document.SaveToFile(pdf_path, FileFormat.PDF)
document.Close()
return pdf_path
def load_document(file_path: str):
"""
Utility function to load a PDF, DOCX, or DOC file by first converting it to PDF.
Args:
file_path (str): The path to the file to load.
Returns:
List[Document]: A list of Document objects representing the contents of the file.
"""
try:
extension = os.path.splitext(file_path)[1].lower()
if extension in ['.doc', '.docx']:
# Convert .doc or .docx to PDF first
pdf_path = convert_word_to_pdf(file_path)
loader = PyPDFLoader(pdf_path)
elif extension == '.pdf':
loader = PyPDFLoader(file_path)
else:
raise ValueError(f"Unsupported file type: {extension}. Only .pdf, .docx, and .doc are supported.")
return loader.load()
except Exception as e:
print(f"Error loading document: {str(e)}")
return None
+73
View File
@@ -0,0 +1,73 @@
import os
import requests
import json
from PyPDF2 import PdfReader
base_path = os.path.join("data", "config_files")
THEME_CONTEXT_PATH = os.path.join(base_path, "theme_context.json")
with open(THEME_CONTEXT_PATH, "r") as f:
themes = json.load(f)
def delete_file(file_path):
try:
os.remove(file_path)
print(f"Deleted file: {file_path}")
except OSError as e:
print(f"Error deleting file {file_path}: {e}")
def delete_all_files_in_directory(directory_path):
try:
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted file: {file_path}")
except OSError as e:
print(f"Error deleting files in {directory_path}: {e}")
def format_questions_text(questions_dict,key):
"""Format questions as text with dashes."""
formatted_text = ""
for question in questions_dict[key]:
formatted_text += f"- {question['question']}\n"
return formatted_text.strip()
def format_theme_text(theme_id):
"""Format questions as text with dashes."""
formatted_text = ""
matching_themes = [t for t in themes if t["id"] == theme_id]
current_theme = matching_themes[0]
formatted_text += f"- {current_theme['id']}\n"
formatted_text += f"- {current_theme['theme']}\n"
formatted_text += f"- {current_theme['context']}\n"
return formatted_text.strip()
def download_pdf_and_extract_text(url: str) -> str:
# Create a temporary file path
temp_file_path = 'temp.pdf'
# Download the PDF from the URL
response = requests.get(url)
response.raise_for_status() # Raise an error for bad responses
with open(temp_file_path, 'wb') as f:
f.write(response.content)
# Load the PDF
reader = PdfReader(temp_file_path)
# Extract text from all pages and combine into one text
combined_text = "\n\n".join(page.extract_text() for page in reader.pages if page.extract_text())
# Delete the temporary file
os.remove(temp_file_path)
return combined_text