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

Chore Lint code #133

Merged
merged 9 commits into from
Sep 15, 2023
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
208 changes: 208 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# Copyright 2013-2023 The Cobra Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

run:
deadline: 5m


# This file contains only configs which differ from defaults.
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
linters-settings:
cyclop:
# The maximal code complexity to report.
# Default: 10
max-complexity: 30
# The maximal average package complexity.
# If it's higher than 0.0 (float) the check is enabled
# Default: 0.0
package-average: 10.0

errcheck:
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
# Such cases aren't reported by default.
# Default: false
check-type-assertions: true

funlen:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
# Default: 60
lines: 100
# Checks the number of statements in a function.
# If lower than 0, disable the check.
# Default: 40
statements: 50

gocognit:
# Minimal code complexity to report
# Default: 30 (but we recommend 10-20)
min-complexity: 20

gocritic:
# Settings passed to gocritic.
# The settings key is the name of a supported gocritic checker.
# The list of supported checkers can be find in https://go-critic.github.io/overview.
settings:
captLocal:
# Whether to restrict checker to params only.
# Default: true
paramsOnly: false
underef:
# Whether to skip (*x).method() calls where x is a pointer receiver.
# Default: true
skipRecvDeref: false

gomnd:
# List of function patterns to exclude from analysis.
# Values always ignored: `time.Date`
# Default: []
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
- strconv.FormatFloat
- strconv.FormatInt
- strconv.FormatUint
- strconv.ParseFloat
- strconv.ParseInt
- strconv.ParseUint

govet:
# Enable all analyzers.
# Default: false
enable-all: true
# Disable analyzers by name.
# Run `go tool vet help` to see all analyzers.
# Default: []
disable:
- fieldalignment # too strict
# Settings per analyzer.
settings:
shadow:
# Whether to be strict about shadowing; can be noisy.
# Default: false
strict: true

nolintlint:
# Exclude following linters from requiring an explanation.
# Default: []
allow-no-explanation: [ funlen, gocognit, lll ]
# Enable to require an explanation of nonzero length after each nolint directive.
# Default: false
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed.
# Default: false
require-specific: true

tenv:
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
# Default: false
all: true

varcheck:
# Check usage of exported fields and variables.
# Default: false
exported-fields: false # default false # TODO: enable after fixing false positives

linters:
disable-all: true
enable:
# enabled by default
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
# disabled by default
- asciicheck
- bidichk
- bodyclose
- contextcheck
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
# - forbidigo
- funlen
# - gochecknoinits
- gocognit
- goconst
# - gocritic this can be enabled
- gocyclo
# - godot
- goimports
# - gomnd this can be enabled
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- lll
- makezero
- nestif
- nilerr
- nilnil
# - nolintlint this can be enabled
- nosprintfhostport
- predeclared
- promlinter
- revive
- rowserrcheck
- sqlclosecheck
- stylecheck
- tenv
# - testpackage
- tparallel
- unconvert
- unparam
- wastedassign
- whitespace
fast: false


issues:
# Maximum count of issues with the same text.
# Set to 0 to disable.
# Default: 3
max-same-issues: 50

exclude-rules:
- source: "^//\\s*go:generate\\s"
linters: [ lll ]
- source: "(noinspection|TODO)"
linters: [ godot ]
- source: "//noinspection"
linters: [ gocritic ]
- source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
linters: [ errorlint ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx
- wrapcheck
10 changes: 5 additions & 5 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ func (c *Chat) AddPromptMessages(messages []PromptMessage) {
}
}

