-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmanage.py
More file actions
435 lines (383 loc) · 14.5 KB
/
manage.py
File metadata and controls
435 lines (383 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
"""数据库管理工具模块.
用于执行数据库相关的管理操作,如迁移、初始化等.
"""
import argparse
import logging
import sys
import click
from config import get_settings
from database import Document, Folder, ProcessingStatus, SessionLocal, create_rag_db, create_tables
from models.users import User
from rag.knowledgebase import KnowledgeBase
# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def init_db() -> None:
"""初始化数据库."""
try:
create_rag_db()
create_tables()
logger.info("数据库初始化完成")
except Exception as e:
logger.error(f"数据库初始化失败: {str(e)}")
sys.exit(1)
def migrate_to_sessions() -> None:
"""迁移到新的sessions表."""
try:
logger.info("迁移完成")
except Exception as e:
logger.error(f"迁移失败: {str(e)}")
sys.exit(1)
def create_system_user():
"""创建系统用户."""
db = SessionLocal()
try:
# 检查是否已存在系统用户
system_user = db.query(User).filter(User.username == "system").first()
if not system_user:
# 创建系统用户
system_user = User(
username="system",
email="system@example.com",
hashed_password=User.get_password_hash("SYSTEM"),
is_active=True,
is_superuser=True,
)
db.add(system_user)
db.commit()
click.echo("系统用户创建成功")
else:
click.echo("系统用户已存在")
finally:
db.close()
def create_superuser(username: str, email: str, password: str):
"""创建超级用户."""
db = SessionLocal()
try:
# 检查用户是否已存在
existing_user = db.query(User).filter((User.username == username) | (User.email == email)).first()
if existing_user:
click.echo("用户名或邮箱已存在")
return
# 创建超级用户
superuser = User(
username=username,
email=email,
hashed_password=User.get_password_hash(password),
is_active=True,
is_superuser=True,
)
db.add(superuser)
db.commit()
click.echo("超级用户创建成功")
finally:
db.close()
def migrate_legacy_db():
"""迁移旧数据库."""
db = SessionLocal()
try:
logger.info("迁移旧数据库...")
# 读取sqlite,./tmp/persist/db.sqlite3
import os
import sqlite3
conn = sqlite3.connect("./tmp/persist/db.sqlite3")
cursor = conn.cursor()
# 读取 api_project 表: id, name, created_at, updated_at
cursor.execute("SELECT id, name, created_at, updated_at FROM api_project")
projects = cursor.fetchall()
# 读取 api_project_collections 表: id, project_id, collection_id
cursor.execute("SELECT id, project_id, collection_id FROM api_project_collections")
project_collections = cursor.fetchall()
# 读取 api_collection 表: id, name, created_at, updated_at
cursor.execute("SELECT id, name, created_at, updated_at FROM api_collection")
collections = cursor.fetchall()
# 读取 api_collection_documents 表: id, collection_id, document_id
cursor.execute("SELECT id, collection_id, document_id FROM api_collection_documents")
collections_documents = cursor.fetchall()
# 读取 api_document 表: id, title, linked_file_path, linked_task_id, created_at, updated_at
cursor.execute("SELECT id, title, linked_file_path, linked_task_id, created_at, updated_at FROM api_document")
documents = cursor.fetchall()
logger.info(documents[:10])
# 创建项目
project_id_to_folder_id = {}
project_id_to_project_name = {}
for project in projects:
project_id = project[0]
project_name = project[1]
project_created_at = project[2]
project_updated_at = project[3]
logger.info(
project_id,
project_name,
project_created_at,
project_updated_at,
)
# 创建文件夹
folder = Folder(
name=project_name,
path=f"/{project_name}",
created_at=project_created_at,
updated_at=project_updated_at,
owner_id=1,
is_folder=True,
)
db.add(folder)
db.flush()
db.refresh(folder)
project_id_to_folder_id[project_id] = folder.id
project_id_to_project_name[project_id] = project_name
logger.info(folder.id)
# 集合对应项目的映射
collection_id_to_project_id = {}
for project_collection in project_collections:
project_id = project_collection[1]
collection_id = project_collection[2]
collection_id_to_project_id[collection_id] = project_id
# 创建集合
collection_id_to_folder_id = {}
folder_id_to_path = {}
collection_id_to_collection_name = {}
for collection in collections:
collection_id = collection[0]
collection_name = collection[1]
collection_created_at = collection[2]
collection_updated_at = collection[3]
if collection_id not in collection_id_to_project_id:
# 该集合已被无引用,被删除了
continue
# 所属的项目
project_id = collection_id_to_project_id[collection_id]
project_name = project_id_to_project_name[project_id]
logger.info(
collection_id,
collection_name,
collection_created_at,
collection_updated_at,
project_id,
)
# 创建文件夹
folder = Folder(
name=collection_name,
parent_id=project_id_to_folder_id[project_id],
path=f"/{project_name}/{collection_name}",
created_at=collection_created_at,
updated_at=collection_updated_at,
owner_id=1,
is_folder=True,
)
db.add(folder)
db.flush()
db.refresh(folder)
collection_id_to_folder_id[collection_id] = folder.id
folder_id_to_path[folder.id] = folder.path
collection_id_to_collection_name[collection_id] = collection_name
# 文档对应集合的映射
document_id_to_collection_id = {}
for collection_document in collections_documents:
collection_id = collection_document[1]
document_id = collection_document[2]
document_id_to_collection_id[document_id] = collection_id
# 创建文档
document_id_to_folder_id = {}
# 读取文件夹,uuid/en/uuid_index_0.md uuid/en/uuid_index_0.md uuid/filename.pdf uuid/thumbnail.png
for doc in documents:
doc_id = doc[0]
doc_title = doc[1]
doc_file_path = doc[2]
doc_uuid = doc[2].split("/")[-1]
doc_persist_path = os.path.join(os.path.dirname(__file__), "./tmp/persist", doc_uuid)
doc_abs_path = os.path.join(doc_persist_path, doc_title)
doc_thumbnail_path = os.path.join(doc_persist_path, "thumbnail.png")
logger.info(
doc_title,
doc_uuid,
doc_file_path,
doc_thumbnail_path,
)
if doc_id not in document_id_to_collection_id:
# 该文档已被无引用,被删除了
continue
collection_id = document_id_to_collection_id[doc_id]
if collection_id not in collection_id_to_folder_id:
# 该集合已被无引用,被删除了
continue
folder_id = collection_id_to_folder_id[collection_id]
folder_path = folder_id_to_path[folder_id]
# 读取文件
with open(doc_abs_path, "rb") as f:
file_content = f.read()
# 读取缩略图
with open(doc_thumbnail_path, "rb") as f:
thumbnail_content = f.read()
# 读取内容页
en_page_length = len(os.listdir(os.path.join(doc_persist_path, "en")))
zh_page_length = len(os.listdir(os.path.join(doc_persist_path, "zh")))
content_pages = {}
translation_pages = {}
for i in range(en_page_length):
with open(
os.path.join(
doc_persist_path,
"en",
f"{doc_uuid}_index_{i}.md",
),
"r",
encoding="utf-8",
) as f:
content_pages[str(i)] = f.read()
for i in range(zh_page_length):
with open(
os.path.join(
doc_persist_path,
"zh",
f"{doc_uuid}_index_{i}.md",
),
"r",
encoding="utf-8",
) as f:
translation_pages[str(i)] = f.read()
padding_length = max(en_page_length, zh_page_length)
for i in range(padding_length):
if i >= en_page_length:
content_pages[str(i)] = ""
if i >= zh_page_length:
translation_pages[str(i)] = ""
doc = Document(
filename=doc_title,
content_type="application/pdf",
file_data=file_content,
file_size=len(file_content),
folder_id=folder_id,
owner_id=1, # 使用当前用户ID
path=f"{folder_path}/{doc_title}", # 构建完整路径
is_folder=False,
mime_type="application/pdf",
processing_status=ProcessingStatus.COMPLETED, # 设置初始状态为待处理
content_pages=content_pages,
summary_pages={},
translation_pages=translation_pages,
keywords_pages={},
thumbnail=thumbnail_content,
total_pages=padding_length,
)
db.add(doc)
db.flush()
db.refresh(doc)
document_id_to_folder_id[doc.id] = folder_id
logger.info(projects)
db.commit()
logger.info("迁移成功完成!")
except Exception as e:
# 如果发生任何错误,回滚所有更改
db.rollback()
logger.error(f"迁移失败,已回滚所有更改: {str(e)}")
# 记录详细的错误信息
logger.exception("详细错误信息:")
sys.exit(1)
finally:
# 清理资源
db.close()
if "conn" in locals():
conn.close()
def ingest_data():
"""导入数据."""
import asyncio
import platform
async def process_documents(documents, rag):
for document in documents:
try:
await rag.upload_document(document)
except Exception as e:
logger.error(f"处理文档 {document.filename} 时出错: {str(e)}")
async def main():
settings = get_settings()
if settings.GLOBAL_MODE == "public":
namespace = "public"
else:
raise ValueError("GLOBAL_MODE 必须为 public")
pg_docs_uri = (
f"postgresql+asyncpg://"
f"{settings.DATABASE_USER}:{settings.DATABASE_PASSWORD}"
f"@{settings.DATABASE_HOST}:{settings.DATABASE_PORT}/{settings.RAG_DATABASE_NAME}"
)
pg_vector_uri = (
f"postgresql+asyncpg://"
f"{settings.DATABASE_USER}:{settings.DATABASE_PASSWORD}"
f"@{settings.DATABASE_HOST}:{settings.DATABASE_PORT}/{settings.RAG_DATABASE_NAME}"
)
rag = KnowledgeBase(
pg_docs_uri,
pg_vector_uri,
"public",
namespace,
)
db = SessionLocal()
try:
documents = db.query(Document).all()
await process_documents(documents, rag)
logger.info("导入数据完成")
finally:
db.close()
if platform.system() == "Windows":
# Windows 平台特殊处理
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
@click.group()
def cli():
"""TheLab管理工具."""
@cli.command()
def initdb():
"""初始化数据库."""
click.echo("正在初始化数据库...")
init_db()
click.echo("数据库表创建完成")
click.echo("正在创建系统用户...")
create_system_user()
click.echo("数据库初始化完成")
@cli.command()
def from_legacy():
"""迁移旧数据库."""
migrate_legacy_db()
@cli.command()
def ingest():
"""导入数据."""
ingest_data()
@cli.command()
@click.option("--username", prompt="用户名", help="超级用户的用户名")
@click.option("--email", prompt="邮箱", help="超级用户的邮箱")
@click.option(
"--password",
prompt=True,
hide_input=True,
confirmation_prompt=True,
help="超级用户的密码",
)
def createsuperuser(username, email, password):
"""创建超级用户."""
create_superuser(username, email, password)
def main():
"""主函数."""
parser = argparse.ArgumentParser(description="数据库管理工具")
subparsers = parser.add_subparsers(help="可用命令")
# 初始化数据库命令
init_parser = subparsers.add_parser("init", help="初始化数据库")
init_parser.set_defaults(func=init_db)
# 从旧数据库迁移命令
from_legacy_parser = subparsers.add_parser("from-legacy", help="从旧数据库迁移")
from_legacy_parser.set_defaults(func=migrate_legacy_db)
# 导入数据命令
ingest_parser = subparsers.add_parser("ingest", help="导入数据")
ingest_parser.set_defaults(func=ingest_data)
# 迁移到sessions表命令
migrate_parser = subparsers.add_parser("migrate-sessions", help="迁移到新的sessions表")
migrate_parser.set_defaults(func=migrate_to_sessions)
# 解析命令行参数
args = parser.parse_args()
if hasattr(args, "func"):
args.func(args)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
cli()