backedn chat apis and uplaod apis integrated

This commit is contained in:
OwusuBlessing
2025-02-12 00:12:02 +01:00
parent 7200de4846
commit d1ed8b9e3f
4 changed files with 126 additions and 137 deletions
+39 -18
View File
@@ -78,7 +78,7 @@ async def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
class ChatRequest(BaseModel):
resume_url: Optional[str] = None
query: str=None
conversation_id: str
chat_id: int
theme_id: Optional[int] = 1
full_history_url: Optional[str] = None
form_id:Optional[int] = None
@@ -95,7 +95,7 @@ class ChatResponse(BaseModel):
class GeneratePDFRequest(BaseModel):
resume_url: Optional[str] = None
conversation_id: str
chat_id: int
theme_id: Optional[int] = 1
full_history_url: Optional[str] = None
form_id:Optional[int] = None
@@ -215,7 +215,7 @@ async def chat_endpoint(
query = "Let's get started"
response = ai_chat(
query=query,
conversation_id=request.conversation_id,
conversation_id=request.chat_id,
theme_id=request.theme_id,
resume=resume_docs,
full_history=full_history_docs,
@@ -304,36 +304,57 @@ async def generate_pdf_endpoint(
# Here you would fetch the conversation data using the conversation_id
# This is a placeholder - replace with your actual conversation data fetching logic
# Get AI-generated theme content
# Get AI-generated theme content
response = ai_chat(
query="NOW GENERATE THE STARTPOP FRAMEWORK",
conversation_id=request.conversation_id,
conversation_id=request.chat_id,
theme_id=request.theme_id,
resume=resume_docs,
full_history=full_history_docs,
form_response=form_response_docs,
generate_theme="YES"
)
print(f"AI Response for theme: {response}")
# Ensure AI response is valid
if not isinstance(response, str):
raise HTTPException(status_code=500, detail="Invalid AI response format")
# Generate PDF
response = json.loads(response)
pdf_content = create_pdf(response)
response_data = json.loads(response)
pdf_content = create_pdf(response_data)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"theme_{timestamp}.pdf"
print(f"Returning PDF with filename: {filename}")
return Response(
content=pdf_content,
media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="{filename}"'}
)
file_path = f"theme_{timestamp}.pdf"
# Save the PDF locally temporarily
with open(file_path, "wb") as file:
file.write(pdf_content)
# Upload the PDF to S3 using the API
upload_url = f"{os.getenv('BACKEND_BASE_URL')}/v3/api/custom/theme/doc-upload?x-project={x_api_key}"
with open(file_path, 'rb') as file:
files = {'file': file}
upload_response = requests.post(upload_url, files=files)
# Check if the upload was successful
if upload_response.status_code != 200:
raise HTTPException(status_code=upload_response.status_code, detail="File upload to S3 failed: " + upload_response.text)
upload_data = upload_response.json() # Get the response in JSON format
# Extract the uploaded file URL
theme_url = upload_data.get("url") # Adjust this key based on the actual API response structure
if not theme_url:
raise HTTPException(status_code=500, detail="Failed to retrieve theme URL from upload response")
# Clean up the temporary file
os.remove(file_path)
# Return JSON response with theme URL and text
return {
"theme_url": theme_url,
"theme_text": response_data
}
except Exception as e:
print(f"Error generating theme: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")