feat: 新增FAQ条目答案策略字段,支持'all'和'random'两种模式
This commit is contained in:
@@ -22,6 +22,7 @@ type FAQEntry struct {
|
|||||||
SimilarQuestions []string `json:"similar_questions"`
|
SimilarQuestions []string `json:"similar_questions"`
|
||||||
NegativeQuestions []string `json:"negative_questions"`
|
NegativeQuestions []string `json:"negative_questions"`
|
||||||
Answers []string `json:"answers"`
|
Answers []string `json:"answers"`
|
||||||
|
AnswerStrategy string `json:"answer_strategy"`
|
||||||
IndexMode string `json:"index_mode"`
|
IndexMode string `json:"index_mode"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
@@ -36,6 +37,7 @@ type FAQEntryPayload struct {
|
|||||||
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||||
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||||
Answers []string `json:"answers"`
|
Answers []string `json:"answers"`
|
||||||
|
AnswerStrategy *string `json:"answer_strategy,omitempty"`
|
||||||
TagID string `json:"tag_id,omitempty"`
|
TagID string `json:"tag_id,omitempty"`
|
||||||
TagName string `json:"tag_name,omitempty"`
|
TagName string `json:"tag_name,omitempty"`
|
||||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||||
|
|||||||
@@ -3990,6 +3990,11 @@ func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.Knowled
|
|||||||
if meta == nil {
|
if meta == nil {
|
||||||
meta = &types.FAQChunkMetadata{StandardQuestion: chunk.Content}
|
meta = &types.FAQChunkMetadata{StandardQuestion: chunk.Content}
|
||||||
}
|
}
|
||||||
|
// 默认使用 all 策略
|
||||||
|
answerStrategy := meta.AnswerStrategy
|
||||||
|
if answerStrategy == "" {
|
||||||
|
answerStrategy = types.AnswerStrategyAll
|
||||||
|
}
|
||||||
entry := &types.FAQEntry{
|
entry := &types.FAQEntry{
|
||||||
ID: chunk.ID,
|
ID: chunk.ID,
|
||||||
ChunkID: chunk.ID,
|
ChunkID: chunk.ID,
|
||||||
@@ -4002,6 +4007,7 @@ func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.Knowled
|
|||||||
SimilarQuestions: meta.SimilarQuestions,
|
SimilarQuestions: meta.SimilarQuestions,
|
||||||
NegativeQuestions: meta.NegativeQuestions,
|
NegativeQuestions: meta.NegativeQuestions,
|
||||||
Answers: meta.Answers,
|
Answers: meta.Answers,
|
||||||
|
AnswerStrategy: answerStrategy,
|
||||||
IndexMode: kb.FAQConfig.IndexMode,
|
IndexMode: kb.FAQConfig.IndexMode,
|
||||||
UpdatedAt: chunk.UpdatedAt,
|
UpdatedAt: chunk.UpdatedAt,
|
||||||
CreatedAt: chunk.CreatedAt,
|
CreatedAt: chunk.CreatedAt,
|
||||||
@@ -4110,11 +4116,22 @@ func (s *knowledgeService) resolveTagID(ctx context.Context, kbID string, payloa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeFAQEntryPayload(payload *types.FAQEntryPayload) (*types.FAQChunkMetadata, error) {
|
func sanitizeFAQEntryPayload(payload *types.FAQEntryPayload) (*types.FAQChunkMetadata, error) {
|
||||||
|
// 处理 AnswerStrategy,默认为 all
|
||||||
|
answerStrategy := types.AnswerStrategyAll
|
||||||
|
if payload.AnswerStrategy != nil && *payload.AnswerStrategy != "" {
|
||||||
|
switch *payload.AnswerStrategy {
|
||||||
|
case types.AnswerStrategyAll, types.AnswerStrategyRandom:
|
||||||
|
answerStrategy = *payload.AnswerStrategy
|
||||||
|
default:
|
||||||
|
return nil, werrors.NewBadRequestError("answer_strategy 必须是 'all' 或 'random'")
|
||||||
|
}
|
||||||
|
}
|
||||||
meta := &types.FAQChunkMetadata{
|
meta := &types.FAQChunkMetadata{
|
||||||
StandardQuestion: strings.TrimSpace(payload.StandardQuestion),
|
StandardQuestion: strings.TrimSpace(payload.StandardQuestion),
|
||||||
SimilarQuestions: payload.SimilarQuestions,
|
SimilarQuestions: payload.SimilarQuestions,
|
||||||
NegativeQuestions: payload.NegativeQuestions,
|
NegativeQuestions: payload.NegativeQuestions,
|
||||||
Answers: payload.Answers,
|
Answers: payload.Answers,
|
||||||
|
AnswerStrategy: answerStrategy,
|
||||||
Version: 1,
|
Version: 1,
|
||||||
Source: "faq",
|
Source: "faq",
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-31
@@ -11,12 +11,13 @@ import (
|
|||||||
|
|
||||||
// FAQChunkMetadata 定义 FAQ 条目在 Chunk.Metadata 中的结构
|
// FAQChunkMetadata 定义 FAQ 条目在 Chunk.Metadata 中的结构
|
||||||
type FAQChunkMetadata struct {
|
type FAQChunkMetadata struct {
|
||||||
StandardQuestion string `json:"standard_question"`
|
StandardQuestion string `json:"standard_question"`
|
||||||
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||||
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||||
Answers []string `json:"answers,omitempty"`
|
Answers []string `json:"answers,omitempty"`
|
||||||
Version int `json:"version,omitempty"`
|
AnswerStrategy AnswerStrategy `json:"answer_strategy,omitempty"`
|
||||||
Source string `json:"source,omitempty"`
|
Version int `json:"version,omitempty"`
|
||||||
|
Source string `json:"source,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GeneratedQuestion 表示AI生成的单个问题
|
// GeneratedQuestion 表示AI生成的单个问题
|
||||||
@@ -162,37 +163,49 @@ func CalculateFAQContentHash(meta *FAQChunkMetadata) string {
|
|||||||
return hex.EncodeToString(hash[:])
|
return hex.EncodeToString(hash[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AnswerStrategy 定义答案返回策略
|
||||||
|
type AnswerStrategy string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// AnswerStrategyAll 返回所有答案
|
||||||
|
AnswerStrategyAll AnswerStrategy = "all"
|
||||||
|
// AnswerStrategyRandom 随机返回一个答案
|
||||||
|
AnswerStrategyRandom AnswerStrategy = "random"
|
||||||
|
)
|
||||||
|
|
||||||
// FAQEntry 表示返回给前端的 FAQ 条目
|
// FAQEntry 表示返回给前端的 FAQ 条目
|
||||||
type FAQEntry struct {
|
type FAQEntry struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
ChunkID string `json:"chunk_id"`
|
ChunkID string `json:"chunk_id"`
|
||||||
KnowledgeID string `json:"knowledge_id"`
|
KnowledgeID string `json:"knowledge_id"`
|
||||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||||
TagID string `json:"tag_id"`
|
TagID string `json:"tag_id"`
|
||||||
IsEnabled bool `json:"is_enabled"`
|
IsEnabled bool `json:"is_enabled"`
|
||||||
IsRecommended bool `json:"is_recommended"`
|
IsRecommended bool `json:"is_recommended"`
|
||||||
StandardQuestion string `json:"standard_question"`
|
StandardQuestion string `json:"standard_question"`
|
||||||
SimilarQuestions []string `json:"similar_questions"`
|
SimilarQuestions []string `json:"similar_questions"`
|
||||||
NegativeQuestions []string `json:"negative_questions"`
|
NegativeQuestions []string `json:"negative_questions"`
|
||||||
Answers []string `json:"answers"`
|
Answers []string `json:"answers"`
|
||||||
IndexMode FAQIndexMode `json:"index_mode"`
|
AnswerStrategy AnswerStrategy `json:"answer_strategy"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
IndexMode FAQIndexMode `json:"index_mode"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
Score float64 `json:"score,omitempty"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
MatchType MatchType `json:"match_type,omitempty"`
|
Score float64 `json:"score,omitempty"`
|
||||||
ChunkType ChunkType `json:"chunk_type"`
|
MatchType MatchType `json:"match_type,omitempty"`
|
||||||
|
ChunkType ChunkType `json:"chunk_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
|
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
|
||||||
type FAQEntryPayload struct {
|
type FAQEntryPayload struct {
|
||||||
StandardQuestion string `json:"standard_question" binding:"required"`
|
StandardQuestion string `json:"standard_question" binding:"required"`
|
||||||
SimilarQuestions []string `json:"similar_questions"`
|
SimilarQuestions []string `json:"similar_questions"`
|
||||||
NegativeQuestions []string `json:"negative_questions"`
|
NegativeQuestions []string `json:"negative_questions"`
|
||||||
Answers []string `json:"answers" binding:"required"`
|
Answers []string `json:"answers" binding:"required"`
|
||||||
TagID string `json:"tag_id"`
|
AnswerStrategy *AnswerStrategy `json:"answer_strategy,omitempty"`
|
||||||
TagName string `json:"tag_name"`
|
TagID string `json:"tag_id"`
|
||||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
TagName string `json:"tag_name"`
|
||||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||||
|
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
Reference in New Issue
Block a user