refactor(service): 重构模型网关服务结构
This commit is contained in:
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
// ParseAndValidate 解析并校验结果
|
||||
func ParseAndValidate(raw map[string]any, model *entity.AsynchModel) (map[string]any, error) {
|
||||
func ParseAndValidate(raw map[string]any, model *entity.ModelGatewayModel) (map[string]any, error) {
|
||||
// 1) 解析 content 字符串为 rounds 数组
|
||||
contentVal, ok := raw[model.ResponseBody]
|
||||
if !ok {
|
||||
@@ -94,53 +94,6 @@ func ParseStructResult(raw map[string]any, responseBody string) map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
// ValidatePromptResult 校验模型返回结果的 JSON 结构完整性
|
||||
// raw 必须包含 "rounds" 字段,格式为 []map[string]any
|
||||
func ValidatePromptResult(raw map[string]any, model *entity.AsynchModel) error {
|
||||
// 1) 获取 rounds
|
||||
roundsRaw, ok := raw["rounds"]
|
||||
if !ok {
|
||||
return fmt.Errorf("缺少 rounds 字段")
|
||||
}
|
||||
rounds, ok := roundsRaw.([]any)
|
||||
if !ok {
|
||||
return fmt.Errorf("rounds 不是数组")
|
||||
}
|
||||
if len(rounds) == 0 {
|
||||
return fmt.Errorf("rounds 数组为空")
|
||||
}
|
||||
|
||||
// 2) 没有配置必填字段,跳过
|
||||
if len(model.RequiredFields) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 3) 逐条校验
|
||||
for i, r := range rounds {
|
||||
round, ok := r.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, field := range model.RequiredFields {
|
||||
if gjson.New(round).Get(field).IsNil() {
|
||||
return fmt.Errorf("rounds[%d] 缺少必填字段: %s", i, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateRequiredFields 校验单个 round 对象的必选字段
|
||||
func validateRequiredFields(round map[string]any, requiredFields []string, prefix string) error {
|
||||
for _, field := range requiredFields {
|
||||
if gjson.New(round).Get(field).IsNil() {
|
||||
return fmt.Errorf("%s 缺少必填字段: %s", prefix, field)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseHeadMsgHeaders 从 head_msg JSON 中提取请求头
|
||||
// head_msg 格式示例:
|
||||
//
|
||||
@@ -198,16 +151,17 @@ func MapResponsePayload(mapping map[string]any, result map[string]any) (map[stri
|
||||
return mapped, nil
|
||||
}
|
||||
|
||||
// GetModelBody 获取数据库中保存的模型信息
|
||||
func GetModelBody(v map[string]any) map[string]any {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
if p, ok := v["body"]; ok {
|
||||
return gconv.Map(p)
|
||||
}
|
||||
return v
|
||||
}
|
||||
//
|
||||
//// GetModelBody 获取数据库中保存的模型信息
|
||||
//func GetModelBody(v map[string]any) map[string]any {
|
||||
// if v == nil {
|
||||
// return nil
|
||||
// }
|
||||
// if p, ok := v["body"]; ok {
|
||||
// return gconv.Map(p)
|
||||
// }
|
||||
// return v
|
||||
//}
|
||||
|
||||
// BodyToQuery 将 body 转为 url.Values
|
||||
func BodyToQuery(payload map[string]any) (url.Values, error) {
|
||||
|
||||
Reference in New Issue
Block a user