Skip to content

Commit

Permalink
Feat Add index insert data callback (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis authored Nov 2, 2023
1 parent 0e8ac72 commit e8af1a4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
7 changes: 5 additions & 2 deletions examples/embeddings/jsondb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import (
func main() {

index := index.New(
jsondb.New("db.json"),
jsondb.New().WithPersist("db.json"),
openaiembedder.New(openaiembedder.AdaEmbeddingV2),
).WithIncludeContents(true)
).WithIncludeContents(true).WithAddDataCallback(func(data *index.Data) error {
data.Metadata["contentLen"] = len(data.Metadata["content"].(string))
return nil
})

indexIsEmpty, _ := index.IsEmpty(context.Background())

Expand Down
2 changes: 1 addition & 1 deletion examples/embeddings/knowledge_base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
func main() {

index := index.New(
jsondb.New("db.json"),
jsondb.New().WithPersist("db.json"),
openaiembedder.New(openaiembedder.AdaEmbeddingV2),
).WithIncludeContents(true)

Expand Down
2 changes: 1 addition & 1 deletion examples/embeddings/simplekb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func main() {
docs, _ := loader.NewPDFToTextLoader("./kb").WithTextSplitter(textsplitter.NewRecursiveCharacterTextSplitter(2000, 200)).Load(context.Background())
index := index.New(jsondb.New("db.json"), openaiembedder.New(openaiembedder.AdaEmbeddingV2)).WithIncludeContents(true)
index := index.New(jsondb.New(), openaiembedder.New(openaiembedder.AdaEmbeddingV2)).WithIncludeContents(true)
index.LoadFromDocuments(context.Background(), docs)
qapipeline.New(openai.NewChat().WithVerbose(true)).WithIndex(index).Query(context.Background(), "What is the NATO purpose?", option.WithTopK(1))
}
2 changes: 1 addition & 1 deletion examples/llm/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

embedder := openaiembedder.New(openaiembedder.AdaEmbeddingV2)
index := index.New(
jsondb.New("db.json"),
jsondb.New().WithPersist("db.json"),
embedder,
)
llm := openai.NewCompletion().WithCompletionCache(cache.New(embedder, index).WithTopK(3))
Expand Down
28 changes: 28 additions & 0 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
defaultIncludeContent = true
)

type AddDataCallback func(data *Data) error

type Data struct {
ID string
Values []float64
Expand All @@ -45,6 +47,7 @@ type Index struct {
embedder Embedder
batchInsertSize int
includeContent bool
addDataCallback AddDataCallback
}

func New(vectorDB VectorDB, embedder Embedder) *Index {
Expand All @@ -53,6 +56,7 @@ func New(vectorDB VectorDB, embedder Embedder) *Index {
embedder: embedder,
batchInsertSize: defaultBatchInsertSize,
includeContent: defaultIncludeContent,
addDataCallback: nil,
}
}

Expand All @@ -66,6 +70,13 @@ func (i *Index) WithBatchInsertSize(batchInsertSize int) *Index {
return i
}

// WithAddDataCallback allows to modify the data before it is added to the index.
// This can be useful to add additional metadata to the vector.
func (i *Index) WithAddDataCallback(callback AddDataCallback) *Index {
i.addDataCallback = callback
return i
}

func (i *Index) LoadFromDocuments(ctx context.Context, documents []document.Document) error {
err := i.batchUpsert(ctx, documents)
if err != nil {
Expand All @@ -78,6 +89,14 @@ func (i *Index) Add(ctx context.Context, data *Data) error {
if data == nil {
return nil
}

if i.addDataCallback != nil {
err := i.addDataCallback(data)
if err != nil {
return fmt.Errorf("%w: %w", ErrInternal, err)
}
}

return i.vectorDB.Insert(ctx, []Data{*data})
}

Expand Down Expand Up @@ -126,6 +145,15 @@ func (i *Index) batchUpsert(ctx context.Context, documents []document.Document)
return err
}

if i.addDataCallback != nil {
for j := range data {
callbackErr := i.addDataCallback(&data[j])
if callbackErr != nil {
return fmt.Errorf("%w: %w", ErrInternal, callbackErr)
}
}
}

err = i.vectorDB.Insert(ctx, data)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions index/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Option func(*Options)

type Options struct {
TopK int
Filter interface{}
Filter any
}

func WithTopK(topK int) Option {
Expand All @@ -13,7 +13,7 @@ func WithTopK(topK int) Option {
}
}

func WithFilter(filter interface{}) Option {
func WithFilter(filter any) Option {
return func(opts *Options) {
opts.Filter = filter
}
Expand Down
23 changes: 19 additions & 4 deletions index/vectordb/jsondb/jsondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,34 @@ type data struct {
Values []float64 `json:"values"`
}

// DB is a simple in-memory vector database
// that stores the data in a json file only
// if the persist option is enabled.
type DB struct {
data []data
dbPath string
}

type FilterFn func([]index.SearchResult) []index.SearchResult

func New(dbPath string) *DB {
func New() *DB {
index := &DB{
data: []data{},
dbPath: dbPath,
data: []data{},
}

return index
}

func (i DB) save() error {
func (i *DB) WithPersist(dbPath string) *DB {
i.dbPath = dbPath
return i
}

func (i *DB) save() error {
if i.dbPath == "" {
return nil
}

jsonContent, err := json.Marshal(i.data)
if err != nil {
return err
Expand All @@ -48,6 +59,10 @@ func (i DB) save() error {
}

func (i *DB) load() error {
if i.dbPath == "" {
return nil
}

if len(i.data) > 0 {
return nil
}
Expand Down

0 comments on commit e8af1a4

Please sign in to comment.