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

lint: fixup lint settings, refactor package #1018

Merged
merged 2 commits into from
Sep 13, 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
31 changes: 15 additions & 16 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
name: ci

# GitHub Actions CI workflow for Go project
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
# Cache is managed by golangci-lint
# https://github.com/actions/setup-go#caching-dependency-files-and-build-outputs
cache: false
- name: golangci-lint
uses: golangci/[email protected]
go-version-file: go.mod
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=4m
version: v1.59.1
version: v1.61
args: --timeout=5m
build-examples:
name: Build Examples
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
cache: false
- uses: actions/checkout@v3
- name: Build examples
run: make build-examples
build-test:
name: Build and Test
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- uses: actions/checkout@v3
- name: Build
run: go build -v ./...
- name: Test
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GENAI_API_KEY: ${{ secrets.GENAI_API_KEY }}
run: go test -v ./...

9 changes: 9 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ linters:
- style
- test
- unused
enable:
- forbidigo
disable:
- gci # We don't use gci.
- godox # We allow TODO lines.
- tagliatelle # As we're dealing with third parties we must accept snake case.
- wsl # We don't agree with wsl's style rules
Expand Down Expand Up @@ -54,6 +57,12 @@ linters-settings:
ignore-file-rules:
- "**/*_test.go"
- "**/mock/**/*.go"
forbidigo:
forbid:
- 'import "[^"]+/(util|common|helpers)"'
gosec:
excludes:
- G115 # https://github.com/securego/gosec/issues/1212
run:
exclude-dirs:
- 'exp'
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lint-all:
lint-deps:
@command -v golangci-lint >/dev/null 2>&1 || { \
echo >&2 "golangci-lint not found. Installing..."; \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.1; \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0; \
}

.PHONY: docs
Expand Down
13 changes: 7 additions & 6 deletions chains/sequential.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"fmt"
"strings"

"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/maputil"
"github.com/tmc/langchaingo/internal/setutil"
"github.com/tmc/langchaingo/memory"
"github.com/tmc/langchaingo/schema"
)
Expand Down Expand Up @@ -42,11 +43,11 @@ func NewSequentialChain(chains []Chain, inputKeys []string, outputKeys []string,
}

func (c *SequentialChain) validateSeqChain() error {
knownKeys := util.ToSet(c.inputKeys)
knownKeys := setutil.ToSet(c.inputKeys)

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

for i, c := range c.chains {
// Check that chain has input keys that are in knownKeys
missingKeys := util.Difference(c.GetInputKeys(), knownKeys)
missingKeys := setutil.Difference(c.GetInputKeys(), knownKeys)
if len(missingKeys) > 0 {
return fmt.Errorf(
"%w: missing required input keys: [%v], only had: [%v]",
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(util.ListKeys(knownKeys), delimiter),
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(maputil.ListKeys(knownKeys), delimiter),
)
}

// Check that chain does not have output keys that are already in knownKeys
overlappingKeys := util.Intersection(c.GetOutputKeys(), knownKeys)
overlappingKeys := setutil.Intersection(c.GetOutputKeys(), knownKeys)
if len(overlappingKeys) > 0 {
return fmt.Errorf(
"%w: chain at index %d has output keys that already exist: %v",
Expand Down
3 changes: 1 addition & 2 deletions documentloaders/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func (c CSV) Load(_ context.Context) ([]schema.Document, error) {

var content []string
for i, value := range row {
if c.columns != nil &&
len(c.columns) > 0 &&
if len(c.columns) > 0 &&
!slices.Contains(c.columns, header[i]) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions embeddings/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/sliceutil"
)

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

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

return batchedTexts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package imageutil

import (
"fmt"
Expand Down
9 changes: 9 additions & 0 deletions internal/maputil/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package maputil

func ListKeys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
28 changes: 1 addition & 27 deletions internal/util/util.go → internal/setutil/set.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package util contains general helper functions.
package util
package setutil

// ToSet converts a list to a set.
func ToSet(list []string) map[string]struct{} {
Expand Down Expand Up @@ -31,28 +30,3 @@ func Intersection(list []string, set map[string]struct{}) []string {
}
return intersection
}

func ListKeys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}

// MinInt returns the minimum value in nums.
// If nums is empty, it returns 0.
func MinInt(nums []int) int {
var min int
for idx := 0; idx < len(nums); idx++ {
item := nums[idx]
if idx == 0 {
min = item
continue
}
if item < min {
min = item
}
}
return min
}
18 changes: 18 additions & 0 deletions internal/sliceutil/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sliceutil

// MinInt returns the minimum value in nums.
// If nums is empty, it returns 0.
func MinInt(nums []int) int {
var m int
for idx := 0; idx < len(nums); idx++ {
item := nums[idx]
if idx == 0 {
m = item
continue
}
if item < m {
m = item
}
}
return m
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package sliceutil

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions llms/googleai/googleai.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/google/generative-ai-go/genai"
"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/imageutil"
"github.com/tmc/langchaingo/llms"
"google.golang.org/api/iterator"
)
Expand Down Expand Up @@ -187,7 +187,7 @@ func convertParts(parts []llms.ContentPart) ([]genai.Part, error) {
case llms.BinaryContent:
out = genai.Blob{MIMEType: p.MIMEType, Data: p.Data}
case llms.ImageURLContent:
typ, data, err := util.DownloadImageData(p.URL)
typ, data, err := imageutil.DownloadImageData(p.URL)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions llms/googleai/vertex/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"

"cloud.google.com/go/vertexai/genai"
"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/imageutil"
"github.com/tmc/langchaingo/llms"
"google.golang.org/api/iterator"
)
Expand Down Expand Up @@ -190,7 +190,7 @@ func convertParts(parts []llms.ContentPart) ([]genai.Part, error) {
case llms.BinaryContent:
out = genai.Blob{MIMEType: p.MIMEType, Data: p.Data}
case llms.ImageURLContent:
typ, data, err := util.DownloadImageData(p.URL)
typ, data, err := imageutil.DownloadImageData(p.URL)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion memory/sqlite3/sqlite3_history_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func WithDB(db *sql.DB) SqliteChatMessageHistoryOption {
// to use a context internally when running Schema.
func WithContext(ctx context.Context) SqliteChatMessageHistoryOption {
return func(m *SqliteChatMessageHistory) {
m.Ctx = ctx
m.Ctx = ctx //nolint:fatcontext
}
}

Expand Down
Loading