diff --git a/dashboard/app/(main)/create/copilot/page.tsx b/dashboard/app/(main)/create/copilot/page.tsx index ac53410aa..cf16f80e4 100644 --- a/dashboard/app/(main)/create/copilot/page.tsx +++ b/dashboard/app/(main)/create/copilot/page.tsx @@ -153,67 +153,71 @@ function UploadSwaggerStep() { const bothSelected = swaggerFile && !_.isEmpty(swaggerEndpoints); // spagetti 🍝 async function handleCreateCopilot() { - const hasSwaggerFile = Boolean(swaggerFile); - const hasSwaggerEndpoints = !_.isEmpty(swaggerEndpoints); - const isBothSelected = bothSelected; - - if (!hasSwaggerFile && !hasSwaggerEndpoints) { + if (!swaggerFile && _.isEmpty(swaggerEndpoints)) { toast({ title: "No swagger file uploaded or created!", - description: "Please upload a swagger file or create one using the form", + description: + "Please upload a swagger file to continue, or create one using the form", variant: "destructive", }); return; } - - if (isBothSelected) { + if (bothSelected) { toast({ title: "Both swagger file and swagger definition created!", - description: "Please reset one of them to continue, you can't use both at the same time", + description: + "Please reset one of them to continue, you can't use both at the same time", variant: "destructive", }); return; } - - setLoading(true); - - try { - if (!createdCopilot) { - const swaggerContent = hasSwaggerFile - ? swaggerFile - : generateSwaggerDefinition(swaggerEndpoints); - - const swaggerFileObject = new File([JSON.stringify(swaggerContent)], "swagger.json", { - type: "application/json", - }); - - const res = await createCopilot({ swagger_file: swaggerFileObject }); - if (res.data) { - setCopilot(res.data.chatbot); - toast({ - title: "Copilot Created Successfully", - description: "You have created your copilot successfully", - variant: "success", - }); - popConfetti(5); - _.delay(nextStep, 1000); + else { + setLoading(true); + try { + if (!createdCopilot) { + if (swaggerFile) { + const res = await createCopilot({ + swagger_file: swaggerFile, + }); + if (res.data) { + setCopilot(res.data.chatbot); + toast({ + title: "Copilot Created Successfully", + description: "You have created your copilot successfully", + variant: "success", + }); + popConfetti(5) + _.delay(nextStep, 1000); + } + } + if (!_.isEmpty(swaggerEndpoints)) { + const swaggerDefinition = generateSwaggerDefinition(swaggerEndpoints); + const swagger_file = new File([JSON.stringify(swaggerDefinition)], "swagger.json", { + type: "application/json", + }) + console.log(swagger_file); + const res = await createCopilot({ + swagger_file, + }); + if (res.data) { + setCopilot(res.data.chatbot); + toast({ + title: "Copilot Created Successfully", + description: "You have created your copilot successfully", + variant: "success", + }); + popConfetti(5) + _.delay(nextStep, 1000); + } + } } + } catch (error) { + setLoading(false); } - } catch (error) { - // @ts-ignore - const failure = error?.response?.data?.failure; - toast({ - title: "Error", - description: failure, - variant: "destructive", - }); - // go to next step - nextStep(); - } finally { - setLoading(false); + } + setLoading(false); } - return (
{loading && ( diff --git a/llm-server/.DS_Store b/llm-server/.DS_Store index cbe79b365..f87d811b0 100644 Binary files a/llm-server/.DS_Store and b/llm-server/.DS_Store differ diff --git a/llm-server/Dockerfile b/llm-server/Dockerfile index fe7cf7fe9..0e92a13e4 100644 --- a/llm-server/Dockerfile +++ b/llm-server/Dockerfile @@ -14,4 +14,4 @@ CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:5678", "--wait-for-client", # Production stage FROM common AS production EXPOSE 8002 -CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=8002", "--reload"] +CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=8002"] diff --git a/llm-server/models/repository/copilot_repo.py b/llm-server/models/repository/copilot_repo.py index 5b9b57533..1b1211255 100644 --- a/llm-server/models/repository/copilot_repo.py +++ b/llm-server/models/repository/copilot_repo.py @@ -126,8 +126,7 @@ def create_copilot( try: session.add(new_chatbot) session.commit() - session.refresh(new_chatbot) - return new_chatbot + return chatbot_to_dict(new_chatbot) except Exception as e: session.rollback() logger.error("An exception occurred", app="OPENCOPILOT", error=str(e), incident="swagger") @@ -222,7 +221,7 @@ def update_copilot( enhanced_privacy: Optional[bool] = None, smart_sync: Optional[bool] = None, website: Optional[str] = None, -) -> Type[Chatbot]: +) -> dict[str, Any]: """ Updates an existing Chatbot instance in the database. @@ -265,8 +264,7 @@ def update_copilot( chatbot.updated_at = datetime.datetime.utcnow() session.commit() - session.refresh(chatbot) - return chatbot + return chatbot_to_dict(chatbot) except exc.NoResultFound: session.rollback() raise ValueError(f"No Chatbot found with id: {copilot_id}") diff --git a/llm-server/routes/_swagger/service.py b/llm-server/routes/_swagger/service.py index e99cec237..ac5aa0feb 100644 --- a/llm-server/routes/_swagger/service.py +++ b/llm-server/routes/_swagger/service.py @@ -36,19 +36,21 @@ def save_swagger_paths_to_qdrant(swagger_doc: ResolvingParser, bot_id: str): # delete documents with metadata in api with the current bot id, before reingesting documents: List[Document] = [] paths = swagger_doc.specification.get("paths", {}) - + for path, operations in paths.items(): for method, operation in operations.items(): try: operation["method"] = method operation["path"] = path del operation["responses"] - + # Check if "summary" key is present before accessing it - summary = operation.get("summary", "") - description = operation.get("description", "") - - document = Document(page_content=f"{summary}; {description}") + summary = operation.get('summary', '') + description = operation.get('description', '') + + document = Document( + page_content=f"{summary}; {description}" + ) document.metadata["bot_id"] = bot_id document.metadata["operation"] = operation @@ -68,8 +70,6 @@ def save_swagger_paths_to_qdrant(swagger_doc: ResolvingParser, bot_id: str): incident="api_ingestion_qdrant", point_ids=point_ids, ) - - return point_ids except KeyError as e: # Handle the specific key error at a higher level if needed logger.error(f"KeyError in processing paths: {e}") @@ -77,7 +77,6 @@ def save_swagger_paths_to_qdrant(swagger_doc: ResolvingParser, bot_id: str): # Handle other exceptions logger.error(f"An error occurred: {e}") - def add_swagger_file(request: Request, id: str) -> Dict[str, str]: if request.content_type == "application/json": # JSON file diff --git a/llm-server/routes/copilot/copilot_controller.py b/llm-server/routes/copilot/copilot_controller.py index ca2cbd009..e929020b1 100644 --- a/llm-server/routes/copilot/copilot_controller.py +++ b/llm-server/routes/copilot/copilot_controller.py @@ -5,7 +5,11 @@ from flask import Blueprint, jsonify, request, Response from prance import ValidationError from sqlalchemy.exc import SQLAlchemyError +from routes.root_service import get_swagger_doc +from routes._swagger import reindex_service from werkzeug.utils import secure_filename +from utils.base import resolve_abs_local_file_path_from +from utils.get_logger import CustomLogger import routes._swagger.service as swagger_service from enums.initial_prompt import ChatBotInitialPromptEnum @@ -18,10 +22,7 @@ SessionLocal, update_copilot, ) -from routes._swagger import reindex_service -from routes.root_service import get_swagger_doc -from utils.base import resolve_abs_local_file_path_from -from utils.get_logger import CustomLogger +from utils.llm_consts import EXPERIMENTAL_FEATURES_ENABLED from utils.swagger_parser import SwaggerParser logger = CustomLogger(module_name=__name__) @@ -64,26 +65,24 @@ def handle_swagger_file(): swagger_doc = get_swagger_doc(filename) - swagger_service.save_swagger_paths_to_qdrant(swagger_doc, chatbot.id) + swagger_service.save_swagger_paths_to_qdrant(swagger_doc, chatbot["id"]) swagger_service.save_swaggerfile_to_mongo( - filename, str(chatbot.id), swagger_doc + filename, str(chatbot["id"]), swagger_doc ) except ValidationError as e: - logger.error("Failed to parse json", e=str(e), fn="handle_swagger_file") return ( jsonify( { "failure": "The copilot was created, but we failed to handle the swagger file duo to some" " validation issues, your copilot will work fine but without the ability to" - " talk with any APIs. error: {}".format(str(e)), - "cp": chatbot_to_dict(copilot) + " talk with any APIs. error: {}".format(str(e)) } ), 400, ) - return jsonify({"file_name": filename, "chatbot": chatbot_to_dict(chatbot)}) + return jsonify({"file_name": filename, "chatbot": chatbot}) return jsonify({"failure": "could_not_handle_swagger_file"}), 400 @@ -150,7 +149,7 @@ def general_settings_update(copilot_id): ) # Return the updated chatbot information - return jsonify({"chatbot": chatbot_to_dict(updated_copilot)}) + return jsonify({"chatbot": updated_copilot}) except ValueError as e: # Handle not found error return jsonify({"error": str(e)}), 404 @@ -181,7 +180,7 @@ def validator(copilot_id): jsonify( { "error": "Failed to load the swagger file for validation. error: " - + str(e) + + str(e) } ), 400, diff --git a/llm-server/utils/.DS_Store b/llm-server/utils/.DS_Store index 7f26f7404..e0f99a9be 100644 Binary files a/llm-server/utils/.DS_Store and b/llm-server/utils/.DS_Store differ diff --git a/workers/.idea/workspace.xml b/workers/.idea/workspace.xml deleted file mode 100644 index f0d84e2b9..000000000 --- a/workers/.idea/workspace.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file