Skip to content

Conversation

@cheshire137
Copy link
Member

@cheshire137 cheshire137 commented Oct 9, 2024

Part of https://github.com/github/models/issues/330. This adds a new Actions build for running the Go linter. I also fixed outstanding things the linter flagged. I'd love to make this new build required once this PR lands, to help keep things tidy, especially once we open source the repo and get external contributors.

I tried the view, run with a prompt, run in interactive mode, and list commands locally and they seem all right. I would appreciate additional verification from reviewers that this PR doesn't break anything. 🙇🏻‍♀️

As copied from another internal repo.
@cheshire137 cheshire137 self-assigned this Oct 9, 2024
> Error return value of `io.WriteString` is not checked
Error: internal/azure_models/client.go:2:9: var-naming: don't use an underscore in package name (revive)
  package azure_models
          ^
Linter errors like:

Error: internal/azuremodels/client.go:56:33: should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
  	httpReq, err := http.NewRequest("POST", prodInferenceURL, body)
  	                               ^
  Error: cmd/run/run.go:191:1: Function name: NewRunCommand, Cyclomatic Complexity: 50, Halstead Volume: 6931.94, Maintainability Index: 13 (maintidx)
  func NewRunCommand() *cobra.Command {
  ^
Working toward fixing linter issues like these:

  Error: cmd/run/run.go:320:5: deferInLoop: Possible resource leak, 'defer' is called in the 'for' loop (gocritic)
  				defer sp.Stop()
  				^
  Error: cmd/run/run.go:327:5: deferInLoop: Possible resource leak, 'defer' is called in the 'for' loop (gocritic)
  				defer resp.Reader.Close()
  				^
return cmd
}

func filterToChatModels(models []*azuremodels.ModelSummary) []*azuremodels.ModelSummary {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from the ux package because it was only used here.

@@ -1,14 +1,15 @@
// Package list provides a gh command to list available models.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot helped with the package and function docs. cc @brannon for input on whether Copilot and I described things accurately.

token, _ := auth.TokenForHost("github.com")
if token == "" {
io.WriteString(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n")
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved io.WriteString calls to a new function that handles the returned error.

This PR is big enough already!
Use: "run [model] [prompt]",
Short: "Run inference with the specified model",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Copy link
Member Author

@cheshire137 cheshire137 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of changes to shrink this function because the linter complained about its complexity.

"github.com/cli/go-gh/v2/pkg/tableprinter"
"github.com/cli/go-gh/v2/pkg/term"
"github.com/github/gh-models/internal/azure_models"
"github.com/github/gh-models/internal/azuremodels"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter dislikes underscores in package names.

return cmd
}

type runCommandHandler struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a type to bundle together various things that are used in several pieces of the run handler function, so the large function could be more easily split into multiple functions.

}

type ChatChoiceMessage struct {
type chatChoiceMessage struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some types internal to this package since they weren't used outside it.

return fmt.Sprintf("up to %d input tokens and %d output tokens", m.MaxInputTokens, m.MaxOutputTokens)
}

func Ptr[T any](value T) *T {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this to the util package since it felt generic.

return true
} else if !isFeaturedI && isFeaturedJ {
return false
} else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter complained about using else branches when each branch returned.


func mainRun() exitCode {
cmd := cmd.NewRootCommand()
rootCmd := cmd.NewRootCommand()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name cmd shadowed an import.

@cheshire137 cheshire137 marked this pull request as ready for review October 10, 2024 22:25
@cheshire137 cheshire137 requested a review from a team as a code owner October 10, 2024 22:25
@cheshire137 cheshire137 requested a review from brannon October 10, 2024 22:26
Copy link
Collaborator

@sgoedecke sgoedecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I've run through a bunch of commands locally on this branch and it all worked great 👍

@cheshire137 cheshire137 merged commit f664ba4 into main Oct 11, 2024
4 checks passed
@cheshire137 cheshire137 deleted the go-linter branch October 11, 2024 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants