fix(chat): keep custom metadata on knowledge-chat references (#4087)
* fix(chat): keep custom metadata on knowledge-chat references Redis round-trips and graph or analysis result builders dropped knowledge custom metadata, so chat citations lost fields such as course and lesson URLs. * test(chat): wrap metadata fixtures to satisfy line length lint --------- Co-authored-by: wizardchen <wizardchen@tencent.com>main
parent
097bd475f6
commit
8125ed250f
|
|
@ -125,14 +125,15 @@ func (p *PluginDataAnalysis) OnEvent(
|
|||
// 5. Store result
|
||||
// Create a new SearchResult for the analysis output
|
||||
analysisResult := &types.SearchResult{
|
||||
ID: "analysis_" + knowledge.ID,
|
||||
Content: toolResult.Output,
|
||||
Score: 1.0,
|
||||
MatchType: types.MatchTypeDataAnalysis,
|
||||
KnowledgeID: knowledge.ID,
|
||||
KnowledgeTitle: knowledge.Title,
|
||||
KnowledgeFilename: knowledge.FileName,
|
||||
KnowledgeDescription: knowledge.Description,
|
||||
ID: "analysis_" + knowledge.ID,
|
||||
Content: toolResult.Output,
|
||||
Score: 1.0,
|
||||
MatchType: types.MatchTypeDataAnalysis,
|
||||
KnowledgeID: knowledge.ID,
|
||||
KnowledgeTitle: knowledge.Title,
|
||||
KnowledgeFilename: knowledge.FileName,
|
||||
KnowledgeDescription: knowledge.Description,
|
||||
KnowledgeCustomMetadata: knowledge.CustomMetadataText(),
|
||||
}
|
||||
|
||||
chatManage.MergeResult = append(chatManage.MergeResult, analysisResult)
|
||||
|
|
|
|||
|
|
@ -241,26 +241,28 @@ func filterSeenChunk(ctx context.Context, graph *types.GraphData, searchResult [
|
|||
// score for the missing retrieval score.
|
||||
func chunk2SearchResult(chunk *types.Chunk, knowledge *types.Knowledge) *types.SearchResult {
|
||||
return &types.SearchResult{
|
||||
ID: chunk.ID,
|
||||
Content: chunk.Content,
|
||||
ContentRevision: chunk.ContentRevision,
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
ChunkIndex: chunk.ChunkIndex,
|
||||
KnowledgeTitle: knowledge.Title,
|
||||
StartAt: chunk.StartAt,
|
||||
EndAt: chunk.EndAt,
|
||||
Seq: chunk.ChunkIndex,
|
||||
Score: 0,
|
||||
MatchType: types.MatchTypeGraph,
|
||||
Metadata: knowledge.GetMetadata(),
|
||||
ChunkType: string(chunk.ChunkType),
|
||||
ParentChunkID: chunk.ParentChunkID,
|
||||
ImageInfo: chunk.ImageInfo,
|
||||
KnowledgeFilename: knowledge.FileName,
|
||||
KnowledgeSource: knowledge.Source,
|
||||
KnowledgeChannel: knowledge.Channel,
|
||||
ChunkMetadata: chunk.Metadata,
|
||||
KnowledgeBaseID: knowledge.KnowledgeBaseID,
|
||||
SourceLocators: chunk.SourceLocators,
|
||||
ID: chunk.ID,
|
||||
Content: chunk.Content,
|
||||
ContentRevision: chunk.ContentRevision,
|
||||
KnowledgeID: chunk.KnowledgeID,
|
||||
ChunkIndex: chunk.ChunkIndex,
|
||||
KnowledgeTitle: knowledge.Title,
|
||||
StartAt: chunk.StartAt,
|
||||
EndAt: chunk.EndAt,
|
||||
Seq: chunk.ChunkIndex,
|
||||
Score: 0,
|
||||
MatchType: types.MatchTypeGraph,
|
||||
Metadata: knowledge.GetMetadata(),
|
||||
ChunkType: string(chunk.ChunkType),
|
||||
ParentChunkID: chunk.ParentChunkID,
|
||||
ImageInfo: chunk.ImageInfo,
|
||||
KnowledgeFilename: knowledge.FileName,
|
||||
KnowledgeSource: knowledge.Source,
|
||||
KnowledgeChannel: knowledge.Channel,
|
||||
KnowledgeDescription: knowledge.Description,
|
||||
KnowledgeCustomMetadata: knowledge.CustomMetadataText(),
|
||||
ChunkMetadata: chunk.Metadata,
|
||||
KnowledgeBaseID: knowledge.KnowledgeBaseID,
|
||||
SourceLocators: chunk.SourceLocators,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
package chatpipeline
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestChunk2SearchResultIncludesCustomMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
knowledge := &types.Knowledge{
|
||||
Title: "Course",
|
||||
Description: "desc",
|
||||
FileName: "lesson.md",
|
||||
Source: "api",
|
||||
Channel: "web",
|
||||
KnowledgeBaseID: "kb-1",
|
||||
CustomMetadata: types.JSON(`{
|
||||
"lesson_url":"https://example.com/lesson",
|
||||
"course_url":"https://example.com/course"
|
||||
}`),
|
||||
}
|
||||
chunk := &types.Chunk{
|
||||
ID: "chunk-1",
|
||||
KnowledgeID: "knowledge-1",
|
||||
Content: "body",
|
||||
ChunkType: types.ChunkTypeText,
|
||||
}
|
||||
|
||||
got := chunk2SearchResult(chunk, knowledge)
|
||||
|
||||
require.Equal(t,
|
||||
"course_url: https://example.com/course\nlesson_url: https://example.com/lesson",
|
||||
got.KnowledgeCustomMetadata,
|
||||
)
|
||||
require.Equal(t, "desc", got.KnowledgeDescription)
|
||||
require.Equal(t, "web", got.KnowledgeChannel)
|
||||
require.Equal(t, 0.0, got.Score)
|
||||
}
|
||||
|
|
@ -463,22 +463,25 @@ func getFloat64(m map[string]interface{}, key string) float64 {
|
|||
// through JSON/Redis serialization, preserving all fields including metadata.
|
||||
func searchResultFromMap(refMap map[string]interface{}) *types.SearchResult {
|
||||
sr := &types.SearchResult{
|
||||
ID: getString(refMap, "id"),
|
||||
Content: getString(refMap, "content"),
|
||||
KnowledgeID: getString(refMap, "knowledge_id"),
|
||||
ChunkIndex: int(getFloat64(refMap, "chunk_index")),
|
||||
KnowledgeTitle: getString(refMap, "knowledge_title"),
|
||||
StartAt: int(getFloat64(refMap, "start_at")),
|
||||
EndAt: int(getFloat64(refMap, "end_at")),
|
||||
Seq: int(getFloat64(refMap, "seq")),
|
||||
Score: getFloat64(refMap, "score"),
|
||||
ChunkType: getString(refMap, "chunk_type"),
|
||||
ParentChunkID: getString(refMap, "parent_chunk_id"),
|
||||
ImageInfo: getString(refMap, "image_info"),
|
||||
KnowledgeFilename: getString(refMap, "knowledge_filename"),
|
||||
KnowledgeSource: getString(refMap, "knowledge_source"),
|
||||
KnowledgeDescription: getString(refMap, "knowledge_description"),
|
||||
KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
|
||||
ID: getString(refMap, "id"),
|
||||
Content: getString(refMap, "content"),
|
||||
KnowledgeID: getString(refMap, "knowledge_id"),
|
||||
ChunkIndex: int(getFloat64(refMap, "chunk_index")),
|
||||
KnowledgeTitle: getString(refMap, "knowledge_title"),
|
||||
StartAt: int(getFloat64(refMap, "start_at")),
|
||||
EndAt: int(getFloat64(refMap, "end_at")),
|
||||
Seq: int(getFloat64(refMap, "seq")),
|
||||
Score: getFloat64(refMap, "score"),
|
||||
ChunkType: getString(refMap, "chunk_type"),
|
||||
ParentChunkID: getString(refMap, "parent_chunk_id"),
|
||||
ImageInfo: getString(refMap, "image_info"),
|
||||
KnowledgeFilename: getString(refMap, "knowledge_filename"),
|
||||
KnowledgeSource: getString(refMap, "knowledge_source"),
|
||||
KnowledgeChannel: getString(refMap, "knowledge_channel"),
|
||||
KnowledgeDescription: getString(refMap, "knowledge_description"),
|
||||
KnowledgeCustomMetadata: getString(refMap, "knowledge_custom_metadata"),
|
||||
MatchedContent: getString(refMap, "matched_content"),
|
||||
KnowledgeBaseID: getString(refMap, "knowledge_base_id"),
|
||||
}
|
||||
if meta, ok := refMap["metadata"].(map[string]interface{}); ok {
|
||||
metadata := make(map[string]string)
|
||||
|
|
|
|||
|
|
@ -62,23 +62,26 @@ func TestValidateUnscopedTagIDs(t *testing.T) {
|
|||
// survive.
|
||||
func TestSearchResultFromMap_RoundTrip(t *testing.T) {
|
||||
original := &types.SearchResult{
|
||||
ID: "chunk-1",
|
||||
Content: "first part\nsecond part",
|
||||
KnowledgeID: "knowledge-1",
|
||||
ChunkIndex: 3,
|
||||
KnowledgeTitle: "title",
|
||||
StartAt: 10,
|
||||
EndAt: 20,
|
||||
Seq: 2,
|
||||
Score: 4.5,
|
||||
ChunkType: "text",
|
||||
ParentChunkID: "parent-1",
|
||||
ImageInfo: `[{"url":"cdn.example.com"}]`,
|
||||
KnowledgeFilename: "doc.txt",
|
||||
KnowledgeSource: "upload",
|
||||
KnowledgeDescription: "desc",
|
||||
KnowledgeBaseID: "kb-1",
|
||||
Metadata: map[string]string{"page": "3"},
|
||||
ID: "chunk-1",
|
||||
Content: "first part\nsecond part",
|
||||
KnowledgeID: "knowledge-1",
|
||||
ChunkIndex: 3,
|
||||
KnowledgeTitle: "title",
|
||||
StartAt: 10,
|
||||
EndAt: 20,
|
||||
Seq: 2,
|
||||
Score: 4.5,
|
||||
ChunkType: "text",
|
||||
ParentChunkID: "parent-1",
|
||||
ImageInfo: `[{"url":"cdn.example.com"}]`,
|
||||
KnowledgeFilename: "doc.txt",
|
||||
KnowledgeSource: "upload",
|
||||
KnowledgeChannel: "api",
|
||||
KnowledgeDescription: "desc",
|
||||
KnowledgeCustomMetadata: "course_url: https://example.com/course\nlesson_url: https://example.com/lesson",
|
||||
MatchedContent: "matched question",
|
||||
KnowledgeBaseID: "kb-1",
|
||||
Metadata: map[string]string{"page": "3"},
|
||||
MatchedImages: []types.MatchedImage{
|
||||
{ChunkID: "image-1", KnowledgeBaseID: "kb-1", URL: "resource://image"},
|
||||
},
|
||||
|
|
@ -110,6 +113,9 @@ func TestSearchResultFromMap_RoundTrip(t *testing.T) {
|
|||
assert.Equal(t, original.KnowledgeFilename, got.KnowledgeFilename)
|
||||
assert.Equal(t, original.KnowledgeSource, got.KnowledgeSource)
|
||||
assert.Equal(t, original.KnowledgeDescription, got.KnowledgeDescription)
|
||||
assert.Equal(t, original.KnowledgeChannel, got.KnowledgeChannel)
|
||||
assert.Equal(t, original.KnowledgeCustomMetadata, got.KnowledgeCustomMetadata)
|
||||
assert.Equal(t, original.MatchedContent, got.MatchedContent)
|
||||
assert.Equal(t, original.KnowledgeBaseID, got.KnowledgeBaseID)
|
||||
assert.Equal(t, original.Metadata, got.Metadata)
|
||||
assert.Equal(t, original.SourceLocators, got.SourceLocators)
|
||||
|
|
|
|||
Loading…
Reference in New Issue