Skip to content

Commit e1c74bb

Browse files
authored
Merge pull request #27 from DEEIX-AI/support_system_prompt
feat: introduce system prompt feature with validation
2 parents cdacfd7 + 911a923 commit e1c74bb

37 files changed

Lines changed: 523 additions & 66 deletions

backend/docs/docs.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ const docTemplate = `{
15451545
},
15461546
{
15471547
"type": "string",
1548-
"description": "路由状态:active/inactive/unbound",
1548+
"description": "路由状态:bound/active/inactive",
15491549
"name": "route_status",
15501550
"in": "query"
15511551
},
@@ -8900,6 +8900,10 @@ const docTemplate = `{
89008900
"inactive"
89018901
]
89028902
},
8903+
"systemPrompt": {
8904+
"type": "string",
8905+
"maxLength": 20000
8906+
},
89038907
"vendor": {
89048908
"type": "string",
89058909
"maxLength": 64
@@ -9209,6 +9213,9 @@ const docTemplate = `{
92099213
"status": {
92109214
"type": "string"
92119215
},
9216+
"systemPrompt": {
9217+
"type": "string"
9218+
},
92129219
"updatedAt": {
92139220
"type": "string"
92149221
},
@@ -9516,6 +9523,10 @@ const docTemplate = `{
95169523
"inactive"
95179524
]
95189525
},
9526+
"systemPrompt": {
9527+
"type": "string",
9528+
"maxLength": 20000
9529+
},
95199530
"vendor": {
95209531
"type": "string",
95219532
"maxLength": 64

backend/docs/swagger.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@
15381538
},
15391539
{
15401540
"type": "string",
1541-
"description": "路由状态:active/inactive/unbound",
1541+
"description": "路由状态:bound/active/inactive",
15421542
"name": "route_status",
15431543
"in": "query"
15441544
},
@@ -8893,6 +8893,10 @@
88938893
"inactive"
88948894
]
88958895
},
8896+
"systemPrompt": {
8897+
"type": "string",
8898+
"maxLength": 20000
8899+
},
88968900
"vendor": {
88978901
"type": "string",
88988902
"maxLength": 64
@@ -9202,6 +9206,9 @@
92029206
"status": {
92039207
"type": "string"
92049208
},
9209+
"systemPrompt": {
9210+
"type": "string"
9211+
},
92059212
"updatedAt": {
92069213
"type": "string"
92079214
},
@@ -9509,6 +9516,10 @@
95099516
"inactive"
95109517
]
95119518
},
9519+
"systemPrompt": {
9520+
"type": "string",
9521+
"maxLength": 20000
9522+
},
95129523
"vendor": {
95139524
"type": "string",
95149525
"maxLength": 64

backend/docs/swagger.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,9 @@ definitions:
15441544
- active
15451545
- inactive
15461546
type: string
1547+
systemPrompt:
1548+
maxLength: 20000
1549+
type: string
15471550
vendor:
15481551
maxLength: 64
15491552
type: string
@@ -1757,6 +1760,8 @@ definitions:
17571760
type: integer
17581761
status:
17591762
type: string
1763+
systemPrompt:
1764+
type: string
17601765
updatedAt:
17611766
type: string
17621767
vendor:
@@ -1960,6 +1965,9 @@ definitions:
19601965
- active
19611966
- inactive
19621967
type: string
1968+
systemPrompt:
1969+
maxLength: 20000
1970+
type: string
19631971
vendor:
19641972
maxLength: 64
19651973
type: string
@@ -4306,7 +4314,7 @@ paths:
43064314
in: query
43074315
name: q
43084316
type: string
4309-
- description: 路由状态:active/inactive/unbound
4317+
- description: 路由状态:bound/active/inactive
43104318
in: query
43114319
name: route_status
43124320
type: string

backend/internal/application/channel/dto.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ type ModelView struct {
123123
KindsJSON string
124124
Icon string
125125
CapabilitiesJSON string
126+
SystemPrompt string
126127
Status string
127128
Description string
128129
SortOrder int

backend/internal/application/channel/errs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535
ErrInvalidUpstreamBaseURL = errors.New("invalid upstream base url")
3636
// ErrInvalidKinds 模型类型无效。
3737
ErrInvalidKinds = errors.New("invalid kinds")
38+
// ErrSystemPromptTooLong 系统提示词长度超过允许范围。
39+
ErrSystemPromptTooLong = errors.New("system prompt too long")
3840
// ErrInvalidModelOrder 模型排序参数无效。
3941
ErrInvalidModelOrder = errors.New("invalid model order")
4042
// ErrProtocolRequired 无法通过瀑布规则推断协议。

backend/internal/application/channel/input.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type CreateModelInput struct {
4545
KindsJSON string
4646
Icon string
4747
CapabilitiesJSON string
48+
SystemPrompt string
4849
Status string
4950
Description string
5051
}
@@ -56,6 +57,7 @@ type UpdateModelInput struct {
5657
KindsJSON *string
5758
Icon *string
5859
CapabilitiesJSON *string
60+
SystemPrompt *string
5961
Status *string
6062
Description *string
6163
}

backend/internal/application/channel/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type ResolvedRoute struct {
5858
ModelVendor string
5959
ModelIcon string
6060
ModelCapabilitiesJSON string
61+
ModelSystemPrompt string
6162
UpstreamModel string
6263
UpstreamCbFailureThreshold int
6364
UpstreamCbModelThreshold int

backend/internal/application/channel/service_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func toModelView(item repository.ChannelModelListRow) ModelView {
7575
KindsJSON: item.KindsJSON,
7676
Icon: item.Icon,
7777
CapabilitiesJSON: item.CapabilitiesJSON,
78+
SystemPrompt: item.SystemPrompt,
7879
Status: item.Status,
7980
Description: item.Description,
8081
SortOrder: item.SortOrder,

backend/internal/application/channel/service_model.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
// 模型管理
1616
// ---------------------------------------------------------------------------
1717

18+
const maxSystemPromptChars = 20000
19+
1820
// ListModelsInput 定义模型列表筛选排序条件。
1921
type ListModelsInput struct {
2022
Query string
@@ -220,13 +222,18 @@ func (s *Service) CreateModel(ctx context.Context, input CreateModelInput) (*Mod
220222
if err := validateOptionalJSON(strings.TrimSpace(input.CapabilitiesJSON)); err != nil {
221223
return nil, ErrInvalidJSONConfig
222224
}
225+
systemPrompt := strings.TrimSpace(input.SystemPrompt)
226+
if len([]rune(systemPrompt)) > maxSystemPromptChars {
227+
return nil, ErrSystemPromptTooLong
228+
}
223229

224230
item := &domainchannel.PlatformModel{
225231
PlatformModelName: platformModelName,
226232
Vendor: normalizeModelVendor(input.Vendor, platformModelName),
227233
KindsJSON: kindsJSON,
228234
Icon: normalizeModelIcon(input.Icon, input.Vendor, platformModelName),
229235
CapabilitiesJSON: strings.TrimSpace(input.CapabilitiesJSON),
236+
SystemPrompt: systemPrompt,
230237
Status: normalizeStatus(input.Status),
231238
Description: strings.TrimSpace(input.Description),
232239
}
@@ -281,6 +288,13 @@ func (s *Service) UpdateModel(ctx context.Context, modelID uint, input UpdateMod
281288
}
282289
update.CapabilitiesJSON = &normalized
283290
}
291+
if input.SystemPrompt != nil {
292+
systemPrompt := strings.TrimSpace(*input.SystemPrompt)
293+
if len([]rune(systemPrompt)) > maxSystemPromptChars {
294+
return nil, ErrSystemPromptTooLong
295+
}
296+
update.SystemPrompt = &systemPrompt
297+
}
284298
if input.Status != nil {
285299
status := normalizeStatus(*input.Status)
286300
update.Status = &status

backend/internal/application/channel/service_routing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ func buildResolvedRoute(row repository.ChannelUpstreamRouteRow, apiKey string) *
318318
ModelVendor: strings.TrimSpace(row.ModelVendor),
319319
ModelIcon: strings.TrimSpace(row.ModelIcon),
320320
ModelCapabilitiesJSON: strings.TrimSpace(row.ModelCapabilitiesJSON),
321+
ModelSystemPrompt: strings.TrimSpace(row.ModelSystemPrompt),
321322
UpstreamModel: strings.TrimSpace(row.UpstreamModelName),
322323
UpstreamCbFailureThreshold: row.UpstreamCbFailureThreshold,
323324
UpstreamCbModelThreshold: row.UpstreamCbModelThreshold,

0 commit comments

Comments
 (0)