added ollama

This commit is contained in:
rsteube 2024-03-24 17:51:10 +01:00
parent 791b846a53
commit 1bd9061929
13 changed files with 291 additions and 0 deletions

View File

@ -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(),
)
}

View File

@ -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(":"),
)
}

View File

@ -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),
)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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(":"),
)
}

View File

@ -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(),
)
}

View File

@ -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")
}

View File

@ -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(),
)
}

View File

@ -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)
}

View File

@ -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(),
)
}

View File

@ -0,0 +1,7 @@
package main
import "github.com/carapace-sh/carapace-bin/completers/ollama_completer/cmd"
func main() {
cmd.Execute()
}

View File

@ -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...)
})
}