forked from longtao.wu/droboticflow
🛠️ Harden AI operator hyperparameter parsing
This commit is contained in:
parent
2424bfd7f3
commit
03d19341ee
@ -82,6 +82,87 @@ type GeneratedOperatorFile struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type generatedHyperParamPayload struct {
|
||||
Key string `json:"key"`
|
||||
Value any `json:"value"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
func (generated *GeneratedOperator) UnmarshalJSON(data []byte) error {
|
||||
type generatedOperatorPayload struct {
|
||||
Name string `json:"name"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
ImageID string `json:"imageId,omitempty"`
|
||||
ContainerCommand []string `json:"containerCommand,omitempty"`
|
||||
ContainerArgs []string `json:"containerArgs,omitempty"`
|
||||
RuntimeContract map[string]any `json:"runtimeContract,omitempty"`
|
||||
Resources *domain.OperatorResources `json:"resources,omitempty"`
|
||||
Inputs []GeneratedOperatorPort `json:"inputs"`
|
||||
Outputs []GeneratedOperatorPort `json:"outputs"`
|
||||
Hyperparams []generatedHyperParamPayload `json:"hyperparams"`
|
||||
SkillDefinition string `json:"skillDefinition"`
|
||||
Implementation *GeneratedOperatorImplementation `json:"implementation,omitempty"`
|
||||
}
|
||||
|
||||
var payload generatedOperatorPayload
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
hyperparams := make([]domain.HyperParam, 0, len(payload.Hyperparams))
|
||||
for _, item := range payload.Hyperparams {
|
||||
hyperparams = append(hyperparams, domain.HyperParam{
|
||||
Key: item.Key,
|
||||
Value: stringifyGeneratedHyperParamValue(item.Value),
|
||||
Description: item.Description,
|
||||
Type: item.Type,
|
||||
})
|
||||
}
|
||||
*generated = GeneratedOperator{
|
||||
Name: payload.Name,
|
||||
Category: payload.Category,
|
||||
Description: payload.Description,
|
||||
Version: payload.Version,
|
||||
Image: payload.Image,
|
||||
ImageID: payload.ImageID,
|
||||
ContainerCommand: payload.ContainerCommand,
|
||||
ContainerArgs: payload.ContainerArgs,
|
||||
RuntimeContract: payload.RuntimeContract,
|
||||
Resources: payload.Resources,
|
||||
Inputs: payload.Inputs,
|
||||
Outputs: payload.Outputs,
|
||||
Hyperparams: hyperparams,
|
||||
SkillDefinition: payload.SkillDefinition,
|
||||
Implementation: payload.Implementation,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func stringifyGeneratedHyperParamValue(value any) string {
|
||||
switch typed := value.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return typed
|
||||
case bool:
|
||||
if typed {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
case float64:
|
||||
return strings.TrimSpace(fmt.Sprint(typed))
|
||||
default:
|
||||
payload, err := json.Marshal(typed)
|
||||
if err != nil {
|
||||
return strings.TrimSpace(fmt.Sprint(typed))
|
||||
}
|
||||
return string(payload)
|
||||
}
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
Status() Status
|
||||
GenerateWorkflow(context.Context, string, []domain.OperatorRecord) (*GeneratedWorkflow, error)
|
||||
|
||||
@ -124,7 +124,7 @@ func TestGenerateOperatorEnrichesRunnableExternalScaffold(t *testing.T) {
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"content": "{\"name\":\"视频目标检测裁剪\",\"category\":\"CV\",\"description\":\"检测视频目标并裁剪片段\",\"inputs\":[{\"name\":\"videos\",\"type\":\"video\"}],\"outputs\":[{\"name\":\"clips\",\"type\":\"video\"}],\"hyperparams\":[{\"key\":\"confidence threshold\",\"value\":\"0.35\",\"description\":\"检测阈值\"}],\"skillDefinition\":\"接收视频并输出裁剪片段\"}"
|
||||
"content": "{\"name\":\"视频目标检测裁剪\",\"category\":\"CV\",\"description\":\"检测视频目标并裁剪片段\",\"inputs\":[{\"name\":\"videos\",\"type\":\"video\"}],\"outputs\":[{\"name\":\"clips\",\"type\":\"video\"}],\"hyperparams\":[{\"key\":\"confidence threshold\",\"value\":0.35,\"description\":\"检测阈值\"}],\"skillDefinition\":\"接收视频并输出裁剪片段\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -151,6 +151,9 @@ func TestGenerateOperatorEnrichesRunnableExternalScaffold(t *testing.T) {
|
||||
if len(item.Hyperparams) != 1 || item.Hyperparams[0].Key != "CONFIDENCE_THRESHOLD" || item.Hyperparams[0].Type != "number" {
|
||||
t.Fatalf("expected normalized hyperparam, got %#v", item.Hyperparams)
|
||||
}
|
||||
if item.Hyperparams[0].Value != "0.35" {
|
||||
t.Fatalf("expected numeric hyperparam value to be stringified, got %#v", item.Hyperparams[0])
|
||||
}
|
||||
if item.RuntimeContract == nil || item.RuntimeContract["protocol"] != "droboticflow.external-operator/v1" {
|
||||
t.Fatalf("expected runtime contract, got %#v", item.RuntimeContract)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user