Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all golangci-lint warnings #82

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
// 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