Skip to content

Commit

Permalink
added ollama
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Mar 24, 2024
1 parent 791b846 commit 1bd9061
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 0 deletions.
24 changes: 24 additions & 0 deletions completers/ollama_completer/cmd/cp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var cpCmd = &cobra.Command{
Use: "cp SOURCE TARGET",
Short: "Copy a model",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(cpCmd).Standalone()

rootCmd.AddCommand(cpCmd)

carapace.Gen(cpCmd).PositionalCompletion(
ollama.ActionModels().MultiParts(":"),
ollama.ActionModels().MultiParts(":").FilterArgs(),
)
}
28 changes: 28 additions & 0 deletions completers/ollama_completer/cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var createCmd = &cobra.Command{
Use: "create MODEL",
Short: "Create a model from a Modelfile",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(createCmd).Standalone()

createCmd.Flags().StringP("file", "f", "", "Name of the Modelfile (default \"Modelfile\")")
rootCmd.AddCommand(createCmd)

carapace.Gen(createCmd).FlagCompletion(carapace.ActionMap{
"file": carapace.ActionFiles(),
})

carapace.Gen(createCmd).PositionalCompletion(
ollama.ActionModels().MultiParts(":"),
)
}
22 changes: 22 additions & 0 deletions completers/ollama_completer/cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var helpCmd = &cobra.Command{
Use: "help [command]",
Short: "Help about any command",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(helpCmd).Standalone()

rootCmd.AddCommand(helpCmd)

carapace.Gen(helpCmd).PositionalAnyCompletion(
carapace.ActionCommands(rootCmd),
)
}
19 changes: 19 additions & 0 deletions completers/ollama_completer/cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List models",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(listCmd).Standalone()

rootCmd.AddCommand(listCmd)
}
21 changes: 21 additions & 0 deletions completers/ollama_completer/cmd/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var pullCmd = &cobra.Command{
Use: "pull MODEL",
Short: "Pull a model from a registry",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(pullCmd).Standalone()

pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")
rootCmd.AddCommand(pullCmd)

// TODO complete remote models
}
24 changes: 24 additions & 0 deletions completers/ollama_completer/cmd/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var pushCmd = &cobra.Command{
Use: "push MODEL",
Short: "Push a model to a registry",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(pushCmd).Standalone()

pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")
rootCmd.AddCommand(pushCmd)

carapace.Gen(pushCmd).PositionalCompletion(
ollama.ActionModels().MultiParts(":"),
)
}
23 changes: 23 additions & 0 deletions completers/ollama_completer/cmd/rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var rmCmd = &cobra.Command{
Use: "rm MODEL [MODEL...]",
Short: "Remove a model",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(rmCmd).Standalone()

rootCmd.AddCommand(rmCmd)

carapace.Gen(rmCmd).PositionalAnyCompletion(
ollama.ActionModels().MultiParts(":").FilterArgs(),
)
}
23 changes: 23 additions & 0 deletions completers/ollama_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "ollama",
Short: "Large language model runner",
Long: "https://ollama.com/",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}

func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().BoolP("version", "v", false, "Show version information")
}
31 changes: 31 additions & 0 deletions completers/ollama_completer/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var runCmd = &cobra.Command{
Use: "run MODEL [PROMPT]",
Short: "Run a model",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(runCmd).Standalone()

runCmd.Flags().String("format", "", "Response format (e.g. json)")
runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
runCmd.Flags().Bool("verbose", false, "Show timings for response")
rootCmd.AddCommand(runCmd)

carapace.Gen(runCmd).FlagCompletion(carapace.ActionMap{
"format": carapace.ActionValues("json"),
})

carapace.Gen(runCmd).PositionalCompletion(
ollama.ActionModels(),
)
}
19 changes: 19 additions & 0 deletions completers/ollama_completer/cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start ollama",
Aliases: []string{"start"},
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(serveCmd).Standalone()

rootCmd.AddCommand(serveCmd)
}
28 changes: 28 additions & 0 deletions completers/ollama_completer/cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/ollama"
"github.com/spf13/cobra"
)

var showCmd = &cobra.Command{
Use: "show MODEL",
Short: "Show information for a model",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(showCmd).Standalone()

showCmd.Flags().Bool("license", false, "Show license of a model")
showCmd.Flags().Bool("modelfile", false, "Show Modelfile of a model")
showCmd.Flags().Bool("parameters", false, "Show parameters of a model")
showCmd.Flags().Bool("system", false, "Show system message of a model")
showCmd.Flags().Bool("template", false, "Show template of a model")
rootCmd.AddCommand(showCmd)

carapace.Gen(showCmd).PositionalCompletion(
ollama.ActionModels(),
)
}
7 changes: 7 additions & 0 deletions completers/ollama_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/carapace-sh/carapace-bin/completers/ollama_completer/cmd"

func main() {
cmd.Execute()
}
22 changes: 22 additions & 0 deletions pkg/actions/tools/ollama/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ollama

import (
"fmt"
"strings"

"github.com/carapace-sh/carapace"
)

// ActionModels completes models
func ActionModels() carapace.Action {
return carapace.ActionExecCommand("ollama", "list")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)
for _, line := range lines[1:] {
if splitted := strings.Split(line, "\t"); len(splitted) >= 4 {
vals = append(vals, splitted[0], fmt.Sprintf("%v - %v", splitted[2], splitted[3]))
}
}
return carapace.ActionValuesDescribed(vals...)
})
}

0 comments on commit 1bd9061

Please sign in to comment.