Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions backend/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,34 @@





# 1) Configure your API key
client = genai.Client(api_key=api_key)



def get_system_prompt(combined_plans_text, user_experience, question, user_info):
prompt = f"""You are a healthcare plan assistant. Only use the text provided to answer user questions.
Below is the text for multiple plans:
{combined_plans_text}
The user experience level is with healthcare plans is {user_experience}.
Use the associated score to justify each plan and its ranking.
The user experience level is {user_experience} and there info is {user_info}
Answer the following question ONLY from the text above:
Question: {question}
"""
return prompt


def get_chatbot_response(question: str, history: dict) -> str:
client = genai.Client(api_key=api_key)
# Combine plan textract stuff

combined_plans_text = ""

for key, value in history.items():
combined_plans_text += f"---\nPlan: {key}\n\n{value['text']}\n"



# Prompting type
prompt = f"""You are a healthcare plan assistant. Only use the text provided to answer user questions.

Below is the text for multiple plans:
{combined_plans_text}

Answer the following question ONLY from the text above:

Question: {question}
"""
prompt = get_system_prompt(history["combined_plan_text"], history["experience"], question, str(history["user_info"]))

response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[prompt],
)

# 5) Return the text response
# Return the text response
return response.text


Expand Down
52 changes: 0 additions & 52 deletions backend/insurance_databases.py

This file was deleted.

1 change: 1 addition & 0 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ class UserInputForm(BaseModel):
address: Address
weights: Weights
premium: List[float]
user_experience: str
22 changes: 11 additions & 11 deletions backend/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# cd backend
# fastapi dev root.py

history = {}
history = {"user_info": "","combined_plan_text": None, "experience": None, "chat_history" : []}



Expand Down Expand Up @@ -64,11 +64,10 @@ async def upload_pdfs(form_data: str = Form(...),
for i, j in zip(premiums, files):
plans[j] = i



# upload to s3 and textract
# { "name": filename, "text": "TEXT RESULTS " }
results = await upload_and_extract(plans)
textract_results = await upload_and_extract(plans)



# The weights:
Expand All @@ -95,27 +94,28 @@ async def process_plan(plan_name: str, plan_content: str, plan_premium: float):
total_score = ranking_instance.total_scores()
return plan_name, unweighted_scores, weighted_scores, total_score, plan_content

tasks = [process_plan(name, content[0], content[1]) for name, content in results.items()]
tasks = [process_plan(name, content[0], content[1]) for name, content in textract_results.items()]

# Run all tasks concurrently
results = await asyncio.gather(*tasks)

to_frontend = []

combined_plan_text = ""
# Store the results in the history
for name, unweighted_scores, weighted_scores, total_score, plan_content in results:
# {'file_name': 'weighted_scores: dict, 'total_score': float, 'text': str}
history[name] = {
"weighted_scores": weighted_scores,
"total_score": total_score,
"text": plan_content
}

to_frontend.append({
"name" : name,
"weightedScores": weighted_scores,
"totalScore": total_score,
})

combined_plan_text += f"Plan Name: {name}(associated score: {total_score}), content: {plan_content}\n\n"

history["combined_plan_text"] = combined_plan_text
history["experience"] = user_input['user_experience']
history["user_info"] = str(user_input)

return to_frontend

11 changes: 9 additions & 2 deletions backend/upload_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def start_textract_job(bucket: str, key: str) -> str:

# GET TEXTRACT RESULTS
def get_textract_result(job_id: str) -> str:
time.sleep(5)
while True:
response = textract.get_document_text_detection(JobId=job_id)
status = response["JobStatus"]
Expand All @@ -46,19 +47,25 @@ async def upload_and_extract(files: dict):
job_map = {}
upload_jobs = []
# start all s3 jobs and wait till they finish
s3_start = time.time();
print('STARTING S3 UPLOAD')
for file, premium in files.items():
job_map[file.filename] = premium
job = asyncio.to_thread(upload_to_s3, file, S3_BUCKET_NAME, file.filename);
upload_jobs.append(job)
await asyncio.gather(*upload_jobs)

print('S3 UPLOAD COMPLETE: '+str(time.time()-s3_start))
textract_jobs = {}
textract_start = time.time()
print('STARTING TEXTRACT UPLOAD: ')
for file, premium in files.items():
job_id = await asyncio.to_thread(start_textract_job, S3_BUCKET_NAME, file.filename)
textract_jobs[file.filename] = (job_id, premium)

results = {}
print('TEXTRACT DONE STARTING JOBS: '+str(time.time()-textract_start))
for filename, (job_id, premium) in textract_jobs.items():
result = await asyncio.to_thread(get_textract_result, job_id)
results[filename] = [result, premium]
print('TEXTRACT FINISHED: '+str(time.time()-textract_start))
return results

4 changes: 2 additions & 2 deletions frontend/src/pages/Ranking/Rankings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {SelectChangeEvent} from "@mui/material/Select";
import {ResultsProps} from "../../App";

import {sendInputData} from "../sendInputAPI.ts";
import {getResults} from "../resultsAPI.ts";

import styles from "./rankings.module.css";
import {useFlow} from "../../context/FlowContext.tsx";

Expand Down Expand Up @@ -93,7 +93,7 @@ const Rankings: React.FC<ResultsProps> = ({results, setResults}) => {

setLoading(true)
// success = true, if form upload worked someone handle that..
const results = await sendInputData(fullUserData, files, planCost);
const results = await sendInputData(fullUserData, files, planCost, selectedOption);
setLoading(false);


Expand Down
21 changes: 0 additions & 21 deletions frontend/src/pages/resultsAPI.ts

This file was deleted.

9 changes: 5 additions & 4 deletions frontend/src/pages/sendInputAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface FormDataInput {
[key: string]: any;
}

function structureToJSON(data: FormDataInput, planCost: number[]) {
function structureToJSON(data: FormDataInput, planCost: number[], userExperience) {

return {
"first_name": data.firstName || "",
Expand Down Expand Up @@ -48,7 +48,8 @@ function structureToJSON(data: FormDataInput, planCost: number[]) {
"convenience_of_coverage": parseFloat(data["Convenience of Accessing Benefits"]) || 0,
"geographic_coverage": parseFloat(data["Geographic coverage"]) || 0,
},
"premium" : planCost
"premium" : planCost,
"user_experience" : userExperience
};
}

Expand All @@ -57,12 +58,12 @@ function structureToJSON(data: FormDataInput, planCost: number[]) {
* @param data the user's filled out form data
* @param files the uploaded pdfs of the insurance plans
*/
async function sendInputData(data: FormDataInput, files: File[], planCost: number[]) {
async function sendInputData(data: FormDataInput, files: File[], planCost: number[], userExperience: string) {
console.log("Post request");
try {
const formData = new FormData();
// add the user form data
const jsonData = structureToJSON(data, planCost);
const jsonData = structureToJSON(data, planCost, userExperience);
formData.append("form_data", JSON.stringify(jsonData));
// add all the uploaded files
files.forEach((file) => {
Expand Down