@@ -41,7 +41,7 @@ func Load(ctx context.Context, dir, dataDir string, embedder Embedder, rag bool,
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("read knowledge directory %q: %w", dir, err)
|
||||
}
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(strings.ToLower(e.Name()), ".json") {
|
||||
@@ -70,14 +70,27 @@ func Load(ctx context.Context, dir, dataDir string, embedder Embedder, rag bool,
|
||||
s.docs = append(s.docs, d)
|
||||
}
|
||||
if rag && len(s.docs) > 0 {
|
||||
if s.embedder == nil {
|
||||
return nil, fmt.Errorf("RAG is enabled but no embedding provider is configured")
|
||||
}
|
||||
if err := s.index(ctx); err != nil {
|
||||
return s, err
|
||||
}
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
func (s *Store) Count() int { s.mu.RLock(); defer s.mu.RUnlock(); return len(s.docs) }
|
||||
func (s *Store) Count() int {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return len(s.docs)
|
||||
}
|
||||
func (s *Store) ByID(id string) (model.KnowledgeDoc, bool) {
|
||||
if s == nil {
|
||||
return model.KnowledgeDoc{}, false
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, d := range s.docs {
|
||||
@@ -88,6 +101,9 @@ func (s *Store) ByID(id string) (model.KnowledgeDoc, bool) {
|
||||
return model.KnowledgeDoc{}, false
|
||||
}
|
||||
func (s *Store) Search(ctx context.Context, text string, topK int) ([]model.KnowledgeHit, error) {
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("knowledge store is not initialized")
|
||||
}
|
||||
s.mu.RLock()
|
||||
docs := append([]model.KnowledgeDoc(nil), s.docs...)
|
||||
vecs := make(map[string][]float64, len(s.vectors))
|
||||
|
||||
Reference in New Issue
Block a user