zhaoqingang
2024-12-06 9e51ec752c93d850db15cd61a7a3463d8fe94344
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
from datetime import datetime
from enum import IntEnum
from sqlalchemy import Column, String, Enum as SQLAlchemyEnum, Integer, BigInteger, DateTime, Text, Float, Boolean
from app.models.base_model import Base
 
 
class AgentType(IntEnum):
    RAGFLOW = 1
    BISHENG = 2
    BASIC = 3
    DIFY = 4
 
 
class AgentModel(Base):
    __tablename__ = "agent"
    id = Column(String(255), primary_key=True)
    name = Column(String(255))
    sort = Column(Integer, default=0, nullable=False)
    agent_type = Column(SQLAlchemyEnum(AgentType), nullable=False)
    type = Column(String(255), nullable=False)
 
    # to_dict 方法
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'agent_type': self.agent_type,
            'type': self.type
        }
 
 
class CanvasModel(Base):
    __tablename__ = 'canvas'
 
    id = Column(String(32), primary_key=True)      # id
    create_date = Column(DateTime, default=datetime.now)
    update_date = Column(DateTime, default=datetime.now)
    avatar = Column(Text)                    # 图标
    user_id = Column(String(255))            # 用户id
    title = Column(String(255))               # 标题
    description = Column(Text)               # 说明
    canvas_type = Column(String(32))           # agent类型
    dsl = Column(Text)
    agent_type = Column(String(2))            # agent平台
 
 
 
    def get_id(self):
        return str(self.id)
 
    def to_json(self):
        return {
            'id': self.id,
            'create_date': self.create_date,
            'update_date': self.update_date,
            'avatar': self.avatar,
            'user_id': self.user_id,
            'title': self.title,
            'description': self.description,
            'canvas_type': self.canvas_type,
            'dsl': self.dsl
        }
 
 
class UnifiedAgentModel(Base):
    __tablename__ = 'unified_agent'
 
    id = Column(String(32), primary_key=True)
    tenant_id = Column(String(32))
    name = Column(String(255))
    description = Column(Text)
    icon = Column(Text)
    prompt_type = Column(String(16))
    prompt_config = Column(Text, default='{}')
    status = Column(String(1))
    prompts = Column(Text, default='[]')
    language = Column(String(32))
    llm_id = Column(String(128), default='')
    llm_setting = Column(Text, default='{}')
    similarity_threshold = Column(Float, default=0.0)
    vector_similarity_weight = Column(Float, default=0.0)
    top_n = Column(Integer, default=0)
    top_k = Column(Integer, default=0)
    do_refer = Column(String(1), default='')
    rerank_id = Column(String(128), default='')
    kb_ids = Column(Text, default='[]')
    hide = Column(Boolean, default=False)
    type = Column(Integer)
    canvas_type = Column(String(32), default="")
    dsl = Column(Text, default='{}')
 
    def get_id(self):
        return str(self.id)
 
    def to_json(self):
        if self.prompts is None or self.prompts == '':
            self.prompts = '[]'
        if self.prompt_config is None or self.prompt_config == '':
            self.prompt_config = '{}'
        if self.dsl is None or self.dsl == '':
            self.dsl = '{}'
        if self.kb_ids is None or self.kb_ids == '':
            self.kb_ids = '[]'
        return {
            'id': self.id,
            'create_time': self.create_time,
            'create_date': self.create_date,
            'update_time': self.update_time,
            'update_date': self.update_date,
            'tenant_id': self.tenant_id,
            'name': self.name,
            'description': self.description,
            'icon': self.icon,
            'language': self.language,
            'llm_id': self.llm_id,
            'similarity_threshold': self.similarity_threshold,
            'vector_similarity_weight': self.vector_similarity_weight,
            'top_n': self.top_n,
            'top_k': self.top_k,
            'do_refer': self.do_refer,
            'kb_ids': self.kb_ids,
            'status': self.status,
            'prompt_type': self.prompt_type,
            'prompt_config': json.loads(self.prompt_config),
            'prompts': json.loads(self.prompts),
            'dsl': json.loads(self.dsl)
        }
 
    @staticmethod
    def is_type(record_id, t):
        record = UnifiedAgentModel.get_by_id(record_id)
        return record and record.type == t