diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 43ea1da..cd1458f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,6 +10,27 @@ on: jobs: + lint: + runs-on: ubuntu-latest + strategy: + matrix: + # We make use of the `slices` feature, only available in 1.21 and newer + go-version: [ '1.21', '1.22' ] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.56.2 + args: --verbose + build: runs-on: ubuntu-latest strategy: diff --git a/collection_test.go b/collection_test.go index c652655..6ec2738 100644 --- a/collection_test.go +++ b/collection_test.go @@ -604,7 +604,10 @@ func benchmarkCollection_Query(b *testing.B, n int, withContent bool) { // Let's say we embed 500 tokens, that's ~375 words, ~1875 characters doc.Content = randomString(r, 1875) } - c.AddDocument(ctx, doc) + + if err := c.AddDocument(ctx, doc); err != nil { + b.Fatal("expected nil, got", err) + } } b.ResetTimer() diff --git a/db_test.go b/db_test.go index 0ac7221..6f390cb 100644 --- a/db_test.go +++ b/db_test.go @@ -394,7 +394,9 @@ func TestDB_DeleteCollection(t *testing.T) { } // Delete collection - db.DeleteCollection(name) + if err := db.DeleteCollection(name); err != nil { + t.Fatal("expected no error, got", err) + } // Check expectations // We don't have access to the documents field, but we can rely on DB.ListCollections() @@ -426,7 +428,9 @@ func TestDB_Reset(t *testing.T) { } // Reset DB - db.Reset() + if err := db.Reset(); err != nil { + t.Fatal("expected no error, got", err) + } // Check expectations // We don't have access to the documents field, but we can rely on DB.ListCollections() diff --git a/persistence_test.go b/persistence_test.go index 4515d58..48bf692 100644 --- a/persistence_test.go +++ b/persistence_test.go @@ -28,7 +28,9 @@ func TestPersistenceWrite(t *testing.T) { t.Run("gob", func(t *testing.T) { tempFilePath := tempDir + ".gob" - persistToFile(tempFilePath, obj, false, "") + if err := persistToFile(tempFilePath, obj, false, ""); err != nil { + t.Fatal("expected nil, got", err) + } // Check if the file exists. _, err = os.Stat(tempFilePath) @@ -57,7 +59,9 @@ func TestPersistenceWrite(t *testing.T) { t.Run("gob gzipped", func(t *testing.T) { tempFilePath := tempDir + ".gob.gz" - persistToFile(tempFilePath, obj, true, "") + if err := persistToFile(tempFilePath, obj, true, ""); err != nil { + t.Fatal("expected nil, got", err) + } // Check if the file exists. _, err = os.Stat(tempFilePath) diff --git a/vector.go b/vector.go index 972b6b2..97b2fc1 100644 --- a/vector.go +++ b/vector.go @@ -2,35 +2,11 @@ package chromem import ( "errors" - "fmt" "math" ) const isNormalizedPrecisionTolerance = 1e-6 -// cosineSimilarity calculates the cosine similarity between two vectors. -// Vectors are normalized first. -// The resulting value represents the similarity, so a higher value means the -// vectors are more similar. -func cosineSimilarity(a, b []float32) (float32, error) { - // The vectors must have the same length - if len(a) != len(b) { - return 0, errors.New("vectors must have the same length") - } - - if !isNormalized(a) || !isNormalized(b) { - a, b = normalizeVector(a), normalizeVector(b) - } - dotProduct, err := dotProduct(a, b) - if err != nil { - return 0, fmt.Errorf("couldn't calculate dot product: %w", err) - } - - // Vectors are already normalized, so no need to divide by magnitudes - - return dotProduct, nil -} - // dotProduct calculates the dot product between two vectors. // It's the same as cosine similarity for normalized vectors. // The resulting value represents the similarity, so a higher value means the