func (p *Chat) addMessagePromptTemplate(message PromptMessage) {
p.promptMessages = append(p.promptMessages, message)
func (c *Chat) addMessagePromptTemplate(message PromptMessage) {
c.promptMessages = append(c.promptMessages, message)
}

// ToMessages converts the chat prompt template to a list of messages.
func (p *Chat) ToMessages() (Messages, error) {
func (c *Chat) ToMessages() (Messages, error) {
var messages Messages
var err error

for _, messagePromptTemplate := range p.promptMessages {
for _, messagePromptTemplate := range c.promptMessages {
var message Message
message.Type = messagePromptTemplate.Type
message.Name = messagePromptTemplate.Name
Expand All @@ -81,7 +81,7 @@ func (p *Chat) ToMessages() (Messages, error) {
if len(messagePromptTemplate.Prompt.String()) == 0 {
err = messagePromptTemplate.Prompt.Format(types.M{})
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrChatMessages, err)
return nil, fmt.Errorf("%w: %w", ErrChatMessages, err)
}
}
message.Content = messagePromptTemplate.Prompt.String()
Expand Down
1 change: 0 additions & 1 deletion chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestChat_ToMessages(t *testing.T) {

prompt1 := prompt.NewPromptTemplate(
"You are a helpful assistant that translates {{.input_language}} to {{.output_language}}.").WithInputs(
types.M{
Expand Down
4 changes: 2 additions & 2 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewJSONDecoder() *JSONDecoder {
func (d *JSONDecoder) Decode(input string) (types.M, error) {
err := json.Unmarshal([]byte(input), &d.output)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrDecoding, err)
return nil, fmt.Errorf("%w: %w", ErrDecoding, err)
}

return types.M{
Expand All @@ -47,7 +47,7 @@ func NewRegExDecoder(regex string) *RegExDecoder {
func (d *RegExDecoder) Decode(input string) (types.M, error) {
re, err := regexp.Compile(d.regex)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrDecoding, err)
return nil, fmt.Errorf("%w: %w", ErrDecoding, err)
}

matches := re.FindStringSubmatch(input)
Expand Down
1 change: 0 additions & 1 deletion document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (d *Document) GetContent() string {

// GetEnrichedContent returns the document content with the metadata appended
func (d *Document) GetEnrichedContent() string {

if d.Metadata == nil {
return d.Content
}
Expand Down
6 changes: 3 additions & 3 deletions embedder/cohere/cohere.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (e *Embedder) WithModel(model EmbedderModel) *Embedder {
}

// Embed returns the embeddings for the given texts
func (h *Embedder) Embed(ctx context.Context, texts []string) ([]embedder.Embedding, error) {
func (e *Embedder) Embed(ctx context.Context, texts []string) ([]embedder.Embedding, error) {
resp := &response.Embed{}
err := h.client.Embed(
err := e.client.Embed(
ctx,
&request.Embed{
Texts: texts,
Model: &h.model,
Model: &e.model,
},
resp,
)
Expand Down
1 change: 0 additions & 1 deletion embedder/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var (
type Embedding []float64

func (e Embedding) ToFloat32() []float32 {

vect := make([]float32, len(e))
for i, v := range e {
vect[i] = float32(v)
Expand Down
1 change: 0 additions & 1 deletion embedder/huggingface/feature_extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type featureExtractionRequest struct {
}

func (h *HuggingFaceEmbedder) featureExtraction(ctx context.Context, text []string) ([]embedder.Embedding, error) {

isTrue := true
request := featureExtractionRequest{
Inputs: text,
Expand Down
3 changes: 2 additions & 1 deletion embedder/huggingface/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
const APIBaseURL = "https://api-inference.huggingface.co/pipeline/feature-extraction/"

func (h *HuggingFaceEmbedder) doRequest(ctx context.Context, jsonBody []byte, model string) ([]byte, error) {

req, err := http.NewRequestWithContext(ctx, http.MethodPost, APIBaseURL+model, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
Expand Down Expand Up @@ -57,6 +56,7 @@ func checkRespForError(respJSON []byte) error {
apiErr := apiError{}
err := json.Unmarshal(buf, &apiErr)
if err != nil {
//nolint:nilerr
return nil
}
if apiErr.Error != "" {
Expand All @@ -70,6 +70,7 @@ func checkRespForError(respJSON []byte) error {
apiErrs := apiErrors{}
err := json.Unmarshal(buf, &apiErrs)
if err != nil {
//nolint:nilerr
return nil
}
if apiErrs.Errors != nil {
Expand Down
9 changes: 3 additions & 6 deletions embedder/llamacpp/llamacpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ func (l *LlamaCppEmbedder) WithArgs(llamacppArgs []string) *LlamaCppEmbedder {
}

// Embed returns the embeddings for the given texts
func (o *LlamaCppEmbedder) Embed(ctx context.Context, texts []string) ([]embedder.Embedding, error) {

func (l *LlamaCppEmbedder) Embed(ctx context.Context, texts []string) ([]embedder.Embedding, error) {
embeddings := make([]embedder.Embedding, len(texts))
for i, text := range texts {
embedding, err := o.embed(ctx, text)
embedding, err := l.embed(ctx, text)
if err != nil {
return nil, err
}
Expand All @@ -57,7 +56,6 @@ func (o *LlamaCppEmbedder) Embed(ctx context.Context, texts []string) ([]embedde
}

func (l *LlamaCppEmbedder) embed(ctx context.Context, text string) (embedder.Embedding, error) {

_, err := os.Stat(l.llamacppPath)
if err != nil {
return nil, err
Expand All @@ -66,17 +64,16 @@ func (l *LlamaCppEmbedder) embed(ctx context.Context, text string) (embedder.Emb
llamacppArgs := []string{"-m", l.modelPath, "-p", text}
llamacppArgs = append(llamacppArgs, l.llamacppArgs...)

//nolint:gosec
out, err := exec.CommandContext(ctx, l.llamacppPath, llamacppArgs...).Output()
if err != nil {
return nil, err
}

return parseEmbeddings(string(out))

}

func parseEmbeddings(str string) (embedder.Embedding, error) {

strSlice := strings.Split(strings.TrimSpace(str), " ")
floatSlice := make([]float64, len(strSlice))
for i, s := range strSlice {
Expand Down
1 change: 0 additions & 1 deletion embedder/openai/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func normalizeEmbeddings(embeddings []embedder.Embedding, lens []float64) embedder.Embedding {

chunkAvgEmbeddings := average(embeddings, lens)
norm := norm(chunkAvgEmbeddings)

Expand Down
Loading