feat: load question expansion prompts from yaml
Deployment Config CI / Deployment Config (push) Successful in 27s
Backend CI / Backend (push) Successful in 16m5s

This commit is contained in:
2026-05-13 20:24:51 +08:00
parent 8890cd1ca4
commit 34ef5873ca
9 changed files with 190 additions and 49 deletions
@@ -236,7 +236,9 @@ func (s *QuestionExpansionService) GenerateByAIDistill(ctx context.Context, bran
return nil, err
}
cacheKey := questionDistillCacheKey(actor.TenantID, brandID, questionDistillPromptVersion, brandCtx.BrandName, brandCtx.CompetitorNames, seedTopic)
prompt := buildQuestionDistillPrompt(brandCtx.BrandName, brandCtx.CompetitorNames, seedTopic)
promptVersion := questionPromptHash(prompt)
cacheKey := questionDistillCacheKey(actor.TenantID, brandID, promptVersion, brandCtx.BrandName, brandCtx.CompetitorNames, seedTopic)
if s.cache != nil {
if raw, cacheErr := s.cache.Get(ctx, cacheKey); cacheErr == nil && len(raw) > 0 {
var cached []QuestionCandidate
@@ -266,14 +268,13 @@ func (s *QuestionExpansionService) GenerateByAIDistill(ctx context.Context, bran
Metadata: map[string]any{
"brand_id": brandID,
"seed_topic": seedTopic,
"prompt_version": questionDistillPromptVersion,
"prompt_version": promptVersion,
},
})
if err != nil {
return nil, err
}
prompt := buildQuestionDistillPrompt(brandCtx.BrandName, brandCtx.CompetitorNames, seedTopic)
result, err := s.llm.Generate(ctx, llm.GenerateRequest{
Prompt: prompt,
Timeout: questionDistillTimeout,
@@ -346,8 +347,10 @@ func (s *QuestionExpansionService) FillQuestionCombination(ctx context.Context,
regionTerms = questionCombinationRegionTermsFromIPRegion(s.ipGeo.Lookup(clientIP))
}
fallback := buildQuestionCombinationFillFallback(brandCtx, regionTerms, req)
prompt := buildQuestionCombinationFillPrompt(brandCtx, fallback.Region, req)
promptVersion := questionPromptHash(prompt)
cacheKey := questionCombinationFillCacheKey(actor.TenantID, brandID, brandCtx, regionTerms, req)
cacheKey := questionCombinationFillCacheKey(actor.TenantID, brandID, promptVersion, brandCtx, regionTerms, req)
if s.cache != nil {
if raw, cacheErr := s.cache.Get(ctx, cacheKey); cacheErr == nil && len(raw) > 0 {
var cached QuestionCombinationFillResult
@@ -381,7 +384,7 @@ func (s *QuestionExpansionService) FillQuestionCombination(ctx context.Context,
FixedPoints: 1,
Metadata: map[string]any{
"brand_id": brandID,
"prompt_version": "question_combination_fill_v1",
"prompt_version": promptVersion,
},
})
if err != nil {
@@ -389,7 +392,7 @@ func (s *QuestionExpansionService) FillQuestionCombination(ctx context.Context,
}
result, err := s.llm.Generate(ctx, llm.GenerateRequest{
Prompt: buildQuestionCombinationFillPrompt(brandCtx, fallback.Region, req),
Prompt: prompt,
Timeout: questionCombinationFillTimeout,
MaxOutputTokens: 900,
ResponseFormat: &llm.ResponseFormat{
@@ -1051,10 +1054,11 @@ func inferQuestionCombinationCategory(brandName, description string) string {
}
}
func questionCombinationFillCacheKey(tenantID, brandID int64, brandCtx *questionBrandContext, regionTerms []string, req QuestionCombinationFillRequest) string {
func questionCombinationFillCacheKey(tenantID, brandID int64, promptVersion string, brandCtx *questionBrandContext, regionTerms []string, req QuestionCombinationFillRequest) string {
raw, _ := json.Marshal(struct {
TenantID int64 `json:"tenant_id"`
BrandID int64 `json:"brand_id"`
PromptVersion string `json:"prompt_version"`
SchemaVersion string `json:"schema_version"`
BrandName string `json:"brand_name"`
Description string `json:"description"`
@@ -1064,6 +1068,7 @@ func questionCombinationFillCacheKey(tenantID, brandID int64, brandCtx *question
}{
TenantID: tenantID,
BrandID: brandID,
PromptVersion: promptVersion,
SchemaVersion: "question_combination_fill_v1",
BrandName: strings.TrimSpace(brandCtx.BrandName),
Description: strings.TrimSpace(nilToString(brandCtx.Description)),
@@ -1081,6 +1086,11 @@ func questionCombinationFillCacheKey(tenantID, brandID int64, brandCtx *question
return "question_combination_fill:" + hex.EncodeToString(sum[:])
}
func questionPromptHash(prompt string) string {
sum := sha1.Sum([]byte(strings.TrimSpace(prompt)))
return hex.EncodeToString(sum[:])
}
func nilToString(value *string) string {
if value == nil {
return ""