api separation and fixes done

This commit is contained in:
OwusuBlessing
2025-02-11 19:20:53 +01:00
parent b22d4afeb6
commit f430714a3d
6 changed files with 556 additions and 264 deletions
+15 -49
View File
@@ -58,7 +58,6 @@ app.add_middleware(
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 '):
@@ -95,12 +94,12 @@ class ChatResponse(BaseModel):
class GeneratePDFRequest(BaseModel):
conversation_id: str
feedback: Optional[str] = None
previous_results: Optional[Dict[str, Any]] = None
resume_url: Optional[str] = None
conversation_id: str
theme_id: Optional[int] = 1
full_history_url: Optional[str] = None
form_id:Optional[int] = None
generate_theme:str="YES"
class QuizRequest(BaseModel):
pdf_url: str
@@ -209,58 +208,25 @@ async def chat_endpoint(
status_code=400,
detail="Unable to fetch onboarding data"
)
# Get AI chat response
if request.generate_theme == "YES":
print("Generating PDF from response...") # Debugging print
print(f"Calling AI chat function...") # Debugging print
response = ai_chat(
query="NOW GENERATE THEME FOR THE CURRENT THEME",
conversation_id=request.conversation_id,
theme_id=request.theme_id,
resume=resume_docs,
full_history=full_history_docs,
form_response=form_response_docs,
generate_theme=request.generate_theme
)
print(f"pdf content {response}")
response = json.loads(response)
pdf_content = create_pdf(response)
# 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
print(f"Returning PDF with filename: {filename}") # Debugging print
return Response(
content=pdf_content,
media_type="application/pdf",
headers={
"Content-Disposition": f'attachment; filename="{filename}"'
}
)
# Parse response
print("Parsing AI response...") # Debugging print
query = request.query
if not query:
query = "Let's get started"
response = ai_chat(
query=request.query,
query=query,
conversation_id=request.conversation_id,
theme_id=request.theme_id,
resume=resume_docs,
full_history=full_history_docs,
form_response=form_response_docs,
generate_theme=request.generate_theme
form_response=form_response_docs
)
parsed_response = json.loads(response)
print(response)
return ChatResponse(
message=parsed_response.get("message", ""),
end=parsed_response.get("end", "no") == "yes",
pop_theme_generation=parsed_response.get("pop_theme_generation","no") == "yes",
message=response.get("message", ""),
end=response.get("end", "no") == "yes",
pop_theme_generation=response.get("pop_theme_generation","no") == "yes",
error=None
)
@@ -275,7 +241,7 @@ async def chat_endpoint(
@app.post("/rescue-career/generate-theme")
async def generate_pdf_endpoint(
request: ChatRequest,
request: GeneratePDFRequest,
api_key: str = Depends(get_api_key)
):
@@ -339,7 +305,7 @@ async def generate_pdf_endpoint(
# This is a placeholder - replace with your actual conversation data fetching logic
# Get AI-generated theme content
response = ai_chat(
query="NOW GENERATE THE STARTPOP FRAME WORK, THEME_GENERATION_STATE: YES",
query="NOW GENERATE THE STARTPOP FRAMEWORK",
conversation_id=request.conversation_id,
theme_id=request.theme_id,
resume=resume_docs,