Skip to content

Commit

Permalink
More updates for linter
Browse files Browse the repository at this point in the history
  • Loading branch information
cheshire137 committed Oct 10, 2024
1 parent 6ad6204 commit 2920dee
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
12 changes: 11 additions & 1 deletion cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewListCommand() *cobra.Command {

// For now, filter to just chat models.
// Once other tasks are supported (like embeddings), update the list to show all models, with the task as a column.
models = ux.FilterToChatModels(models)
models = filterToChatModels(models)
ux.SortModels(models)

isTTY := terminal.IsTerminalOutput()
Expand Down Expand Up @@ -77,3 +77,13 @@ func NewListCommand() *cobra.Command {

return cmd
}

func filterToChatModels(models []*azuremodels.ModelSummary) []*azuremodels.ModelSummary {
var chatModels []*azuremodels.ModelSummary
for _, model := range models {
if ux.IsChatModel(model) {
chatModels = append(chatModels, model)
}
}
return chatModels
}
5 changes: 4 additions & 1 deletion cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,10 @@ func NewRunCommand() *cobra.Command {
}

util.WriteToOut(out, "\n")
messageBuilder.WriteString("\n")
_, err = messageBuilder.WriteString("\n")
if err != nil {
return err
}

conversation.AddMessage(azuremodels.ChatMessageRoleAssistant, messageBuilder.String())

Expand Down
7 changes: 6 additions & 1 deletion internal/azuremodels/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/text/language/display"
)

// Client provides a client for interacting with the Azure models API.
type Client struct {
client *http.Client
token string
Expand Down Expand Up @@ -93,7 +94,7 @@ func (c *Client) GetChatCompletionStream(req ChatCompletionOptions) (*ChatComple
// GetModelDetails returns the details of the specified model in a prticular registry.
func (c *Client) GetModelDetails(registry, modelName, version string) (*ModelDetails, error) {
url := fmt.Sprintf("%s/asset-gallery/v1.0/%s/models/%s/version/%s", azureAiStudioURL, registry, modelName, version)
httpReq, err := http.NewRequest("GET", url, nil)
httpReq, err := http.NewRequest("GET", url, http.NoBody)

Check failure on line 97 in internal/azuremodels/client.go

View workflow job for this annotation

GitHub Actions / lint

should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
if err != nil {
return nil, err
}
Expand All @@ -105,6 +106,8 @@ func (c *Client) GetModelDetails(registry, modelName, version string) (*ModelDet
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, c.handleHTTPError(resp)
}
Expand Down Expand Up @@ -192,6 +195,8 @@ func (c *Client) ListModels() ([]*ModelSummary, error) {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, c.handleHTTPError(resp)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/azuremodels/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type modelCatalogSearchSummary struct {
Summary string `json:"summary"`
}

// ModelSummary includes basic information about a model.
type ModelSummary struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -127,6 +128,7 @@ type modelCatalogDetailsResponse struct {
} `json:"modelLimits"`
}

// ModelDetails includes detailed information about a model.
type ModelDetails struct {
Description string `json:"description"`
Evaluation string `json:"evaluation"`
Expand Down
1 change: 1 addition & 0 deletions internal/sse/eventreader.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Forked from https://github.com/Azure/azure-sdk-for-go/blob/4661007ca1fd68b2e31f3502d4282904014fd731/sdk/ai/azopenai/event_reader.go#L18

// Package sse provides a reader for Server-Sent Events (SSE) streams.
package sse

import (
Expand Down
11 changes: 1 addition & 10 deletions internal/ux/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ package ux

import "github.com/github/gh-models/internal/azuremodels"

// IsChatModel returns true if the given model is for chat completions.
func IsChatModel(model *azuremodels.ModelSummary) bool {
return model.Task == "chat-completion"
}

func FilterToChatModels(models []*azuremodels.ModelSummary) []*azuremodels.ModelSummary {
var chatModels []*azuremodels.ModelSummary
for _, model := range models {
if IsChatModel(model) {
chatModels = append(chatModels, model)
}
}
return chatModels
}
19 changes: 11 additions & 8 deletions internal/ux/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
featuredModelNames = []string{}
)

// SortModels sorts the given models in place, with featured models first, and then by friendly name.
func SortModels(models []*azuremodels.ModelSummary) {
sort.Slice(models, func(i, j int) bool {
// Sort featured models first, by name
Expand All @@ -20,15 +21,17 @@ func SortModels(models []*azuremodels.ModelSummary) {

if isFeaturedI && !isFeaturedJ {
return true
} else if !isFeaturedI && isFeaturedJ {
return false
} else {
// Otherwise, sort by friendly name
// Note: sometimes the casing returned by the API is inconsistent, so sort using lowercase values.
friendlyNameI := strings.ToLower(models[i].FriendlyName)
friendlyNameJ := strings.ToLower(models[j].FriendlyName)
}

return friendlyNameI < friendlyNameJ
if !isFeaturedI && isFeaturedJ {
return false
}

// Otherwise, sort by friendly name
// Note: sometimes the casing returned by the API is inconsistent, so sort using lowercase values.
friendlyNameI := strings.ToLower(models[i].FriendlyName)
friendlyNameJ := strings.ToLower(models[j].FriendlyName)

return friendlyNameI < friendlyNameJ
})
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main provides the entry point for the gh-models extension.
package main

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

func mainRun() exitCode {
cmd := cmd.NewRootCommand()
rootCmd := cmd.NewRootCommand()
exitCode := exitOK

ctx := context.Background()

if _, err := cmd.ExecuteContextC(ctx); err != nil {
if _, err := rootCmd.ExecuteContextC(ctx); err != nil {
exitCode = exitError
}

Expand Down

0 comments on commit 2920dee

Please sign in to comment.