mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-23 23:16:58 +08:00
Fix IDE warnings (#12085)
### What problem does this PR solve? As title ### Type of change - [x] Refactoring Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@ -108,7 +108,7 @@ def _load_user():
|
|||||||
authorization = request.headers.get("Authorization")
|
authorization = request.headers.get("Authorization")
|
||||||
g.user = None
|
g.user = None
|
||||||
if not authorization:
|
if not authorization:
|
||||||
return
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
access_token = str(jwt.loads(authorization))
|
access_token = str(jwt.loads(authorization))
|
||||||
|
|||||||
@ -326,7 +326,6 @@ async def list_tools() -> Response:
|
|||||||
try:
|
try:
|
||||||
tools = await asyncio.to_thread(tool_call_session.get_tools, timeout)
|
tools = await asyncio.to_thread(tool_call_session.get_tools, timeout)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
tools = []
|
|
||||||
return get_data_error_result(message=f"MCP list tools error: {e}")
|
return get_data_error_result(message=f"MCP list tools error: {e}")
|
||||||
|
|
||||||
results[server_key] = []
|
results[server_key] = []
|
||||||
@ -428,7 +427,6 @@ async def test_mcp() -> Response:
|
|||||||
try:
|
try:
|
||||||
tools = await asyncio.to_thread(tool_call_session.get_tools, timeout)
|
tools = await asyncio.to_thread(tool_call_session.get_tools, timeout)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
tools = []
|
|
||||||
return get_data_error_result(message=f"Test MCP error: {e}")
|
return get_data_error_result(message=f"Test MCP error: {e}")
|
||||||
finally:
|
finally:
|
||||||
# PERF: blocking call to close sessions — consider moving to background thread or task queue
|
# PERF: blocking call to close sessions — consider moving to background thread or task queue
|
||||||
|
|||||||
@ -287,7 +287,7 @@ def list_chat(tenant_id):
|
|||||||
chats = DialogService.get_list(tenant_id, page_number, items_per_page, orderby, desc, id, name)
|
chats = DialogService.get_list(tenant_id, page_number, items_per_page, orderby, desc, id, name)
|
||||||
if not chats:
|
if not chats:
|
||||||
return get_result(data=[])
|
return get_result(data=[])
|
||||||
list_assts = []
|
list_assistants = []
|
||||||
key_mapping = {
|
key_mapping = {
|
||||||
"parameters": "variables",
|
"parameters": "variables",
|
||||||
"prologue": "opener",
|
"prologue": "opener",
|
||||||
@ -321,5 +321,5 @@ def list_chat(tenant_id):
|
|||||||
del res["kb_ids"]
|
del res["kb_ids"]
|
||||||
res["datasets"] = kb_list
|
res["datasets"] = kb_list
|
||||||
res["avatar"] = res.pop("icon")
|
res["avatar"] = res.pop("icon")
|
||||||
list_assts.append(res)
|
list_assistants.append(res)
|
||||||
return get_result(data=list_assts)
|
return get_result(data=list_assistants)
|
||||||
|
|||||||
@ -205,7 +205,8 @@ async def create(tenant_id):
|
|||||||
if not FileService.is_parent_folder_exist(pf_id):
|
if not FileService.is_parent_folder_exist(pf_id):
|
||||||
return get_json_result(data=False, message="Parent Folder Doesn't Exist!", code=RetCode.BAD_REQUEST)
|
return get_json_result(data=False, message="Parent Folder Doesn't Exist!", code=RetCode.BAD_REQUEST)
|
||||||
if FileService.query(name=req["name"], parent_id=pf_id):
|
if FileService.query(name=req["name"], parent_id=pf_id):
|
||||||
return get_json_result(data=False, message="Duplicated folder name in the same folder.", code=409)
|
return get_json_result(data=False, message="Duplicated folder name in the same folder.",
|
||||||
|
code=RetCode.CONFLICT)
|
||||||
|
|
||||||
if input_file_type == FileType.FOLDER.value:
|
if input_file_type == FileType.FOLDER.value:
|
||||||
file_type = FileType.FOLDER.value
|
file_type = FileType.FOLDER.value
|
||||||
@ -565,11 +566,13 @@ async def rename(tenant_id):
|
|||||||
|
|
||||||
if file.type != FileType.FOLDER.value and pathlib.Path(req["name"].lower()).suffix != pathlib.Path(
|
if file.type != FileType.FOLDER.value and pathlib.Path(req["name"].lower()).suffix != pathlib.Path(
|
||||||
file.name.lower()).suffix:
|
file.name.lower()).suffix:
|
||||||
return get_json_result(data=False, message="The extension of file can't be changed", code=RetCode.BAD_REQUEST)
|
return get_json_result(data=False, message="The extension of file can't be changed",
|
||||||
|
code=RetCode.BAD_REQUEST)
|
||||||
|
|
||||||
for existing_file in FileService.query(name=req["name"], pf_id=file.parent_id):
|
for existing_file in FileService.query(name=req["name"], pf_id=file.parent_id):
|
||||||
if existing_file.name == req["name"]:
|
if existing_file.name == req["name"]:
|
||||||
return get_json_result(data=False, message="Duplicated file name in the same folder.", code=409)
|
return get_json_result(data=False, message="Duplicated file name in the same folder.",
|
||||||
|
code=RetCode.CONFLICT)
|
||||||
|
|
||||||
if not FileService.update_by_id(req["file_id"], {"name": req["name"]}):
|
if not FileService.update_by_id(req["file_id"], {"name": req["name"]}):
|
||||||
return get_json_result(message="Database error (File rename)!", code=RetCode.SERVER_ERROR)
|
return get_json_result(message="Database error (File rename)!", code=RetCode.SERVER_ERROR)
|
||||||
@ -631,9 +634,10 @@ async def get(tenant_id, file_id):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return server_error_response(e)
|
return server_error_response(e)
|
||||||
|
|
||||||
|
|
||||||
@manager.route("/file/download/<attachment_id>", methods=["GET"]) # noqa: F821
|
@manager.route("/file/download/<attachment_id>", methods=["GET"]) # noqa: F821
|
||||||
@token_required
|
@token_required
|
||||||
async def download_attachment(tenant_id,attachment_id):
|
async def download_attachment(tenant_id, attachment_id):
|
||||||
try:
|
try:
|
||||||
ext = request.args.get("ext", "markdown")
|
ext = request.args.get("ext", "markdown")
|
||||||
data = await asyncio.to_thread(settings.STORAGE_IMPL.get, tenant_id, attachment_id)
|
data = await asyncio.to_thread(settings.STORAGE_IMPL.get, tenant_id, attachment_id)
|
||||||
@ -645,6 +649,7 @@ async def download_attachment(tenant_id,attachment_id):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return server_error_response(e)
|
return server_error_response(e)
|
||||||
|
|
||||||
|
|
||||||
@manager.route('/file/mv', methods=['POST']) # noqa: F821
|
@manager.route('/file/mv', methods=['POST']) # noqa: F821
|
||||||
@token_required
|
@token_required
|
||||||
async def move(tenant_id):
|
async def move(tenant_id):
|
||||||
|
|||||||
@ -448,7 +448,7 @@ async def chat_completion_openai_like(tenant_id, chat_id):
|
|||||||
@token_required
|
@token_required
|
||||||
async def agents_completion_openai_compatibility(tenant_id, agent_id):
|
async def agents_completion_openai_compatibility(tenant_id, agent_id):
|
||||||
req = await get_request_json()
|
req = await get_request_json()
|
||||||
tiktokenenc = tiktoken.get_encoding("cl100k_base")
|
tiktoken_encode = tiktoken.get_encoding("cl100k_base")
|
||||||
messages = req.get("messages", [])
|
messages = req.get("messages", [])
|
||||||
if not messages:
|
if not messages:
|
||||||
return get_error_data_result("You must provide at least one message.")
|
return get_error_data_result("You must provide at least one message.")
|
||||||
@ -456,7 +456,7 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id):
|
|||||||
return get_error_data_result(f"You don't own the agent {agent_id}")
|
return get_error_data_result(f"You don't own the agent {agent_id}")
|
||||||
|
|
||||||
filtered_messages = [m for m in messages if m["role"] in ["user", "assistant"]]
|
filtered_messages = [m for m in messages if m["role"] in ["user", "assistant"]]
|
||||||
prompt_tokens = sum(len(tiktokenenc.encode(m["content"])) for m in filtered_messages)
|
prompt_tokens = sum(len(tiktoken_encode.encode(m["content"])) for m in filtered_messages)
|
||||||
if not filtered_messages:
|
if not filtered_messages:
|
||||||
return jsonify(
|
return jsonify(
|
||||||
get_data_openai(
|
get_data_openai(
|
||||||
@ -464,7 +464,7 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id):
|
|||||||
content="No valid messages found (user or assistant).",
|
content="No valid messages found (user or assistant).",
|
||||||
finish_reason="stop",
|
finish_reason="stop",
|
||||||
model=req.get("model", ""),
|
model=req.get("model", ""),
|
||||||
completion_tokens=len(tiktokenenc.encode("No valid messages found (user or assistant).")),
|
completion_tokens=len(tiktoken_encode.encode("No valid messages found (user or assistant).")),
|
||||||
prompt_tokens=prompt_tokens,
|
prompt_tokens=prompt_tokens,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -501,6 +501,8 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id):
|
|||||||
):
|
):
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@manager.route("/agents/<agent_id>/completions", methods=["POST"]) # noqa: F821
|
@manager.route("/agents/<agent_id>/completions", methods=["POST"]) # noqa: F821
|
||||||
@token_required
|
@token_required
|
||||||
@ -920,6 +922,7 @@ async def chatbot_completions(dialog_id):
|
|||||||
async for answer in iframe_completion(dialog_id, **req):
|
async for answer in iframe_completion(dialog_id, **req):
|
||||||
return get_result(data=answer)
|
return get_result(data=answer)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
@manager.route("/chatbots/<dialog_id>/info", methods=["GET"]) # noqa: F821
|
@manager.route("/chatbots/<dialog_id>/info", methods=["GET"]) # noqa: F821
|
||||||
async def chatbots_inputs(dialog_id):
|
async def chatbots_inputs(dialog_id):
|
||||||
@ -967,6 +970,7 @@ async def agent_bot_completions(agent_id):
|
|||||||
async for answer in agent_completion(objs[0].tenant_id, agent_id, **req):
|
async for answer in agent_completion(objs[0].tenant_id, agent_id, **req):
|
||||||
return get_result(data=answer)
|
return get_result(data=answer)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
@manager.route("/agentbots/<agent_id>/inputs", methods=["GET"]) # noqa: F821
|
@manager.route("/agentbots/<agent_id>/inputs", methods=["GET"]) # noqa: F821
|
||||||
async def begin_inputs(agent_id):
|
async def begin_inputs(agent_id):
|
||||||
|
|||||||
@ -660,7 +660,7 @@ def user_register(user_id, user):
|
|||||||
tenant_llm = get_init_tenant_llm(user_id)
|
tenant_llm = get_init_tenant_llm(user_id)
|
||||||
|
|
||||||
if not UserService.save(**user):
|
if not UserService.save(**user):
|
||||||
return
|
return None
|
||||||
TenantService.insert(**tenant)
|
TenantService.insert(**tenant)
|
||||||
UserTenantService.insert(**usr_tenant)
|
UserTenantService.insert(**usr_tenant)
|
||||||
TenantLLMService.insert_many(tenant_llm)
|
TenantLLMService.insert_many(tenant_llm)
|
||||||
|
|||||||
@ -54,6 +54,7 @@ class RetCode(IntEnum, CustomEnum):
|
|||||||
SERVER_ERROR = 500
|
SERVER_ERROR = 500
|
||||||
FORBIDDEN = 403
|
FORBIDDEN = 403
|
||||||
NOT_FOUND = 404
|
NOT_FOUND = 404
|
||||||
|
CONFLICT = 409
|
||||||
|
|
||||||
|
|
||||||
class StatusEnum(Enum):
|
class StatusEnum(Enum):
|
||||||
|
|||||||
Reference in New Issue
Block a user