55
66
77def request_post (
8- endpoint : str , data : dict , headers : dict ,
9- use_json : bool = False , files : list = None , return_status : bool = False
8+ endpoint : str ,
9+ data : dict ,
10+ headers : dict ,
11+ use_json : bool = False ,
12+ files : list = None ,
13+ return_status : bool = False ,
1014):
1115 url = f"{ RAG_URL } { endpoint } "
1216 try :
@@ -20,7 +24,8 @@ def request_post(
2024 if response .status_code == 200 :
2125 return (
2226 (response .status_code , response .json ())
23- if return_status else response .json ()
27+ if return_status
28+ else response .json ()
2429 )
2530
2631 print (f"[ERROR] POST { url } : { response .status_code } - { response .text } " )
@@ -37,13 +42,11 @@ def rag_register_user(is_super_user: bool = False):
3742 "username" : os .getenv ("RAG_USERNAME" ),
3843 "password" : os .getenv ("RAG_PASSWORD" ),
3944 "is_active" : True ,
40- "is_superuser" : is_super_user
45+ "is_superuser" : is_super_user ,
4146 }
4247 headers = {"Content-Type" : "application/json" }
4348 status , result = request_post (
44- "auth/register" ,
45- payload , headers ,
46- use_json = True , return_status = True
49+ "auth/register" , payload , headers , use_json = True , return_status = True
4750 )
4851 if status == 200 and result :
4952 print ("✅ User registered successfully." )
@@ -55,7 +58,7 @@ def rag_register_user(is_super_user: bool = False):
5558def rag_login ():
5659 payload = {
5760 "username" : os .getenv ("RAG_USERNAME" ),
58- "password" : os .getenv ("RAG_PASSWORD" )
61+ "password" : os .getenv ("RAG_PASSWORD" ),
5962 }
6063 headers = {"Content-Type" : "application/x-www-form-urlencoded" }
6164 status , result = request_post (
@@ -82,21 +85,22 @@ def rag_login():
8285
8386def rag_create_knowledge_base (token : str , title : str , description : str ):
8487 payload = {"name" : title , "description" : description }
85- headers = {
86- "Content-Type" : "application/json" ,
87- "Authorization" : token
88- }
88+ headers = {"Content-Type" : "application/json" , "Authorization" : token }
8989 result = request_post ("knowledge-base" , payload , headers , use_json = True )
90- return result .get ('id' ) if result else False
90+ return result .get ("id" ) if result else False
9191
9292
9393def rag_upload_documents (token : str , kb_id : int , file_paths : list ):
9494 endpoint = f"knowledge-base/{ kb_id } /documents/upload"
9595 headers = {"Authorization" : token }
9696
97- files = [(
98- "files" , (os .path .basename (path ), open (path , "rb" ), "application/pdf" )
99- ) for path in file_paths ]
97+ files = [
98+ (
99+ "files" ,
100+ (os .path .basename (path ), open (path , "rb" ), "application/pdf" ),
101+ )
102+ for path in file_paths
103+ ]
100104
101105 try :
102106 result = request_post (endpoint , data = {}, headers = headers , files = files )
@@ -108,8 +112,5 @@ def rag_upload_documents(token: str, kb_id: int, file_paths: list):
108112
109113def rag_process_documents (token : str , kb_id : int , upload_results : list ):
110114 endpoint = f"knowledge-base/{ kb_id } /documents/process"
111- headers = {
112- "Authorization" : token ,
113- "Content-Type" : "application/json"
114- }
115+ headers = {"Authorization" : token , "Content-Type" : "application/json" }
115116 return request_post (endpoint , upload_results , headers , use_json = True )
0 commit comments