Skip to content

Commit

Permalink
feat: sqlite-vec support whereDocument query
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Feb 18, 2025
1 parent 42b4326 commit 8ebb151
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
15 changes: 0 additions & 15 deletions knowledge/pkg/vectorstore/pgvector/pgvector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"log/slog"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -541,21 +540,7 @@ func (v VectorStore) ExportCollectionsToFile(ctx context.Context, path string, c
return fmt.Errorf("function ExportCollectionsToFile not implemented for vectorstore pgvector")
}

func validateWhereDocument(whereDocument []cg.WhereDocument) error {
for _, wd := range whereDocument {
if slices.Contains([]cg.WhereDocumentOperator{cg.WhereDocumentOperatorAnd, cg.WhereDocumentOperatorOr}, wd.Operator) {
return fmt.Errorf("pgvector does not support whereDocument operator %s", wd.Operator)
}
}
return nil
}

func buildWhereClause(args []any, where map[string]string, whereDocument []cg.WhereDocument) (string, []any, error) {

if err := validateWhereDocument(whereDocument); err != nil {
return "", nil, err
}

if len(where)+len(whereDocument) == 0 {
return "TRUE", args, nil
}
Expand Down
15 changes: 12 additions & 3 deletions knowledge/pkg/vectorstore/sqlite-vec/sqlite-vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

sqlitevec "github.com/asg017/sqlite-vec-go-bindings/ncruces"
dbtypes "github.com/gptscript-ai/knowledge/pkg/index/types"
"github.com/gptscript-ai/knowledge/pkg/vectorstore/helper"
vs "github.com/gptscript-ai/knowledge/pkg/vectorstore/types"
cg "github.com/philippgille/chromem-go"
"gorm.io/gorm"
Expand Down Expand Up @@ -314,15 +315,15 @@ func (v *VectorStore) RemoveDocument(ctx context.Context, documentID string, col
return nil
}

func (v *VectorStore) GetDocuments(ctx context.Context, collection string, where map[string]string, whereDocument []cg.WhereDocument) ([]vs.Document, error) {
func (v *VectorStore) GetDocuments(_ context.Context, collection string, where map[string]string, whereDocument []cg.WhereDocument) ([]vs.Document, error) {
if len(whereDocument) > 0 {
return nil, fmt.Errorf("sqlite-vec does not support whereDocument")
}

var docs []vs.Document

// Build metadata filter query
whereQueries := []string{}
var whereQueries []string
args := []interface{}{collection}

for k, v := range where {
Expand All @@ -333,6 +334,14 @@ func (v *VectorStore) GetDocuments(ctx context.Context, collection string, where
args = append(args, v)
}

if len(whereDocument) > 0 {
wc, err := helper.BuildWhereDocumentClause(whereDocument, "AND")
if err != nil {
return nil, fmt.Errorf("failed to build whereDocument clause: %w", err)
}
whereQueries = append(whereQueries, wc)
}

whereQuery := strings.Join(whereQueries, " AND ")
if len(whereQuery) > 0 {
whereQuery = " AND " + whereQuery
Expand All @@ -341,7 +350,7 @@ func (v *VectorStore) GetDocuments(ctx context.Context, collection string, where
query := fmt.Sprintf(`
SELECT id, content, metadata
FROM [%s]
WHERE collection_id = ?%s
WHERE collection_id = ?%s;
`, v.embeddingsTableName, whereQuery)

rows, err := v.db.Raw(query, args...).Rows()
Expand Down

0 comments on commit 8ebb151

Please sign in to comment.