Skip to content

Commit bf1667f

Browse files
authored
lint: fixup lint settings (#1018)
1 parent 66d7710 commit bf1667f

File tree

17 files changed

+78
-169
lines changed

17 files changed

+78
-169
lines changed

.github/workflows/ci.yaml

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,49 @@
1-
name: ci
2-
1+
# GitHub Actions CI workflow for Go project
2+
name: CI
33
on:
44
push:
55
branches:
66
- main
77
pull_request:
88
branches:
99
- main
10-
1110
jobs:
1211
lint:
12+
name: Lint
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v4
1616
- uses: actions/setup-go@v5
1717
with:
18-
go-version: '1.22'
19-
# Cache is managed by golangci-lint
20-
# https://github.com/actions/setup-go#caching-dependency-files-and-build-outputs
21-
cache: false
22-
- name: golangci-lint
23-
uses: golangci/[email protected]
18+
go-version-file: go.mod
19+
- name: Run golangci-lint
20+
uses: golangci/golangci-lint-action@v6
2421
with:
25-
args: --timeout=4m
26-
version: v1.59.1
22+
version: v1.61
23+
args: --timeout=5m
2724
build-examples:
25+
name: Build Examples
2826
runs-on: ubuntu-latest
2927
steps:
30-
- uses: actions/setup-go@v4
28+
- uses: actions/checkout@v4
29+
- uses: actions/setup-go@v5
3130
with:
3231
go-version: stable
33-
cache: false
34-
- uses: actions/checkout@v3
3532
- name: Build examples
3633
run: make build-examples
3734
build-test:
35+
name: Build and Test
3836
runs-on: ubuntu-latest
3937
steps:
40-
- uses: actions/setup-go@v4
38+
- uses: actions/checkout@v4
39+
- uses: actions/setup-go@v5
4140
with:
4241
go-version: stable
43-
- uses: actions/checkout@v3
4442
- name: Build
4543
run: go build -v ./...
4644
- name: Test
4745
env:
4846
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
4947
GENAI_API_KEY: ${{ secrets.GENAI_API_KEY }}
5048
run: go test -v ./...
49+

.golangci.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ linters:
1313
- style
1414
- test
1515
- unused
16+
enable:
17+
- forbidigo
1618
disable:
19+
- gci # We don't use gci.
1720
- godox # We allow TODO lines.
1821
- tagliatelle # As we're dealing with third parties we must accept snake case.
1922
- wsl # We don't agree with wsl's style rules
@@ -54,6 +57,12 @@ linters-settings:
5457
ignore-file-rules:
5558
- "**/*_test.go"
5659
- "**/mock/**/*.go"
60+
forbidigo:
61+
forbid:
62+
- 'import "[^"]+/(util|common|helpers)"'
63+
gosec:
64+
excludes:
65+
- G115 # https://github.com/securego/gosec/issues/1212
5766
run:
5867
exclude-dirs:
5968
- 'exp'

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ lint-all:
2626
lint-deps:
2727
@command -v golangci-lint >/dev/null 2>&1 || { \
2828
echo >&2 "golangci-lint not found. Installing..."; \
29-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.1; \
29+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0; \
3030
}
3131

3232
.PHONY: docs

chains/sequential.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"fmt"
77
"strings"
88

9-
"github.com/tmc/langchaingo/internal/util"
9+
"github.com/tmc/langchaingo/internal/maputil"
10+
"github.com/tmc/langchaingo/internal/setutil"
1011
"github.com/tmc/langchaingo/memory"
1112
"github.com/tmc/langchaingo/schema"
1213
)
@@ -42,11 +43,11 @@ func NewSequentialChain(chains []Chain, inputKeys []string, outputKeys []string,
4243
}
4344

