Skip to content

Commit

Permalink
Merge pull request #82 from erikdubbelboer/fix-lint
Browse files Browse the repository at this point in the history
Fix all golangci-lint warnings
  • Loading branch information
philippgille authored Jun 2, 2024
2 parents 0e92288 + de8e41c commit 34ba879
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
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("expected nil, got", err)
}
}

b.ResetTimer()
Expand Down
8 changes: 6 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
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 34ba879

Please sign in to comment.