Expand semantic decomposition checks #352
@@ -48,6 +48,20 @@ func TestHouseholdMarketplaceBrandSearchUsesResearchRoute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHouseholdSemanticMemoryControlsMarketplaceAlias(t *testing.T) {
|
||||||
|
withoutMemory := interactiondomain.DecomposeTask("найди на WB бренд marka-wear")
|
||||||
|
if withoutMemory.Source != "" || withoutMemory.Object != "" {
|
||||||
|
t.Fatalf("core invented marketplace meaning without memory: %+v", withoutMemory)
|
||||||
|
}
|
||||||
|
withMemory := interactiondomain.DecomposeTaskWithLexicon("поищи марку marka-wear в Wildberries", interactiondomain.TaskLexicon{
|
||||||
|
ObjectAliases: []interactiondomain.TaskLexiconEntry{{Alias: "марку", Value: "BRAND"}},
|
||||||
|
SourceAliases: []interactiondomain.TaskLexiconEntry{{Alias: "wildberries", Value: "WILDBERRIES"}},
|
||||||
|
})
|
||||||
|
if withMemory.Intent != interactiondomain.TaskIntentSearch || withMemory.Object != "BRAND" || withMemory.Source != "WILDBERRIES" || withMemory.Target != "marka-wear" || !withMemory.NeedsResearch {
|
||||||
|
t.Fatalf("semantic memory did not control marketplace decomposition: %+v", withMemory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHouseholdBearDrawingUsesCreativeLocalRoute(t *testing.T) {
|
func TestHouseholdBearDrawingUsesCreativeLocalRoute(t *testing.T) {
|
||||||
candidate := interactiondomain.BuildDeterministicCandidate("нарисуй бурого медведя", "", time.Unix(10, 0).UnixMilli())
|
candidate := interactiondomain.BuildDeterministicCandidate("нарисуй бурого медведя", "", time.Unix(10, 0).UnixMilli())
|
||||||
decomposition := interactiondomain.DecomposeTaskWithLexicon("нарисуй бурого медведя", interactiondomain.TaskLexicon{ObjectAliases: []interactiondomain.TaskLexiconEntry{{Alias: "медвед", Value: "IMAGE"}}})
|
decomposition := interactiondomain.DecomposeTaskWithLexicon("нарисуй бурого медведя", interactiondomain.TaskLexicon{ObjectAliases: []interactiondomain.TaskLexiconEntry{{Alias: "медвед", Value: "IMAGE"}}})
|
||||||
|
|||||||
+30
-20
@@ -510,6 +510,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var effectiveTaskMessage = interactiondomain.StripAssistantVocative(utterance.Content)
|
var effectiveTaskMessage = interactiondomain.StripAssistantVocative(utterance.Content)
|
||||||
|
creativeUserRequest := interactiondomain.IsCreativeRequest(utterance.Content)
|
||||||
continuationMessage := ""
|
continuationMessage := ""
|
||||||
travelContinuation := false
|
travelContinuation := false
|
||||||
var previousTask *interaction.HumanUtterance
|
var previousTask *interaction.HumanUtterance
|
||||||
@@ -531,13 +532,15 @@ func main() {
|
|||||||
log.Printf("interaction principal history loaded: %d utterances", len(recentUserHistory))
|
log.Printf("interaction principal history loaded: %d utterances", len(recentUserHistory))
|
||||||
}
|
}
|
||||||
conversationHistory = append(conversationHistory, recentUserHistory...)
|
conversationHistory = append(conversationHistory, recentUserHistory...)
|
||||||
if continuation := interactiondomain.ContinueLocalSearch(effectiveTaskMessage, conversationHistory); continuation != "" {
|
if !creativeUserRequest {
|
||||||
continuationMessage = continuation
|
if continuation := interactiondomain.ContinueLocalSearch(effectiveTaskMessage, conversationHistory); continuation != "" {
|
||||||
effectiveTaskMessage = continuation
|
continuationMessage = continuation
|
||||||
} else if continuation := interactiondomain.ContinueTravelOrigin(effectiveTaskMessage, conversationHistory); continuation != "" {
|
effectiveTaskMessage = continuation
|
||||||
continuationMessage = continuation
|
} else if continuation := interactiondomain.ContinueTravelOrigin(effectiveTaskMessage, conversationHistory); continuation != "" {
|
||||||
travelContinuation = true
|
continuationMessage = continuation
|
||||||
effectiveTaskMessage = continuation
|
travelContinuation = true
|
||||||
|
effectiveTaskMessage = continuation
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if request.ConversationID != "" && interactiondomain.IsRetryRequest(utterance.Content) {
|
if request.ConversationID != "" && interactiondomain.IsRetryRequest(utterance.Content) {
|
||||||
if len(conversationHistory) > 0 {
|
if len(conversationHistory) > 0 {
|
||||||
@@ -567,12 +570,12 @@ func main() {
|
|||||||
semantic = continuedSemantic
|
semantic = continuedSemantic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if travelContinuation && interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory) == "" {
|
if !creativeUserRequest && travelContinuation && interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory) == "" {
|
||||||
semantic.CapabilityHints = append(semantic.CapabilityHints, string(capability.CapabilityKindWebResearch))
|
semantic.CapabilityHints = append(semantic.CapabilityHints, string(capability.CapabilityKindWebResearch))
|
||||||
semantic.NeedsCognition = false
|
semantic.NeedsCognition = false
|
||||||
semantic.Interpretation.NeedsCognition = false
|
semantic.Interpretation.NeedsCognition = false
|
||||||
}
|
}
|
||||||
creativeRequest := interactiondomain.IsCreativeRequest(utterance.Content) || interactiondomain.IsCreativeRequest(effectiveTaskMessage)
|
creativeRequest := creativeUserRequest || interactiondomain.IsCreativeRequest(effectiveTaskMessage)
|
||||||
creativeReply := interactiondomain.CreativeDeterministicReply(utterance.Content)
|
creativeReply := interactiondomain.CreativeDeterministicReply(utterance.Content)
|
||||||
if creativeReply == "" {
|
if creativeReply == "" {
|
||||||
creativeReply = interactiondomain.CreativeDeterministicReply(effectiveTaskMessage)
|
creativeReply = interactiondomain.CreativeDeterministicReply(effectiveTaskMessage)
|
||||||
@@ -652,13 +655,15 @@ func main() {
|
|||||||
cognitiveAvailable = false
|
cognitiveAvailable = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if travelReply := interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory); travelReply != "" {
|
if !creativeRequest {
|
||||||
semantic.Interpretation.Clarification = interaction.ClarificationRequired
|
if travelReply := interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory); travelReply != "" {
|
||||||
semantic.Interpretation.Applicability = interaction.SemanticCandidateClarify
|
semantic.Interpretation.Clarification = interaction.ClarificationRequired
|
||||||
semantic.Reply = travelReply
|
semantic.Interpretation.Applicability = interaction.SemanticCandidateClarify
|
||||||
deterministicReply = travelReply
|
semantic.Reply = travelReply
|
||||||
semantic.NeedsCognition = false
|
deterministicReply = travelReply
|
||||||
semantic.Interpretation.NeedsCognition = false
|
semantic.NeedsCognition = false
|
||||||
|
semantic.Interpretation.NeedsCognition = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := interactionRepository.AppendUtterance(r.Context(), conversation, utterance, intent, relations); err != nil {
|
if err := interactionRepository.AppendUtterance(r.Context(), conversation, utterance, intent, relations); err != nil {
|
||||||
http.Error(w, "save chat message", http.StatusInternalServerError)
|
http.Error(w, "save chat message", http.StatusInternalServerError)
|
||||||
@@ -794,9 +799,11 @@ func main() {
|
|||||||
planningReply = helpPath.Reply
|
planningReply = helpPath.Reply
|
||||||
needsCognition = false
|
needsCognition = false
|
||||||
}
|
}
|
||||||
if travelReply := interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory); travelReply != "" {
|
if !creativeRequest {
|
||||||
planningReply = travelReply
|
if travelReply := interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory); travelReply != "" {
|
||||||
needsCognition = false
|
planningReply = travelReply
|
||||||
|
needsCognition = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if planningReply == "" && needsCognition && cognitiveAttempted && !cognitiveAvailable {
|
if planningReply == "" && needsCognition && cognitiveAttempted && !cognitiveAvailable {
|
||||||
planningReply = humanCognitiveDegradedReply(utterance.Content)
|
planningReply = humanCognitiveDegradedReply(utterance.Content)
|
||||||
@@ -907,7 +914,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
if identityReply == "" && creativeReply == "" && creativeFallbackReply == "" {
|
if identityReply == "" && creativeReply == "" && creativeFallbackReply == "" {
|
||||||
stableAct := interactiondomain.ClassifySpeechAct(utterance.Content, utterance.CorrectsUtteranceID)
|
stableAct := interactiondomain.ClassifySpeechAct(utterance.Content, utterance.CorrectsUtteranceID)
|
||||||
travelClarification := interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory)
|
travelClarification := ""
|
||||||
|
if !creativeRequest {
|
||||||
|
travelClarification = interactiondomain.TravelSearchClarification(effectiveTaskMessage, conversationHistory)
|
||||||
|
}
|
||||||
if stableReply, stable := interactiondomain.StableConversationalReply(utterance.Content, stableAct); stable && travelClarification == "" {
|
if stableReply, stable := interactiondomain.StableConversationalReply(utterance.Content, stableAct); stable && travelClarification == "" {
|
||||||
planningReply = stableReply
|
planningReply = stableReply
|
||||||
needsCognition = false
|
needsCognition = false
|
||||||
|
|||||||
@@ -181,6 +181,26 @@ func TestDecomposeMarketplaceBrandRequestIntoExecutionAtoms(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecomposeDoesNotInventMutableAliases(t *testing.T) {
|
||||||
|
got := DecomposeTask("найди на WB бренд marka-wear")
|
||||||
|
if got.Source != "" || got.Object != "" || got.NeedsResearch {
|
||||||
|
t.Fatalf("parser invented a mutable alias without memory: %+v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecomposeUsesRememberedAliasesAcrossWordings(t *testing.T) {
|
||||||
|
lexicon := TaskLexicon{
|
||||||
|
ObjectAliases: []TaskLexiconEntry{{Alias: "бренд", Value: "BRAND"}, {Alias: "марку", Value: "BRAND"}},
|
||||||
|
SourceAliases: []TaskLexiconEntry{{Alias: "wb", Value: "WILDBERRIES"}, {Alias: "wildberries", Value: "WILDBERRIES"}},
|
||||||
|
}
|
||||||
|
for _, message := range []string{"найди на WB бренд marka-wear", "поищи марку marka-wear в Wildberries"} {
|
||||||
|
got := DecomposeTaskWithLexicon(message, lexicon)
|
||||||
|
if got.Intent != TaskIntentSearch || got.Object != "BRAND" || got.Source != "WILDBERRIES" || got.Target != "marka-wear" || !got.NeedsResearch {
|
||||||
|
t.Fatalf("remembered aliases failed for %q: %+v", message, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecomposeCreativeRequestDoesNotInventResearch(t *testing.T) {
|
func TestDecomposeCreativeRequestDoesNotInventResearch(t *testing.T) {
|
||||||
lexicon := TaskLexicon{ObjectAliases: []TaskLexiconEntry{{Alias: "медвед", Value: "IMAGE"}}}
|
lexicon := TaskLexicon{ObjectAliases: []TaskLexiconEntry{{Alias: "медвед", Value: "IMAGE"}}}
|
||||||
got := DecomposeTaskWithLexicon("нарисуй бурого медведя", lexicon)
|
got := DecomposeTaskWithLexicon("нарисуй бурого медведя", lexicon)
|
||||||
|
|||||||
@@ -53,13 +53,13 @@ func DecomposeTaskWithLexicon(message string, lexicon TaskLexicon) TaskDecomposi
|
|||||||
result.Intent = TaskIntentCreate
|
result.Intent = TaskIntentCreate
|
||||||
}
|
}
|
||||||
for _, entry := range lexicon.ObjectAliases {
|
for _, entry := range lexicon.ObjectAliases {
|
||||||
if containsStemInTokens(tokens, entry.Alias) {
|
if lexiconAliasMatches(tokens, entry.Alias) {
|
||||||
result.Object = entry.Value
|
result.Object = entry.Value
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, entry := range lexicon.SourceAliases {
|
for _, entry := range lexicon.SourceAliases {
|
||||||
if containsStemInTokens(tokens, entry.Alias) {
|
if lexiconAliasMatches(tokens, entry.Alias) {
|
||||||
result.Source = entry.Value
|
result.Source = entry.Value
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ func DecomposeTaskWithLexicon(message string, lexicon TaskLexicon) TaskDecomposi
|
|||||||
}
|
}
|
||||||
if result.Target == "" && result.Object != "" {
|
if result.Target == "" && result.Object != "" {
|
||||||
for _, entry := range lexicon.ObjectAliases {
|
for _, entry := range lexicon.ObjectAliases {
|
||||||
if entry.Value == result.Object {
|
if entry.Value == result.Object && lexiconAliasMatches(tokens, entry.Alias) {
|
||||||
result.Target = valueAfterCue(lower, entry.Alias)
|
result.Target = valueAfterCue(lower, entry.Alias)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -87,6 +87,14 @@ func DecomposeTaskWithLexicon(message string, lexicon TaskLexicon) TaskDecomposi
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lexiconAliasMatches(tokens []string, alias string) bool {
|
||||||
|
alias = strings.TrimSpace(strings.ToLower(alias))
|
||||||
|
if len([]rune(alias)) < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return containsStemInTokens(tokens, alias)
|
||||||
|
}
|
||||||
|
|
||||||
func valueAfterAnyCue(message string, cues ...string) string {
|
func valueAfterAnyCue(message string, cues ...string) string {
|
||||||
for _, cue := range cues {
|
for _, cue := range cues {
|
||||||
if value := valueAfterCue(message, cue); value != "" {
|
if value := valueAfterCue(message, cue); value != "" {
|
||||||
@@ -103,8 +111,14 @@ func valueAfterCue(message, cue string) string {
|
|||||||
}
|
}
|
||||||
value := strings.TrimSpace(message[index+len(cue):])
|
value := strings.TrimSpace(message[index+len(cue):])
|
||||||
value = strings.Trim(value, " \t:,-")
|
value = strings.Trim(value, " \t:,-")
|
||||||
if end := strings.IndexAny(value, "?!,.;"); end >= 0 {
|
end := len(value)
|
||||||
value = value[:end]
|
if punctuation := strings.IndexAny(value, "?!,.;"); punctuation >= 0 && punctuation < end {
|
||||||
|
end = punctuation
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(value)
|
for _, boundary := range []string{" в ", " на ", " из ", " для ", " около "} {
|
||||||
|
if index := strings.Index(value, boundary); index >= 0 && index < end {
|
||||||
|
end = index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(value[:end])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user