add page, row_per_page to api/combine

This commit is contained in:
fadingNA
2024-11-06 22:16:00 -05:00
parent 41cbcbc07f
commit bf0dd6946e

View File

@@ -74,7 +74,8 @@ class DeleteConversation(Resource):
) )
try: try:
conversations_collection.delete_one({"_id": ObjectId(conversation_id)}) conversations_collection.delete_one(
{"_id": ObjectId(conversation_id)})
except Exception as err: except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400) return make_response(jsonify({"success": False, "error": str(err)}), 400)
return make_response(jsonify({"success": True}), 200) return make_response(jsonify({"success": True}), 200)
@@ -221,7 +222,8 @@ class DeleteByIds(Resource):
ids = request.args.get("path") ids = request.args.get("path")
if not ids: if not ids:
return make_response( return make_response(
jsonify({"success": False, "message": "Missing required fields"}), 400 jsonify(
{"success": False, "message": "Missing required fields"}), 400
) )
try: try:
@@ -244,7 +246,8 @@ class DeleteOldIndexes(Resource):
source_id = request.args.get("source_id") source_id = request.args.get("source_id")
if not source_id: if not source_id:
return make_response( return make_response(
jsonify({"success": False, "message": "Missing required fields"}), 400 jsonify(
{"success": False, "message": "Missing required fields"}), 400
) )
try: try:
@@ -255,7 +258,8 @@ class DeleteOldIndexes(Resource):
return make_response(jsonify({"status": "not found"}), 404) return make_response(jsonify({"status": "not found"}), 404)
if settings.VECTOR_STORE == "faiss": if settings.VECTOR_STORE == "faiss":
shutil.rmtree(os.path.join(current_dir, "indexes", str(doc["_id"]))) shutil.rmtree(os.path.join(
current_dir, "indexes", str(doc["_id"])))
else: else:
vectorstore = VectorCreator.create_vectorstore( vectorstore = VectorCreator.create_vectorstore(
settings.VECTOR_STORE, source_id=str(doc["_id"]) settings.VECTOR_STORE, source_id=str(doc["_id"])
@@ -305,7 +309,8 @@ class UploadFile(Resource):
user = secure_filename(request.form["user"]) user = secure_filename(request.form["user"])
job_name = secure_filename(request.form["name"]) job_name = secure_filename(request.form["name"])
try: try:
save_dir = os.path.join(current_dir, settings.UPLOAD_FOLDER, user, job_name) save_dir = os.path.join(
current_dir, settings.UPLOAD_FOLDER, user, job_name)
os.makedirs(save_dir, exist_ok=True) os.makedirs(save_dir, exist_ok=True)
if len(files) > 1: if len(files) > 1:
@@ -435,6 +440,8 @@ class CombinedJson(Resource):
user = "local" user = "local"
sort_field = request.args.get('sort', 'date') # Default to 'date' sort_field = request.args.get('sort', 'date') # Default to 'date'
sort_order = request.args.get('order', "desc") # Default to 'desc' sort_order = request.args.get('order', "desc") # Default to 'desc'
page_number = request.args.get('page', 1) # Default to 1
rows_per_page = request.args.get('rows', 10) # Default to 10
data = [ data = [
{ {
"name": "default", "name": "default",
@@ -447,7 +454,7 @@ class CombinedJson(Resource):
] ]
try: try:
for index in sources_collection.find({"user": user}).sort(sort_field, 1 if sort_order=="asc" else -1): for index in sources_collection.find({"user": user}).sort(sort_field, 1 if sort_order == "asc" else -1):
data.append( data.append(
{ {
"id": str(index["_id"]), "id": str(index["_id"]),
@@ -485,10 +492,14 @@ class CombinedJson(Resource):
"retriever": "brave_search", "retriever": "brave_search",
} }
) )
first_index = (int(page_number) - 1) * int(rows_per_page)
last_index = first_index + int(rows_per_page)
paginated_docs = data[first_index:last_index]
except Exception as err: except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400) return make_response(jsonify({"success": False, "error": str(err)}), 400)
return make_response(jsonify(data), 200) return make_response(jsonify(paginated_data), 200)
@user_ns.route("/api/docs_check") @user_ns.route("/api/docs_check")
@@ -595,7 +606,8 @@ class GetSinglePrompt(Resource):
try: try:
if prompt_id == "default": if prompt_id == "default":
with open( with open(
os.path.join(current_dir, "prompts", "chat_combine_default.txt"), os.path.join(current_dir, "prompts",
"chat_combine_default.txt"),
"r", "r",
) as f: ) as f:
chat_combine_template = f.read() chat_combine_template = f.read()
@@ -603,7 +615,8 @@ class GetSinglePrompt(Resource):
elif prompt_id == "creative": elif prompt_id == "creative":
with open( with open(
os.path.join(current_dir, "prompts", "chat_combine_creative.txt"), os.path.join(current_dir, "prompts",
"chat_combine_creative.txt"),
"r", "r",
) as f: ) as f:
chat_reduce_creative = f.read() chat_reduce_creative = f.read()
@@ -611,7 +624,8 @@ class GetSinglePrompt(Resource):
elif prompt_id == "strict": elif prompt_id == "strict":
with open( with open(
os.path.join(current_dir, "prompts", "chat_combine_strict.txt"), "r" os.path.join(current_dir, "prompts",
"chat_combine_strict.txt"), "r"
) as f: ) as f:
chat_reduce_strict = f.read() chat_reduce_strict = f.read()
return make_response(jsonify({"content": chat_reduce_strict}), 200) return make_response(jsonify({"content": chat_reduce_strict}), 200)
@@ -627,7 +641,8 @@ class GetSinglePrompt(Resource):
class DeletePrompt(Resource): class DeletePrompt(Resource):
delete_prompt_model = api.model( delete_prompt_model = api.model(
"DeletePromptModel", "DeletePromptModel",
{"id": fields.String(required=True, description="Prompt ID to delete")}, {"id": fields.String(
required=True, description="Prompt ID to delete")},
) )
@api.expect(delete_prompt_model) @api.expect(delete_prompt_model)
@@ -747,7 +762,8 @@ class CreateApiKey(Resource):
"chunks": data["chunks"], "chunks": data["chunks"],
} }
if "source" in data and ObjectId.is_valid(data["source"]): if "source" in data and ObjectId.is_valid(data["source"]):
new_api_key["source"] = DBRef("sources", ObjectId(data["source"])) new_api_key["source"] = DBRef(
"sources", ObjectId(data["source"]))
if "retriever" in data: if "retriever" in data:
new_api_key["retriever"] = data["retriever"] new_api_key["retriever"] = data["retriever"]
@@ -763,7 +779,8 @@ class CreateApiKey(Resource):
class DeleteApiKey(Resource): class DeleteApiKey(Resource):
delete_api_key_model = api.model( delete_api_key_model = api.model(
"DeleteApiKeyModel", "DeleteApiKeyModel",
{"id": fields.String(required=True, description="API Key ID to delete")}, {"id": fields.String(
required=True, description="API Key ID to delete")},
) )
@api.expect(delete_api_key_model) @api.expect(delete_api_key_model)
@@ -776,7 +793,8 @@ class DeleteApiKey(Resource):
return missing_fields return missing_fields
try: try:
result = api_key_collection.delete_one({"_id": ObjectId(data["id"])}) result = api_key_collection.delete_one(
{"_id": ObjectId(data["id"])})
if result.deleted_count == 0: if result.deleted_count == 0:
return {"success": False, "message": "API Key not found"}, 404 return {"success": False, "message": "API Key not found"}, 404
except Exception as err: except Exception as err:
@@ -811,7 +829,8 @@ class ShareConversation(Resource):
is_promptable = request.args.get("isPromptable", type=inputs.boolean) is_promptable = request.args.get("isPromptable", type=inputs.boolean)
if is_promptable is None: if is_promptable is None:
return make_response( return make_response(
jsonify({"success": False, "message": "isPromptable is required"}), 400 jsonify(
{"success": False, "message": "isPromptable is required"}), 400
) )
user = data.get("user", "local") user = data.get("user", "local")
@@ -975,7 +994,8 @@ class ShareConversation(Resource):
) )
return make_response( return make_response(
jsonify( jsonify(
{"success": True, "identifier": str(explicit_binary.as_uuid())} {"success": True, "identifier": str(
explicit_binary.as_uuid())}
), ),
201, 201,
) )
@@ -991,7 +1011,8 @@ class GetPubliclySharedConversations(Resource):
query_uuid = Binary.from_uuid( query_uuid = Binary.from_uuid(
uuid.UUID(identifier), UuidRepresentation.STANDARD uuid.UUID(identifier), UuidRepresentation.STANDARD
) )
shared = shared_conversations_collections.find_one({"uuid": query_uuid}) shared = shared_conversations_collections.find_one(
{"uuid": query_uuid})
conversation_queries = [] conversation_queries = []
if ( if (
@@ -1071,7 +1092,8 @@ class GetMessageAnalytics(Resource):
try: try:
api_key = ( api_key = (
api_key_collection.find_one({"_id": ObjectId(api_key_id)})["key"] api_key_collection.find_one(
{"_id": ObjectId(api_key_id)})["key"]
if api_key_id if api_key_id
else None else None
) )
@@ -1119,7 +1141,8 @@ class GetMessageAnalytics(Resource):
jsonify({"success": False, "message": "Invalid option"}), 400 jsonify({"success": False, "message": "Invalid option"}), 400
) )
start_date = end_date - datetime.timedelta(days=filter_days) start_date = end_date - datetime.timedelta(days=filter_days)
start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0) start_date = start_date.replace(
hour=0, minute=0, second=0, microsecond=0)
end_date = end_date.replace( end_date = end_date.replace(
hour=23, minute=59, second=59, microsecond=999999 hour=23, minute=59, second=59, microsecond=999999
) )
@@ -1162,11 +1185,14 @@ class GetMessageAnalytics(Resource):
for entry in message_data: for entry in message_data:
if filter_option == "last_hour": if filter_option == "last_hour":
daily_messages[entry["_id"]["minute"]] = entry["total_messages"] daily_messages[entry["_id"]["minute"]
] = entry["total_messages"]
elif filter_option == "last_24_hour": elif filter_option == "last_24_hour":
daily_messages[entry["_id"]["hour"]] = entry["total_messages"] daily_messages[entry["_id"]["hour"]
] = entry["total_messages"]
else: else:
daily_messages[entry["_id"]["day"]] = entry["total_messages"] daily_messages[entry["_id"]["day"]
] = entry["total_messages"]
except Exception as err: except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400) return make_response(jsonify({"success": False, "error": str(err)}), 400)
@@ -1206,7 +1232,8 @@ class GetTokenAnalytics(Resource):
try: try:
api_key = ( api_key = (
api_key_collection.find_one({"_id": ObjectId(api_key_id)})["key"] api_key_collection.find_one(
{"_id": ObjectId(api_key_id)})["key"]
if api_key_id if api_key_id
else None else None
) )
@@ -1264,7 +1291,8 @@ class GetTokenAnalytics(Resource):
jsonify({"success": False, "message": "Invalid option"}), 400 jsonify({"success": False, "message": "Invalid option"}), 400
) )
start_date = end_date - datetime.timedelta(days=filter_days) start_date = end_date - datetime.timedelta(days=filter_days)
start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0) start_date = start_date.replace(
hour=0, minute=0, second=0, microsecond=0)
end_date = end_date.replace( end_date = end_date.replace(
hour=23, minute=59, second=59, microsecond=999999 hour=23, minute=59, second=59, microsecond=999999
) )
@@ -1313,11 +1341,14 @@ class GetTokenAnalytics(Resource):
for entry in token_usage_data: for entry in token_usage_data:
if filter_option == "last_hour": if filter_option == "last_hour":
daily_token_usage[entry["_id"]["minute"]] = entry["total_tokens"] daily_token_usage[entry["_id"]
["minute"]] = entry["total_tokens"]
elif filter_option == "last_24_hour": elif filter_option == "last_24_hour":
daily_token_usage[entry["_id"]["hour"]] = entry["total_tokens"] daily_token_usage[entry["_id"]
["hour"]] = entry["total_tokens"]
else: else:
daily_token_usage[entry["_id"]["day"]] = entry["total_tokens"] daily_token_usage[entry["_id"]
["day"]] = entry["total_tokens"]
except Exception as err: except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400) return make_response(jsonify({"success": False, "error": str(err)}), 400)
@@ -1357,7 +1388,8 @@ class GetFeedbackAnalytics(Resource):
try: try:
api_key = ( api_key = (
api_key_collection.find_one({"_id": ObjectId(api_key_id)})["key"] api_key_collection.find_one(
{"_id": ObjectId(api_key_id)})["key"]
if api_key_id if api_key_id
else None else None
) )
@@ -1459,7 +1491,8 @@ class GetFeedbackAnalytics(Resource):
jsonify({"success": False, "message": "Invalid option"}), 400 jsonify({"success": False, "message": "Invalid option"}), 400
) )
start_date = end_date - datetime.timedelta(days=filter_days) start_date = end_date - datetime.timedelta(days=filter_days)
start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0) start_date = start_date.replace(
hour=0, minute=0, second=0, microsecond=0)
end_date = end_date.replace( end_date = end_date.replace(
hour=23, minute=59, second=59, microsecond=999999 hour=23, minute=59, second=59, microsecond=999999
) )
@@ -1575,7 +1608,8 @@ class GetUserLogs(Resource):
try: try:
api_key = ( api_key = (
api_key_collection.find_one({"_id": ObjectId(api_key_id)})["key"] api_key_collection.find_one(
{"_id": ObjectId(api_key_id)})["key"]
if api_key_id if api_key_id
else None else None
) )
@@ -1686,8 +1720,6 @@ class TextToSpeech(Resource):
try: try:
tts_instance = GoogleTTS() tts_instance = GoogleTTS()
audio_base64, detected_language = tts_instance.text_to_speech(text) audio_base64, detected_language = tts_instance.text_to_speech(text)
return make_response(jsonify({"success": True,'audio_base64': audio_base64,'lang':detected_language}), 200) return make_response(jsonify({"success": True, 'audio_base64': audio_base64, 'lang': detected_language}), 200)
except Exception as err: except Exception as err:
return make_response(jsonify({"success": False, "error": str(err)}), 400) return make_response(jsonify({"success": False, "error": str(err)}), 400)