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

Add lint workflow #10

Merged
merged 21 commits into from
Oct 11, 2024
Merged

Add lint workflow #10

merged 21 commits into from
Oct 11, 2024

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
.github/workflows/lint.yml Fixed Show fixed Hide fixed
> 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()
  				^
@@ -75,3 +78,13 @@ func NewListCommand() *cobra.Command {

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.

@@ -28,28 +30,29 @@ func NewListCommand() *cobra.Command {

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!
@@ -176,73 +189,26 @@ func isPipe(r io.Reader) bool {
return false
}

// NewRunCommand returns a new gh command for running a model.
func NewRunCommand() *cobra.Command {
cmd := &cobra.Command{
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/auth"
"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.

@@ -439,3 +364,186 @@ func NewRunCommand() *cobra.Command {

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.

@@ -30,27 +36,30 @@ type ChatCompletionOptions struct {
TopP *float64 `json:"top_p,omitempty"`
}

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.

func (m *ModelDetails) ContextLimits() string {
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.

sort.Slice(models, func(i, j int) bool {
// Sort featured models first, by name
isFeaturedI := slices.Contains(featuredModelNames, models[i].Name)
isFeaturedJ := slices.Contains(featuredModelNames, models[j].Name)

if isFeaturedI && !isFeaturedJ {
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.

@@ -20,12 +21,12 @@ func main() {
}

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
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.

2 participants