Skip to content

Commit a9f1179

Browse files
committed
feat: application
1 parent 165efaa commit a9f1179

File tree

144 files changed

+1628
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+1628
-288
lines changed

apps/application/api/application_api.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from rest_framework import serializers
1313

1414
from application.serializers.application import ApplicationCreateSerializer, ApplicationListResponse, \
15-
ApplicationQueryRequest
15+
ApplicationImportRequest, ApplicationEditSerializer
1616
from common.mixins.api_mixin import APIMixin
17-
from common.result import ResultSerializer, ResultPageSerializer
17+
from common.result import ResultSerializer, ResultPageSerializer, DefaultResultSerializer
1818

1919

2020
class ApplicationCreateRequest(ApplicationCreateSerializer.SimplateRequest):
@@ -120,3 +120,50 @@ def get_request():
120120
@staticmethod
121121
def get_response():
122122
return ApplicationCreateResponse
123+
124+
125+
class ApplicationImportAPI(APIMixin):
126+
@staticmethod
127+
def get_parameters():
128+
ApplicationCreateAPI.get_parameters()
129+
130+
@staticmethod
131+
def get_request():
132+
return ApplicationImportRequest
133+
134+
135+
class ApplicationOperateAPI(APIMixin):
136+
@staticmethod
137+
def get_parameters():
138+
return [
139+
OpenApiParameter(
140+
name="workspace_id",
141+
description="工作空间id",
142+
type=OpenApiTypes.STR,
143+
location='path',
144+
required=True,
145+
),
146+
OpenApiParameter(
147+
name="application_id",
148+
description="应用id",
149+
type=OpenApiTypes.STR,
150+
location='path',
151+
required=True,
152+
)
153+
]
154+
155+
156+
class ApplicationExportAPI(APIMixin):
157+
@staticmethod
158+
def get_parameters():
159+
return ApplicationOperateAPI.get_parameters()
160+
161+
@staticmethod
162+
def get_response():
163+
return DefaultResultSerializer
164+
165+
166+
class ApplicationEditAPI(APIMixin):
167+
@staticmethod
168+
def get_request():
169+
return ApplicationEditSerializer

