mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 10:12:51 +00:00
feat: add initial model structure for knowledge management
This commit is contained in:
parent
f08b551a72
commit
ec643fca7f
|
|
@ -3,7 +3,7 @@
|
|||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
import uuid_utils as uuid
|
||||
from textwrap import dedent
|
||||
|
||||
from diskcache import Cache
|
||||
|
|
@ -18,7 +18,7 @@ class ToolExecutor:
|
|||
def __init__(self, sandbox=False):
|
||||
self.sandbox = sandbox
|
||||
if sandbox:
|
||||
self.sandbox_path = '/opt/maxkb/app/sandbox'
|
||||
self.sandbox_path = '/opt/maxkb-app/sandbox'
|
||||
self.user = 'sandbox'
|
||||
else:
|
||||
self.sandbox_path = os.path.join(PROJECT_DIR, 'data', 'sandbox')
|
||||
|
|
@ -35,7 +35,7 @@ class ToolExecutor:
|
|||
os.umask(old_mask)
|
||||
|
||||
def exec_code(self, code_str, keywords):
|
||||
_id = str(uuid.uuid1())
|
||||
_id = str(uuid.uuid7())
|
||||
success = '{"code":200,"msg":"成功","data":exec_result}'
|
||||
err = '{"code":500,"msg":str(e),"data":None}'
|
||||
path = r'' + self.sandbox_path + ''
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class KnowledgeConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'knowledge'
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
import uuid_utils.compat as uuid
|
||||
from django.db import models
|
||||
from django.db.models.signals import pre_delete
|
||||
from django.dispatch import receiver
|
||||
|
||||
from common.db.sql_execute import select_one
|
||||
from common.mixins.app_model_mixin import AppModelMixin
|
||||
from models_provider.models import Model
|
||||
from users.models import User
|
||||
|
||||
|
||||
class KnowledgeType(models.TextChoices):
|
||||
base = 0, '通用类型'
|
||||
web = 1, 'web站点类型'
|
||||
lark = 2, '飞书类型'
|
||||
yuque = 3, '语雀类型'
|
||||
|
||||
|
||||
def default_model():
|
||||
# todo : 这里需要从数据库中获取默认的模型
|
||||
return uuid.UUID('42f63a3d-427e-11ef-b3ec-a8a1595801ab')
|
||||
|
||||
|
||||
class DataSet(AppModelMixin):
|
||||
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid7, editable=False, verbose_name="主键id")
|
||||
name = models.CharField(max_length=150, verbose_name="知识库名称")
|
||||
desc = models.CharField(max_length=256, verbose_name="描述")
|
||||
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name="所属用户")
|
||||
type = models.IntegerField(verbose_name='类型', choices=KnowledgeType.choices, default=KnowledgeType.base)
|
||||
embedding_mode = models.ForeignKey(Model, on_delete=models.DO_NOTHING, verbose_name="向量模型",
|
||||
default=default_model)
|
||||
meta = models.JSONField(verbose_name="元数据", default=dict)
|
||||
|
||||
class Meta:
|
||||
db_table = "knowledge"
|
||||
|
||||
|
||||
class File(AppModelMixin):
|
||||
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid7, editable=False, verbose_name="主键id")
|
||||
file_name = models.CharField(max_length=256, verbose_name="文件名称", default="")
|
||||
loid = models.IntegerField(verbose_name="loid")
|
||||
meta = models.JSONField(verbose_name="文件关联数据", default=dict)
|
||||
|
||||
class Meta:
|
||||
db_table = "file"
|
||||
|
||||
def save(self, bytea=None, force_insert=False, force_update=False, using=None, update_fields=None):
|
||||
result = select_one("SELECT lo_from_bytea(%s, %s::bytea) as loid", [0, bytea])
|
||||
self.loid = result['loid']
|
||||
super().save()
|
||||
|
||||
def get_bytes(self):
|
||||
result = select_one(f'SELECT lo_get({self.loid}) as "data"', [])
|
||||
return result['data']
|
||||
|
||||
|
||||
@receiver(pre_delete, sender=File)
|
||||
def on_delete_file(sender, instance, **kwargs):
|
||||
select_one(f'SELECT lo_unlink({instance.loid})', [])
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
|
|
@ -293,7 +293,7 @@ class ModelSerializer(serializers.Serializer):
|
|||
|
||||
credential = self.data.get('credential')
|
||||
model_data = {
|
||||
'id': uuid.uuid1(),
|
||||
'id': uuid.uuid7(),
|
||||
'status': status,
|
||||
'user_id': self.data.get('user_id'),
|
||||
'name': self.data.get('name'),
|
||||
|
|
|
|||
Loading…
Reference in New Issue