Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions apps/application/api/application_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from rest_framework import serializers

from application.serializers.application import ApplicationCreateSerializer, ApplicationListResponse, \
ApplicationQueryRequest
ApplicationImportRequest, ApplicationEditSerializer
from common.mixins.api_mixin import APIMixin
from common.result import ResultSerializer, ResultPageSerializer
from common.result import ResultSerializer, ResultPageSerializer, DefaultResultSerializer


class ApplicationCreateRequest(ApplicationCreateSerializer.SimplateRequest):
Expand Down Expand Up @@ -120,3 +120,50 @@ def get_request():
@staticmethod
def get_response():
return ApplicationCreateResponse


class ApplicationImportAPI(APIMixin):
@staticmethod
def get_parameters():
ApplicationCreateAPI.get_parameters()

@staticmethod
def get_request():
return ApplicationImportRequest


class ApplicationOperateAPI(APIMixin):
@staticmethod
def get_parameters():
return [
OpenApiParameter(
name="workspace_id",
description="工作空间id",
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="application_id",
description="应用id",
type=OpenApiTypes.STR,
location='path',
required=True,
)
]


class ApplicationExportAPI(APIMixin):
@staticmethod
def get_parameters():
return ApplicationOperateAPI.get_parameters()

@staticmethod
def get_response():
return DefaultResultSerializer


class ApplicationEditAPI(APIMixin):
@staticmethod
def get_request():
return ApplicationEditSerializer
2 changes: 2 additions & 0 deletions apps/application/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
"""
from .application import *
from .application_access_token import *
from .application_chat import *
from .application_api_key import *
13 changes: 13 additions & 0 deletions apps/application/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,16 @@ class ApplicationKnowledgeMapping(AppModelMixin):

class Meta:
db_table = "application_knowledge_mapping"


class WorkFlowVersion(AppModelMixin):
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
application = models.ForeignKey(Application, on_delete=models.CASCADE)
workspace_id = models.CharField(max_length=64, verbose_name="工作空间id", default="default", db_index=True)
name = models.CharField(verbose_name="版本名称", max_length=128, default="")
publish_user_id = models.UUIDField(verbose_name="发布者id", max_length=128, default=None, null=True)
publish_user_name = models.CharField(verbose_name="发布者名称", max_length=128, default="")
work_flow = models.JSONField(verbose_name="工作流数据", default=dict)

class Meta:
db_table = "application_work_flow_version"
19 changes: 16 additions & 3 deletions apps/application/models/application_api_key.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

import uuid

from django.contrib.postgres.fields import ArrayField
from django.db import models

from application.models import Application
from common.mixins.app_model_mixin import AppModelMixin

from users.models import User


Expand All @@ -23,4 +21,19 @@ class ApplicationApiKey(AppModelMixin):
, default=list)

class Meta:
db_table = "application_api_key"
db_table = "application_api_key"


class ApplicationPublicAccessClient(AppModelMixin):
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
client_id = models.UUIDField(max_length=128, default=uuid.uuid1, verbose_name="公共访问链接客户端id")
client_type = models.CharField(max_length=64, verbose_name="客户端类型")
application = models.ForeignKey(Application, on_delete=models.CASCADE, verbose_name="应用id")
access_num = models.IntegerField(default=0, verbose_name="访问总次数次数")
intraday_access_num = models.IntegerField(default=0, verbose_name="当日访问次数")

class Meta:
db_table = "application_public_access_client"
indexes = [
models.Index(fields=['application_id', 'client_id']),
]
83 changes: 83 additions & 0 deletions apps/application/models/application_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎虎
@file: application_chat_log.py
@date:2025/5/29 17:12
@desc:
"""
import uuid_utils.compat as uuid
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.utils.translation import gettext_lazy as _
from langchain_core.messages import HumanMessage, AIMessage

from application.models import Application
from common.encoder.encoder import SystemEncoder
from common.mixins.app_model_mixin import AppModelMixin


def default_asker():
return {'user_name': '游客'}


class Chat(AppModelMixin):
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid7(), editable=False, verbose_name="主键id")
application = models.ForeignKey(Application, on_delete=models.CASCADE)
abstract = models.CharField(max_length=1024, verbose_name="摘要")
asker = models.JSONField(verbose_name="访问者", default=default_asker, encoder=SystemEncoder)
client_id = models.UUIDField(verbose_name="客户端id", default=None, null=True)
is_deleted = models.BooleanField(verbose_name="", default=False)

class Meta:
db_table = "application_chat"


class VoteChoices(models.TextChoices):
"""订单类型"""
UN_VOTE = "-1", '未投票'
STAR = "0", '赞同'
TRAMPLE = "1", '反对'



class ChatRecord(AppModelMixin):
"""
对话日志 详情
"""
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
chat = models.ForeignKey(Chat, on_delete=models.CASCADE)
vote_status = models.CharField(verbose_name='投票', max_length=10, choices=VoteChoices.choices,
default=VoteChoices.UN_VOTE)
problem_text = models.CharField(max_length=10240, verbose_name="问题")
answer_text = models.CharField(max_length=40960, verbose_name="答案")
answer_text_list = ArrayField(verbose_name="改进标注列表",
base_field=models.JSONField()
, default=list)
message_tokens = models.IntegerField(verbose_name="请求token数量", default=0)
answer_tokens = models.IntegerField(verbose_name="响应token数量", default=0)
const = models.IntegerField(verbose_name="总费用", default=0)
details = models.JSONField(verbose_name="对话详情", default=dict, encoder=SystemEncoder)
improve_paragraph_id_list = ArrayField(verbose_name="改进标注列表",
base_field=models.UUIDField(max_length=128, blank=True)
, default=list)
run_time = models.FloatField(verbose_name="运行时长", default=0)
index = models.IntegerField(verbose_name="对话下标")

def get_human_message(self):
if 'problem_padding' in self.details:
return HumanMessage(content=self.details.get('problem_padding').get('padding_problem_text'))
return HumanMessage(content=self.problem_text)

def get_ai_message(self):
answer_text = self.answer_text
if answer_text is None or len(str(answer_text).strip()) == 0:
answer_text = _(
'Sorry, no relevant content was found. Please re-describe your problem or provide more information. ')
return AIMessage(content=answer_text)

def get_node_details_runtime_node_id(self, runtime_node_id):
return self.details.get(runtime_node_id, None)

class Meta:
db_table = "application_chat_record"
Loading
Loading