apps/application/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
"""
99
from .application import *
1010
from .application_access_token import *
11+
from .application_chat import *
12+
from .application_api_key import *

apps/application/models/application.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,16 @@ class ApplicationKnowledgeMapping(AppModelMixin):
117117

118118
class Meta:
119119
db_table = "application_knowledge_mapping"
120+
121+
122+
class WorkFlowVersion(AppModelMixin):
123+
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
124+
application = models.ForeignKey(Application, on_delete=models.CASCADE)
125+
workspace_id = models.CharField(max_length=64, verbose_name="工作空间id", default="default", db_index=True)
126+
name = models.CharField(verbose_name="版本名称", max_length=128, default="")
127+
publish_user_id = models.UUIDField(verbose_name="发布者id", max_length=128, default=None, null=True)
128+
publish_user_name = models.CharField(verbose_name="发布者名称", max_length=128, default="")
129+
work_flow = models.JSONField(verbose_name="工作流数据", default=dict)
130+
131+
class Meta:
132+
db_table = "application_work_flow_version"

apps/application/models/application_api_key.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
21
import uuid
32

43
from django.contrib.postgres.fields import ArrayField
54
from django.db import models
65

76
from application.models import Application
87
from common.mixins.app_model_mixin import AppModelMixin
9-
108
from users.models import User
119

1210

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

2523
class Meta:
26-
db_table = "application_api_key"
24+
db_table = "application_api_key"
25+
26+
27+
class ApplicationPublicAccessClient(AppModelMixin):
28+
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
29+
client_id = models.UUIDField(max_length=128, default=uuid.uuid1, verbose_name="公共访问链接客户端id")
30+
client_type = models.CharField(max_length=64, verbose_name="客户端类型")
31+
application = models.ForeignKey(Application, on_delete=models.CASCADE, verbose_name="应用id")
32+
access_num = models.IntegerField(default=0, verbose_name="访问总次数次数")
33+
intraday_access_num = models.IntegerField(default=0, verbose_name="当日访问次数")
34+
35+
class Meta:
36+
db_table = "application_public_access_client"
37+
indexes = [
38+
models.Index(fields=['application_id', 'client_id']),
39+
]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎虎
5+
@file: application_chat_log.py
6+
@date:2025/5/29 17:12
7+
@desc:
8+
"""
9+
import uuid_utils.compat as uuid
10+
from django.contrib.postgres.fields import ArrayField
11+
from django.db import models
12+
from django.utils.translation import gettext_lazy as _
13+
from langchain_core.messages import HumanMessage, AIMessage
14+
15+
from application.models import Application
16+
from common.encoder.encoder import SystemEncoder
17+
from common.mixins.app_model_mixin import AppModelMixin
18+
19+
20+
def default_asker():
21+
return {'user_name': '游客'}
22+
23+
24+
class Chat(AppModelMixin):
25+
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid7(), editable=False, verbose_name="主键id")
26+
application = models.ForeignKey(Application, on_delete=models.CASCADE)
27+
abstract = models.CharField(max_length=1024, verbose_name="摘要")
28+
asker = models.JSONField(verbose_name="访问者", default=default_asker, encoder=SystemEncoder)
29+
client_id = models.UUIDField(verbose_name="客户端id", default=None, null=True)
30+
is_deleted = models.BooleanField(verbose_name="", default=False)
31+
32+
class Meta:
33+
db_table = "application_chat"
34+
35+
36+
class VoteChoices(models.TextChoices):
37+
"""订单类型"""
38+
UN_VOTE = "-1", '未投票'
39+
STAR = "0", '赞同'
40+
TRAMPLE = "1", '反对'
41+
42+
43+
44+
class ChatRecord(AppModelMixin):
45+
"""
46+
对话日志 详情
47+
"""
48+
id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid1, editable=False, verbose_name="主键id")
49+
chat = models.ForeignKey(Chat, on_delete=models.CASCADE)
50+
vote_status = models.CharField(verbose_name='投票', max_length=10, choices=VoteChoices.choices,
51+
default=VoteChoices.UN_VOTE)
52+
problem_text = models.CharField(max_length=10240, verbose_name="问题")
53+
answer_text = models.CharField(max_length=40960, verbose_name="答案")
54+
answer_text_list = ArrayField(verbose_name="改进标注列表",
55+
base_field=models.JSONField()
56+
, default=list)
57+
message_tokens = models.IntegerField(verbose_name="请求token数量", default=0)
58+
answer_tokens = models.IntegerField(verbose_name="响应token数量", default=0)
59+
const = models.IntegerField(verbose_name="总费用", default=0)
60+
details = models.JSONField(verbose_name="对话详情", default=dict, encoder=SystemEncoder)
61+
improve_paragraph_id_list = ArrayField(verbose_name="改进标注列表",
62+
base_field=models.UUIDField(max_length=128, blank=True)
63+
, default=list)
64+
run_time = models.FloatField(verbose_name="运行时长", default=0)
65+
index = models.IntegerField(verbose_name="对话下标")
66+
67+
def get_human_message(self):
68+
if 'problem_padding' in self.details:
69+
return HumanMessage(content=self.details.get('problem_padding').get('padding_problem_text'))
70+
return HumanMessage(content=self.problem_text)
71+
72+
def get_ai_message(self):
73+
answer_text = self.answer_text
74+
if answer_text is None or len(str(answer_text).strip()) == 0:
75+
answer_text = _(
76+
'Sorry, no relevant content was found. Please re-describe your problem or provide more information. ')
77+
return AIMessage(content=answer_text)
78+
79+
def get_node_details_runtime_node_id(self, runtime_node_id):
80+
return self.details.get(runtime_node_id, None)
81+
82+
class Meta:
83+
db_table = "application_chat_record"

0 commit comments

Comments
 (0)