feat: add initial model structure for knowledge management
This commit is contained in:
parent
f08b551a72
commit
ec643fca7f
@ -3,7 +3,7 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import uuid_utils as uuid
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
from diskcache import Cache
|
from diskcache import Cache
|
||||||
@ -18,7 +18,7 @@ class ToolExecutor:
|
|||||||
def __init__(self, sandbox=False):
|
def __init__(self, sandbox=False):
|
||||||
self.sandbox = sandbox
|
self.sandbox = sandbox
|
||||||
if sandbox:
|
if sandbox:
|
||||||
self.sandbox_path = '/opt/maxkb/app/sandbox'
|
self.sandbox_path = '/opt/maxkb-app/sandbox'
|
||||||
self.user = 'sandbox'
|
self.user = 'sandbox'
|
||||||
else:
|
else:
|
||||||
self.sandbox_path = os.path.join(PROJECT_DIR, 'data', 'sandbox')
|
self.sandbox_path = os.path.join(PROJECT_DIR, 'data', 'sandbox')
|
||||||
@ -35,7 +35,7 @@ class ToolExecutor:
|
|||||||
os.umask(old_mask)
|
os.umask(old_mask)
|
||||||
|
|
||||||
def exec_code(self, code_str, keywords):
|
def exec_code(self, code_str, keywords):
|
||||||
_id = str(uuid.uuid1())
|
_id = str(uuid.uuid7())
|
||||||
success = '{"code":200,"msg":"成功","data":exec_result}'
|
success = '{"code":200,"msg":"成功","data":exec_result}'
|
||||||
err = '{"code":500,"msg":str(e),"data":None}'
|
err = '{"code":500,"msg":str(e),"data":None}'
|
||||||
path = r'' + self.sandbox_path + ''
|
path = r'' + self.sandbox_path + ''
|
||||||
|
|||||||
0
apps/knowledge/__init__.py
Normal file
0
apps/knowledge/__init__.py
Normal file
3
apps/knowledge/admin.py
Normal file
3
apps/knowledge/admin.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
6
apps/knowledge/apps.py
Normal file
6
apps/knowledge/apps.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class KnowledgeConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'knowledge'
|
||||||
0
apps/knowledge/migrations/__init__.py
Normal file
0
apps/knowledge/migrations/__init__.py
Normal file
0
apps/knowledge/models/__init__.py
Normal file
0
apps/knowledge/models/__init__.py
Normal file
59
apps/knowledge/models/knowledge.py
Normal file
59
apps/knowledge/models/knowledge.py
Normal file
@ -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})', [])
|
||||||
3
apps/knowledge/tests.py
Normal file
3
apps/knowledge/tests.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
3
apps/knowledge/urls.py
Normal file
3
apps/knowledge/urls.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
||||||
0
apps/knowledge/views/__init__.py
Normal file
0
apps/knowledge/views/__init__.py
Normal file
@ -293,7 +293,7 @@ class ModelSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
credential = self.data.get('credential')
|
credential = self.data.get('credential')
|
||||||
model_data = {
|
model_data = {
|
||||||
'id': uuid.uuid1(),
|
'id': uuid.uuid7(),
|
||||||
'status': status,
|
'status': status,
|
||||||
'user_id': self.data.get('user_id'),
|
'user_id': self.data.get('user_id'),
|
||||||
'name': self.data.get('name'),
|
'name': self.data.get('name'),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user