Skip to content

Commit d612eec

Browse files
authored
Merge pull request #34 from RoseSecurity/create-list-models-command
feat: add kuzco list models command
2 parents 449e39b + 18025d0 commit d612eec

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

cmd/list.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// listCmd commands lists models
8+
var listCmd = &cobra.Command{
9+
Use: "list",
10+
Short: "Lists available Ollama models",
11+
Long: `Queries and lists available Ollama models for Kuzco use`,
12+
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
13+
}
14+
15+
func init() {
16+
rootCmd.AddCommand(listCmd)
17+
}

cmd/list_models.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/charmbracelet/glamour"
8+
"github.com/spf13/cobra"
9+
10+
u "github.com/RoseSecurity/kuzco/pkg/utils"
11+
)
12+
13+
// ListModelsCmd lists Ollama models
14+
var listModelsCmd = &cobra.Command{
15+
Use: "models",
16+
Short: "Lists available Ollama models",
17+
Long: `This command lists Ollama models`,
18+
Example: "list models",
19+
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
20+
Run: func(cmd *cobra.Command, args []string) {
21+
models, err := u.ListModels(addr)
22+
if err != nil {
23+
u.LogErrorAndExit(err)
24+
}
25+
26+
// Prepare a markdown list of models
27+
var modelList strings.Builder
28+
modelList.WriteString("### Available Ollama Models\n\n")
29+
30+
if len(models) == 0 {
31+
modelList.WriteString("No models available.\n")
32+
} else {
33+
for _, model := range models {
34+
modelList.WriteString(fmt.Sprintf("- %s\n", model))
35+
}
36+
}
37+
38+
renderer, err := glamour.NewTermRenderer(glamour.WithAutoStyle())
39+
if err != nil {
40+
u.LogErrorAndExit(fmt.Errorf("error creating markdown renderer: %v", err))
41+
}
42+
43+
rendered, err := renderer.Render(modelList.String())
44+
if err != nil {
45+
u.LogErrorAndExit(fmt.Errorf("error rendering markdown: %v", err))
46+
}
47+
48+
fmt.Print(rendered)
49+
},
50+
}
51+
52+
func init() {
53+
listModelsCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)")
54+
listCmd.AddCommand(listModelsCmd)
55+
listModelsCmd.DisableFlagParsing = false
56+
}

pkg/utils/list_models.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package utils
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
type Model struct {
11+
Name string `json:"name"`
12+
}
13+
14+
// ListModels lists available models from Ollama
15+
func ListModels(addr string) ([]string, error) {
16+
if _, err := url.Parse(addr); err != nil {
17+
return nil, fmt.Errorf("invalid address provided: %v", err)
18+
}
19+
20+
resp, err := http.Get(fmt.Sprintf("%s/api/tags", addr))
21+
if err != nil {
22+
return nil, fmt.Errorf("error fetching models from Ollama: %v", err)
23+
}
24+
defer resp.Body.Close()
25+
26+
if resp.StatusCode != http.StatusOK {
27+
return nil, fmt.Errorf("failed to retrieve models: status code %d", resp.StatusCode)
28+
}
29+
30+
var result struct {
31+
Models []Model `json:"models"`
32+
}
33+
34+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
35+
return nil, fmt.Errorf("error decoding response: %v", err)
36+
}
37+
38+
var modelNames []string
39+
for _, model := range result.Models {
40+
modelNames = append(modelNames, model.Name)
41+
}
42+
43+
return modelNames, nil
44+
}

0 commit comments

Comments
 (0)