feat: implement preview of user level memory graph

Signed-off-by: joeyczheng <joeyczheng@tencent.com>
This commit is contained in:
joeyczheng
2026-02-12 16:02:09 +08:00
committed by lyingbug
parent 6adecdb30e
commit 12baa885d2
18 changed files with 624 additions and 18 deletions
+6
View File
@@ -4,8 +4,10 @@ package types
// including query processing, search parameters, and model configurations
type ChatManage struct {
SessionID string `json:"session_id"` // Unique identifier for the chat session
UserID string `json:"user_id"` // Unique identifier for the user
Query string `json:"query,omitempty"` // Original user query
RewriteQuery string `json:"rewrite_query,omitempty"` // Query after rewriting for better retrieval
EnableMemory bool `json:"enable_memory"` // Whether memory feature is enabled
History []*History `json:"history,omitempty"` // Chat history for context
KnowledgeBaseIDs []string `json:"knowledge_base_ids"` // IDs of knowledge bases to search (multi-KB support)
@@ -147,6 +149,8 @@ const (
CHAT_COMPLETION_STREAM EventType = "chat_completion_stream" // Stream chat completion
STREAM_FILTER EventType = "stream_filter" // Filter streaming output
FILTER_TOP_K EventType = "filter_top_k" // Keep only top K results
MEMORY_RETRIEVAL EventType = "memory_retrieval" // Retrieve memory context
MEMORY_STORAGE EventType = "memory_storage" // Store conversation to memory
)
// Pipline defines the sequence of events for different chat modes
@@ -160,8 +164,10 @@ var Pipline = map[string][]EventType{
},
"chat_history_stream": { // Streaming chat with conversation history
LOAD_HISTORY,
MEMORY_RETRIEVAL,
CHAT_COMPLETION_STREAM,
STREAM_FILTER,
MEMORY_STORAGE,
},
"rag": { // Retrieval Augmented Generation
CHUNK_SEARCH,
+13 -13
View File
@@ -8,24 +8,24 @@ import "context"
type Entity struct {
ID string // Unique identifier for the entity
ChunkIDs []string // References to document chunks where this entity appears
Frequency int `json:"-"` // Number of occurrences in the corpus
Degree int `json:"-"` // Number of connections to other entities
Title string `json:"title"` // Display name of the entity
Type string `json:"type"` // Classification of the entity (e.g., person, concept, organization)
Description string `json:"description"` // Brief explanation or context about the entity
Frequency int `json:"-"` // Number of occurrences in the corpus
Degree int `json:"-"` // Number of connections to other entities
Title string `json:"title" jsonschema:"display name of the entity"` // Display name of the entity
Type string `json:"type" jsonschema:"type of the entity"` // Classification of the entity (e.g., person, concept, organization)
Description string `json:"description" jsonschema:"brief explanation or context about the entity"` // Brief explanation or context about the entity
}
// Relationship represents a connection between two entities in the knowledge graph.
// It captures the semantic connection between entities identified in the document chunks.
type Relationship struct {
ID string `json:"-"` // Unique identifier for the relationship
ChunkIDs []string `json:"-"` // References to document chunks where this relationship is established
CombinedDegree int `json:"-"` // Sum of degrees of the connected entities, used for ranking
Weight float64 `json:"-"` // Strength of the relationship based on textual evidence
Source string `json:"source"` // ID of the entity where the relationship starts
Target string `json:"target"` // ID of the entity where the relationship ends
Description string `json:"description"` // Description of how these entities are related
Strength int `json:"strength"` // Normalized measure of relationship importance (1-10)
ID string `json:"-"` // Unique identifier for the relationship
ChunkIDs []string `json:"-"` // References to document chunks where this relationship is established
CombinedDegree int `json:"-"` // Sum of degrees of the connected entities, used for ranking
Weight float64 `json:"-"` // Strength of the relationship based on textual evidence
Source string `json:"source" jsonschema:"ID of the entity where the relationship starts"` // ID of the entity where the relationship starts
Target string `json:"target" jsonschema:"ID of the entity where the relationship ends"` // ID of the entity where the relationship ends
Description string `json:"description" jsonschema:"description of how these entities are related"` // Description of how these entities are related
Strength int `json:"strength" jsonschema:"normalized measure of relationship importance (1-10)"` // Normalized measure of relationship importance (1-10)
}
// GraphBuilder defines the interface for building and querying the knowledge graph.
+28
View File
@@ -0,0 +1,28 @@
package interfaces
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
// MemoryService defines the interface for the memory system
type MemoryService interface {
// AddEpisode processes a conversation session and adds it as an episode to the memory graph
AddEpisode(ctx context.Context, userID string, sessionID string, messages []types.Message) error
// RetrieveMemory retrieves relevant memory context based on the current query and user
RetrieveMemory(ctx context.Context, userID string, query string) (*types.MemoryContext, error)
}
// MemoryRepository defines the interface for storing and retrieving memory data
type MemoryRepository interface {
// SaveEpisode saves an episode and its associated entities and relationships to the graph
SaveEpisode(ctx context.Context, episode *types.Episode, entities []*types.Entity, relations []*types.Relationship) error
// FindRelatedEpisodes finds episodes related to the given keywords for a specific user
FindRelatedEpisodes(ctx context.Context, userID string, keywords []string, limit int) ([]*types.Episode, error)
// IsAvailable checks if the memory repository is available
IsAvailable(ctx context.Context) bool
}
+2 -1
View File
@@ -36,11 +36,12 @@ type SessionService interface {
// summaryModelID: optional summary model ID override (if empty, uses session/KB default)
// webSearchEnabled: whether to enable web search to supplement knowledge base results
// customAgent: optional custom agent for config override (multiTurnEnabled, historyTurns)
// enableMemory: whether to enable memory feature for this request
// Events are emitted through eventBus (references, answer chunks, completion)
KnowledgeQA(ctx context.Context,
session *types.Session, query string, knowledgeBaseIDs []string, knowledgeIDs []string,
assistantMessageID string, summaryModelID string, webSearchEnabled bool, eventBus *event.EventBus,
customAgent *types.CustomAgent,
customAgent *types.CustomAgent, enableMemory bool,
) error
// KnowledgeQAByEvent performs knowledge-based question answering by event
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
+19
View File
@@ -0,0 +1,19 @@
package types
import "time"
// Episode represents a conversation episode or a distinct interaction event
type Episode struct {
ID string `json:"id"`
UserID string `json:"user_id"`
SessionID string `json:"session_id"`
Summary string `json:"summary"`
CreatedAt time.Time `json:"created_at"`
}
// MemoryContext represents the retrieved memory context for a conversation
type MemoryContext struct {
RelatedEpisodes []Episode `json:"related_episodes"`
RelatedEntities []Entity `json:"related_entities"`
RelatedRelations []Relationship `json:"related_relations"`
}