59 lines
2.2 KiB
Go
59 lines
2.2 KiB
Go
package extraction
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Dify orchestrates the model steps. These rules also apply to its final output
|
||
|
|
// before the existing story writers can save or merge anything.
|
||
|
|
func WorkflowPrompts() (string, string) { return eventExtractionPrompt, dailyExtractionPrompt }
|
||
|
|
func WorkflowContent(in GenerationInput) string { return generationContent(in) }
|
||
|
|
|
||
|
|
func ValidateWorkflowResult(in GenerationInput, result GenerationResult, modelRef string) (GenerationResult, error) {
|
||
|
|
result.Summary = strings.TrimSpace(result.Summary)
|
||
|
|
result.DecisionReason = strings.TrimSpace(result.DecisionReason)
|
||
|
|
result.OpenTopic = strings.TrimSpace(result.OpenTopic)
|
||
|
|
result.Keywords = normalizedKeywords(result.Keywords)
|
||
|
|
if result.Stories == nil || result.DailyEvents == nil {
|
||
|
|
return GenerationResult{}, errors.New("workflow event arrays required")
|
||
|
|
}
|
||
|
|
if err := normalizeEvents(&result, in); err != nil {
|
||
|
|
return GenerationResult{}, err
|
||
|
|
}
|
||
|
|
normalizeDecisionContent(&result)
|
||
|
|
if !validGeneratedContent(result) {
|
||
|
|
return GenerationResult{}, errors.New("workflow content failed validation")
|
||
|
|
}
|
||
|
|
actual := make([]DailyEvent, 0, len(result.DailyEvents))
|
||
|
|
for _, d := range result.DailyEvents {
|
||
|
|
if d.TimeScope != "today_actual" || strings.TrimSpace(d.Evidence) == "" || !strings.Contains(in.Content, d.Evidence) {
|
||
|
|
return GenerationResult{}, errors.New("daily event lacks original evidence")
|
||
|
|
}
|
||
|
|
future := false
|
||
|
|
for _, phrase := range []string{"明天", "明早", "前几天", "打算", "计划", "尚未", "还没动手", "想着", "还想", "一会儿", "以后再"} {
|
||
|
|
if strings.Contains(d.Content, phrase) {
|
||
|
|
future = true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !future {
|
||
|
|
actual = append(actual, d)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result.DailyEvents = actual
|
||
|
|
result.ModelRef = modelRef
|
||
|
|
result.PromptVersion = "content-events-dify-v1"
|
||
|
|
result.AllowStory = result.Meaningful && result.StoryWorthy
|
||
|
|
return result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func ParseWorkflowResult(in GenerationInput, raw json.RawMessage, modelRef string) (GenerationResult, error) {
|
||
|
|
var result GenerationResult
|
||
|
|
if json.Unmarshal(raw, &result) != nil {
|
||
|
|
return result, errors.New("invalid workflow extraction JSON")
|
||
|
|
}
|
||
|
|
return ValidateWorkflowResult(in, result, modelRef)
|
||
|
|
}
|