Skip to content

Commit

Permalink
Fix all golangci-lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed May 26, 2024
1 parent 0e92288 commit f642808
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 29 deletions.
5 changes: 4 additions & 1 deletion collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(err)
}
}

b.ResetTimer()
Expand Down
4 changes: 2 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func TestDB_DeleteCollection(t *testing.T) {
}

// Delete collection
db.DeleteCollection(name)
db.DeleteCollection(name) // nolint: errcheck

// Check expectations
// We don't have access to the documents field, but we can rely on DB.ListCollections()
Expand Down Expand Up @@ -426,7 +426,7 @@ func TestDB_Reset(t *testing.T) {
}

// Reset DB
db.Reset()
db.Reset() // nolint: errcheck

// Check expectations
// We don't have access to the documents field, but we can rely on DB.ListCollections()
Expand Down
8 changes: 6 additions & 2 deletions persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 0 additions & 24 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f642808

Please sign in to comment.