4445
func (c *SequentialChain) validateSeqChain() error {
45-
knownKeys := util.ToSet(c.inputKeys)
46+
knownKeys := setutil.ToSet(c.inputKeys)
4647

4748
// Make sure memory keys don't collide with input keys
4849
memoryKeys := c.memory.MemoryVariables(context.Background())
49-
overlappingKeys := util.Intersection(memoryKeys, knownKeys)
50+
overlappingKeys := setutil.Intersection(memoryKeys, knownKeys)
5051
if len(overlappingKeys) > 0 {
5152
return fmt.Errorf(
5253
"%w: input keys [%v] also exist in the memory keys: [%v] - please use input keys and memory keys that don't overlap",
@@ -61,16 +62,16 @@ func (c *SequentialChain) validateSeqChain() error {
6162

6263
for i, c := range c.chains {
6364
// Check that chain has input keys that are in knownKeys
64-
missingKeys := util.Difference(c.GetInputKeys(), knownKeys)
65+
missingKeys := setutil.Difference(c.GetInputKeys(), knownKeys)
6566
if len(missingKeys) > 0 {
6667
return fmt.Errorf(
6768
"%w: missing required input keys: [%v], only had: [%v]",
68-
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(util.ListKeys(knownKeys), delimiter),
69+
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(maputil.ListKeys(knownKeys), delimiter),
6970
)
7071
}
7172

7273
// Check that chain does not have output keys that are already in knownKeys
73-
overlappingKeys := util.Intersection(c.GetOutputKeys(), knownKeys)
74+
overlappingKeys := setutil.Intersection(c.GetOutputKeys(), knownKeys)
7475
if len(overlappingKeys) > 0 {
7576
return fmt.Errorf(
7677
"%w: chain at index %d has output keys that already exist: %v",

documentloaders/csv.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func (c CSV) Load(_ context.Context) ([]schema.Document, error) {
5151

5252
var content []string
5353
for i, value := range row {
54-
if c.columns != nil &&
55-
len(c.columns) > 0 &&
54+
if len(c.columns) > 0 &&
5655
!slices.Contains(c.columns, header[i]) {
5756
continue
5857
}

embeddings/embedding.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/tmc/langchaingo/internal/util"
8+
"github.com/tmc/langchaingo/internal/sliceutil"
99
)
1010

1111
// NewEmbedder creates a new Embedder from the given EmbedderClient, with
@@ -89,7 +89,7 @@ func BatchTexts(texts []string, batchSize int) [][]string {
8989
batchedTexts := make([][]string, 0, len(texts)/batchSize+1)
9090

9191
for i := 0; i < len(texts); i += batchSize {
92-
batchedTexts = append(batchedTexts, texts[i:util.MinInt([]int{i + batchSize, len(texts)})])
92+
batchedTexts = append(batchedTexts, texts[i:sliceutil.MinInt([]int{i + batchSize, len(texts)})])
9393
}
9494

9595
return batchedTexts

internal/util/download.go renamed to internal/imageutil/download.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package imageutil
22

33
import (
44
"fmt"

internal/maputil/map.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package maputil
2+
3+
func ListKeys[T any](m map[string]T) []string {
4+
keys := make([]string, 0, len(m))
5+
for k := range m {
6+
keys = append(keys, k)
7+
}
8+
return keys
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Package util contains general helper functions.
2-
package util
1+
package setutil
32

43
// ToSet converts a list to a set.
54
func ToSet(list []string) map[string]struct{} {
@@ -31,28 +30,3 @@ func Intersection(list []string, set map[string]struct{}) []string {
3130
}
3231
return intersection
3332
}
34-
35-
func ListKeys[T any](m map[string]T) []string {
36-
keys := make([]string, 0, len(m))
37-
for k := range m {
38-
keys = append(keys, k)
39-
}
40-
return keys
41-
}
42-
43-
// MinInt returns the minimum value in nums.
44-
// If nums is empty, it returns 0.
45-
func MinInt(nums []int) int {
46-
var min int
47-
for idx := 0; idx < len(nums); idx++ {
48-
item := nums[idx]
49-
if idx == 0 {
50-
min = item
51-
continue
52-
}
53-
if item < min {
54-
min = item
55-
}
56-
}
57-
return min
58-
}

internal/sliceutil/slice.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sliceutil
2+
3+
// MinInt returns the minimum value in nums.
4+
// If nums is empty, it returns 0.
5+
func MinInt(nums []int) int {
6+
var m int
7+
for idx := 0; idx < len(nums); idx++ {
8+
item := nums[idx]
9+
if idx == 0 {
10+
m = item
11+
continue
12+
}
13+
if item < m {
14+
m = item
15+
}
16+
}
17+
return m
18+
}

internal/util/util_test.go renamed to internal/sliceutil/slice_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package sliceutil
22

33
import (
44
"testing"

llms/googleai/googleai.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/google/generative-ai-go/genai"
13-
"github.com/tmc/langchaingo/internal/util"
13+
"github.com/tmc/langchaingo/internal/imageutil"
1414
"github.com/tmc/langchaingo/llms"
1515
"google.golang.org/api/iterator"
1616
)
@@ -187,7 +187,7 @@ func convertParts(parts []llms.ContentPart) ([]genai.Part, error) {
187187
case llms.BinaryContent:
188188
out = genai.Blob{MIMEType: p.MIMEType, Data: p.Data}
189189
case llms.ImageURLContent:
190-
typ, data, err := util.DownloadImageData(p.URL)
190+
typ, data, err := imageutil.DownloadImageData(p.URL)
191191
if err != nil {
192192
return nil, err
193193
}

llms/googleai/vertex/vertex.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414

1515
"cloud.google.com/go/vertexai/genai"
16-
"github.com/tmc/langchaingo/internal/util"
16+
"github.com/tmc/langchaingo/internal/imageutil"
1717
"github.com/tmc/langchaingo/llms"
1818
"google.golang.org/api/iterator"
1919
)
@@ -190,7 +190,7 @@ func convertParts(parts []llms.ContentPart) ([]genai.Part, error) {
190190
case llms.BinaryContent:
191191
out = genai.Blob{MIMEType: p.MIMEType, Data: p.Data}
192192
case llms.ImageURLContent:
193-
typ, data, err := util.DownloadImageData(p.URL)
193+
typ, data, err := imageutil.DownloadImageData(p.URL)
194194
if err != nil {
195195
return nil, err
196196
}

memory/sqlite3/sqlite3_history_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func WithDB(db *sql.DB) SqliteChatMessageHistoryOption {
4242
// to use a context internally when running Schema.
4343
func WithContext(ctx context.Context) SqliteChatMessageHistoryOption {
4444
return func(m *SqliteChatMessageHistory) {
45-
m.Ctx = ctx
45+
m.Ctx = ctx //nolint:fatcontext
4646
}
4747
}
4848

0 commit comments

Comments
 (0)