Skip to content

Commit

Permalink
Revert "Handle 400 status code on copilot creation process. "
Browse files Browse the repository at this point in the history
  • Loading branch information
gharbat authored Dec 5, 2023
1 parent 97722ea commit 43b47fc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 93 deletions.
94 changes: 49 additions & 45 deletions dashboard/app/(main)/create/copilot/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="relative p-1">
{loading && (
Expand Down
Binary file modified llm-server/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion llm-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
8 changes: 3 additions & 5 deletions llm-server/models/repository/copilot_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}")
Expand Down
17 changes: 8 additions & 9 deletions llm-server/routes/_swagger/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -68,16 +70,13 @@ 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}")
except Exception as e:
# 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
Expand Down
23 changes: 11 additions & 12 deletions llm-server/routes/copilot/copilot_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -181,7 +180,7 @@ def validator(copilot_id):
jsonify(
{
"error": "Failed to load the swagger file for validation. error: "
+ str(e)
+ str(e)
}
),
400,
Expand Down
Binary file modified llm-server/utils/.DS_Store
Binary file not shown.
21 changes: 0 additions & 21 deletions workers/.idea/workspace.xml

This file was deleted.

0 comments on commit 43b47fc

Please sign in to comment.