Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -453,7 +453,7 @@ async def get_document(company_id: str = Query(...), company_name: str = Query(.
|
|
453 |
companies.append({
|
454 |
"row_id":row[0],
|
455 |
"company_id": row[1],
|
456 |
-
|
457 |
"document_name": row[3],
|
458 |
"document_desc": row[4],
|
459 |
"department": row[5],
|
@@ -469,7 +469,55 @@ async def get_document(company_id: str = Query(...), company_name: str = Query(.
|
|
469 |
except mysql.connector.Error as err:
|
470 |
logging.error(f"Database error: {err}")
|
471 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
#to get data for view in knowledgebase
|
474 |
@app.get("/api/getknowledgebase/{company_id}")
|
475 |
async def get_company_details(company_id: int):
|
@@ -503,29 +551,42 @@ async def get_knowledge_from_db(company_id: int):
|
|
503 |
raise HTTPException(status_code=500, detail="Failed to fetch company")
|
504 |
|
505 |
# to edit the knowledgebase details
|
506 |
-
@app.put("/api/putknowledgebase/{
|
507 |
-
async def update_company_details(
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
|
|
|
|
|
|
518 |
|
519 |
-
#
|
520 |
-
|
521 |
-
|
|
|
|
|
|
|
522 |
|
523 |
-
|
524 |
-
|
|
|
|
|
|
|
|
|
|
|
525 |
|
|
|
526 |
company_data = {
|
|
|
527 |
'company_id': company_id,
|
528 |
-
|
529 |
'document_name': documentName,
|
530 |
'document_desc': documentDescription,
|
531 |
'department': department,
|
@@ -534,26 +595,40 @@ async def update_company_details(
|
|
534 |
'last_updated': lastUpdated
|
535 |
}
|
536 |
|
537 |
-
|
|
|
538 |
if not updated_company:
|
539 |
raise HTTPException(status_code=500, detail="Failed to update company")
|
540 |
return updated_company
|
|
|
541 |
async def update_knowledge_in_db(kid: int, company_data: dict):
|
542 |
try:
|
543 |
logging.info(f"Updating knowledge base for ID: {kid}")
|
544 |
cnx = get_db_connection()
|
545 |
if cnx is None:
|
546 |
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
547 |
-
|
548 |
cursor = cnx.cursor()
|
549 |
update_query = """
|
550 |
-
UPDATE
|
551 |
-
SET kb.company_id,kb.document_name
|
|
|
552 |
WHERE kb.kid = %s;
|
553 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
554 |
cursor.execute(update_query, (
|
555 |
company_data['company_id'],
|
556 |
-
# company_data['file_path'],
|
557 |
company_data['document_name'],
|
558 |
company_data['document_desc'],
|
559 |
company_data['department'],
|
@@ -567,12 +642,18 @@ async def update_knowledge_in_db(kid: int, company_data: dict):
|
|
567 |
success = cursor.rowcount > 0
|
568 |
cursor.close()
|
569 |
cnx.close()
|
|
|
570 |
if not success:
|
|
|
571 |
return None
|
|
|
572 |
return company_data
|
573 |
except mysql.connector.Error as err:
|
574 |
-
logging.error(f"
|
575 |
raise HTTPException(status_code=500, detail="Failed to update company")
|
|
|
|
|
|
|
576 |
|
577 |
|
578 |
def delete_knowledge_from_db(company_id: int) -> bool:
|
@@ -594,7 +675,8 @@ def delete_knowledge_from_db(company_id: int) -> bool:
|
|
594 |
except mysql.connector.Error as err:
|
595 |
logging.error(f"Error deleting company: {err}")
|
596 |
raise HTTPException(status_code=500, detail="Failed to delete company")
|
597 |
-
|
|
|
598 |
@app.delete("/api/delknowledgebase/{company_id}")
|
599 |
async def delete_company(company_id: int):
|
600 |
deletion_success = delete_knowledge_from_db(company_id)
|
@@ -619,6 +701,7 @@ async def save_data_connectors( request: Request,
|
|
619 |
password: str=Form(...),
|
620 |
selectedTables: List[str] = Form(...)):
|
621 |
logging.info(f"Received form submission for database_connectors")
|
|
|
622 |
try:
|
623 |
cnx = get_db_connection()
|
624 |
cursor = cnx.cursor()
|
@@ -634,7 +717,7 @@ async def save_data_connectors( request: Request,
|
|
634 |
SET databasetype = %s, serverip = %s, port = %s, database_name = %s, username = %s, password = %s, dbtablename = %s
|
635 |
WHERE company_id = %s
|
636 |
"""
|
637 |
-
values = (",".join(database), server, port, ",".join(databaseName), username, password, ",".join(selectedTables), company_id)
|
638 |
logging.info(f"Executing update query: {query} with values: {values}")
|
639 |
cursor.execute(query, values)
|
640 |
cnx.commit()
|
@@ -645,7 +728,7 @@ async def save_data_connectors( request: Request,
|
|
645 |
INSERT INTO data_connectors(company_id, databasetype, serverip, port, database_name, username, password, dbtablename)
|
646 |
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
647 |
"""
|
648 |
-
values = (company_id, ",".join(database), server, port, ",".join(databaseName), username, password, ",".join(selectedTables))
|
649 |
logging.info(f"Executing insert query: {query} with values: {values}")
|
650 |
cursor.execute(query, values)
|
651 |
cnx.commit()
|
@@ -653,26 +736,23 @@ async def save_data_connectors( request: Request,
|
|
653 |
|
654 |
cursor.close()
|
655 |
cnx.close()
|
656 |
-
|
657 |
-
|
658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
659 |
except mysql.connector.Error as err:
|
660 |
logging.error(f"Database error: {err}")
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
# logging.info(f"Executing query: {query} with values: {values}")
|
666 |
-
# cursor.execute(query, values)
|
667 |
-
# cnx.commit()
|
668 |
-
# logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")
|
669 |
-
# cursor.close()
|
670 |
-
# cnx.close()
|
671 |
-
# logging.info(f"Data_connectors for {database} inserted successfully")
|
672 |
-
# return RedirectResponse(url="/data_connectors", status_code=302)
|
673 |
-
# except mysql.connector.Error as err:
|
674 |
-
# logging.error(f"Database error: {err}")
|
675 |
-
# raise HTTPException(status_code=500, detail="Internal Server Error")
|
676 |
|
677 |
@app.get("/api/check_data_connectors")
|
678 |
async def get_data_connectors(company_id: str = Query(...), company_name: str = Query(...)):
|
@@ -761,7 +841,7 @@ async def get_api_connectors(company_id: str = Query(...), company_name: str = Q
|
|
761 |
cnx =get_db_connection()
|
762 |
cursor = cnx.cursor()
|
763 |
query = """
|
764 |
-
SELECT ac.company_id, ac.api_name, ac.api_endpoint,ac.auth_token,ac.input_param, ac.output_json, ac.description
|
765 |
FROM u852023448_redmindgpt.api_connectors ac
|
766 |
JOIN u852023448_redmindgpt.company_detail cd ON ac.company_id = cd.company_id
|
767 |
WHERE ac.company_id = %s and cd.company_name=%s
|
@@ -779,9 +859,10 @@ async def get_api_connectors(company_id: str = Query(...), company_name: str = Q
|
|
779 |
companies=[]
|
780 |
for row in result:
|
781 |
companies.append({
|
782 |
-
|
783 |
-
|
784 |
-
"
|
|
|
785 |
# "Auth_Bearer": result[3],
|
786 |
# "Inputjson": result[4],
|
787 |
#"OutputJson": result[5],
|
@@ -796,7 +877,197 @@ async def get_api_connectors(company_id: str = Query(...), company_name: str = Q
|
|
796 |
except mysql.connector.Error as err:
|
797 |
logging.error(f"Database error: {err}")
|
798 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
|
|
|
|
799 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
800 |
|
801 |
@app.get("/prompt_template")
|
802 |
async def prompt_template(request: Request):
|
@@ -837,7 +1108,7 @@ async def get_prompt_connectors(company_id: str = Query(...), company_name: str
|
|
837 |
cnx =get_db_connection()
|
838 |
cursor = cnx.cursor()
|
839 |
query = """
|
840 |
-
SELECT pt.company_id,pt.scenario,pt.prompts,pt.comments
|
841 |
FROM u852023448_redmindgpt.prompt_templates pt
|
842 |
JOIN u852023448_redmindgpt.company_detail cd ON pt.company_id = cd.company_id
|
843 |
WHERE pt.company_id = %s and cd.company_name=%s
|
@@ -855,9 +1126,10 @@ async def get_prompt_connectors(company_id: str = Query(...), company_name: str
|
|
855 |
companies=[]
|
856 |
for row in result:
|
857 |
companies.append({
|
858 |
-
|
859 |
-
|
860 |
-
"
|
|
|
861 |
# "Auth_Bearer": result[3],
|
862 |
# "Inputjson": result[4],
|
863 |
#"OutputJson": result[5],
|
@@ -872,7 +1144,179 @@ async def get_prompt_connectors(company_id: str = Query(...), company_name: str
|
|
872 |
except mysql.connector.Error as err:
|
873 |
logging.error(f"Database error: {err}")
|
874 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
875 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
876 |
|
877 |
@app.get("/chatbot")
|
878 |
async def chatbot(request: Request):
|
|
|
453 |
companies.append({
|
454 |
"row_id":row[0],
|
455 |
"company_id": row[1],
|
456 |
+
"file_path":row[2],
|
457 |
"document_name": row[3],
|
458 |
"document_desc": row[4],
|
459 |
"department": row[5],
|
|
|
469 |
except mysql.connector.Error as err:
|
470 |
logging.error(f"Database error: {err}")
|
471 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
472 |
+
|
473 |
+
#on update of modal form the data table is refresh the value in datatable
|
474 |
+
@app.get("/api/document_update")
|
475 |
+
async def get_document(company_id: str = Query(...)):
|
476 |
+
print(f"Received companyId and name: {company_id},{company_id}") # Log rec
|
477 |
+
#async def get_data_connectors(company_id: str, company_name: str):
|
478 |
+
logging.info(f"Received request for company_id and company_id: {company_id},{company_id}")
|
479 |
+
try:
|
480 |
+
cnx = get_db_connection()
|
481 |
+
cursor = cnx.cursor()
|
482 |
+
query = """
|
483 |
+
SELECT kb.kid,kb.company_id, kb.file_path, kb.document_name, kb.document_desc,kb.department,kb.version,kb.vectorDBflag,kb.last_updated
|
484 |
+
FROM redmind_gpt.knowledge_base kb
|
485 |
+
JOIN redmind_gpt.company_detail cd ON kb.company_id = cd.company_id
|
486 |
+
WHERE kb.company_id = %s
|
487 |
+
"""
|
488 |
+
logging.info(f"Executing query: {query} with company_id: {company_id}")
|
489 |
+
values= (company_id,)
|
490 |
+
# logging.info(f"Query parameters: {params}")
|
491 |
+
print(f"Query parameters: {values}")
|
492 |
+
|
493 |
+
cursor.execute(query, values) # Pa
|
494 |
+
result = cursor.fetchall()
|
495 |
+
logging.info(f"Query result: {result}")
|
496 |
+
cursor.close
|
497 |
+
cnx.close()
|
498 |
+
companies=[]
|
499 |
+
for row in result:
|
500 |
+
|
501 |
+
companies.append({
|
502 |
+
"kid":row[0],
|
503 |
+
"company_id": row[1],
|
504 |
+
"file_path":row[2],
|
505 |
+
"document_name": row[3],
|
506 |
+
"document_desc": row[4],
|
507 |
+
"department": row[5],
|
508 |
+
"version": row[6],
|
509 |
+
"vectorDBflag":row[7],
|
510 |
+
"last_updated": row[8]
|
511 |
+
})
|
512 |
+
if companies:
|
513 |
+
return companies
|
514 |
+
else:
|
515 |
+
logging.warning(f"No document found for company_id: {company_id}")
|
516 |
+
raise HTTPException(status_code=404, detail="Data document not found")
|
517 |
+
except mysql.connector.Error as err:
|
518 |
+
logging.error(f"Database error: {err}")
|
519 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
520 |
+
|
521 |
#to get data for view in knowledgebase
|
522 |
@app.get("/api/getknowledgebase/{company_id}")
|
523 |
async def get_company_details(company_id: int):
|
|
|
551 |
raise HTTPException(status_code=500, detail="Failed to fetch company")
|
552 |
|
553 |
# to edit the knowledgebase details
|
554 |
+
@app.put("/api/putknowledgebase/{companyId}")
|
555 |
+
async def update_company_details(
|
556 |
+
request: Request,
|
557 |
+
companyId: int,
|
558 |
+
company_id: str = Form(...),
|
559 |
+
file_path: UploadFile = File(...),
|
560 |
+
documentName: str = Form(...),
|
561 |
+
documentDescription: str = Form(...),
|
562 |
+
department: str = Form(...),
|
563 |
+
version: str = Form(...),
|
564 |
+
vectorDBFlag: str = Form(...),
|
565 |
+
lastUpdated: str = Form(...)
|
566 |
+
):
|
567 |
+
logging.info(f"Received request for company data with ID inside edit/update knowledgebase: {companyId}")
|
568 |
+
print(f"Received request for company data with ID inside edit/update knowledgebase file name: {file_path.filename}")
|
569 |
|
570 |
+
# Create the upload folder if it doesn't exist
|
571 |
+
upload_folder = "uploads/"
|
572 |
+
os.makedirs(upload_folder, exist_ok=True)
|
573 |
+
|
574 |
+
# Construct the file path for saving
|
575 |
+
saved_file_path = os.path.join(upload_folder, file_path.filename)
|
576 |
|
577 |
+
try:
|
578 |
+
# Save the uploaded file to the server
|
579 |
+
with open(saved_file_path, "wb") as buffer:
|
580 |
+
shutil.copyfileobj(file_path.file, buffer)
|
581 |
+
except Exception as e:
|
582 |
+
logging.error(f"Error saving file: {e}")
|
583 |
+
raise HTTPException(status_code=500, detail="Failed to save file")
|
584 |
|
585 |
+
# Prepare the company data dictionary
|
586 |
company_data = {
|
587 |
+
'kid': companyId,
|
588 |
'company_id': company_id,
|
589 |
+
'file_path': saved_file_path, # Use the path where the file was saved
|
590 |
'document_name': documentName,
|
591 |
'document_desc': documentDescription,
|
592 |
'department': department,
|
|
|
595 |
'last_updated': lastUpdated
|
596 |
}
|
597 |
|
598 |
+
# Update the knowledge base in the database
|
599 |
+
updated_company = await update_knowledge_in_db(companyId, company_data)
|
600 |
if not updated_company:
|
601 |
raise HTTPException(status_code=500, detail="Failed to update company")
|
602 |
return updated_company
|
603 |
+
|
604 |
async def update_knowledge_in_db(kid: int, company_data: dict):
|
605 |
try:
|
606 |
logging.info(f"Updating knowledge base for ID: {kid}")
|
607 |
cnx = get_db_connection()
|
608 |
if cnx is None:
|
609 |
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
610 |
+
|
611 |
cursor = cnx.cursor()
|
612 |
update_query = """
|
613 |
+
UPDATE redmind_gpt.knowledge_base kb
|
614 |
+
SET kb.company_id = %s, kb.document_name = %s, kb.document_desc = %s,
|
615 |
+
kb.department = %s, kb.version = %s, kb.vectorDBflag = %s, kb.last_updated = %s
|
616 |
WHERE kb.kid = %s;
|
617 |
"""
|
618 |
+
logging.info(f"Executing update query: {update_query}")
|
619 |
+
logging.info(f"With values: {(
|
620 |
+
company_data['company_id'],
|
621 |
+
company_data['document_name'],
|
622 |
+
company_data['document_desc'],
|
623 |
+
company_data['department'],
|
624 |
+
company_data['version'],
|
625 |
+
company_data['vectorDBflag'],
|
626 |
+
company_data['last_updated'],
|
627 |
+
kid
|
628 |
+
)}")
|
629 |
+
|
630 |
cursor.execute(update_query, (
|
631 |
company_data['company_id'],
|
|
|
632 |
company_data['document_name'],
|
633 |
company_data['document_desc'],
|
634 |
company_data['department'],
|
|
|
642 |
success = cursor.rowcount > 0
|
643 |
cursor.close()
|
644 |
cnx.close()
|
645 |
+
|
646 |
if not success:
|
647 |
+
logging.info("No rows updated")
|
648 |
return None
|
649 |
+
logging.info("Update successful")
|
650 |
return company_data
|
651 |
except mysql.connector.Error as err:
|
652 |
+
logging.error(f"Database error: {err}")
|
653 |
raise HTTPException(status_code=500, detail="Failed to update company")
|
654 |
+
except Exception as e:
|
655 |
+
logging.error(f"Unexpected error: {e}")
|
656 |
+
raise HTTPException(status_code=500, detail="Unexpected error occurred")
|
657 |
|
658 |
|
659 |
def delete_knowledge_from_db(company_id: int) -> bool:
|
|
|
675 |
except mysql.connector.Error as err:
|
676 |
logging.error(f"Error deleting company: {err}")
|
677 |
raise HTTPException(status_code=500, detail="Failed to delete company")
|
678 |
+
|
679 |
+
#to perform delete operation in knowlegebase
|
680 |
@app.delete("/api/delknowledgebase/{company_id}")
|
681 |
async def delete_company(company_id: int):
|
682 |
deletion_success = delete_knowledge_from_db(company_id)
|
|
|
701 |
password: str=Form(...),
|
702 |
selectedTables: List[str] = Form(...)):
|
703 |
logging.info(f"Received form submission for database_connectors")
|
704 |
+
print(f"Received form submission for database_connectors")
|
705 |
try:
|
706 |
cnx = get_db_connection()
|
707 |
cursor = cnx.cursor()
|
|
|
717 |
SET databasetype = %s, serverip = %s, port = %s, database_name = %s, username = %s, password = %s, dbtablename = %s
|
718 |
WHERE company_id = %s
|
719 |
"""
|
720 |
+
values = (",".join(database), server, port, ",".join(databaseName), username, password or '', ",".join(selectedTables), company_id)
|
721 |
logging.info(f"Executing update query: {query} with values: {values}")
|
722 |
cursor.execute(query, values)
|
723 |
cnx.commit()
|
|
|
728 |
INSERT INTO data_connectors(company_id, databasetype, serverip, port, database_name, username, password, dbtablename)
|
729 |
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
730 |
"""
|
731 |
+
values = (company_id, ",".join(database), server, port, ",".join(databaseName), username, password or '', ",".join(selectedTables))
|
732 |
logging.info(f"Executing insert query: {query} with values: {values}")
|
733 |
cursor.execute(query, values)
|
734 |
cnx.commit()
|
|
|
736 |
|
737 |
cursor.close()
|
738 |
cnx.close()
|
739 |
+
|
740 |
+
# logging.info(f"Data_connectors for {database} processed successfully")
|
741 |
+
# return JSONResponse(content={"status": "success", "message": "Data saved successfully"}, status_code=200)
|
742 |
+
response = {
|
743 |
+
"msg": "Data saved successfully",
|
744 |
+
"url": "/save_data_connectors", # The URL you want to redirect to
|
745 |
+
"created": True
|
746 |
+
}
|
747 |
+
return JSONResponse(content=response)
|
748 |
+
|
749 |
+
|
750 |
except mysql.connector.Error as err:
|
751 |
logging.error(f"Database error: {err}")
|
752 |
+
return JSONResponse(content={"status": "error", "message": "Internal Server Error"}, status_code=500)
|
753 |
+
except Exception as e:
|
754 |
+
logging.error(f"Unexpected error: {e}")
|
755 |
+
return JSONResponse(content={"status": "error", "message": "Unexpected Server Error"}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
756 |
|
757 |
@app.get("/api/check_data_connectors")
|
758 |
async def get_data_connectors(company_id: str = Query(...), company_name: str = Query(...)):
|
|
|
841 |
cnx =get_db_connection()
|
842 |
cursor = cnx.cursor()
|
843 |
query = """
|
844 |
+
SELECT ac.id, ac.company_id, ac.api_name, ac.api_endpoint,ac.auth_token,ac.input_param, ac.output_json, ac.description
|
845 |
FROM u852023448_redmindgpt.api_connectors ac
|
846 |
JOIN u852023448_redmindgpt.company_detail cd ON ac.company_id = cd.company_id
|
847 |
WHERE ac.company_id = %s and cd.company_name=%s
|
|
|
859 |
companies=[]
|
860 |
for row in result:
|
861 |
companies.append({
|
862 |
+
"row_id":row[0],
|
863 |
+
"company_id": row[1],
|
864 |
+
"APIName":row[2],
|
865 |
+
"APIEndpoint": row[3]
|
866 |
# "Auth_Bearer": result[3],
|
867 |
# "Inputjson": result[4],
|
868 |
#"OutputJson": result[5],
|
|
|
877 |
except mysql.connector.Error as err:
|
878 |
logging.error(f"Database error: {err}")
|
879 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
880 |
+
#to view the table details in modal
|
881 |
+
@app.get("/api/viewapiconnectors/{company_id}")
|
882 |
+
async def get_company_details(company_id: int):
|
883 |
|
884 |
+
company = await get_api_from_db(company_id)
|
885 |
+
if not company:
|
886 |
+
raise HTTPException(status_code=404, detail="Company not found")
|
887 |
+
return company
|
888 |
+
|
889 |
+
async def get_api_from_db(company_id: int):
|
890 |
+
try:
|
891 |
+
# Establish a connection to the database
|
892 |
+
cnx = get_db_connection()
|
893 |
+
if cnx is None:
|
894 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
895 |
+
cursor = cnx.cursor(dictionary=True)
|
896 |
+
query = "SELECT * FROM api_connectors WHERE id = %s"
|
897 |
+
cursor.execute(query, (company_id,))
|
898 |
+
company = cursor.fetchone()
|
899 |
+
cursor.close()
|
900 |
+
cnx.close()
|
901 |
+
if company:
|
902 |
+
logging.info(f"api details:{company}")
|
903 |
+
return company
|
904 |
+
else:
|
905 |
+
raise HTTPException(status_code=404, detail="Company not found or file not found for the company")
|
906 |
+
except mysql.connector.Error as err:
|
907 |
+
logging.error(f"Error fetching company: {err}")
|
908 |
+
raise HTTPException(status_code=500, detail="Failed to fetch company")
|
909 |
+
#to edit the api details in modal form
|
910 |
+
@app.put("/api/editapiconnectors/{companyId}")
|
911 |
+
async def update_company_details(
|
912 |
+
request: Request,
|
913 |
+
companyId: int,
|
914 |
+
company_id:str=Form(...),
|
915 |
+
APIName:str=Form(...),
|
916 |
+
APIEndpoint:str=Form(...),
|
917 |
+
Auth_Bearer:str=Form(...),
|
918 |
+
Inputjson:str=Form(...),
|
919 |
+
OutputJson:str=Form(...),
|
920 |
+
Description:str=Form(...)):
|
921 |
+
logging.info(f"Received form submission for database_connectors")
|
922 |
+
logging.info(f"Received request for company data with ID inside edit/update knowledgebase: {companyId}")
|
923 |
+
|
924 |
+
# Prepare the company data dictionary
|
925 |
+
company_data = {
|
926 |
+
'kid': companyId,
|
927 |
+
'company_id': company_id,
|
928 |
+
'api_name': APIName,
|
929 |
+
'api_endpoint': APIEndpoint,
|
930 |
+
'auth_token': Auth_Bearer,
|
931 |
+
'input_param': Inputjson,
|
932 |
+
'output_json': OutputJson,
|
933 |
+
'description': Description
|
934 |
+
}
|
935 |
+
|
936 |
+
# Update the knowledge base in the database
|
937 |
+
updated_company = await update_api_in_db(companyId, company_data)
|
938 |
+
if not updated_company:
|
939 |
+
raise HTTPException(status_code=500, detail="Failed to update company")
|
940 |
+
return updated_company
|
941 |
+
|
942 |
+
async def update_api_in_db(id: int, company_data: dict):
|
943 |
+
try:
|
944 |
+
logging.info(f"Updating api for ID: {id}")
|
945 |
+
cnx = get_db_connection()
|
946 |
+
if cnx is None:
|
947 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
948 |
+
|
949 |
+
cursor = cnx.cursor()
|
950 |
+
update_query = """
|
951 |
+
UPDATE redmind_gpt.api_connectors ac
|
952 |
+
SET ac.company_id = %s, ac.api_name = %s, ac.api_endpoint= %s,
|
953 |
+
ac.auth_token= %s, ac.input_param= %s,ac.output_json = %s, ac.description= %s
|
954 |
+
WHERE ac.id = %s;
|
955 |
+
"""
|
956 |
+
logging.info(f"Executing update query: {update_query}")
|
957 |
+
logging.info(f"With values: {(
|
958 |
+
company_data['company_id'],
|
959 |
+
company_data['api_name'],
|
960 |
+
company_data['api_endpoint'],
|
961 |
+
company_data['auth_token'],
|
962 |
+
company_data['input_param'],
|
963 |
+
company_data['output_json'],
|
964 |
+
company_data['description'],
|
965 |
+
id
|
966 |
+
)}")
|
967 |
+
|
968 |
+
cursor.execute(update_query, (
|
969 |
+
company_data['company_id'],
|
970 |
+
company_data['api_name'],
|
971 |
+
company_data['api_endpoint'],
|
972 |
+
company_data['auth_token'],
|
973 |
+
company_data['input_param'],
|
974 |
+
company_data['output_json'],
|
975 |
+
company_data['description'],
|
976 |
+
id
|
977 |
+
))
|
978 |
+
|
979 |
+
cnx.commit()
|
980 |
+
success = cursor.rowcount > 0
|
981 |
+
cursor.close()
|
982 |
+
cnx.close()
|
983 |
+
|
984 |
+
if not success:
|
985 |
+
logging.info("No rows updated")
|
986 |
+
return None
|
987 |
+
logging.info("Update successful")
|
988 |
+
return company_data
|
989 |
+
except mysql.connector.Error as err:
|
990 |
+
logging.error(f"Database error: {err}")
|
991 |
+
raise HTTPException(status_code=500, detail="Failed to update company")
|
992 |
+
except Exception as e:
|
993 |
+
logging.error(f"Unexpected error: {e}")
|
994 |
+
raise HTTPException(status_code=500, detail="Unexpected error occurred")
|
995 |
+
|
996 |
+
#on update of modal form the data table is refreshed to dispalyupdated value in datatable
|
997 |
+
@app.get("/api/api_updatetable")
|
998 |
+
async def get_document(company_id: str = Query(...)):
|
999 |
+
print(f"Received companyId and name for api datatable update: {company_id},{company_id}") # Log rec
|
1000 |
+
#async def get_data_connectors(company_id: str, company_name: str):
|
1001 |
+
logging.info(f"Received request for company_id and company_id: {company_id},{company_id}")
|
1002 |
+
try:
|
1003 |
+
cnx = get_db_connection()
|
1004 |
+
cursor = cnx.cursor()
|
1005 |
+
query=""" SELECT ac.id,ac.company_id, ac.api_name, ac.api_endpoint,ac.auth_token,ac.input_param, ac.output_json, ac.description
|
1006 |
+
FROM redmind_gpt.api_connectors ac
|
1007 |
+
JOIN redmind_gpt.company_detail cd ON ac.company_id = cd.company_id
|
1008 |
+
WHERE ac.company_id = %s
|
1009 |
+
"""
|
1010 |
+
logging.info(f"Executing query: {query} with company_id: {company_id}")
|
1011 |
+
values= (company_id,)
|
1012 |
+
# logging.info(f"Query parameters: {params}")
|
1013 |
+
print(f"Query parameters: {values}")
|
1014 |
+
|
1015 |
+
cursor.execute(query, values) # Pa
|
1016 |
+
result = cursor.fetchall()
|
1017 |
+
logging.info(f"Query result for update table: {result}")
|
1018 |
+
cursor.close
|
1019 |
+
cnx.close()
|
1020 |
+
companies=[]
|
1021 |
+
for row in result:
|
1022 |
+
|
1023 |
+
companies.append({
|
1024 |
+
"row_id":row[0],
|
1025 |
+
"company_id": row[1],
|
1026 |
+
"api_name":row[2],
|
1027 |
+
"api_endpoint": row[3],
|
1028 |
+
# "Auth_Bearer": row[4],
|
1029 |
+
# "Inputjson": row[5],
|
1030 |
+
# "OutputJson": row[6],
|
1031 |
+
# "description": row[7]
|
1032 |
+
|
1033 |
+
})
|
1034 |
+
if companies:
|
1035 |
+
return companies
|
1036 |
+
else:
|
1037 |
+
logging.warning(f"No document found for company_id: {company_id}")
|
1038 |
+
raise HTTPException(status_code=404, detail="Data document not found")
|
1039 |
+
except mysql.connector.Error as err:
|
1040 |
+
logging.error(f"Database error: {err}")
|
1041 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
1042 |
+
|
1043 |
+
#to delete api details from db
|
1044 |
+
@app.delete("/api/deleteapi/{company_id}")
|
1045 |
+
async def delete_company(company_id: int):
|
1046 |
+
deletion_success = delete_api_from_db(company_id)
|
1047 |
+
if not deletion_success:
|
1048 |
+
raise HTTPException(status_code=404, detail="Company not found or failed to delete")
|
1049 |
+
return {"message": "Company deleted successfully"}
|
1050 |
+
|
1051 |
+
def delete_api_from_db(company_id: int) -> bool:
|
1052 |
+
print(f"Received api for company_id: {company_id}") # Debug statement
|
1053 |
+
logging.info(f"Received request for api for company id: {company_id}")
|
1054 |
+
try:
|
1055 |
+
# Establish a connection to the database
|
1056 |
+
cnx = get_db_connection()
|
1057 |
+
if cnx is None:
|
1058 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
1059 |
+
cursor = cnx.cursor()
|
1060 |
+
delete_query = "DELETE FROM api_connectors WHERE id = %s"
|
1061 |
+
cursor.execute(delete_query, (company_id,))
|
1062 |
+
cnx.commit()
|
1063 |
+
success = cursor.rowcount > 0
|
1064 |
+
cursor.close()
|
1065 |
+
cnx.close()
|
1066 |
+
return success
|
1067 |
+
except mysql.connector.Error as err:
|
1068 |
+
logging.error(f"Error deleting company: {err}")
|
1069 |
+
raise HTTPException(status_code=500, detail="Failed to delete company")
|
1070 |
+
|
1071 |
|
1072 |
@app.get("/prompt_template")
|
1073 |
async def prompt_template(request: Request):
|
|
|
1108 |
cnx =get_db_connection()
|
1109 |
cursor = cnx.cursor()
|
1110 |
query = """
|
1111 |
+
SELECT pt.id,pt.company_id,pt.scenario,pt.prompts,pt.comments
|
1112 |
FROM u852023448_redmindgpt.prompt_templates pt
|
1113 |
JOIN u852023448_redmindgpt.company_detail cd ON pt.company_id = cd.company_id
|
1114 |
WHERE pt.company_id = %s and cd.company_name=%s
|
|
|
1126 |
companies=[]
|
1127 |
for row in result:
|
1128 |
companies.append({
|
1129 |
+
"row_id":row[0],
|
1130 |
+
"company_id": row[1],
|
1131 |
+
"scenario":row[2],
|
1132 |
+
"prompt": row[3]
|
1133 |
# "Auth_Bearer": result[3],
|
1134 |
# "Inputjson": result[4],
|
1135 |
#"OutputJson": result[5],
|
|
|
1144 |
except mysql.connector.Error as err:
|
1145 |
logging.error(f"Database error: {err}")
|
1146 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
1147 |
+
def delete_prompt_template_from_db(row_id: int) -> bool:
|
1148 |
+
logging.info(f"Received request for prompt_template company id: {row_id}")
|
1149 |
+
logging.info(f"Received request for prompt_template row id: {row_id}")
|
1150 |
+
|
1151 |
+
try:
|
1152 |
+
# Establish a connection to the database
|
1153 |
+
cnx = get_db_connection()
|
1154 |
+
if cnx is None:
|
1155 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
1156 |
+
cursor = cnx.cursor()
|
1157 |
+
delete_query = "DELETE FROM prompt_templates WHERE id = %s"
|
1158 |
+
|
1159 |
+
logging.info(f"sql delete query for prompt template ===> {delete_query}")
|
1160 |
+
|
1161 |
+
cursor.execute(delete_query, (row_id,))
|
1162 |
+
cnx.commit()
|
1163 |
+
success = cursor.rowcount > 0
|
1164 |
+
logging.info (f"deleted succesfully ! ===> {success}")
|
1165 |
+
|
1166 |
+
cursor.close()
|
1167 |
+
cnx.close()
|
1168 |
+
return success
|
1169 |
+
except mysql.connector.Error as err:
|
1170 |
+
print('python')
|
1171 |
+
logging.error(f"Error deleting company: {err}")
|
1172 |
+
raise HTTPException(status_code=500, detail="Failed to delete company")
|
1173 |
+
|
1174 |
+
@app.delete("/api/prompt_template_for_del/{row_id}")
|
1175 |
+
async def delete_company(row_id: int):
|
1176 |
+
deletion_success = delete_prompt_template_from_db(row_id)
|
1177 |
+
logging.info(f"company row_id +++> {row_id}")
|
1178 |
+
|
1179 |
+
if not deletion_success:
|
1180 |
+
raise HTTPException(status_code=404, detail="Company not found or failed to delete")
|
1181 |
+
return {"message": "Company deleted successfully"}
|
1182 |
+
|
1183 |
+
# promt_template view function ! ............
|
1184 |
+
|
1185 |
+
#to get data for view in promt_templae by id
|
1186 |
+
@app.get("/api/getpromttemplate/{company_id}")
|
1187 |
+
async def get_promt_company_details(company_id: int):
|
1188 |
+
company = await get_promt_from_db(company_id)
|
1189 |
+
if not company:
|
1190 |
+
raise HTTPException(status_code=404, detail="Company not found")
|
1191 |
+
return company
|
1192 |
+
|
1193 |
+
async def get_promt_from_db(company_id: int):
|
1194 |
+
try:
|
1195 |
+
# Establish a connection to the database
|
1196 |
+
cnx = get_db_connection()
|
1197 |
+
if cnx is None:
|
1198 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
1199 |
+
cursor = cnx.cursor(dictionary=True)
|
1200 |
+
query = "SELECT * FROM prompt_templates WHERE id = %s"
|
1201 |
+
logging.info(f"row_id in db addresss ========> {company_id}")
|
1202 |
+
|
1203 |
+
cursor.execute(query, (company_id,))
|
1204 |
+
company = cursor.fetchone()
|
1205 |
+
cursor.close()
|
1206 |
+
cnx.close()
|
1207 |
+
if company:
|
1208 |
+
logging.info(f"row_id in db addresss ========> {company}")
|
1209 |
+
return company
|
1210 |
+
|
1211 |
+
else:
|
1212 |
+
raise HTTPException(status_code=404, detail="Company not found or file not found for the company")
|
1213 |
+
except mysql.connector.Error as err:
|
1214 |
+
logging.error(f"Error fetching company: {err}")
|
1215 |
+
raise HTTPException(status_code=500, detail="Failed to fetch company")
|
1216 |
+
|
1217 |
+
# Function to update company details
|
1218 |
+
@app.put("/api/putprompttemplates/{kid}")
|
1219 |
+
async def update_company_details(
|
1220 |
+
kid: int,
|
1221 |
+
scenario: str = Form(...),
|
1222 |
+
prompt: str = Form(...),
|
1223 |
+
comments: str = Form(...)
|
1224 |
+
):
|
1225 |
+
logging.info(f"Received request for company data with ID: {kid}")
|
1226 |
+
company_data = {
|
1227 |
+
'scenario': scenario,
|
1228 |
+
'prompts': prompt,
|
1229 |
+
'comments': comments,
|
1230 |
+
}
|
1231 |
+
updated_company = await update_prompt_in_db(kid, company_data)
|
1232 |
+
if not updated_company:
|
1233 |
+
raise HTTPException(status_code=500, detail="Failed to update company")
|
1234 |
+
return updated_company
|
1235 |
|
1236 |
+
# Database query function to update company data
|
1237 |
+
async def update_prompt_in_db(kid: int, company_data: dict):
|
1238 |
+
try:
|
1239 |
+
logging.info(f"Updating prompt for ID: {kid}")
|
1240 |
+
cnx = get_db_connection()
|
1241 |
+
if cnx is None:
|
1242 |
+
raise HTTPException(status_code=500, detail="Failed to connect to the database")
|
1243 |
+
|
1244 |
+
cursor = cnx.cursor()
|
1245 |
+
update_query = """
|
1246 |
+
UPDATE redmind_gpt.prompt_templates pt
|
1247 |
+
SET pt.scenario=%s, pt.prompts=%s, pt.comments=%s
|
1248 |
+
WHERE pt.id = %s;
|
1249 |
+
"""
|
1250 |
+
logging.info(f"row_id in prompt db address ========> {kid}")
|
1251 |
+
logging.info(f"SQL update query for company ===> {update_query}")
|
1252 |
+
|
1253 |
+
cursor.execute(update_query, (
|
1254 |
+
company_data['scenario'],
|
1255 |
+
company_data['prompts'],
|
1256 |
+
company_data['comments'],
|
1257 |
+
kid
|
1258 |
+
))
|
1259 |
+
|
1260 |
+
cnx.commit()
|
1261 |
+
success = cursor.rowcount > 0
|
1262 |
+
cursor.close()
|
1263 |
+
cnx.close()
|
1264 |
+
|
1265 |
+
if not success:
|
1266 |
+
return None
|
1267 |
+
return company_data
|
1268 |
+
|
1269 |
+
except mysql.connector.Error as err:
|
1270 |
+
logging.error(f"Error updating company: {err}")
|
1271 |
+
raise HTTPException(status_code=500, detail="Failed to update company")
|
1272 |
+
|
1273 |
+
# to refresh prompt data table
|
1274 |
+
@app.get("/api/prompt_update")
|
1275 |
+
async def get_document(company_id: str = Query(...)):
|
1276 |
+
print(f"Received companyId and name: {company_id},{company_id}") # Log rec
|
1277 |
+
#async def get_data_connectors(company_id: str, company_name: str):
|
1278 |
+
logging.info(f"Received request for company_id and company_id: {company_id},{company_id}")
|
1279 |
+
try:
|
1280 |
+
cnx = get_db_connection()
|
1281 |
+
cursor = cnx.cursor()
|
1282 |
+
query = """
|
1283 |
+
SELECT pt.id,pt.company_id,pt.scenario,pt.prompts,pt.comments
|
1284 |
+
FROM redmind_gpt.prompt_templates pt
|
1285 |
+
JOIN redmind_gpt.company_detail cd ON pt.company_id = cd.company_id
|
1286 |
+
WHERE pt.company_id = %s
|
1287 |
+
"""
|
1288 |
+
logging.info(f"Executing query: {query} with company_id: {company_id}")
|
1289 |
+
values= (company_id,)
|
1290 |
+
# logging.info(f"Query parameters: {params}")
|
1291 |
+
print(f"Query parameters: {values}")
|
1292 |
+
|
1293 |
+
cursor.execute(query, values) # Pa
|
1294 |
+
result = cursor.fetchall()
|
1295 |
+
logging.info(f"Query result: {result}")
|
1296 |
+
cursor.close
|
1297 |
+
cnx.close()
|
1298 |
+
companies=[]
|
1299 |
+
for row in result:
|
1300 |
+
companies.append({
|
1301 |
+
'id':row[0],
|
1302 |
+
"company_id": row[1],
|
1303 |
+
"scenario":row[2],
|
1304 |
+
"prompts": row[3]
|
1305 |
+
# "Auth_Bearer": result[3],
|
1306 |
+
# "Inputjson": result[4],
|
1307 |
+
#"OutputJson": result[5],
|
1308 |
+
#"description": result[6]
|
1309 |
+
|
1310 |
+
})
|
1311 |
+
if companies:
|
1312 |
+
logging.info(f"the primary key id is {companies}")
|
1313 |
+
return companies
|
1314 |
+
else:
|
1315 |
+
logging.warning(f"No document found for company_id: {company_id}")
|
1316 |
+
raise HTTPException(status_code=404, detail="Data document not found")
|
1317 |
+
except mysql.connector.Error as err:
|
1318 |
+
logging.error(f"Database error: {err}")
|
1319 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
1320 |
|
1321 |
@app.get("/chatbot")
|
1322 |
async def chatbot(request: Request):
|