feat(agent): enhance suggested questions API with tag scopes
Update the SuggestedQuestionsRequest to include TagScopes for better filtering of knowledge items based on their parent knowledge bases. Modify related API documentation and frontend interfaces to accommodate the new structure. Ensure backend services handle the new tag scope logic correctly, including tests for the updated functionality.
This commit is contained in:
@@ -175,13 +175,16 @@ func (c *ChatManage) Clone() *ChatManage {
|
||||
copy(kidsCopy, t.KnowledgeIDs)
|
||||
tagIDsCopy := make([]string, len(t.TagIDs))
|
||||
copy(tagIDsCopy, t.TagIDs)
|
||||
scopeTagIDsCopy := make([]string, len(t.ScopeTagIDs))
|
||||
copy(scopeTagIDsCopy, t.ScopeTagIDs)
|
||||
searchTargets[i] = &SearchTarget{
|
||||
Type: t.Type,
|
||||
KnowledgeBaseID: t.KnowledgeBaseID,
|
||||
TenantID: t.TenantID,
|
||||
KnowledgeIDs: kidsCopy,
|
||||
TagIDs: tagIDsCopy,
|
||||
DisableDirectLoad: t.DisableDirectLoad,
|
||||
Type: t.Type,
|
||||
KnowledgeBaseID: t.KnowledgeBaseID,
|
||||
TenantID: t.TenantID,
|
||||
KnowledgeIDs: kidsCopy,
|
||||
TagIDs: tagIDsCopy,
|
||||
ScopeTagIDs: scopeTagIDsCopy,
|
||||
DisableRecallThresholds: t.DisableRecallThresholds,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ const (
|
||||
MatchTypeRelationChunk // 关系Chunk匹配类型
|
||||
MatchTypeGraph
|
||||
MatchTypeWebSearch // 网络搜索匹配类型
|
||||
MatchTypeDirectLoad // 直接加载匹配类型
|
||||
MatchTypeDirectLoad // Deprecated: reserved to preserve serialized enum values
|
||||
MatchTypeDataAnalysis // 数据分析匹配类型
|
||||
)
|
||||
|
||||
|
||||
@@ -99,9 +99,9 @@ type ChunkRepository interface {
|
||||
FAQChunkDiff(ctx context.Context, srcTenantID uint64, srcKBID string, dstTenantID uint64, dstKBID string) (chunksToAdd []string, chunksToDelete []string, err error)
|
||||
|
||||
// ListRecommendedFAQChunks lists FAQ chunks with the recommended flag set.
|
||||
// Filter by kbIDs and/or knowledgeIDs. At least one of them must be non-empty.
|
||||
// Filter by explicitly selected kbIDs, knowledgeIDs, and/or FAQ tagIDs.
|
||||
// Returns up to `limit` chunks sorted by updated_at descending.
|
||||
ListRecommendedFAQChunks(ctx context.Context, tenantID uint64, kbIDs []string, knowledgeIDs []string, limit int) ([]*types.Chunk, error)
|
||||
ListRecommendedFAQChunks(ctx context.Context, tenantID uint64, kbIDs []string, knowledgeIDs []string, tagIDs []string, limit int) ([]*types.Chunk, error)
|
||||
|
||||
// ListRecentDocumentChunksWithQuestions lists recent document chunks that have generated questions.
|
||||
// Filter by kbIDs and/or knowledgeIDs. At least one of them must be non-empty.
|
||||
|
||||
@@ -73,17 +73,17 @@ type CustomAgentService interface {
|
||||
// - agentID: Agent ID
|
||||
// - kbIDs: Optional knowledge base IDs to override agent config
|
||||
// - knowledgeIDs: Optional knowledge item IDs to further filter
|
||||
// - tagIDs: Optional knowledge tag IDs; resolved to knowledge item IDs (OR semantics)
|
||||
// - tagScopes: Optional KB-scoped knowledge tags (OR semantics within each KB)
|
||||
// - limit: Maximum number of questions to return
|
||||
// Returns:
|
||||
// - List of suggested questions
|
||||
// - Possible errors
|
||||
GetSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, tagIDs []string, limit int) ([]types.SuggestedQuestion, error)
|
||||
GetSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, tagScopes []types.TagScope, limit int) ([]types.SuggestedQuestion, error)
|
||||
|
||||
// GetKnowledgeSuggestedQuestions returns only knowledge-derived candidates.
|
||||
// It is independent of whether starter suggestions are enabled and is used
|
||||
// as a source/fallback for contextual follow-up generation.
|
||||
GetKnowledgeSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, tagIDs []string, limit int) ([]types.SuggestedQuestion, error)
|
||||
GetKnowledgeSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, tagScopes []types.TagScope, limit int) ([]types.SuggestedQuestion, error)
|
||||
}
|
||||
|
||||
// CustomAgentRepository defines the custom agent repository interface
|
||||
|
||||
@@ -268,6 +268,7 @@ type MessageExecutionContext struct {
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
|
||||
TagIDs []string `json:"tag_ids,omitempty"`
|
||||
TagScopes []TagScope `json:"tag_scopes,omitempty"`
|
||||
MCPServiceIDs []string `json:"mcp_service_ids,omitempty"`
|
||||
SkillNames []string `json:"skill_names,omitempty"`
|
||||
WebSearchEnabled bool `json:"web_search_enabled"`
|
||||
|
||||
@@ -36,16 +36,50 @@ type SearchTarget struct {
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
|
||||
// TagIDs limits retrieval to chunks/documents carrying any of these KB-local tags.
|
||||
TagIDs []string `json:"tag_ids,omitempty"`
|
||||
// DisableDirectLoad forces the target through retrieval even when it is
|
||||
// represented as specific knowledge IDs. Tag-derived document scopes need
|
||||
// this so tag filtering limits the candidate documents without loading every
|
||||
// matching document chunk as context.
|
||||
DisableDirectLoad bool `json:"disable_direct_load,omitempty"`
|
||||
// ScopeTagIDs records the logical tag scope selected by the user. For
|
||||
// document KBs this is kept for tracing after the relation-table lookup has
|
||||
// been resolved to KnowledgeIDs; TagIDs remains the physical index filter.
|
||||
ScopeTagIDs []string `json:"scope_tag_ids,omitempty"`
|
||||
// DisableRecallThresholds keeps recall broad inside an already constrained,
|
||||
// user-selected scope. The reranker still orders candidates, but vector and
|
||||
// keyword thresholds cannot erase the whole explicit scope before reranking.
|
||||
DisableRecallThresholds bool `json:"disable_recall_thresholds,omitempty"`
|
||||
}
|
||||
|
||||
// SearchTargets is a list of search targets, pre-computed at request entry point
|
||||
type SearchTargets []*SearchTarget
|
||||
|
||||
// RecallThresholds returns the effective recall thresholds for this target.
|
||||
func (st *SearchTarget) RecallThresholds(vectorThreshold, keywordThreshold float64) (float64, float64) {
|
||||
if st != nil && st.DisableRecallThresholds {
|
||||
return 0, 0
|
||||
}
|
||||
return vectorThreshold, keywordThreshold
|
||||
}
|
||||
|
||||
// HasRecallThresholdOverride reports whether any target represents an
|
||||
// authoritative scope whose candidates must reach reranking before filtering.
|
||||
func (st SearchTargets) HasRecallThresholdOverride() bool {
|
||||
for _, target := range st {
|
||||
if target != nil && target.DisableRecallThresholds {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HasKnowledgeRetrievalScope reports whether a request has any effective
|
||||
// knowledge retrieval scope. SearchTargets are the unified runtime form and
|
||||
// must be considered alongside the legacy/raw KB and knowledge ID fields so
|
||||
// tag-only mentions are not mistaken for pure chat.
|
||||
func HasKnowledgeRetrievalScope(
|
||||
searchTargets SearchTargets,
|
||||
knowledgeBaseIDs []string,
|
||||
knowledgeIDs []string,
|
||||
) bool {
|
||||
return len(searchTargets) > 0 || len(knowledgeBaseIDs) > 0 || len(knowledgeIDs) > 0
|
||||
}
|
||||
|
||||
// GetAllKnowledgeBaseIDs returns all unique knowledge base IDs from the search targets
|
||||
func (st SearchTargets) GetAllKnowledgeBaseIDs() []string {
|
||||
seen := make(map[string]bool)
|
||||
@@ -161,6 +195,7 @@ type SearchParams struct {
|
||||
DisableVectorMatch bool `json:"disable_vector_match"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids"`
|
||||
TagIDs []string `json:"tag_ids"` // Tag IDs for filtering (used for FAQ priority filtering)
|
||||
ScopeTagIDs []string `json:"scope_tag_ids,omitempty"`
|
||||
OnlyRecommended bool `json:"only_recommended"`
|
||||
// KnowledgeBaseIDs overrides the single KB ID passed to HybridSearch,
|
||||
// allowing a single retrieval call to span multiple KBs that share the
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package types
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHasKnowledgeRetrievalScope(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
searchTargets SearchTargets
|
||||
knowledgeBaseIDs []string
|
||||
knowledgeIDs []string
|
||||
want bool
|
||||
}{
|
||||
{name: "empty", want: false},
|
||||
{name: "knowledge base IDs", knowledgeBaseIDs: []string{"kb-1"}, want: true},
|
||||
{name: "knowledge IDs", knowledgeIDs: []string{"doc-1"}, want: true},
|
||||
{
|
||||
name: "tag-only search target",
|
||||
searchTargets: SearchTargets{
|
||||
{
|
||||
Type: SearchTargetTypeKnowledgeBase,
|
||||
KnowledgeBaseID: "kb-1",
|
||||
TagIDs: []string{"tag-1"},
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "resolved document tag target",
|
||||
searchTargets: SearchTargets{
|
||||
{
|
||||
Type: SearchTargetTypeKnowledge,
|
||||
KnowledgeBaseID: "kb-1",
|
||||
KnowledgeIDs: []string{"doc-1"},
|
||||
ScopeTagIDs: []string{"tag-1"},
|
||||
DisableRecallThresholds: true,
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := HasKnowledgeRetrievalScope(tt.searchTargets, tt.knowledgeBaseIDs, tt.knowledgeIDs)
|
||||
if got != tt.want {
|
||||
t.Fatalf("HasKnowledgeRetrievalScope() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchTargetRecallThresholds(t *testing.T) {
|
||||
normal := &SearchTarget{}
|
||||
vector, keyword := normal.RecallThresholds(0.5, 0.24)
|
||||
if vector != 0.5 || keyword != 0.24 {
|
||||
t.Fatalf("normal thresholds = (%v, %v), want (0.5, 0.24)", vector, keyword)
|
||||
}
|
||||
|
||||
explicit := &SearchTarget{DisableRecallThresholds: true}
|
||||
vector, keyword = explicit.RecallThresholds(0.5, 0.24)
|
||||
if vector != 0 || keyword != 0 {
|
||||
t.Fatalf("explicit thresholds = (%v, %v), want (0, 0)", vector, keyword)
|
||||
}
|
||||
if !(SearchTargets{explicit}).HasRecallThresholdOverride() {
|
||||
t.Fatal("expected explicit target to advertise recall threshold override")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatManageCloneCopiesSearchTargetScope(t *testing.T) {
|
||||
original := &ChatManage{
|
||||
PipelineRequest: PipelineRequest{
|
||||
SearchTargets: SearchTargets{
|
||||
{
|
||||
Type: SearchTargetTypeKnowledge,
|
||||
KnowledgeBaseID: "kb-1",
|
||||
KnowledgeIDs: []string{"doc-1"},
|
||||
ScopeTagIDs: []string{"tag-1"},
|
||||
DisableRecallThresholds: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cloned := original.Clone()
|
||||
if len(cloned.SearchTargets) != 1 {
|
||||
t.Fatalf("cloned search targets length = %d, want 1", len(cloned.SearchTargets))
|
||||
}
|
||||
got := cloned.SearchTargets[0]
|
||||
if !got.DisableRecallThresholds || len(got.ScopeTagIDs) != 1 || got.ScopeTagIDs[0] != "tag-1" {
|
||||
t.Fatalf("cloned target lost explicit scope: %#v", got)
|
||||
}
|
||||
|
||||
got.KnowledgeIDs[0] = "changed-doc"
|
||||
got.ScopeTagIDs[0] = "changed-tag"
|
||||
if original.SearchTargets[0].KnowledgeIDs[0] != "doc-1" || original.SearchTargets[0].ScopeTagIDs[0] != "tag-1" {
|
||||
t.Fatal("Clone() did not deep-copy search target scope slices")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user