Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
change: no error on empty collection, so that the LLM session can 're…
Browse files Browse the repository at this point in the history
…cover' from the tool call
  • Loading branch information
iwilltry42 committed Jun 18, 2024
1 parent a612f1b commit 5808864
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
vserr "github.com/gptscript-ai/knowledge/pkg/vectorstore/errors"
"log/slog"

"github.com/gptscript-ai/knowledge/pkg/datastore"
Expand Down Expand Up @@ -73,6 +75,11 @@ func (s *ClientRetrieve) Run(cmd *cobra.Command, args []string) error {

sources, err := c.Retrieve(cmd.Context(), datasetID, query, retrieveOpts)
if err != nil {
// An empty collection is not a hard error - the LLM session can "recover" from it
if errors.Is(err, vserr.ErrCollectionEmpty) {
fmt.Printf("Dataset %q does not contain any documents\n", datasetID)
return nil
}
return err
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/vectorstore/chromem/chromem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chromem

import (
"context"
"fmt"
"github.com/gptscript-ai/knowledge/pkg/vectorstore/errors"
"log/slog"
"maps"
"runtime"
Expand Down Expand Up @@ -55,7 +57,7 @@ func (s *Store) AddDocuments(ctx context.Context, docs []vs.Document, collection

col := s.db.GetCollection(collection, s.embeddingFunc)
if col == nil {
return nil, vs.ErrCollectionNotFound{Collection: collection}
return nil, fmt.Errorf("%w: %q", errors.ErrCollectionNotFound, collection)
}

err := col.AddDocuments(ctx, chromemDocs, runtime.NumCPU()/2)
Expand Down Expand Up @@ -102,7 +104,11 @@ func convertStringMapToAnyMap(m map[string]string) map[string]any {
func (s *Store) SimilaritySearch(ctx context.Context, query string, numDocuments int, collection string) ([]vs.Document, error) {
col := s.db.GetCollection(collection, s.embeddingFunc)
if col == nil {
return nil, vs.ErrCollectionNotFound{Collection: collection}
return nil, fmt.Errorf("%w: %q", errors.ErrCollectionNotFound, collection)
}

if col.Count() == 0 {
return nil, fmt.Errorf("%w: %q", errors.ErrCollectionEmpty, collection)
}

if numDocuments > col.Count() {
Expand Down Expand Up @@ -139,7 +145,7 @@ func (s *Store) RemoveCollection(_ context.Context, collection string) error {
func (s *Store) RemoveDocument(ctx context.Context, documentID string, collection string) error {
col := s.db.GetCollection(collection, s.embeddingFunc)
if col == nil {
return vs.ErrCollectionNotFound{Collection: collection}
return fmt.Errorf("%w: %q", errors.ErrCollectionNotFound, collection)
}
return col.Delete(ctx, nil, nil, documentID)
}
9 changes: 0 additions & 9 deletions pkg/vectorstore/errors.go

This file was deleted.

10 changes: 10 additions & 0 deletions pkg/vectorstore/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package errors

import (
"errors"
)

var (
ErrCollectionNotFound = errors.New("collection not found")
ErrCollectionEmpty = errors.New("collection is empty")
)

0 comments on commit 5808864

Please sign in to comment.