ragflow-cli 0.21.0.dev3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.4
2
+ Name: ragflow-cli
3
+ Version: 0.21.0.dev3
4
+ Summary: Admin Service's client of [RAGFlow](https://github.com/infiniflow/ragflow). The Admin Service provides user management and system monitoring.
5
+ Author-email: Lynn <lynn_inf@hotmail.com>
6
+ License: Apache License, Version 2.0
7
+ Requires-Python: <3.13,>=3.10
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: requests<3.0.0,>=2.30.0
10
+ Requires-Dist: beartype<0.19.0,>=0.18.5
11
+ Requires-Dist: pycryptodomex>=3.10.0
12
+ Requires-Dist: lark>=1.1.0
13
+
14
+ # RAGFlow Admin Service & CLI
15
+
16
+ ### Introduction
17
+
18
+ Admin Service is a dedicated management component designed to monitor, maintain, and administrate the RAGFlow system. It provides comprehensive tools for ensuring system stability, performing operational tasks, and managing users and permissions efficiently.
19
+
20
+ The service offers real-time monitoring of critical components, including the RAGFlow server, Task Executor processes, and dependent services such as MySQL, Elasticsearch, Redis, and MinIO. It automatically checks their health status, resource usage, and uptime, and performs restarts in case of failures to minimize downtime.
21
+
22
+ For user and system management, it supports listing, creating, modifying, and deleting users and their associated resources like knowledge bases and Agents.
23
+
24
+ Built with scalability and reliability in mind, the Admin Service ensures smooth system operation and simplifies maintenance workflows.
25
+
26
+ It consists of a server-side Service and a command-line client (CLI), both implemented in Python. User commands are parsed using the Lark parsing toolkit.
27
+
28
+ - **Admin Service**: A backend service that interfaces with the RAGFlow system to execute administrative operations and monitor its status.
29
+ - **Admin CLI**: A command-line interface that allows users to connect to the Admin Service and issue commands for system management.
30
+
31
+
32
+
33
+ ### Starting the Admin Service
34
+
35
+ #### Launching from source code
36
+
37
+ 1. Before start Admin Service, please make sure RAGFlow system is already started.
38
+
39
+ 2. Launch from source code:
40
+
41
+ ```bash
42
+ python admin/admin_server.py
43
+ ```
44
+ The service will start and listen for incoming connections from the CLI on the configured port.
45
+
46
+ #### Using docker image
47
+
48
+ 1. Before startup, please configure the `docker_compose.yml` file to enable admin server:
49
+
50
+ ```bash
51
+ command:
52
+ - --enable-adminserver
53
+ ```
54
+
55
+ 2. Start the containers, the service will start and listen for incoming connections from the CLI on the configured port.
56
+
57
+
58
+
59
+ ### Using the Admin CLI
60
+
61
+ 1. Ensure the Admin Service is running.
62
+ 2. Install ragflow-cli.
63
+ ```bash
64
+ pip install ragflow-cli
65
+ ```
66
+ 3. Launch the CLI client:
67
+ ```bash
68
+ ragflow-cli -h 0.0.0.0 -p 9381
69
+ ```
70
+ Enter superuser's password to login. Default password is `admin`.
71
+
72
+
73
+
74
+ ## Supported Commands
75
+
76
+ Commands are case-insensitive and must be terminated with a semicolon (`;`).
77
+
78
+ ### Service Management Commands
79
+
80
+ - `LIST SERVICES;`
81
+ - Lists all available services within the RAGFlow system.
82
+ - `SHOW SERVICE <id>;`
83
+ - Shows detailed status information for the service identified by `<id>`.
84
+
85
+
86
+ ### User Management Commands
87
+
88
+ - `LIST USERS;`
89
+ - Lists all users known to the system.
90
+ - `SHOW USER '<username>';`
91
+ - Shows details and permissions for the specified user. The username must be enclosed in single or double quotes.
92
+
93
+ - `CREATE USER <username> <password>;`
94
+ - Create user by username and password. The username and password must be enclosed in single or double quotes.
95
+
96
+ - `DROP USER '<username>';`
97
+ - Removes the specified user from the system. Use with caution.
98
+ - `ALTER USER PASSWORD '<username>' '<new_password>';`
99
+ - Changes the password for the specified user.
100
+ - `ALTER USER ACTIVE <username> <on/off>;`
101
+ - Changes the user to active or inactive.
102
+
103
+
104
+ ### Data and Agent Commands
105
+
106
+ - `LIST DATASETS OF '<username>';`
107
+ - Lists the datasets associated with the specified user.
108
+ - `LIST AGENTS OF '<username>';`
109
+ - Lists the agents associated with the specified user.
110
+
111
+ ### Meta-Commands
112
+
113
+ Meta-commands are prefixed with a backslash (`\`).
114
+
115
+ - `\?` or `\help`
116
+ - Shows help information for the available commands.
117
+ - `\q` or `\quit`
118
+ - Exits the CLI application.
119
+
120
+ ## Examples
121
+
122
+ ```commandline
123
+ admin> list users;
124
+ +-------------------------------+------------------------+-----------+-------------+
125
+ | create_date | email | is_active | nickname |
126
+ +-------------------------------+------------------------+-----------+-------------+
127
+ | Fri, 22 Nov 2024 16:03:41 GMT | jeffery@infiniflow.org | 1 | Jeffery |
128
+ | Fri, 22 Nov 2024 16:10:55 GMT | aya@infiniflow.org | 1 | Waterdancer |
129
+ +-------------------------------+------------------------+-----------+-------------+
130
+
131
+ admin> list services;
132
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
133
+ | extra | host | id | name | port | service_type |
134
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
135
+ | {} | 0.0.0.0 | 0 | ragflow_0 | 9380 | ragflow_server |
136
+ | {'meta_type': 'mysql', 'password': 'infini_rag_flow', 'username': 'root'} | localhost | 1 | mysql | 5455 | meta_data |
137
+ | {'password': 'infini_rag_flow', 'store_type': 'minio', 'user': 'rag_flow'} | localhost | 2 | minio | 9000 | file_store |
138
+ | {'password': 'infini_rag_flow', 'retrieval_type': 'elasticsearch', 'username': 'elastic'} | localhost | 3 | elasticsearch | 1200 | retrieval |
139
+ | {'db_name': 'default_db', 'retrieval_type': 'infinity'} | localhost | 4 | infinity | 23817 | retrieval |
140
+ | {'database': 1, 'mq_type': 'redis', 'password': 'infini_rag_flow'} | localhost | 5 | redis | 6379 | message_queue |
141
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
142
+ ```
@@ -0,0 +1,129 @@
1
+ # RAGFlow Admin Service & CLI
2
+
3
+ ### Introduction
4
+
5
+ Admin Service is a dedicated management component designed to monitor, maintain, and administrate the RAGFlow system. It provides comprehensive tools for ensuring system stability, performing operational tasks, and managing users and permissions efficiently.
6
+
7
+ The service offers real-time monitoring of critical components, including the RAGFlow server, Task Executor processes, and dependent services such as MySQL, Elasticsearch, Redis, and MinIO. It automatically checks their health status, resource usage, and uptime, and performs restarts in case of failures to minimize downtime.
8
+
9
+ For user and system management, it supports listing, creating, modifying, and deleting users and their associated resources like knowledge bases and Agents.
10
+
11
+ Built with scalability and reliability in mind, the Admin Service ensures smooth system operation and simplifies maintenance workflows.
12
+
13
+ It consists of a server-side Service and a command-line client (CLI), both implemented in Python. User commands are parsed using the Lark parsing toolkit.
14
+
15
+ - **Admin Service**: A backend service that interfaces with the RAGFlow system to execute administrative operations and monitor its status.
16
+ - **Admin CLI**: A command-line interface that allows users to connect to the Admin Service and issue commands for system management.
17
+
18
+
19
+
20
+ ### Starting the Admin Service
21
+
22
+ #### Launching from source code
23
+
24
+ 1. Before start Admin Service, please make sure RAGFlow system is already started.
25
+
26
+ 2. Launch from source code:
27
+
28
+ ```bash
29
+ python admin/admin_server.py
30
+ ```
31
+ The service will start and listen for incoming connections from the CLI on the configured port.
32
+
33
+ #### Using docker image
34
+
35
+ 1. Before startup, please configure the `docker_compose.yml` file to enable admin server:
36
+
37
+ ```bash
38
+ command:
39
+ - --enable-adminserver
40
+ ```
41
+
42
+ 2. Start the containers, the service will start and listen for incoming connections from the CLI on the configured port.
43
+
44
+
45
+
46
+ ### Using the Admin CLI
47
+
48
+ 1. Ensure the Admin Service is running.
49
+ 2. Install ragflow-cli.
50
+ ```bash
51
+ pip install ragflow-cli
52
+ ```
53
+ 3. Launch the CLI client:
54
+ ```bash
55
+ ragflow-cli -h 0.0.0.0 -p 9381
56
+ ```
57
+ Enter superuser's password to login. Default password is `admin`.
58
+
59
+
60
+
61
+ ## Supported Commands
62
+
63
+ Commands are case-insensitive and must be terminated with a semicolon (`;`).
64
+
65
+ ### Service Management Commands
66
+
67
+ - `LIST SERVICES;`
68
+ - Lists all available services within the RAGFlow system.
69
+ - `SHOW SERVICE <id>;`
70
+ - Shows detailed status information for the service identified by `<id>`.
71
+
72
+
73
+ ### User Management Commands
74
+
75
+ - `LIST USERS;`
76
+ - Lists all users known to the system.
77
+ - `SHOW USER '<username>';`
78
+ - Shows details and permissions for the specified user. The username must be enclosed in single or double quotes.
79
+
80
+ - `CREATE USER <username> <password>;`
81
+ - Create user by username and password. The username and password must be enclosed in single or double quotes.
82
+
83
+ - `DROP USER '<username>';`
84
+ - Removes the specified user from the system. Use with caution.
85
+ - `ALTER USER PASSWORD '<username>' '<new_password>';`
86
+ - Changes the password for the specified user.
87
+ - `ALTER USER ACTIVE <username> <on/off>;`
88
+ - Changes the user to active or inactive.
89
+
90
+
91
+ ### Data and Agent Commands
92
+
93
+ - `LIST DATASETS OF '<username>';`
94
+ - Lists the datasets associated with the specified user.
95
+ - `LIST AGENTS OF '<username>';`
96
+ - Lists the agents associated with the specified user.
97
+
98
+ ### Meta-Commands
99
+
100
+ Meta-commands are prefixed with a backslash (`\`).
101
+
102
+ - `\?` or `\help`
103
+ - Shows help information for the available commands.
104
+ - `\q` or `\quit`
105
+ - Exits the CLI application.
106
+
107
+ ## Examples
108
+
109
+ ```commandline
110
+ admin> list users;
111
+ +-------------------------------+------------------------+-----------+-------------+
112
+ | create_date | email | is_active | nickname |
113
+ +-------------------------------+------------------------+-----------+-------------+
114
+ | Fri, 22 Nov 2024 16:03:41 GMT | jeffery@infiniflow.org | 1 | Jeffery |
115
+ | Fri, 22 Nov 2024 16:10:55 GMT | aya@infiniflow.org | 1 | Waterdancer |
116
+ +-------------------------------+------------------------+-----------+-------------+
117
+
118
+ admin> list services;
119
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
120
+ | extra | host | id | name | port | service_type |
121
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
122
+ | {} | 0.0.0.0 | 0 | ragflow_0 | 9380 | ragflow_server |
123
+ | {'meta_type': 'mysql', 'password': 'infini_rag_flow', 'username': 'root'} | localhost | 1 | mysql | 5455 | meta_data |
124
+ | {'password': 'infini_rag_flow', 'store_type': 'minio', 'user': 'rag_flow'} | localhost | 2 | minio | 9000 | file_store |
125
+ | {'password': 'infini_rag_flow', 'retrieval_type': 'elasticsearch', 'username': 'elastic'} | localhost | 3 | elasticsearch | 1200 | retrieval |
126
+ | {'db_name': 'default_db', 'retrieval_type': 'infinity'} | localhost | 4 | infinity | 23817 | retrieval |
127
+ | {'database': 1, 'mq_type': 'redis', 'password': 'infini_rag_flow'} | localhost | 5 | redis | 6379 | message_queue |
128
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
129
+ ```
@@ -0,0 +1,24 @@
1
+ [project]
2
+ name = "ragflow-cli"
3
+ version = "0.21.0.dev3"
4
+ description = "Admin Service's client of [RAGFlow](https://github.com/infiniflow/ragflow). The Admin Service provides user management and system monitoring. "
5
+ authors = [{ name = "Lynn", email = "lynn_inf@hotmail.com" }]
6
+ license = { text = "Apache License, Version 2.0" }
7
+ readme = "README.md"
8
+ requires-python = ">=3.10,<3.13"
9
+ dependencies = [
10
+ "requests>=2.30.0,<3.0.0",
11
+ "beartype>=0.18.5,<0.19.0",
12
+ "pycryptodomex>=3.10.0",
13
+ "lark>=1.1.0",
14
+ ]
15
+
16
+ [dependency-groups]
17
+ test = [
18
+ "pytest>=8.3.5",
19
+ "requests>=2.32.3",
20
+ "requests-toolbelt>=1.0.0",
21
+ ]
22
+
23
+ [project.scripts]
24
+ ragflow-cli = "ragflow_cli.admin_client:main"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,653 @@
1
+ #
2
+ # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ import argparse
18
+ import base64
19
+ from cmd import Cmd
20
+
21
+ from Cryptodome.PublicKey import RSA
22
+ from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
23
+ from typing import Dict, List, Any
24
+ from lark import Lark, Transformer, Tree, Token
25
+ import requests
26
+ from requests.auth import HTTPBasicAuth
27
+
28
+ GRAMMAR = r"""
29
+ start: command
30
+
31
+ command: sql_command | meta_command
32
+
33
+ sql_command: list_services
34
+ | show_service
35
+ | startup_service
36
+ | shutdown_service
37
+ | restart_service
38
+ | list_users
39
+ | show_user
40
+ | drop_user
41
+ | alter_user
42
+ | create_user
43
+ | activate_user
44
+ | list_datasets
45
+ | list_agents
46
+
47
+ // meta command definition
48
+ meta_command: "\\" meta_command_name [meta_args]
49
+
50
+ meta_command_name: /[a-zA-Z?]+/
51
+ meta_args: (meta_arg)+
52
+
53
+ meta_arg: /[^\\s"']+/ | quoted_string
54
+
55
+ // command definition
56
+
57
+ LIST: "LIST"i
58
+ SERVICES: "SERVICES"i
59
+ SHOW: "SHOW"i
60
+ CREATE: "CREATE"i
61
+ SERVICE: "SERVICE"i
62
+ SHUTDOWN: "SHUTDOWN"i
63
+ STARTUP: "STARTUP"i
64
+ RESTART: "RESTART"i
65
+ USERS: "USERS"i
66
+ DROP: "DROP"i
67
+ USER: "USER"i
68
+ ALTER: "ALTER"i
69
+ ACTIVE: "ACTIVE"i
70
+ PASSWORD: "PASSWORD"i
71
+ DATASETS: "DATASETS"i
72
+ OF: "OF"i
73
+ AGENTS: "AGENTS"i
74
+
75
+ list_services: LIST SERVICES ";"
76
+ show_service: SHOW SERVICE NUMBER ";"
77
+ startup_service: STARTUP SERVICE NUMBER ";"
78
+ shutdown_service: SHUTDOWN SERVICE NUMBER ";"
79
+ restart_service: RESTART SERVICE NUMBER ";"
80
+
81
+ list_users: LIST USERS ";"
82
+ drop_user: DROP USER quoted_string ";"
83
+ alter_user: ALTER USER PASSWORD quoted_string quoted_string ";"
84
+ show_user: SHOW USER quoted_string ";"
85
+ create_user: CREATE USER quoted_string quoted_string ";"
86
+ activate_user: ALTER USER ACTIVE quoted_string status ";"
87
+
88
+ list_datasets: LIST DATASETS OF quoted_string ";"
89
+ list_agents: LIST AGENTS OF quoted_string ";"
90
+
91
+ identifier: WORD
92
+ quoted_string: QUOTED_STRING
93
+ status: WORD
94
+
95
+ QUOTED_STRING: /'[^']+'/ | /"[^"]+"/
96
+ WORD: /[a-zA-Z0-9_\-\.]+/
97
+ NUMBER: /[0-9]+/
98
+
99
+ %import common.WS
100
+ %ignore WS
101
+ """
102
+
103
+ class AdminTransformer(Transformer):
104
+
105
+ def start(self, items):
106
+ return items[0]
107
+
108
+ def command(self, items):
109
+ return items[0]
110
+
111
+ def list_services(self, items):
112
+ result = {'type': 'list_services'}
113
+ return result
114
+
115
+ def show_service(self, items):
116
+ service_id = int(items[2])
117
+ return {"type": "show_service", "number": service_id}
118
+
119
+ def startup_service(self, items):
120
+ service_id = int(items[2])
121
+ return {"type": "startup_service", "number": service_id}
122
+
123
+ def shutdown_service(self, items):
124
+ service_id = int(items[2])
125
+ return {"type": "shutdown_service", "number": service_id}
126
+
127
+ def restart_service(self, items):
128
+ service_id = int(items[2])
129
+ return {"type": "restart_service", "number": service_id}
130
+
131
+ def list_users(self, items):
132
+ return {"type": "list_users"}
133
+
134
+ def show_user(self, items):
135
+ user_name = items[2]
136
+ return {"type": "show_user", "username": user_name}
137
+
138
+ def drop_user(self, items):
139
+ user_name = items[2]
140
+ return {"type": "drop_user", "username": user_name}
141
+
142
+ def alter_user(self, items):
143
+ user_name = items[3]
144
+ new_password = items[4]
145
+ return {"type": "alter_user", "username": user_name, "password": new_password}
146
+
147
+ def create_user(self, items):
148
+ user_name = items[2]
149
+ password = items[3]
150
+ return {"type": "create_user", "username": user_name, "password": password, "role": "user"}
151
+
152
+ def activate_user(self, items):
153
+ user_name = items[3]
154
+ activate_status = items[4]
155
+ return {"type": "activate_user", "activate_status": activate_status, "username": user_name}
156
+
157
+ def list_datasets(self, items):
158
+ user_name = items[3]
159
+ return {"type": "list_datasets", "username": user_name}
160
+
161
+ def list_agents(self, items):
162
+ user_name = items[3]
163
+ return {"type": "list_agents", "username": user_name}
164
+
165
+ def meta_command(self, items):
166
+ command_name = str(items[0]).lower()
167
+ args = items[1:] if len(items) > 1 else []
168
+
169
+ # handle quoted parameter
170
+ parsed_args = []
171
+ for arg in args:
172
+ if hasattr(arg, 'value'):
173
+ parsed_args.append(arg.value)
174
+ else:
175
+ parsed_args.append(str(arg))
176
+
177
+ return {'type': 'meta', 'command': command_name, 'args': parsed_args}
178
+
179
+ def meta_command_name(self, items):
180
+ return items[0]
181
+
182
+ def meta_args(self, items):
183
+ return items
184
+
185
+ def encrypt(input_string):
186
+ pub = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB\n-----END PUBLIC KEY-----'
187
+ pub_key = RSA.importKey(pub)
188
+ cipher = Cipher_pkcs1_v1_5.new(pub_key)
189
+ cipher_text = cipher.encrypt(base64.b64encode(input_string.encode('utf-8')))
190
+ return base64.b64encode(cipher_text).decode("utf-8")
191
+
192
+ def encode_to_base64(input_string):
193
+ base64_encoded = base64.b64encode(input_string.encode('utf-8'))
194
+ return base64_encoded.decode('utf-8')
195
+
196
+ class AdminCLI(Cmd):
197
+ def __init__(self):
198
+ super().__init__()
199
+ self.parser = Lark(GRAMMAR, start='start', parser='lalr', transformer=AdminTransformer())
200
+ self.command_history = []
201
+ self.is_interactive = False
202
+ self.admin_account = "admin@ragflow.io"
203
+ self.admin_password: str = "admin"
204
+ self.host: str = ""
205
+ self.port: int = 0
206
+
207
+ intro = r"""Type "\h" for help."""
208
+ prompt = "admin> "
209
+
210
+ def onecmd(self, command: str) -> bool:
211
+ try:
212
+ print(f"command: {command}")
213
+ result = self.parse_command(command)
214
+ self.execute_command(result)
215
+
216
+ if isinstance(result, Tree):
217
+ return False
218
+
219
+ if result.get('type') == 'meta' and result.get('command') in ['q', 'quit', 'exit']:
220
+ return True
221
+
222
+ except KeyboardInterrupt:
223
+ print("\nUse '\\q' to quit")
224
+ except EOFError:
225
+ print("\nGoodbye!")
226
+ return True
227
+ return False
228
+
229
+ def emptyline(self) -> bool:
230
+ return False
231
+
232
+ def default(self, line: str) -> bool:
233
+ return self.onecmd(line)
234
+
235
+ def parse_command(self, command_str: str) -> dict[str, str] | Tree[Token]:
236
+ if not command_str.strip():
237
+ return {'type': 'empty'}
238
+
239
+ self.command_history.append(command_str)
240
+
241
+ try:
242
+ result = self.parser.parse(command_str)
243
+ return result
244
+ except Exception as e:
245
+ return {'type': 'error', 'message': f'Parse error: {str(e)}'}
246
+
247
+ def verify_admin(self, args):
248
+
249
+ conn_info = self._parse_connection_args(args)
250
+ if 'error' in conn_info:
251
+ print(f"Error: {conn_info['error']}")
252
+ return
253
+
254
+ self.host = conn_info['host']
255
+ self.port = conn_info['port']
256
+ print(f"Attempt to access ip: {self.host}, port: {self.port}")
257
+ url = f'http://{self.host}:{self.port}/api/v1/admin/auth'
258
+
259
+ try_count = 0
260
+ while True:
261
+ try_count += 1
262
+ if try_count > 3:
263
+ return False
264
+
265
+ admin_passwd = input(f"password for {self.admin_account}: ").strip()
266
+ try:
267
+ self.admin_password = encode_to_base64(admin_passwd)
268
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
269
+ if response.status_code == 200:
270
+ res_json = response.json()
271
+ error_code = res_json.get('code', -1)
272
+ if error_code == 0:
273
+ print("Authentication successful.")
274
+ return True
275
+ else:
276
+ error_message = res_json.get('message', 'Unknown error')
277
+ print(f"Authentication failed: {error_message}, try again")
278
+ continue
279
+ else:
280
+ print(f"Bad response,status: {response.status_code}, try again")
281
+ except Exception:
282
+ print(f"Can't access {self.host}, port: {self.port}")
283
+
284
+ def _print_table_simple(self, data):
285
+ if not data:
286
+ print("No data to print")
287
+ return
288
+ if isinstance(data, dict):
289
+ # handle single row data
290
+ data = [data]
291
+
292
+ columns = list(data[0].keys())
293
+ col_widths = {}
294
+
295
+ def get_string_width(text):
296
+ half_width_chars = (
297
+ " !\"#$%&'()*+,-./0123456789:;<=>?@"
298
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
299
+ "abcdefghijklmnopqrstuvwxyz{|}~"
300
+ "\t\n\r"
301
+ )
302
+ width = 0
303
+ for char in text:
304
+ if char in half_width_chars:
305
+ width += 1
306
+ else:
307
+ width += 2
308
+ return width
309
+
310
+ for col in columns:
311
+ max_width = get_string_width(str(col))
312
+ for item in data:
313
+ value_len = get_string_width(str(item.get(col, '')))
314
+ if value_len > max_width:
315
+ max_width = value_len
316
+ col_widths[col] = max(2, max_width)
317
+
318
+ # Generate delimiter
319
+ separator = "+" + "+".join(["-" * (col_widths[col] + 2) for col in columns]) + "+"
320
+
321
+ # Print header
322
+ print(separator)
323
+ header = "|" + "|".join([f" {col:<{col_widths[col]}} " for col in columns]) + "|"
324
+ print(header)
325
+ print(separator)
326
+
327
+ # Print data
328
+ for item in data:
329
+ row = "|"
330
+ for col in columns:
331
+ value = str(item.get(col, ''))
332
+ if len(value) > col_widths[col]:
333
+ value = value[:col_widths[col] - 3] + "..."
334
+ row += f" {value:<{col_widths[col]}} |"
335
+ print(row)
336
+
337
+ print(separator)
338
+
339
+ def run_interactive(self):
340
+
341
+ self.is_interactive = True
342
+ print("RAGFlow Admin command line interface - Type '\\?' for help, '\\q' to quit")
343
+
344
+ while True:
345
+ try:
346
+ command = input("admin> ").strip()
347
+ if not command:
348
+ continue
349
+
350
+ print(f"command: {command}")
351
+ result = self.parse_command(command)
352
+ self.execute_command(result)
353
+
354
+ if isinstance(result, Tree):
355
+ continue
356
+
357
+ if result.get('type') == 'meta' and result.get('command') in ['q', 'quit', 'exit']:
358
+ break
359
+
360
+ except KeyboardInterrupt:
361
+ print("\nUse '\\q' to quit")
362
+ except EOFError:
363
+ print("\nGoodbye!")
364
+ break
365
+
366
+ def run_single_command(self, args):
367
+ conn_info = self._parse_connection_args(args)
368
+ if 'error' in conn_info:
369
+ print(f"Error: {conn_info['error']}")
370
+ return
371
+
372
+ def _parse_connection_args(self, args: List[str]) -> Dict[str, Any]:
373
+ parser = argparse.ArgumentParser(description='Admin CLI Client', add_help=False)
374
+ parser.add_argument('-h', '--host', default='localhost', help='Admin service host')
375
+ parser.add_argument('-p', '--port', type=int, default=8080, help='Admin service port')
376
+
377
+ try:
378
+ parsed_args, remaining_args = parser.parse_known_args(args)
379
+ return {
380
+ 'host': parsed_args.host,
381
+ 'port': parsed_args.port,
382
+ }
383
+ except SystemExit:
384
+ return {'error': 'Invalid connection arguments'}
385
+
386
+ def execute_command(self, parsed_command: Dict[str, Any]):
387
+
388
+ command_dict: dict
389
+ if isinstance(parsed_command, Tree):
390
+ command_dict = parsed_command.children[0]
391
+ else:
392
+ if parsed_command['type'] == 'error':
393
+ print(f"Error: {parsed_command['message']}")
394
+ return
395
+ else:
396
+ command_dict = parsed_command
397
+
398
+ # print(f"Parsed command: {command_dict}")
399
+
400
+ command_type = command_dict['type']
401
+
402
+ match command_type:
403
+ case 'list_services':
404
+ self._handle_list_services(command_dict)
405
+ case 'show_service':
406
+ self._handle_show_service(command_dict)
407
+ case 'restart_service':
408
+ self._handle_restart_service(command_dict)
409
+ case 'shutdown_service':
410
+ self._handle_shutdown_service(command_dict)
411
+ case 'startup_service':
412
+ self._handle_startup_service(command_dict)
413
+ case 'list_users':
414
+ self._handle_list_users(command_dict)
415
+ case 'show_user':
416
+ self._handle_show_user(command_dict)
417
+ case 'drop_user':
418
+ self._handle_drop_user(command_dict)
419
+ case 'alter_user':
420
+ self._handle_alter_user(command_dict)
421
+ case 'create_user':
422
+ self._handle_create_user(command_dict)
423
+ case 'activate_user':
424
+ self._handle_activate_user(command_dict)
425
+ case 'list_datasets':
426
+ self._handle_list_datasets(command_dict)
427
+ case 'list_agents':
428
+ self._handle_list_agents(command_dict)
429
+ case 'meta':
430
+ self._handle_meta_command(command_dict)
431
+ case _:
432
+ print(f"Command '{command_type}' would be executed with API")
433
+
434
+ def _handle_list_services(self, command):
435
+ print("Listing all services")
436
+
437
+ url = f'http://{self.host}:{self.port}/api/v1/admin/services'
438
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
439
+ res_json = response.json()
440
+ if response.status_code == 200:
441
+ self._print_table_simple(res_json['data'])
442
+ else:
443
+ print(f"Fail to get all users, code: {res_json['code']}, message: {res_json['message']}")
444
+
445
+ def _handle_show_service(self, command):
446
+ service_id: int = command['number']
447
+ print(f"Showing service: {service_id}")
448
+
449
+ url = f'http://{self.host}:{self.port}/api/v1/admin/services/{service_id}'
450
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
451
+ res_json = response.json()
452
+ if response.status_code == 200:
453
+ res_data = res_json['data']
454
+ if res_data['alive']:
455
+ print(f"Service {res_data['service_name']} is alive. Detail:")
456
+ if isinstance(res_data['message'], str):
457
+ print(res_data['message'])
458
+ else:
459
+ self._print_table_simple(res_data['message'])
460
+ else:
461
+ print(f"Service {res_data['service_name']} is down. Detail: {res_data['message']}")
462
+ else:
463
+ print(f"Fail to show service, code: {res_json['code']}, message: {res_json['message']}")
464
+
465
+ def _handle_restart_service(self, command):
466
+ service_id: int = command['number']
467
+ print(f"Restart service {service_id}")
468
+
469
+ def _handle_shutdown_service(self, command):
470
+ service_id: int = command['number']
471
+ print(f"Shutdown service {service_id}")
472
+
473
+ def _handle_startup_service(self, command):
474
+ service_id: int = command['number']
475
+ print(f"Startup service {service_id}")
476
+
477
+ def _handle_list_users(self, command):
478
+ print("Listing all users")
479
+
480
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users'
481
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
482
+ res_json = response.json()
483
+ if response.status_code == 200:
484
+ self._print_table_simple(res_json['data'])
485
+ else:
486
+ print(f"Fail to get all users, code: {res_json['code']}, message: {res_json['message']}")
487
+
488
+ def _handle_show_user(self, command):
489
+ username_tree: Tree = command['username']
490
+ username: str = username_tree.children[0].strip("'\"")
491
+ print(f"Showing user: {username}")
492
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}'
493
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
494
+ res_json = response.json()
495
+ if response.status_code == 200:
496
+ self._print_table_simple(res_json['data'])
497
+ else:
498
+ print(f"Fail to get user {username}, code: {res_json['code']}, message: {res_json['message']}")
499
+
500
+ def _handle_drop_user(self, command):
501
+ username_tree: Tree = command['username']
502
+ username: str = username_tree.children[0].strip("'\"")
503
+ print(f"Drop user: {username}")
504
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}'
505
+ response = requests.delete(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
506
+ res_json = response.json()
507
+ if response.status_code == 200:
508
+ print(res_json["message"])
509
+ else:
510
+ print(f"Fail to drop user, code: {res_json['code']}, message: {res_json['message']}")
511
+
512
+ def _handle_alter_user(self, command):
513
+ username_tree: Tree = command['username']
514
+ username: str = username_tree.children[0].strip("'\"")
515
+ password_tree: Tree = command['password']
516
+ password: str = password_tree.children[0].strip("'\"")
517
+ print(f"Alter user: {username}, password: {password}")
518
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/password'
519
+ response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password),
520
+ json={'new_password': encrypt(password)})
521
+ res_json = response.json()
522
+ if response.status_code == 200:
523
+ print(res_json["message"])
524
+ else:
525
+ print(f"Fail to alter password, code: {res_json['code']}, message: {res_json['message']}")
526
+
527
+ def _handle_create_user(self, command):
528
+ username_tree: Tree = command['username']
529
+ username: str = username_tree.children[0].strip("'\"")
530
+ password_tree: Tree = command['password']
531
+ password: str = password_tree.children[0].strip("'\"")
532
+ role: str = command['role']
533
+ print(f"Create user: {username}, password: {password}, role: {role}")
534
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users'
535
+ response = requests.post(
536
+ url,
537
+ auth=HTTPBasicAuth(self.admin_account, self.admin_password),
538
+ json={'username': username, 'password': encrypt(password), 'role': role}
539
+ )
540
+ res_json = response.json()
541
+ if response.status_code == 200:
542
+ self._print_table_simple(res_json['data'])
543
+ else:
544
+ print(f"Fail to create user {username}, code: {res_json['code']}, message: {res_json['message']}")
545
+
546
+ def _handle_activate_user(self, command):
547
+ username_tree: Tree = command['username']
548
+ username: str = username_tree.children[0].strip("'\"")
549
+ activate_tree: Tree = command['activate_status']
550
+ activate_status: str = activate_tree.children[0].strip("'\"")
551
+ if activate_status.lower() in ['on', 'off']:
552
+ print(f"Alter user {username} activate status, turn {activate_status.lower()}.")
553
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/activate'
554
+ response = requests.put(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password),
555
+ json={'activate_status': activate_status})
556
+ res_json = response.json()
557
+ if response.status_code == 200:
558
+ print(res_json["message"])
559
+ else:
560
+ print(f"Fail to alter activate status, code: {res_json['code']}, message: {res_json['message']}")
561
+ else:
562
+ print(f"Unknown activate status: {activate_status}.")
563
+
564
+ def _handle_list_datasets(self, command):
565
+ username_tree: Tree = command['username']
566
+ username: str = username_tree.children[0].strip("'\"")
567
+ print(f"Listing all datasets of user: {username}")
568
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/datasets'
569
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
570
+ res_json = response.json()
571
+ if response.status_code == 200:
572
+ self._print_table_simple(res_json['data'])
573
+ else:
574
+ print(f"Fail to get all datasets of {username}, code: {res_json['code']}, message: {res_json['message']}")
575
+
576
+ def _handle_list_agents(self, command):
577
+ username_tree: Tree = command['username']
578
+ username: str = username_tree.children[0].strip("'\"")
579
+ print(f"Listing all agents of user: {username}")
580
+ url = f'http://{self.host}:{self.port}/api/v1/admin/users/{username}/agents'
581
+ response = requests.get(url, auth=HTTPBasicAuth(self.admin_account, self.admin_password))
582
+ res_json = response.json()
583
+ if response.status_code == 200:
584
+ self._print_table_simple(res_json['data'])
585
+ else:
586
+ print(f"Fail to get all agents of {username}, code: {res_json['code']}, message: {res_json['message']}")
587
+
588
+ def _handle_meta_command(self, command):
589
+ meta_command = command['command']
590
+ args = command.get('args', [])
591
+
592
+ if meta_command in ['?', 'h', 'help']:
593
+ self.show_help()
594
+ elif meta_command in ['q', 'quit', 'exit']:
595
+ print("Goodbye!")
596
+ else:
597
+ print(f"Meta command '{meta_command}' with args {args}")
598
+
599
+ def show_help(self):
600
+ """Help info"""
601
+ help_text = """
602
+ Commands:
603
+ LIST SERVICES
604
+ SHOW SERVICE <service>
605
+ STARTUP SERVICE <service>
606
+ SHUTDOWN SERVICE <service>
607
+ RESTART SERVICE <service>
608
+ LIST USERS
609
+ SHOW USER <user>
610
+ DROP USER <user>
611
+ CREATE USER <user> <password>
612
+ ALTER USER PASSWORD <user> <new_password>
613
+ ALTER USER ACTIVE <user> <on/off>
614
+ LIST DATASETS OF <user>
615
+ LIST AGENTS OF <user>
616
+
617
+ Meta Commands:
618
+ \\?, \\h, \\help Show this help
619
+ \\q, \\quit, \\exit Quit the CLI
620
+ """
621
+ print(help_text)
622
+
623
+
624
+ def main():
625
+ import sys
626
+
627
+ cli = AdminCLI()
628
+
629
+ if len(sys.argv) == 1 or (len(sys.argv) > 1 and sys.argv[1] == '-'):
630
+ print(r"""
631
+ ____ ___ ______________ ___ __ _
632
+ / __ \/ | / ____/ ____/ /___ _ __ / | ____/ /___ ___ (_)___
633
+ / /_/ / /| |/ / __/ /_ / / __ \ | /| / / / /| |/ __ / __ `__ \/ / __ \
634
+ / _, _/ ___ / /_/ / __/ / / /_/ / |/ |/ / / ___ / /_/ / / / / / / / / / /
635
+ /_/ |_/_/ |_\____/_/ /_/\____/|__/|__/ /_/ |_\__,_/_/ /_/ /_/_/_/ /_/
636
+ """)
637
+ if cli.verify_admin(sys.argv):
638
+ cli.cmdloop()
639
+ else:
640
+ print(r"""
641
+ ____ ___ ______________ ___ __ _
642
+ / __ \/ | / ____/ ____/ /___ _ __ / | ____/ /___ ___ (_)___
643
+ / /_/ / /| |/ / __/ /_ / / __ \ | /| / / / /| |/ __ / __ `__ \/ / __ \
644
+ / _, _/ ___ / /_/ / __/ / / /_/ / |/ |/ / / ___ / /_/ / / / / / / / / / /
645
+ /_/ |_/_/ |_\____/_/ /_/\____/|__/|__/ /_/ |_\__,_/_/ /_/ /_/_/_/ /_/
646
+ """)
647
+ if cli.verify_admin(sys.argv):
648
+ cli.cmdloop()
649
+ # cli.run_single_command(sys.argv[1:])
650
+
651
+
652
+ if __name__ == '__main__':
653
+ main()
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.4
2
+ Name: ragflow-cli
3
+ Version: 0.21.0.dev3
4
+ Summary: Admin Service's client of [RAGFlow](https://github.com/infiniflow/ragflow). The Admin Service provides user management and system monitoring.
5
+ Author-email: Lynn <lynn_inf@hotmail.com>
6
+ License: Apache License, Version 2.0
7
+ Requires-Python: <3.13,>=3.10
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: requests<3.0.0,>=2.30.0
10
+ Requires-Dist: beartype<0.19.0,>=0.18.5
11
+ Requires-Dist: pycryptodomex>=3.10.0
12
+ Requires-Dist: lark>=1.1.0
13
+
14
+ # RAGFlow Admin Service & CLI
15
+
16
+ ### Introduction
17
+
18
+ Admin Service is a dedicated management component designed to monitor, maintain, and administrate the RAGFlow system. It provides comprehensive tools for ensuring system stability, performing operational tasks, and managing users and permissions efficiently.
19
+
20
+ The service offers real-time monitoring of critical components, including the RAGFlow server, Task Executor processes, and dependent services such as MySQL, Elasticsearch, Redis, and MinIO. It automatically checks their health status, resource usage, and uptime, and performs restarts in case of failures to minimize downtime.
21
+
22
+ For user and system management, it supports listing, creating, modifying, and deleting users and their associated resources like knowledge bases and Agents.
23
+
24
+ Built with scalability and reliability in mind, the Admin Service ensures smooth system operation and simplifies maintenance workflows.
25
+
26
+ It consists of a server-side Service and a command-line client (CLI), both implemented in Python. User commands are parsed using the Lark parsing toolkit.
27
+
28
+ - **Admin Service**: A backend service that interfaces with the RAGFlow system to execute administrative operations and monitor its status.
29
+ - **Admin CLI**: A command-line interface that allows users to connect to the Admin Service and issue commands for system management.
30
+
31
+
32
+
33
+ ### Starting the Admin Service
34
+
35
+ #### Launching from source code
36
+
37
+ 1. Before start Admin Service, please make sure RAGFlow system is already started.
38
+
39
+ 2. Launch from source code:
40
+
41
+ ```bash
42
+ python admin/admin_server.py
43
+ ```
44
+ The service will start and listen for incoming connections from the CLI on the configured port.
45
+
46
+ #### Using docker image
47
+
48
+ 1. Before startup, please configure the `docker_compose.yml` file to enable admin server:
49
+
50
+ ```bash
51
+ command:
52
+ - --enable-adminserver
53
+ ```
54
+
55
+ 2. Start the containers, the service will start and listen for incoming connections from the CLI on the configured port.
56
+
57
+
58
+
59
+ ### Using the Admin CLI
60
+
61
+ 1. Ensure the Admin Service is running.
62
+ 2. Install ragflow-cli.
63
+ ```bash
64
+ pip install ragflow-cli
65
+ ```
66
+ 3. Launch the CLI client:
67
+ ```bash
68
+ ragflow-cli -h 0.0.0.0 -p 9381
69
+ ```
70
+ Enter superuser's password to login. Default password is `admin`.
71
+
72
+
73
+
74
+ ## Supported Commands
75
+
76
+ Commands are case-insensitive and must be terminated with a semicolon (`;`).
77
+
78
+ ### Service Management Commands
79
+
80
+ - `LIST SERVICES;`
81
+ - Lists all available services within the RAGFlow system.
82
+ - `SHOW SERVICE <id>;`
83
+ - Shows detailed status information for the service identified by `<id>`.
84
+
85
+
86
+ ### User Management Commands
87
+
88
+ - `LIST USERS;`
89
+ - Lists all users known to the system.
90
+ - `SHOW USER '<username>';`
91
+ - Shows details and permissions for the specified user. The username must be enclosed in single or double quotes.
92
+
93
+ - `CREATE USER <username> <password>;`
94
+ - Create user by username and password. The username and password must be enclosed in single or double quotes.
95
+
96
+ - `DROP USER '<username>';`
97
+ - Removes the specified user from the system. Use with caution.
98
+ - `ALTER USER PASSWORD '<username>' '<new_password>';`
99
+ - Changes the password for the specified user.
100
+ - `ALTER USER ACTIVE <username> <on/off>;`
101
+ - Changes the user to active or inactive.
102
+
103
+
104
+ ### Data and Agent Commands
105
+
106
+ - `LIST DATASETS OF '<username>';`
107
+ - Lists the datasets associated with the specified user.
108
+ - `LIST AGENTS OF '<username>';`
109
+ - Lists the agents associated with the specified user.
110
+
111
+ ### Meta-Commands
112
+
113
+ Meta-commands are prefixed with a backslash (`\`).
114
+
115
+ - `\?` or `\help`
116
+ - Shows help information for the available commands.
117
+ - `\q` or `\quit`
118
+ - Exits the CLI application.
119
+
120
+ ## Examples
121
+
122
+ ```commandline
123
+ admin> list users;
124
+ +-------------------------------+------------------------+-----------+-------------+
125
+ | create_date | email | is_active | nickname |
126
+ +-------------------------------+------------------------+-----------+-------------+
127
+ | Fri, 22 Nov 2024 16:03:41 GMT | jeffery@infiniflow.org | 1 | Jeffery |
128
+ | Fri, 22 Nov 2024 16:10:55 GMT | aya@infiniflow.org | 1 | Waterdancer |
129
+ +-------------------------------+------------------------+-----------+-------------+
130
+
131
+ admin> list services;
132
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
133
+ | extra | host | id | name | port | service_type |
134
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
135
+ | {} | 0.0.0.0 | 0 | ragflow_0 | 9380 | ragflow_server |
136
+ | {'meta_type': 'mysql', 'password': 'infini_rag_flow', 'username': 'root'} | localhost | 1 | mysql | 5455 | meta_data |
137
+ | {'password': 'infini_rag_flow', 'store_type': 'minio', 'user': 'rag_flow'} | localhost | 2 | minio | 9000 | file_store |
138
+ | {'password': 'infini_rag_flow', 'retrieval_type': 'elasticsearch', 'username': 'elastic'} | localhost | 3 | elasticsearch | 1200 | retrieval |
139
+ | {'db_name': 'default_db', 'retrieval_type': 'infinity'} | localhost | 4 | infinity | 23817 | retrieval |
140
+ | {'database': 1, 'mq_type': 'redis', 'password': 'infini_rag_flow'} | localhost | 5 | redis | 6379 | message_queue |
141
+ +-------------------------------------------------------------------------------------------+-----------+----+---------------+-------+----------------+
142
+ ```
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/ragflow_cli/admin_client.py
4
+ src/ragflow_cli.egg-info/PKG-INFO
5
+ src/ragflow_cli.egg-info/SOURCES.txt
6
+ src/ragflow_cli.egg-info/dependency_links.txt
7
+ src/ragflow_cli.egg-info/entry_points.txt
8
+ src/ragflow_cli.egg-info/requires.txt
9
+ src/ragflow_cli.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ ragflow-cli = ragflow_cli.admin_client:main
@@ -0,0 +1,4 @@
1
+ requests<3.0.0,>=2.30.0
2
+ beartype<0.19.0,>=0.18.5
3
+ pycryptodomex>=3.10.0
4
+ lark>=1.1.0