From 0929c6d8a7e9d80f8ee2f9f84cf1a12554f2dbdf Mon Sep 17 00:00:00 2001 From: Travis Cline Date: Thu, 12 Sep 2024 18:05:32 -0700 Subject: [PATCH] lint: fixup lint settings --- .github/workflows/ci.yaml | 31 +++++++++-------- .golangci.yaml | 1 + Makefile | 2 +- documentloaders/csv.go | 3 +- llms/llamafile/llamafilellm.go | 41 ++++++++++++++++------- llms/watsonx/watsonxllm.go | 13 +++++-- memory/sqlite3/sqlite3_history_options.go | 2 +- vectorstores/chroma/chroma.go | 17 +++++++++- vectorstores/pinecone/pinecone.go | 17 +++++++++- 9 files changed, 89 insertions(+), 38 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9a6a44a87..1b90f6f89 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,5 +1,5 @@ -name: ci - +# GitHub Actions CI workflow for Go project +name: CI on: push: branches: @@ -7,40 +7,38 @@ on: 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/golangci-lint-action@v6.0.1 + 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.60 + 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 @@ -48,3 +46,4 @@ jobs: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} GENAI_API_KEY: ${{ secrets.GENAI_API_KEY }} run: go test -v ./... + diff --git a/.golangci.yaml b/.golangci.yaml index d347aae9e..cab783544 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -14,6 +14,7 @@ linters: - test - unused 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 diff --git a/Makefile b/Makefile index 868b9f28b..84e3450eb 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/documentloaders/csv.go b/documentloaders/csv.go index 2a41f4f91..9c8f73c25 100644 --- a/documentloaders/csv.go +++ b/documentloaders/csv.go @@ -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 } diff --git a/llms/llamafile/llamafilellm.go b/llms/llamafile/llamafilellm.go index 8e9cc6088..c90e726dc 100644 --- a/llms/llamafile/llamafilellm.go +++ b/llms/llamafile/llamafilellm.go @@ -3,6 +3,7 @@ package llamafile import ( "context" "errors" + "math" "github.com/tmc/langchaingo/callbacks" "github.com/tmc/langchaingo/llms" @@ -167,19 +168,33 @@ func makeLlamaOptionsFromOptions(input *llamafileclient.ChatRequest, opts llms.C // Initialize llamaOptions with values from opts streamValue := opts.StreamingFunc != nil - input.FrequencyPenalty = opts.FrequencyPenalty // Assuming FrequencyPenalty correlates to FrequencyPenalty; adjust if necessary - input.MinP = float64(opts.MinLength) // Assuming there's a direct correlation; adjust if necessary - input.Model = opts.Model // Assuming Model correlates to Model; adjust if necessary - input.NCtx = opts.N // Assuming N corresponds to NCtx; if not, adjust. - input.NPredict = opts.MaxTokens // Assuming MaxTokens correlates to NPredict; - input.PresencePenalty = opts.PresencePenalty // Assuming PresencePenalty correlates to PresencePenalty; - input.RepeatPenalty = opts.RepetitionPenalty // Assuming RepetitionPenalty correlates to RepeatPenalty; - input.Seed = uint32(opts.Seed) // Convert int to uint32 - input.Stop = opts.StopWords // Assuming StopWords correlates to Stop; - input.Stream = &streamValue // True if StreamingFunc provided; adjust logic as needed. - input.Temperature = opts.Temperature // Assuming Temperature correlates to Temperature for precision; - input.TopK = opts.TopK // Assuming TopK correlates to TopK; - input.TopP = opts.TopP // Assuming TopP correlates to TopP; + input.FrequencyPenalty = opts.FrequencyPenalty // Assuming FrequencyPenalty correlates to FrequencyPenalty; adjust if necessary + input.MinP = float64(opts.MinLength) // Assuming there's a direct correlation; adjust if necessary + input.Model = opts.Model // Assuming Model correlates to Model; adjust if necessary + input.NCtx = opts.N // Assuming N corresponds to NCtx; if not, adjust. + input.NPredict = opts.MaxTokens // Assuming MaxTokens correlates to NPredict; + input.PresencePenalty = opts.PresencePenalty // Assuming PresencePenalty correlates to PresencePenalty; + input.RepeatPenalty = opts.RepetitionPenalty // Assuming RepetitionPenalty correlates to RepeatPenalty; + input.Seed = uint32(max(0, min(opts.Seed, math.MaxUint32))) // Convert int to uint32 + input.Stop = opts.StopWords // Assuming StopWords correlates to Stop; + input.Stream = &streamValue // True if StreamingFunc provided; adjust logic as needed. + input.Temperature = opts.Temperature // Assuming Temperature correlates to Temperature for precision; + input.TopK = opts.TopK // Assuming TopK correlates to TopK; + input.TopP = opts.TopP // Assuming TopP correlates to TopP; return input } + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/llms/watsonx/watsonxllm.go b/llms/watsonx/watsonxllm.go index 42f00c272..8978c9ddc 100644 --- a/llms/watsonx/watsonxllm.go +++ b/llms/watsonx/watsonxllm.go @@ -112,19 +112,19 @@ func toWatsonxOptions(options *[]llms.CallOption) []wx.GenerateOption { o = append(o, wx.WithTopP(opts.TopP)) } if opts.TopK != -1 { - o = append(o, wx.WithTopK(uint(opts.TopK))) + o = append(o, wx.WithTopK(uint(max(0, opts.TopK)))) } if opts.Temperature != -1 { o = append(o, wx.WithTemperature(opts.Temperature)) } if opts.Seed != -1 { - o = append(o, wx.WithRandomSeed(uint(opts.Seed))) + o = append(o, wx.WithRandomSeed(uint(max(0, opts.Seed)))) } if opts.RepetitionPenalty != -1 { o = append(o, wx.WithRepetitionPenalty(opts.RepetitionPenalty)) } if opts.MaxTokens != -1 { - o = append(o, wx.WithMaxNewTokens(uint(opts.MaxTokens))) + o = append(o, wx.WithMaxNewTokens(uint(max(0, opts.MaxTokens)))) } if len(opts.StopWords) > 0 { o = append(o, wx.WithStopSequences(opts.StopWords)) @@ -143,3 +143,10 @@ func toWatsonxOptions(options *[]llms.CallOption) []wx.GenerateOption { return o } + +func max(a, b int) int { + if a > b { + return a + } + return b +} diff --git a/memory/sqlite3/sqlite3_history_options.go b/memory/sqlite3/sqlite3_history_options.go index 1a5eacda5..4cd6dc5ce 100644 --- a/memory/sqlite3/sqlite3_history_options.go +++ b/memory/sqlite3/sqlite3_history_options.go @@ -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 } } diff --git a/vectorstores/chroma/chroma.go b/vectorstores/chroma/chroma.go index 82609bc07..9be10e677 100644 --- a/vectorstores/chroma/chroma.go +++ b/vectorstores/chroma/chroma.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math" chromago "github.com/amikos-tech/chroma-go" "github.com/amikos-tech/chroma-go/openai" @@ -139,7 +140,7 @@ func (s Store) SimilaritySearch(ctx context.Context, query string, numDocuments } filter := s.getNamespacedFilter(opts) - qr, queryErr := s.collection.Query(ctx, []string{query}, int32(numDocuments), filter, nil, s.includes) + qr, queryErr := s.collection.Query(ctx, []string{query}, int32(max(0, min(numDocuments, math.MaxInt32))), filter, nil, s.includes) if queryErr != nil { return nil, queryErr } @@ -212,3 +213,17 @@ func (s Store) getNamespacedFilter(opts vectorstores.Options) map[string]any { return map[string]any{"$and": []map[string]any{nameSpaceFilter, filter}} } + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/vectorstores/pinecone/pinecone.go b/vectorstores/pinecone/pinecone.go index 1b22f553a..94acc0706 100644 --- a/vectorstores/pinecone/pinecone.go +++ b/vectorstores/pinecone/pinecone.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "math" "github.com/google/uuid" "github.com/pinecone-io/go-pinecone/pinecone" @@ -159,7 +160,7 @@ func (s Store) SimilaritySearch(ctx context.Context, query string, numDocuments &ctx, &pinecone.QueryByVectorValuesRequest{ Vector: vector, - TopK: uint32(numDocuments), + TopK: uint32(max(0, min(numDocuments, math.MaxUint32))), Filter: protoFilterStruct, IncludeMetadata: true, IncludeValues: true, @@ -245,3 +246,17 @@ func (s Store) createProtoStructFilter(filter any) (*structpb.Struct, error) { return &filterStruct, nil } + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +}