feat: Enhance Kol Generation Service with web search and knowledge group support
- Added `EnableWebSearch` and `KnowledgeGroupIDs` fields to `KolGenerationSubmitRequest`. - Updated `Submit` method in `KolGenerationService` to handle new request fields. - Integrated web search and knowledge group validation based on card configuration. - Introduced caching mechanisms in `KolPackageService`, `KolPromptService`, and `KolSubscriptionAdminService` to improve performance. - Implemented knowledge context resolution in `KolGenerationWorker` to enrich prompts with relevant knowledge snippets. - Added utility functions for handling card configuration in both backend and frontend. - Created tests for knowledge extraction and rendering to ensure accuracy and reliability.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractKnowledgePreciseFactsKeepsAddressAndContactLines(t *testing.T) {
|
||||
facts := extractKnowledgePreciseFacts(`
|
||||
# 合肥门店
|
||||
|
||||
地址:合肥市蜀山区望江西路 123 号 A 座 8 层
|
||||
电话:0551-12345678
|
||||
官网:https://example.com/store
|
||||
普通介绍段落
|
||||
`)
|
||||
|
||||
for _, expected := range []string{
|
||||
"地址:合肥市蜀山区望江西路 123 号 A 座 8 层",
|
||||
"电话:0551-12345678",
|
||||
"官网:https://example.com/store",
|
||||
} {
|
||||
if !containsExactString(facts, expected) {
|
||||
t.Fatalf("extractKnowledgePreciseFacts() = %#v, want %q", facts, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderPromptSectionIncludesPreciseFactsAndSourceURI(t *testing.T) {
|
||||
sourceURI := "https://example.com/store"
|
||||
output := (&KnowledgeService{}).RenderPromptSection(&KnowledgeContext{
|
||||
Snippets: []KnowledgeSnippet{
|
||||
{
|
||||
GroupName: "门店信息",
|
||||
ItemName: "合肥门店",
|
||||
SourceURI: &sourceURI,
|
||||
Text: "这里是一段门店介绍。",
|
||||
PreciseFacts: []string{"地址:合肥市蜀山区望江西路 123 号", "电话:0551-12345678"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
for _, expected := range []string{
|
||||
"以下为必须严格按原文保留的高精度事实",
|
||||
"- 门店信息 / 合肥门店:地址:合肥市蜀山区望江西路 123 号",
|
||||
"来源链接: https://example.com/store",
|
||||
"这里是一段门店介绍。",
|
||||
} {
|
||||
if !strings.Contains(output, expected) {
|
||||
t.Fatalf("RenderPromptSection() = %q, want %q", output, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderPromptSectionSupportsPreciseFactsWithoutSnippets(t *testing.T) {
|
||||
output := (&KnowledgeService{}).RenderPromptSection(&KnowledgeContext{
|
||||
PreciseFacts: []string{
|
||||
"公司资料 / 合肥门店:地址:合肥市蜀山区望江西路 123 号",
|
||||
"公司资料 / 合肥门店:主营业务:全屋定制;橱柜定制",
|
||||
},
|
||||
})
|
||||
|
||||
for _, expected := range []string{
|
||||
"以下为必须严格按原文保留的高精度事实",
|
||||
"- 公司资料 / 合肥门店:地址:合肥市蜀山区望江西路 123 号",
|
||||
"- 公司资料 / 合肥门店:主营业务:全屋定制;橱柜定制",
|
||||
} {
|
||||
if !strings.Contains(output, expected) {
|
||||
t.Fatalf("RenderPromptSection() = %q, want %q", output, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractKnowledgePreciseFactsKeepsBusinessScopeSection(t *testing.T) {
|
||||
facts := extractKnowledgePreciseFacts(`
|
||||
# 公司介绍
|
||||
|
||||
主营业务
|
||||
- 全屋定制
|
||||
- 橱柜定制
|
||||
- 木门与护墙系统
|
||||
|
||||
地址:合肥市蜀山区望江西路 123 号
|
||||
`)
|
||||
|
||||
for _, expected := range []string{
|
||||
"主营业务:全屋定制;橱柜定制;木门与护墙系统",
|
||||
"地址:合肥市蜀山区望江西路 123 号",
|
||||
} {
|
||||
if !containsExactString(facts, expected) {
|
||||
t.Fatalf("extractKnowledgePreciseFacts() = %#v, want %q", facts, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func containsExactString(items []string, target string) bool {
|
||||
for _, item := range items {
|
||||
if item == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user