You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_models.py 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. #
  2. # Copyright 2024 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. import logging
  17. import inspect
  18. import os
  19. import sys
  20. import typing
  21. import operator
  22. from enum import Enum
  23. from functools import wraps
  24. from itsdangerous.url_safe import URLSafeTimedSerializer as Serializer
  25. from flask_login import UserMixin
  26. from playhouse.migrate import MySQLMigrator, PostgresqlMigrator, migrate
  27. from peewee import (
  28. BigIntegerField, BooleanField, CharField,
  29. CompositeKey, IntegerField, TextField, FloatField, DateTimeField,
  30. Field, Model, Metadata
  31. )
  32. from playhouse.pool import PooledMySQLDatabase, PooledPostgresqlDatabase
  33. from api.db import SerializedType, ParserType
  34. from api import settings
  35. from api import utils
  36. def singleton(cls, *args, **kw):
  37. instances = {}
  38. def _singleton():
  39. key = str(cls) + str(os.getpid())
  40. if key not in instances:
  41. instances[key] = cls(*args, **kw)
  42. return instances[key]
  43. return _singleton
  44. CONTINUOUS_FIELD_TYPE = {IntegerField, FloatField, DateTimeField}
  45. AUTO_DATE_TIMESTAMP_FIELD_PREFIX = {
  46. "create",
  47. "start",
  48. "end",
  49. "update",
  50. "read_access",
  51. "write_access"}
  52. class TextFieldType(Enum):
  53. MYSQL = 'LONGTEXT'
  54. POSTGRES = 'TEXT'
  55. class LongTextField(TextField):
  56. field_type = TextFieldType[settings.DATABASE_TYPE.upper()].value
  57. class JSONField(LongTextField):
  58. default_value = {}
  59. def __init__(self, object_hook=None, object_pairs_hook=None, **kwargs):
  60. self._object_hook = object_hook
  61. self._object_pairs_hook = object_pairs_hook
  62. super().__init__(**kwargs)
  63. def db_value(self, value):
  64. if value is None:
  65. value = self.default_value
  66. return utils.json_dumps(value)
  67. def python_value(self, value):
  68. if not value:
  69. return self.default_value
  70. return utils.json_loads(
  71. value, object_hook=self._object_hook, object_pairs_hook=self._object_pairs_hook)
  72. class ListField(JSONField):
  73. default_value = []
  74. class SerializedField(LongTextField):
  75. def __init__(self, serialized_type=SerializedType.PICKLE,
  76. object_hook=None, object_pairs_hook=None, **kwargs):
  77. self._serialized_type = serialized_type
  78. self._object_hook = object_hook
  79. self._object_pairs_hook = object_pairs_hook
  80. super().__init__(**kwargs)
  81. def db_value(self, value):
  82. if self._serialized_type == SerializedType.PICKLE:
  83. return utils.serialize_b64(value, to_str=True)
  84. elif self._serialized_type == SerializedType.JSON:
  85. if value is None:
  86. return None
  87. return utils.json_dumps(value, with_type=True)
  88. else:
  89. raise ValueError(
  90. f"the serialized type {self._serialized_type} is not supported")
  91. def python_value(self, value):
  92. if self._serialized_type == SerializedType.PICKLE:
  93. return utils.deserialize_b64(value)
  94. elif self._serialized_type == SerializedType.JSON:
  95. if value is None:
  96. return {}
  97. return utils.json_loads(
  98. value, object_hook=self._object_hook, object_pairs_hook=self._object_pairs_hook)
  99. else:
  100. raise ValueError(
  101. f"the serialized type {self._serialized_type} is not supported")
  102. def is_continuous_field(cls: typing.Type) -> bool:
  103. if cls in CONTINUOUS_FIELD_TYPE:
  104. return True
  105. for p in cls.__bases__:
  106. if p in CONTINUOUS_FIELD_TYPE:
  107. return True
  108. elif p != Field and p != object:
  109. if is_continuous_field(p):
  110. return True
  111. else:
  112. return False
  113. def auto_date_timestamp_field():
  114. return {f"{f}_time" for f in AUTO_DATE_TIMESTAMP_FIELD_PREFIX}
  115. def auto_date_timestamp_db_field():
  116. return {f"f_{f}_time" for f in AUTO_DATE_TIMESTAMP_FIELD_PREFIX}
  117. def remove_field_name_prefix(field_name):
  118. return field_name[2:] if field_name.startswith('f_') else field_name
  119. class BaseModel(Model):
  120. create_time = BigIntegerField(null=True, index=True)
  121. create_date = DateTimeField(null=True, index=True)
  122. update_time = BigIntegerField(null=True, index=True)
  123. update_date = DateTimeField(null=True, index=True)
  124. def to_json(self):
  125. # This function is obsolete
  126. return self.to_dict()
  127. def to_dict(self):
  128. return self.__dict__['__data__']
  129. def to_human_model_dict(self, only_primary_with: list = None):
  130. model_dict = self.__dict__['__data__']
  131. if not only_primary_with:
  132. return {remove_field_name_prefix(
  133. k): v for k, v in model_dict.items()}
  134. human_model_dict = {}
  135. for k in self._meta.primary_key.field_names:
  136. human_model_dict[remove_field_name_prefix(k)] = model_dict[k]
  137. for k in only_primary_with:
  138. human_model_dict[k] = model_dict[f'f_{k}']
  139. return human_model_dict
  140. @property
  141. def meta(self) -> Metadata:
  142. return self._meta
  143. @classmethod
  144. def get_primary_keys_name(cls):
  145. return cls._meta.primary_key.field_names if isinstance(cls._meta.primary_key, CompositeKey) else [
  146. cls._meta.primary_key.name]
  147. @classmethod
  148. def getter_by(cls, attr):
  149. return operator.attrgetter(attr)(cls)
  150. @classmethod
  151. def query(cls, reverse=None, order_by=None, **kwargs):
  152. filters = []
  153. for f_n, f_v in kwargs.items():
  154. attr_name = '%s' % f_n
  155. if not hasattr(cls, attr_name) or f_v is None:
  156. continue
  157. if type(f_v) in {list, set}:
  158. f_v = list(f_v)
  159. if is_continuous_field(type(getattr(cls, attr_name))):
  160. if len(f_v) == 2:
  161. for i, v in enumerate(f_v):
  162. if isinstance(
  163. v, str) and f_n in auto_date_timestamp_field():
  164. # time type: %Y-%m-%d %H:%M:%S
  165. f_v[i] = utils.date_string_to_timestamp(v)
  166. lt_value = f_v[0]
  167. gt_value = f_v[1]
  168. if lt_value is not None and gt_value is not None:
  169. filters.append(
  170. cls.getter_by(attr_name).between(
  171. lt_value, gt_value))
  172. elif lt_value is not None:
  173. filters.append(
  174. operator.attrgetter(attr_name)(cls) >= lt_value)
  175. elif gt_value is not None:
  176. filters.append(
  177. operator.attrgetter(attr_name)(cls) <= gt_value)
  178. else:
  179. filters.append(operator.attrgetter(attr_name)(cls) << f_v)
  180. else:
  181. filters.append(operator.attrgetter(attr_name)(cls) == f_v)
  182. if filters:
  183. query_records = cls.select().where(*filters)
  184. if reverse is not None:
  185. if not order_by or not hasattr(cls, f"{order_by}"):
  186. order_by = "create_time"
  187. if reverse is True:
  188. query_records = query_records.order_by(
  189. cls.getter_by(f"{order_by}").desc())
  190. elif reverse is False:
  191. query_records = query_records.order_by(
  192. cls.getter_by(f"{order_by}").asc())
  193. return [query_record for query_record in query_records]
  194. else:
  195. return []
  196. @classmethod
  197. def insert(cls, __data=None, **insert):
  198. if isinstance(__data, dict) and __data:
  199. __data[cls._meta.combined["create_time"]
  200. ] = utils.current_timestamp()
  201. if insert:
  202. insert["create_time"] = utils.current_timestamp()
  203. return super().insert(__data, **insert)
  204. # update and insert will call this method
  205. @classmethod
  206. def _normalize_data(cls, data, kwargs):
  207. normalized = super()._normalize_data(data, kwargs)
  208. if not normalized:
  209. return {}
  210. normalized[cls._meta.combined["update_time"]
  211. ] = utils.current_timestamp()
  212. for f_n in AUTO_DATE_TIMESTAMP_FIELD_PREFIX:
  213. if {f"{f_n}_time", f"{f_n}_date"}.issubset(cls._meta.combined.keys()) and \
  214. cls._meta.combined[f"{f_n}_time"] in normalized and \
  215. normalized[cls._meta.combined[f"{f_n}_time"]] is not None:
  216. normalized[cls._meta.combined[f"{f_n}_date"]] = utils.timestamp_to_date(
  217. normalized[cls._meta.combined[f"{f_n}_time"]])
  218. return normalized
  219. class JsonSerializedField(SerializedField):
  220. def __init__(self, object_hook=utils.from_dict_hook,
  221. object_pairs_hook=None, **kwargs):
  222. super(JsonSerializedField, self).__init__(serialized_type=SerializedType.JSON, object_hook=object_hook,
  223. object_pairs_hook=object_pairs_hook, **kwargs)
  224. class PooledDatabase(Enum):
  225. MYSQL = PooledMySQLDatabase
  226. POSTGRES = PooledPostgresqlDatabase
  227. class DatabaseMigrator(Enum):
  228. MYSQL = MySQLMigrator
  229. POSTGRES = PostgresqlMigrator
  230. @singleton
  231. class BaseDataBase:
  232. def __init__(self):
  233. database_config = settings.DATABASE.copy()
  234. db_name = database_config.pop("name")
  235. self.database_connection = PooledDatabase[settings.DATABASE_TYPE.upper()].value(db_name, **database_config)
  236. logging.info('init database on cluster mode successfully')
  237. class PostgresDatabaseLock:
  238. def __init__(self, lock_name, timeout=10, db=None):
  239. self.lock_name = lock_name
  240. self.timeout = int(timeout)
  241. self.db = db if db else DB
  242. def lock(self):
  243. cursor = self.db.execute_sql("SELECT pg_try_advisory_lock(%s)", self.timeout)
  244. ret = cursor.fetchone()
  245. if ret[0] == 0:
  246. raise Exception(f'acquire postgres lock {self.lock_name} timeout')
  247. elif ret[0] == 1:
  248. return True
  249. else:
  250. raise Exception(f'failed to acquire lock {self.lock_name}')
  251. def unlock(self):
  252. cursor = self.db.execute_sql("SELECT pg_advisory_unlock(%s)", self.timeout)
  253. ret = cursor.fetchone()
  254. if ret[0] == 0:
  255. raise Exception(
  256. f'postgres lock {self.lock_name} was not established by this thread')
  257. elif ret[0] == 1:
  258. return True
  259. else:
  260. raise Exception(f'postgres lock {self.lock_name} does not exist')
  261. def __enter__(self):
  262. if isinstance(self.db, PostgresDatabaseLock):
  263. self.lock()
  264. return self
  265. def __exit__(self, exc_type, exc_val, exc_tb):
  266. if isinstance(self.db, PostgresDatabaseLock):
  267. self.unlock()
  268. def __call__(self, func):
  269. @wraps(func)
  270. def magic(*args, **kwargs):
  271. with self:
  272. return func(*args, **kwargs)
  273. return magic
  274. class MysqlDatabaseLock:
  275. def __init__(self, lock_name, timeout=10, db=None):
  276. self.lock_name = lock_name
  277. self.timeout = int(timeout)
  278. self.db = db if db else DB
  279. def lock(self):
  280. # SQL parameters only support %s format placeholders
  281. cursor = self.db.execute_sql(
  282. "SELECT GET_LOCK(%s, %s)", (self.lock_name, self.timeout))
  283. ret = cursor.fetchone()
  284. if ret[0] == 0:
  285. raise Exception(f'acquire mysql lock {self.lock_name} timeout')
  286. elif ret[0] == 1:
  287. return True
  288. else:
  289. raise Exception(f'failed to acquire lock {self.lock_name}')
  290. def unlock(self):
  291. cursor = self.db.execute_sql(
  292. "SELECT RELEASE_LOCK(%s)", (self.lock_name,))
  293. ret = cursor.fetchone()
  294. if ret[0] == 0:
  295. raise Exception(
  296. f'mysql lock {self.lock_name} was not established by this thread')
  297. elif ret[0] == 1:
  298. return True
  299. else:
  300. raise Exception(f'mysql lock {self.lock_name} does not exist')
  301. def __enter__(self):
  302. if isinstance(self.db, PooledMySQLDatabase):
  303. self.lock()
  304. return self
  305. def __exit__(self, exc_type, exc_val, exc_tb):
  306. if isinstance(self.db, PooledMySQLDatabase):
  307. self.unlock()
  308. def __call__(self, func):
  309. @wraps(func)
  310. def magic(*args, **kwargs):
  311. with self:
  312. return func(*args, **kwargs)
  313. return magic
  314. class DatabaseLock(Enum):
  315. MYSQL = MysqlDatabaseLock
  316. POSTGRES = PostgresDatabaseLock
  317. DB = BaseDataBase().database_connection
  318. DB.lock = DatabaseLock[settings.DATABASE_TYPE.upper()].value
  319. def close_connection():
  320. try:
  321. if DB:
  322. DB.close_stale(age=30)
  323. except Exception as e:
  324. logging.exception(e)
  325. class DataBaseModel(BaseModel):
  326. class Meta:
  327. database = DB
  328. @DB.connection_context()
  329. def init_database_tables(alter_fields=[]):
  330. members = inspect.getmembers(sys.modules[__name__], inspect.isclass)
  331. table_objs = []
  332. create_failed_list = []
  333. for name, obj in members:
  334. if obj != DataBaseModel and issubclass(obj, DataBaseModel):
  335. table_objs.append(obj)
  336. logging.debug(f"start create table {obj.__name__}")
  337. try:
  338. obj.create_table()
  339. logging.debug(f"create table success: {obj.__name__}")
  340. except Exception as e:
  341. logging.exception(e)
  342. create_failed_list.append(obj.__name__)
  343. if create_failed_list:
  344. logging.error(f"create tables failed: {create_failed_list}")
  345. raise Exception(f"create tables failed: {create_failed_list}")
  346. migrate_db()
  347. def fill_db_model_object(model_object, human_model_dict):
  348. for k, v in human_model_dict.items():
  349. attr_name = '%s' % k
  350. if hasattr(model_object.__class__, attr_name):
  351. setattr(model_object, attr_name, v)
  352. return model_object
  353. class User(DataBaseModel, UserMixin):
  354. id = CharField(max_length=32, primary_key=True)
  355. access_token = CharField(max_length=255, null=True, index=True)
  356. nickname = CharField(max_length=100, null=False, help_text="nicky name", index=True)
  357. password = CharField(max_length=255, null=True, help_text="password", index=True)
  358. email = CharField(
  359. max_length=255,
  360. null=False,
  361. help_text="email",
  362. index=True)
  363. avatar = TextField(null=True, help_text="avatar base64 string")
  364. language = CharField(
  365. max_length=32,
  366. null=True,
  367. help_text="English|Chinese",
  368. default="Chinese" if "zh_CN" in os.getenv("LANG", "") else "English",
  369. index=True)
  370. color_schema = CharField(
  371. max_length=32,
  372. null=True,
  373. help_text="Bright|Dark",
  374. default="Bright",
  375. index=True)
  376. timezone = CharField(
  377. max_length=64,
  378. null=True,
  379. help_text="Timezone",
  380. default="UTC+8\tAsia/Shanghai",
  381. index=True)
  382. last_login_time = DateTimeField(null=True, index=True)
  383. is_authenticated = CharField(max_length=1, null=False, default="1", index=True)
  384. is_active = CharField(max_length=1, null=False, default="1", index=True)
  385. is_anonymous = CharField(max_length=1, null=False, default="0", index=True)
  386. login_channel = CharField(null=True, help_text="from which user login", index=True)
  387. status = CharField(
  388. max_length=1,
  389. null=True,
  390. help_text="is it validate(0: wasted, 1: validate)",
  391. default="1",
  392. index=True)
  393. is_superuser = BooleanField(null=True, help_text="is root", default=False, index=True)
  394. def __str__(self):
  395. return self.email
  396. def get_id(self):
  397. jwt = Serializer(secret_key=settings.SECRET_KEY)
  398. return jwt.dumps(str(self.access_token))
  399. class Meta:
  400. db_table = "user"
  401. class Tenant(DataBaseModel):
  402. id = CharField(max_length=32, primary_key=True)
  403. name = CharField(max_length=100, null=True, help_text="Tenant name", index=True)
  404. public_key = CharField(max_length=255, null=True, index=True)
  405. llm_id = CharField(max_length=128, null=False, help_text="default llm ID", index=True)
  406. embd_id = CharField(
  407. max_length=128,
  408. null=False,
  409. help_text="default embedding model ID",
  410. index=True)
  411. asr_id = CharField(
  412. max_length=128,
  413. null=False,
  414. help_text="default ASR model ID",
  415. index=True)
  416. img2txt_id = CharField(
  417. max_length=128,
  418. null=False,
  419. help_text="default image to text model ID",
  420. index=True)
  421. rerank_id = CharField(
  422. max_length=128,
  423. null=False,
  424. help_text="default rerank model ID",
  425. index=True)
  426. tts_id = CharField(
  427. max_length=256,
  428. null=True,
  429. help_text="default tts model ID",
  430. index=True)
  431. parser_ids = CharField(
  432. max_length=256,
  433. null=False,
  434. help_text="document processors",
  435. index=True)
  436. credit = IntegerField(default=512, index=True)
  437. status = CharField(
  438. max_length=1,
  439. null=True,
  440. help_text="is it validate(0: wasted, 1: validate)",
  441. default="1",
  442. index=True)
  443. class Meta:
  444. db_table = "tenant"
  445. class UserTenant(DataBaseModel):
  446. id = CharField(max_length=32, primary_key=True)
  447. user_id = CharField(max_length=32, null=False, index=True)
  448. tenant_id = CharField(max_length=32, null=False, index=True)
  449. role = CharField(max_length=32, null=False, help_text="UserTenantRole", index=True)
  450. invited_by = CharField(max_length=32, null=False, index=True)
  451. status = CharField(
  452. max_length=1,
  453. null=True,
  454. help_text="is it validate(0: wasted, 1: validate)",
  455. default="1",
  456. index=True)
  457. class Meta:
  458. db_table = "user_tenant"
  459. class InvitationCode(DataBaseModel):
  460. id = CharField(max_length=32, primary_key=True)
  461. code = CharField(max_length=32, null=False, index=True)
  462. visit_time = DateTimeField(null=True, index=True)
  463. user_id = CharField(max_length=32, null=True, index=True)
  464. tenant_id = CharField(max_length=32, null=True, index=True)
  465. status = CharField(
  466. max_length=1,
  467. null=True,
  468. help_text="is it validate(0: wasted, 1: validate)",
  469. default="1",
  470. index=True)
  471. class Meta:
  472. db_table = "invitation_code"
  473. class LLMFactories(DataBaseModel):
  474. name = CharField(
  475. max_length=128,
  476. null=False,
  477. help_text="LLM factory name",
  478. primary_key=True)
  479. logo = TextField(null=True, help_text="llm logo base64")
  480. tags = CharField(
  481. max_length=255,
  482. null=False,
  483. help_text="LLM, Text Embedding, Image2Text, ASR",
  484. index=True)
  485. status = CharField(
  486. max_length=1,
  487. null=True,
  488. help_text="is it validate(0: wasted, 1: validate)",
  489. default="1",
  490. index=True)
  491. def __str__(self):
  492. return self.name
  493. class Meta:
  494. db_table = "llm_factories"
  495. class LLM(DataBaseModel):
  496. # LLMs dictionary
  497. llm_name = CharField(
  498. max_length=128,
  499. null=False,
  500. help_text="LLM name",
  501. index=True)
  502. model_type = CharField(
  503. max_length=128,
  504. null=False,
  505. help_text="LLM, Text Embedding, Image2Text, ASR",
  506. index=True)
  507. fid = CharField(max_length=128, null=False, help_text="LLM factory id", index=True)
  508. max_tokens = IntegerField(default=0)
  509. tags = CharField(
  510. max_length=255,
  511. null=False,
  512. help_text="LLM, Text Embedding, Image2Text, Chat, 32k...",
  513. index=True)
  514. status = CharField(
  515. max_length=1,
  516. null=True,
  517. help_text="is it validate(0: wasted, 1: validate)",
  518. default="1",
  519. index=True)
  520. def __str__(self):
  521. return self.llm_name
  522. class Meta:
  523. primary_key = CompositeKey('fid', 'llm_name')
  524. db_table = "llm"
  525. class TenantLLM(DataBaseModel):
  526. tenant_id = CharField(max_length=32, null=False, index=True)
  527. llm_factory = CharField(
  528. max_length=128,
  529. null=False,
  530. help_text="LLM factory name",
  531. index=True)
  532. model_type = CharField(
  533. max_length=128,
  534. null=True,
  535. help_text="LLM, Text Embedding, Image2Text, ASR",
  536. index=True)
  537. llm_name = CharField(
  538. max_length=128,
  539. null=True,
  540. help_text="LLM name",
  541. default="",
  542. index=True)
  543. api_key = CharField(max_length=1024, null=True, help_text="API KEY", index=True)
  544. api_base = CharField(max_length=255, null=True, help_text="API Base")
  545. used_tokens = IntegerField(default=0, index=True)
  546. def __str__(self):
  547. return self.llm_name
  548. class Meta:
  549. db_table = "tenant_llm"
  550. primary_key = CompositeKey('tenant_id', 'llm_factory', 'llm_name')
  551. class Knowledgebase(DataBaseModel):
  552. id = CharField(max_length=32, primary_key=True)
  553. avatar = TextField(null=True, help_text="avatar base64 string")
  554. tenant_id = CharField(max_length=32, null=False, index=True)
  555. name = CharField(
  556. max_length=128,
  557. null=False,
  558. help_text="KB name",
  559. index=True)
  560. language = CharField(
  561. max_length=32,
  562. null=True,
  563. default="Chinese" if "zh_CN" in os.getenv("LANG", "") else "English",
  564. help_text="English|Chinese",
  565. index=True)
  566. description = TextField(null=True, help_text="KB description")
  567. embd_id = CharField(
  568. max_length=128,
  569. null=False,
  570. help_text="default embedding model ID",
  571. index=True)
  572. permission = CharField(
  573. max_length=16,
  574. null=False,
  575. help_text="me|team",
  576. default="me",
  577. index=True)
  578. created_by = CharField(max_length=32, null=False, index=True)
  579. doc_num = IntegerField(default=0, index=True)
  580. token_num = IntegerField(default=0, index=True)
  581. chunk_num = IntegerField(default=0, index=True)
  582. similarity_threshold = FloatField(default=0.2, index=True)
  583. vector_similarity_weight = FloatField(default=0.3, index=True)
  584. parser_id = CharField(
  585. max_length=32,
  586. null=False,
  587. help_text="default parser ID",
  588. default=ParserType.NAIVE.value,
  589. index=True)
  590. parser_config = JSONField(null=False, default={"pages": [[1, 1000000]]})
  591. status = CharField(
  592. max_length=1,
  593. null=True,
  594. help_text="is it validate(0: wasted, 1: validate)",
  595. default="1",
  596. index=True)
  597. def __str__(self):
  598. return self.name
  599. class Meta:
  600. db_table = "knowledgebase"
  601. class Document(DataBaseModel):
  602. id = CharField(max_length=32, primary_key=True)
  603. thumbnail = TextField(null=True, help_text="thumbnail base64 string")
  604. kb_id = CharField(max_length=256, null=False, index=True)
  605. parser_id = CharField(
  606. max_length=32,
  607. null=False,
  608. help_text="default parser ID",
  609. index=True)
  610. parser_config = JSONField(null=False, default={"pages": [[1, 1000000]]})
  611. source_type = CharField(
  612. max_length=128,
  613. null=False,
  614. default="local",
  615. help_text="where dose this document come from",
  616. index=True)
  617. type = CharField(max_length=32, null=False, help_text="file extension",
  618. index=True)
  619. created_by = CharField(
  620. max_length=32,
  621. null=False,
  622. help_text="who created it",
  623. index=True)
  624. name = CharField(
  625. max_length=255,
  626. null=True,
  627. help_text="file name",
  628. index=True)
  629. location = CharField(
  630. max_length=255,
  631. null=True,
  632. help_text="where dose it store",
  633. index=True)
  634. size = IntegerField(default=0, index=True)
  635. token_num = IntegerField(default=0, index=True)
  636. chunk_num = IntegerField(default=0, index=True)
  637. progress = FloatField(default=0, index=True)
  638. progress_msg = TextField(
  639. null=True,
  640. help_text="process message",
  641. default="")
  642. process_begin_at = DateTimeField(null=True, index=True)
  643. process_duation = FloatField(default=0)
  644. run = CharField(
  645. max_length=1,
  646. null=True,
  647. help_text="start to run processing or cancel.(1: run it; 2: cancel)",
  648. default="0",
  649. index=True)
  650. status = CharField(
  651. max_length=1,
  652. null=True,
  653. help_text="is it validate(0: wasted, 1: validate)",
  654. default="1",
  655. index=True)
  656. class Meta:
  657. db_table = "document"
  658. class File(DataBaseModel):
  659. id = CharField(
  660. max_length=32,
  661. primary_key=True)
  662. parent_id = CharField(
  663. max_length=32,
  664. null=False,
  665. help_text="parent folder id",
  666. index=True)
  667. tenant_id = CharField(
  668. max_length=32,
  669. null=False,
  670. help_text="tenant id",
  671. index=True)
  672. created_by = CharField(
  673. max_length=32,
  674. null=False,
  675. help_text="who created it",
  676. index=True)
  677. name = CharField(
  678. max_length=255,
  679. null=False,
  680. help_text="file name or folder name",
  681. index=True)
  682. location = CharField(
  683. max_length=255,
  684. null=True,
  685. help_text="where dose it store",
  686. index=True)
  687. size = IntegerField(default=0, index=True)
  688. type = CharField(max_length=32, null=False, help_text="file extension", index=True)
  689. source_type = CharField(
  690. max_length=128,
  691. null=False,
  692. default="",
  693. help_text="where dose this document come from", index=True)
  694. class Meta:
  695. db_table = "file"
  696. class File2Document(DataBaseModel):
  697. id = CharField(
  698. max_length=32,
  699. primary_key=True)
  700. file_id = CharField(
  701. max_length=32,
  702. null=True,
  703. help_text="file id",
  704. index=True)
  705. document_id = CharField(
  706. max_length=32,
  707. null=True,
  708. help_text="document id",
  709. index=True)
  710. class Meta:
  711. db_table = "file2document"
  712. class Task(DataBaseModel):
  713. id = CharField(max_length=32, primary_key=True)
  714. doc_id = CharField(max_length=32, null=False, index=True)
  715. from_page = IntegerField(default=0)
  716. to_page = IntegerField(default=100000000)
  717. begin_at = DateTimeField(null=True, index=True)
  718. process_duation = FloatField(default=0)
  719. progress = FloatField(default=0, index=True)
  720. progress_msg = TextField(
  721. null=True,
  722. help_text="process message",
  723. default="")
  724. retry_count = IntegerField(default=0)
  725. class Dialog(DataBaseModel):
  726. id = CharField(max_length=32, primary_key=True)
  727. tenant_id = CharField(max_length=32, null=False, index=True)
  728. name = CharField(
  729. max_length=255,
  730. null=True,
  731. help_text="dialog application name",
  732. index=True)
  733. description = TextField(null=True, help_text="Dialog description")
  734. icon = TextField(null=True, help_text="icon base64 string")
  735. language = CharField(
  736. max_length=32,
  737. null=True,
  738. default="Chinese" if "zh_CN" in os.getenv("LANG", "") else "English",
  739. help_text="English|Chinese",
  740. index=True)
  741. llm_id = CharField(max_length=128, null=False, help_text="default llm ID")
  742. llm_setting = JSONField(null=False, default={"temperature": 0.1, "top_p": 0.3, "frequency_penalty": 0.7,
  743. "presence_penalty": 0.4, "max_tokens": 512})
  744. prompt_type = CharField(
  745. max_length=16,
  746. null=False,
  747. default="simple",
  748. help_text="simple|advanced",
  749. index=True)
  750. prompt_config = JSONField(null=False, default={"system": "", "prologue": "Hi! I'm your assistant, what can I do for you?",
  751. "parameters": [], "empty_response": "Sorry! No relevant content was found in the knowledge base!"})
  752. similarity_threshold = FloatField(default=0.2)
  753. vector_similarity_weight = FloatField(default=0.3)
  754. top_n = IntegerField(default=6)
  755. top_k = IntegerField(default=1024)
  756. do_refer = CharField(
  757. max_length=1,
  758. null=False,
  759. default="1",
  760. help_text="it needs to insert reference index into answer or not")
  761. rerank_id = CharField(
  762. max_length=128,
  763. null=False,
  764. help_text="default rerank model ID")
  765. kb_ids = JSONField(null=False, default=[])
  766. status = CharField(
  767. max_length=1,
  768. null=True,
  769. help_text="is it validate(0: wasted, 1: validate)",
  770. default="1",
  771. index=True)
  772. class Meta:
  773. db_table = "dialog"
  774. class Conversation(DataBaseModel):
  775. id = CharField(max_length=32, primary_key=True)
  776. dialog_id = CharField(max_length=32, null=False, index=True)
  777. name = CharField(max_length=255, null=True, help_text="converastion name", index=True)
  778. message = JSONField(null=True)
  779. reference = JSONField(null=True, default=[])
  780. class Meta:
  781. db_table = "conversation"
  782. class APIToken(DataBaseModel):
  783. tenant_id = CharField(max_length=32, null=False, index=True)
  784. token = CharField(max_length=255, null=False, index=True)
  785. dialog_id = CharField(max_length=32, null=False, index=True)
  786. source = CharField(max_length=16, null=True, help_text="none|agent|dialog", index=True)
  787. class Meta:
  788. db_table = "api_token"
  789. primary_key = CompositeKey('tenant_id', 'token')
  790. class API4Conversation(DataBaseModel):
  791. id = CharField(max_length=32, primary_key=True)
  792. dialog_id = CharField(max_length=32, null=False, index=True)
  793. user_id = CharField(max_length=255, null=False, help_text="user_id", index=True)
  794. message = JSONField(null=True)
  795. reference = JSONField(null=True, default=[])
  796. tokens = IntegerField(default=0)
  797. source = CharField(max_length=16, null=True, help_text="none|agent|dialog", index=True)
  798. duration = FloatField(default=0, index=True)
  799. round = IntegerField(default=0, index=True)
  800. thumb_up = IntegerField(default=0, index=True)
  801. class Meta:
  802. db_table = "api_4_conversation"
  803. class UserCanvas(DataBaseModel):
  804. id = CharField(max_length=32, primary_key=True)
  805. avatar = TextField(null=True, help_text="avatar base64 string")
  806. user_id = CharField(max_length=255, null=False, help_text="user_id", index=True)
  807. title = CharField(max_length=255, null=True, help_text="Canvas title")
  808. description = TextField(null=True, help_text="Canvas description")
  809. canvas_type = CharField(max_length=32, null=True, help_text="Canvas type", index=True)
  810. dsl = JSONField(null=True, default={})
  811. class Meta:
  812. db_table = "user_canvas"
  813. class CanvasTemplate(DataBaseModel):
  814. id = CharField(max_length=32, primary_key=True)
  815. avatar = TextField(null=True, help_text="avatar base64 string")
  816. title = CharField(max_length=255, null=True, help_text="Canvas title")
  817. description = TextField(null=True, help_text="Canvas description")
  818. canvas_type = CharField(max_length=32, null=True, help_text="Canvas type", index=True)
  819. dsl = JSONField(null=True, default={})
  820. class Meta:
  821. db_table = "canvas_template"
  822. def migrate_db():
  823. with DB.transaction():
  824. migrator = DatabaseMigrator[settings.DATABASE_TYPE.upper()].value(DB)
  825. try:
  826. migrate(
  827. migrator.add_column('file', 'source_type', CharField(max_length=128, null=False, default="",
  828. help_text="where dose this document come from",
  829. index=True))
  830. )
  831. except Exception:
  832. pass
  833. try:
  834. migrate(
  835. migrator.add_column('tenant', 'rerank_id',
  836. CharField(max_length=128, null=False, default="BAAI/bge-reranker-v2-m3",
  837. help_text="default rerank model ID"))
  838. )
  839. except Exception:
  840. pass
  841. try:
  842. migrate(
  843. migrator.add_column('dialog', 'rerank_id', CharField(max_length=128, null=False, default="",
  844. help_text="default rerank model ID"))
  845. )
  846. except Exception:
  847. pass
  848. try:
  849. migrate(
  850. migrator.add_column('dialog', 'top_k', IntegerField(default=1024))
  851. )
  852. except Exception:
  853. pass
  854. try:
  855. migrate(
  856. migrator.alter_column_type('tenant_llm', 'api_key',
  857. CharField(max_length=1024, null=True, help_text="API KEY", index=True))
  858. )
  859. except Exception:
  860. pass
  861. try:
  862. migrate(
  863. migrator.add_column('api_token', 'source',
  864. CharField(max_length=16, null=True, help_text="none|agent|dialog", index=True))
  865. )
  866. except Exception:
  867. pass
  868. try:
  869. migrate(
  870. migrator.add_column("tenant","tts_id",
  871. CharField(max_length=256,null=True,help_text="default tts model ID",index=True))
  872. )
  873. except Exception:
  874. pass
  875. try:
  876. migrate(
  877. migrator.add_column('api_4_conversation', 'source',
  878. CharField(max_length=16, null=True, help_text="none|agent|dialog", index=True))
  879. )
  880. except Exception:
  881. pass
  882. try:
  883. DB.execute_sql('ALTER TABLE llm DROP PRIMARY KEY;')
  884. DB.execute_sql('ALTER TABLE llm ADD PRIMARY KEY (llm_name,fid);')
  885. except Exception:
  886. pass
  887. try:
  888. migrate(
  889. migrator.add_column('task', 'retry_count', IntegerField(default=0))
  890. )
  891. except Exception:
  892. pass
  893. try:
  894. migrate(
  895. migrator.alter_column_type('api_token', 'dialog_id',
  896. CharField(max_length=32, null=True, index=True))
  897. )
  898. except Exception:
  899. pass