Skip to content

Commit

Permalink
fix: golangci-lint v1.55 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mayliex authored and tmc committed Dec 5, 2023
1 parent e1be2fd commit 80f9027
Show file tree
Hide file tree
Showing 45 changed files with 140 additions and 132 deletions.
2 changes: 1 addition & 1 deletion chains/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (

// HTTPRequest http requester interface.
type HTTPRequest interface {
Do(*http.Request) (*http.Response, error)
Do(req *http.Request) (*http.Response, error)
}

type APIChain struct {
Expand Down
4 changes: 2 additions & 2 deletions chains/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chains

import (
"context"
"fmt"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestApply(t *testing.T) {
inputs := make([]map[string]any, numInputs)
for i := 0; i < len(inputs); i++ {
inputs[i] = map[string]any{
"text": fmt.Sprint(i),
"text": strconv.Itoa(i),
}
}

Expand Down
2 changes: 1 addition & 1 deletion chains/constitution/constitutional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestConstitutionCritiqueParsing(t *testing.T) {

for _, rawCritique := range []string{textOne, textTwo, textThree} {
critique := parseCritique(rawCritique)
require.Equal(t, strings.TrimSpace(critique), "This text is bad.",
require.Equal(t, "This text is bad.", strings.TrimSpace(critique),
fmt.Sprintf("Failed on %s with %s", rawCritique, critique))
}
}
Expand Down
2 changes: 1 addition & 1 deletion chains/constitutional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestConstitutionCritiqueParsing(t *testing.T) {

for _, rawCritique := range []string{textOne, textTwo, textThree} {
critique := parseCritique(rawCritique)
require.Equal(t, strings.TrimSpace(critique), "This text is bad.",
require.Equal(t, "This text is bad.", strings.TrimSpace(critique),
fmt.Sprintf("Failed on %s with %s", rawCritique, critique))
}
}
Expand Down
9 changes: 5 additions & 4 deletions chains/llm_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ import (
)

const (
quote = "```"
_llmMathPrompt = `Translate a math problem into a expression that can be evaluated as Starlark.
Use the output of running this code to answer the question.
---
Question: (Question with math problem.)
` + "```" + `starlark
` + quote + `starlark
$(single line expression that solves the problem)
` + "```" + `
` + quote + `
---
Question: What is 37593 * 67?
` + "```" + `starlark
` + quote + `starlark
37593 * 67
` + "```" + `
` + quote + `
---
Question: {{.question}}
Expand Down
2 changes: 1 addition & 1 deletion chains/map_reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestMapReduceInputVariables(t *testing.T) {

inputKeys := c.GetInputKeys()
expectedLength := 3
require.Equal(t, expectedLength, len(inputKeys))
require.Len(t, inputKeys, expectedLength)
}

func TestMapReduce(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion chains/map_rerank_documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMapRerankInputVariables(t *testing.T) {

inputKeys := c.GetInputKeys()
expectedLength := 3
require.Equal(t, expectedLength, len(inputKeys))
require.Len(t, inputKeys, expectedLength)
}

func TestMapRerankDocumentsCall(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion chains/prompt_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// PromptSelector is the interface for selecting a formatter depending on the
// LLM given.
type PromptSelector interface {
GetPrompt(llms.LanguageModel) prompts.PromptTemplate
GetPrompt(llm llms.LanguageModel) prompts.PromptTemplate
}

// ConditionalPromptSelector is a formatter selector that selects a prompt
Expand Down
8 changes: 4 additions & 4 deletions chains/sequential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func TestSimpleSequentialErrors(t *testing.T) {
t.Parallel()
c, err := NewSimpleSequentialChain([]Chain{tc.chain})
if tc.initErr != nil {
assert.ErrorIs(t, err, tc.initErr)
require.ErrorIs(t, err, tc.initErr)
} else {
require.NoError(t, err)
_, err := Run(context.Background(), c, "Do something")
assert.ErrorIs(t, err, tc.execErr)
require.ErrorIs(t, err, tc.execErr)
}
})
}
Expand Down Expand Up @@ -184,11 +184,11 @@ func TestSequentialChainErrors(t *testing.T) {
t.Parallel()
c, err := NewSequentialChain(tc.chains, []string{"input1", "input2"}, []string{"output"}, tc.seqChainOpts...)
if tc.initErr != nil {
assert.ErrorIs(t, err, tc.initErr)
require.ErrorIs(t, err, tc.initErr)
} else {
require.NoError(t, err)
_, err := Call(context.Background(), c, map[string]any{"input1": "foo", "input2": "bar"})
assert.ErrorIs(t, err, tc.execErr)
require.ErrorIs(t, err, tc.execErr)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion chains/sql_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSQLDatabaseChain_Call(t *testing.T) {

ret, ok := result["result"].(string)
require.True(t, ok)
require.Greater(t, len(ret), 0)
require.NotEmpty(t, ret)

t.Log(ret)
}
4 changes: 2 additions & 2 deletions documentloaders/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestCSVLoader(t *testing.T) {
t.Parallel()
file, err := os.Open("./testdata/test.csv")
assert.NoError(t, err)
require.NoError(t, err)

loader := NewCSV(file)

Expand All @@ -33,7 +33,7 @@ country: United Kingdom`
func TestCSVLoaderWithFilteringColumns(t *testing.T) {
t.Parallel()
file, err := os.Open("./testdata/test.csv")
assert.NoError(t, err)
require.NoError(t, err)

loader := NewCSV(file, "city")

Expand Down
4 changes: 2 additions & 2 deletions documentloaders/documentloaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Loader is the interface for loading and splitting documents from a source.
type Loader interface {
// Loads loads from a source and returns documents.
Load(context.Context) ([]schema.Document, error)
Load(ctx context.Context) ([]schema.Document, error)
// LoadAndSplit loads from a source and splits the documents using a text splitter.
LoadAndSplit(context.Context, textsplitter.TextSplitter) ([]schema.Document, error)
LoadAndSplit(ctx context.Context, splitter textsplitter.TextSplitter) ([]schema.Document, error)
}
4 changes: 2 additions & 2 deletions documentloaders/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestHTMLLoader(t *testing.T) {
t.Parallel()
file, err := os.Open("./testdata/test.html")
assert.NoError(t, err)
require.NoError(t, err)

loader := NewHTML(file)

Expand Down Expand Up @@ -46,5 +46,5 @@ func TestHTMLLoader(t *testing.T) {
assert.NotContains(t, content, notexpected[5])

expectedMetadata := map[string]any{}
assert.Equal(t, docs[0].Metadata, expectedMetadata)
assert.Equal(t, expectedMetadata, docs[0].Metadata)
}
27 changes: 14 additions & 13 deletions documentloaders/pdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ledongthuc/pdf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tmc/langchaingo/textsplitter"
)

Expand Down Expand Up @@ -38,13 +39,13 @@ func TestPDFLoader(t *testing.T) {
t.Run("PDFLoad", func(t *testing.T) {
t.Parallel()
f, err := os.Open("./testdata/sample.pdf")
assert.NoError(t, err)
require.NoError(t, err)
defer f.Close()
finfo, err := f.Stat()
assert.NoError(t, err)
require.NoError(t, err)
p := NewPDF(f, finfo.Size())
docs, err := p.Load(context.Background())
assert.NoError(t, err)
require.NoError(t, err)

assert.Len(t, docs, 2)

Expand All @@ -57,13 +58,13 @@ func TestPDFLoader(t *testing.T) {
t.Run("PDFLoadPassword", func(t *testing.T) {
t.Parallel()
f, err := os.Open("./testdata/sample_password.pdf")
assert.NoError(t, err)
require.NoError(t, err)
defer f.Close()
finfo, err := f.Stat()
assert.NoError(t, err)
require.NoError(t, err)
p := NewPDF(f, finfo.Size(), WithPassword("password"))
docs, err := p.Load(context.Background())
assert.NoError(t, err)
require.NoError(t, err)

assert.Len(t, docs, 2)

Expand All @@ -76,15 +77,15 @@ func TestPDFLoader(t *testing.T) {
t.Run("PDFLoadPasswordWrong", func(t *testing.T) {
t.Parallel()
f, err := os.Open("./testdata/sample_password.pdf")
assert.NoError(t, err)
require.NoError(t, err)
defer f.Close()
finfo, err := f.Stat()
assert.NoError(t, err)
require.NoError(t, err)
p := NewPDF(f, finfo.Size(), WithPassword("password1"))
docs, err := p.Load(context.Background())
assert.Errorf(t, err, pdf.ErrInvalidPassword.Error())
require.Errorf(t, err, pdf.ErrInvalidPassword.Error())

assert.Len(t, docs, 0)
assert.Empty(t, docs)
})
}

Expand Down Expand Up @@ -118,16 +119,16 @@ func TestPDFTextSplit(t *testing.T) {
t.Run("PDFTextSplit", func(t *testing.T) {
t.Parallel()
f, err := os.Open("./testdata/sample.pdf")
assert.NoError(t, err)
require.NoError(t, err)
defer f.Close()
finfo, err := f.Stat()
assert.NoError(t, err)
require.NoError(t, err)
p := NewPDF(f, finfo.Size())
split := textsplitter.NewRecursiveCharacter()
split.ChunkSize = 300
split.ChunkOverlap = 30
docs, err := p.LoadAndSplit(context.Background(), split)
assert.NoError(t, err)
require.NoError(t, err)

assert.Len(t, docs, 4)

Expand Down
6 changes: 3 additions & 3 deletions documentloaders/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestTextLoader(t *testing.T) {
t.Parallel()
file, err := os.Open("./testdata/test.txt")
assert.NoError(t, err)
require.NoError(t, err)

loader := NewText(file)

Expand All @@ -21,8 +21,8 @@ func TestTextLoader(t *testing.T) {
require.Len(t, docs, 1)

expectedPageContent := "Foo Bar Baz"
assert.Equal(t, docs[0].PageContent, expectedPageContent)
assert.Equal(t, expectedPageContent, docs[0].PageContent)

expectedMetadata := map[string]any{}
assert.Equal(t, docs[0].Metadata, expectedMetadata)
assert.Equal(t, expectedMetadata, docs[0].Metadata)
}
2 changes: 1 addition & 1 deletion embeddings/openai/openai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestOpenaiEmbeddingsWithAzureAPI(t *testing.T) {
// Azure deployment that uses embeddings model, the name depends on what we define in the Azure deployment section
openai.WithEmbeddingModel("model-embedding"),
)
assert.NoError(t, err)
require.NoError(t, err)

e, err := NewOpenAI(WithClient(*client), WithBatchSize(1), WithStripNewLines(false))
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions embeddings/vector_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCombineVectors(t *testing.T) {
Expand All @@ -23,7 +24,7 @@ func TestCombineVectors(t *testing.T) {

for _, tc := range cases {
combined, err := CombineVectors(tc.vectors, tc.weights)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tc.expected, combined)
}
}
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestGetAverage(t *testing.T) {

for _, tc := range cases {
average, err := getAverage(tc.vectors, tc.weights)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tc.expected, average)
}
}
Expand All @@ -78,6 +79,6 @@ func TestGetNorm(t *testing.T) {
}

for _, tc := range cases {
assert.Equal(t, tc.expected, getNorm(tc.vector))
assert.InEpsilon(t, tc.expected, getNorm(tc.vector), 0.0001)
}
}
Loading

0 comments on commit 80f9027

Please sign in to comment.