2023-07-03 19:22:44 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-07-06 20:49:31 +00:00
|
|
|
"bufio"
|
2023-07-03 19:22:44 +00:00
|
|
|
"context"
|
2023-08-11 17:58:23 +00:00
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/pem"
|
2023-07-06 22:43:04 +00:00
|
|
|
"errors"
|
2023-07-06 16:24:49 +00:00
|
|
|
"fmt"
|
2023-07-18 21:01:19 +00:00
|
|
|
"io"
|
2023-07-03 19:22:44 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2023-07-31 20:25:57 +00:00
|
|
|
"os/exec"
|
2023-09-29 00:13:01 +00:00
|
|
|
"os/signal"
|
2023-07-19 02:34:05 +00:00
|
|
|
"path/filepath"
|
2023-07-31 20:25:57 +00:00
|
|
|
"runtime"
|
2023-07-07 17:12:58 +00:00
|
|
|
"strings"
|
2023-09-29 00:13:01 +00:00
|
|
|
"syscall"
|
2023-07-06 22:43:04 +00:00
|
|
|
"time"
|
2023-07-03 19:22:44 +00:00
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
"github.com/olekukonko/tablewriter"
|
2023-09-22 20:36:08 +00:00
|
|
|
"github.com/pdevine/readline"
|
2023-07-06 20:49:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-08-11 17:58:23 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2023-09-22 20:36:08 +00:00
|
|
|
"golang.org/x/term"
|
2023-07-06 20:49:31 +00:00
|
|
|
|
2023-07-03 20:32:48 +00:00
|
|
|
"github.com/jmorganca/ollama/api"
|
2023-07-18 16:09:45 +00:00
|
|
|
"github.com/jmorganca/ollama/format"
|
2023-07-20 00:24:03 +00:00
|
|
|
"github.com/jmorganca/ollama/progressbar"
|
2023-07-03 20:32:48 +00:00
|
|
|
"github.com/jmorganca/ollama/server"
|
2023-08-22 01:24:42 +00:00
|
|
|
"github.com/jmorganca/ollama/version"
|
2023-07-03 19:22:44 +00:00
|
|
|
)
|
|
|
|
|
2023-09-23 03:49:35 +00:00
|
|
|
type Painter struct {
|
2023-09-24 00:20:30 +00:00
|
|
|
IsMultiLine bool
|
2023-09-23 03:49:35 +00:00
|
|
|
}
|
2023-09-18 19:26:56 +00:00
|
|
|
|
2023-09-23 03:49:35 +00:00
|
|
|
func (p Painter) Paint(line []rune, _ int) []rune {
|
2023-09-18 19:26:56 +00:00
|
|
|
termType := os.Getenv("TERM")
|
2023-09-24 00:20:30 +00:00
|
|
|
if termType == "xterm-256color" && len(line) == 0 {
|
|
|
|
var prompt string
|
|
|
|
if p.IsMultiLine {
|
|
|
|
prompt = "Use \"\"\" to end multi-line input"
|
|
|
|
} else {
|
2023-09-29 00:13:01 +00:00
|
|
|
prompt = "Send a message (/? for help)"
|
2023-09-24 00:20:30 +00:00
|
|
|
}
|
2023-09-18 19:26:56 +00:00
|
|
|
return []rune(fmt.Sprintf("\033[38;5;245m%s\033[%dD\033[0m", prompt, len(prompt)))
|
|
|
|
}
|
2023-09-24 00:20:30 +00:00
|
|
|
// add a space and a backspace to prevent the cursor from walking up the screen
|
|
|
|
line = append(line, []rune(" \b")...)
|
2023-09-18 19:26:56 +00:00
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
func CreateHandler(cmd *cobra.Command, args []string) error {
|
2023-07-17 00:02:22 +00:00
|
|
|
filename, _ := cmd.Flags().GetString("file")
|
2023-07-19 02:34:05 +00:00
|
|
|
filename, err := filepath.Abs(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 00:02:22 +00:00
|
|
|
|
2023-07-17 21:14:41 +00:00
|
|
|
var spinner *Spinner
|
|
|
|
|
2023-07-25 18:25:13 +00:00
|
|
|
var currentDigest string
|
|
|
|
var bar *progressbar.ProgressBar
|
2023-07-17 21:14:41 +00:00
|
|
|
|
2023-07-25 18:25:13 +00:00
|
|
|
request := api.CreateRequest{Name: args[0], Path: filename}
|
|
|
|
fn := func(resp api.ProgressResponse) error {
|
|
|
|
if resp.Digest != currentDigest && resp.Digest != "" {
|
|
|
|
if spinner != nil {
|
|
|
|
spinner.Stop()
|
|
|
|
}
|
|
|
|
currentDigest = resp.Digest
|
2023-10-16 15:07:37 +00:00
|
|
|
// pulling
|
|
|
|
bar = progressbar.DefaultBytes(
|
|
|
|
resp.Total,
|
|
|
|
resp.Status,
|
|
|
|
)
|
|
|
|
bar.Set64(resp.Completed)
|
2023-07-25 18:25:13 +00:00
|
|
|
} else if resp.Digest == currentDigest && resp.Digest != "" {
|
2023-09-28 17:00:34 +00:00
|
|
|
bar.Set64(resp.Completed)
|
2023-07-25 18:25:13 +00:00
|
|
|
} else {
|
|
|
|
currentDigest = ""
|
|
|
|
if spinner != nil {
|
|
|
|
spinner.Stop()
|
|
|
|
}
|
|
|
|
spinner = NewSpinner(resp.Status)
|
|
|
|
go spinner.Spin(100 * time.Millisecond)
|
|
|
|
}
|
2023-08-14 18:15:53 +00:00
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Create(context.Background(), &request, fn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 21:14:41 +00:00
|
|
|
if spinner != nil {
|
|
|
|
spinner.Stop()
|
2023-08-14 18:15:53 +00:00
|
|
|
if spinner.description != "success" {
|
|
|
|
return errors.New("unexpected end to create model")
|
|
|
|
}
|
2023-07-17 21:14:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 16:07:27 +00:00
|
|
|
func RunHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-22 04:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:50:52 +00:00
|
|
|
models, err := client.List(context.Background())
|
2023-07-18 05:44:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-27 02:49:55 +00:00
|
|
|
canonicalModelPath := server.ParseModelPath(args[0])
|
2023-09-21 16:50:52 +00:00
|
|
|
for _, model := range models.Models {
|
2023-09-27 02:49:55 +00:00
|
|
|
if model.Name == canonicalModelPath.GetShortTagname() {
|
2023-09-21 16:50:52 +00:00
|
|
|
return RunGenerate(cmd, args)
|
2023-07-06 22:43:04 +00:00
|
|
|
}
|
2023-09-21 16:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := PullHandler(cmd, args); err != nil {
|
2023-07-06 21:05:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return RunGenerate(cmd, args)
|
2023-07-06 18:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
func PushHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 00:02:22 +00:00
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
insecure, err := cmd.Flags().GetBool("insecure")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 00:31:26 +00:00
|
|
|
var currentDigest string
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
request := api.PushRequest{Name: args[0], Insecure: insecure}
|
2023-07-19 01:51:30 +00:00
|
|
|
fn := func(resp api.ProgressResponse) error {
|
2023-07-23 00:31:26 +00:00
|
|
|
if resp.Digest != currentDigest && resp.Digest != "" {
|
|
|
|
currentDigest = resp.Digest
|
|
|
|
bar = progressbar.DefaultBytes(
|
2023-09-28 17:00:34 +00:00
|
|
|
resp.Total,
|
2023-07-23 00:31:26 +00:00
|
|
|
fmt.Sprintf("pushing %s...", resp.Digest[7:19]),
|
|
|
|
)
|
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
bar.Set64(resp.Completed)
|
2023-07-23 00:31:26 +00:00
|
|
|
} else if resp.Digest == currentDigest && resp.Digest != "" {
|
2023-09-28 17:00:34 +00:00
|
|
|
bar.Set64(resp.Completed)
|
2023-07-23 00:31:26 +00:00
|
|
|
} else {
|
|
|
|
currentDigest = ""
|
|
|
|
fmt.Println(resp.Status)
|
|
|
|
}
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Push(context.Background(), &request, fn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-14 18:15:53 +00:00
|
|
|
|
|
|
|
if bar != nil && !bar.IsFinished() {
|
|
|
|
return errors.New("unexpected end to push model")
|
|
|
|
}
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
func ListHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-18 16:09:45 +00:00
|
|
|
|
|
|
|
models, err := client.List(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data [][]string
|
|
|
|
|
|
|
|
for _, m := range models.Models {
|
2023-07-18 21:01:19 +00:00
|
|
|
if len(args) == 0 || strings.HasPrefix(m.Name, args[0]) {
|
2023-08-29 03:50:24 +00:00
|
|
|
data = append(data, []string{m.Name, m.Digest[:12], humanize.Bytes(uint64(m.Size)), format.HumanTime(m.ModifiedAt, "Never")})
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
2023-07-18 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
2023-08-29 03:50:24 +00:00
|
|
|
table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"})
|
2023-07-18 16:09:45 +00:00
|
|
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetNoWhiteSpace(true)
|
|
|
|
table.SetTablePadding("\t")
|
|
|
|
table.AppendBulk(data)
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
func DeleteHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-20 23:09:23 +00:00
|
|
|
|
2023-08-26 07:47:56 +00:00
|
|
|
for _, name := range args {
|
|
|
|
req := api.DeleteRequest{Name: name}
|
|
|
|
if err := client.Delete(context.Background(), &req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("deleted '%s'\n", name)
|
2023-07-20 23:09:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 18:04:17 +00:00
|
|
|
func ShowHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-09-06 18:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
return errors.New("missing model name")
|
|
|
|
}
|
|
|
|
|
|
|
|
license, errLicense := cmd.Flags().GetBool("license")
|
|
|
|
modelfile, errModelfile := cmd.Flags().GetBool("modelfile")
|
|
|
|
parameters, errParams := cmd.Flags().GetBool("parameters")
|
|
|
|
system, errSystem := cmd.Flags().GetBool("system")
|
|
|
|
template, errTemplate := cmd.Flags().GetBool("template")
|
|
|
|
|
|
|
|
for _, boolErr := range []error{errLicense, errModelfile, errParams, errSystem, errTemplate} {
|
|
|
|
if boolErr != nil {
|
|
|
|
return errors.New("error retrieving flags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flagsSet := 0
|
|
|
|
showType := ""
|
|
|
|
|
|
|
|
if license {
|
|
|
|
flagsSet++
|
|
|
|
showType = "license"
|
|
|
|
}
|
|
|
|
|
|
|
|
if modelfile {
|
|
|
|
flagsSet++
|
|
|
|
showType = "modelfile"
|
|
|
|
}
|
|
|
|
|
|
|
|
if parameters {
|
|
|
|
flagsSet++
|
|
|
|
showType = "parameters"
|
|
|
|
}
|
|
|
|
|
|
|
|
if system {
|
|
|
|
flagsSet++
|
|
|
|
showType = "system"
|
|
|
|
}
|
|
|
|
|
|
|
|
if template {
|
|
|
|
flagsSet++
|
|
|
|
showType = "template"
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagsSet > 1 {
|
2023-09-06 20:38:49 +00:00
|
|
|
return errors.New("only one of '--license', '--modelfile', '--parameters', '--system', or '--template' can be specified")
|
2023-09-06 18:04:17 +00:00
|
|
|
} else if flagsSet == 0 {
|
2023-09-06 20:38:49 +00:00
|
|
|
return errors.New("one of '--license', '--modelfile', '--parameters', '--system', or '--template' must be specified")
|
2023-09-06 18:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := api.ShowRequest{Name: args[0]}
|
|
|
|
resp, err := client.Show(context.Background(), &req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch showType {
|
|
|
|
case "license":
|
|
|
|
fmt.Println(resp.License)
|
|
|
|
case "modelfile":
|
|
|
|
fmt.Println(resp.Modelfile)
|
|
|
|
case "parameters":
|
|
|
|
fmt.Println(resp.Parameters)
|
|
|
|
case "system":
|
|
|
|
fmt.Println(resp.System)
|
|
|
|
case "template":
|
|
|
|
fmt.Println(resp.Template)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:27:28 +00:00
|
|
|
func CopyHandler(cmd *cobra.Command, args []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 15:27:28 +00:00
|
|
|
|
|
|
|
req := api.CopyRequest{Source: args[0], Destination: args[1]}
|
|
|
|
if err := client.Copy(context.Background(), &req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("copied '%s' to '%s'\n", args[0], args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
func PullHandler(cmd *cobra.Command, args []string) error {
|
2023-07-21 22:42:19 +00:00
|
|
|
insecure, err := cmd.Flags().GetBool("insecure")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pull(args[0], insecure)
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
func pull(model string, insecure bool) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 00:02:22 +00:00
|
|
|
|
2023-07-19 01:51:30 +00:00
|
|
|
var currentDigest string
|
2023-07-07 18:26:58 +00:00
|
|
|
var bar *progressbar.ProgressBar
|
2023-07-11 20:05:51 +00:00
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
request := api.PullRequest{Name: model, Insecure: insecure}
|
2023-07-19 01:51:30 +00:00
|
|
|
fn := func(resp api.ProgressResponse) error {
|
|
|
|
if resp.Digest != currentDigest && resp.Digest != "" {
|
|
|
|
currentDigest = resp.Digest
|
2023-07-17 00:02:22 +00:00
|
|
|
bar = progressbar.DefaultBytes(
|
2023-09-28 17:00:34 +00:00
|
|
|
resp.Total,
|
2023-07-19 01:51:30 +00:00
|
|
|
fmt.Sprintf("pulling %s...", resp.Digest[7:19]),
|
2023-07-17 00:02:22 +00:00
|
|
|
)
|
2023-07-19 01:51:30 +00:00
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
bar.Set64(resp.Completed)
|
2023-07-19 01:51:30 +00:00
|
|
|
} else if resp.Digest == currentDigest && resp.Digest != "" {
|
2023-09-28 17:00:34 +00:00
|
|
|
bar.Set64(resp.Completed)
|
2023-07-17 00:02:22 +00:00
|
|
|
} else {
|
2023-07-19 01:51:30 +00:00
|
|
|
currentDigest = ""
|
2023-07-17 00:02:22 +00:00
|
|
|
fmt.Println(resp.Status)
|
|
|
|
}
|
2023-08-14 18:15:53 +00:00
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-07 14:22:37 +00:00
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
if err := client.Pull(context.Background(), &request, fn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-14 18:15:53 +00:00
|
|
|
|
|
|
|
if bar != nil && !bar.IsFinished() {
|
|
|
|
return errors.New("unexpected end to pull model")
|
|
|
|
}
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
return nil
|
2023-07-06 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 01:18:06 +00:00
|
|
|
func RunGenerate(cmd *cobra.Command, args []string) error {
|
2023-07-06 20:49:31 +00:00
|
|
|
if len(args) > 1 {
|
2023-07-12 16:55:07 +00:00
|
|
|
// join all args into a single prompt
|
2023-10-02 18:50:55 +00:00
|
|
|
wordWrap := false
|
|
|
|
if term.IsTerminal(int(os.Stdout.Fd())) {
|
|
|
|
wordWrap = true
|
|
|
|
}
|
|
|
|
|
|
|
|
nowrap, err := cmd.Flags().GetBool("nowordwrap")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nowrap {
|
|
|
|
wordWrap = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return generate(cmd, args[0], strings.Join(args[1:], " "), wordWrap)
|
2023-07-06 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
if readline.IsTerminal(int(os.Stdin.Fd())) {
|
2023-07-13 01:18:06 +00:00
|
|
|
return generateInteractive(cmd, args[0])
|
2023-07-06 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 01:18:06 +00:00
|
|
|
return generateBatch(cmd, args[0])
|
2023-07-06 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:59:42 +00:00
|
|
|
type generateContextKey string
|
2023-07-13 18:02:53 +00:00
|
|
|
|
2023-10-02 18:50:55 +00:00
|
|
|
func generate(cmd *cobra.Command, model, prompt string, wordWrap bool) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-09-18 19:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-07 17:12:58 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
spinner := NewSpinner("")
|
|
|
|
go spinner.Spin(60 * time.Millisecond)
|
2023-07-06 22:43:04 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
var latest api.GenerateResponse
|
2023-07-13 01:18:06 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
generateContext, ok := cmd.Context().Value(generateContextKey("context")).([]int)
|
|
|
|
if !ok {
|
|
|
|
generateContext = []int{}
|
|
|
|
}
|
2023-07-13 18:02:53 +00:00
|
|
|
|
2023-09-22 20:36:08 +00:00
|
|
|
termWidth, _, err := term.GetSize(int(0))
|
|
|
|
if err != nil {
|
2023-10-02 18:50:55 +00:00
|
|
|
wordWrap = false
|
2023-09-22 20:36:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 00:13:01 +00:00
|
|
|
cancelCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT)
|
|
|
|
var abort bool
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
|
|
|
cancel()
|
|
|
|
abort = true
|
|
|
|
}()
|
|
|
|
|
2023-09-22 20:36:08 +00:00
|
|
|
var currentLineLength int
|
|
|
|
var wordBuffer string
|
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
request := api.GenerateRequest{Model: model, Prompt: prompt, Context: generateContext}
|
|
|
|
fn := func(response api.GenerateResponse) error {
|
|
|
|
if !spinner.IsFinished() {
|
|
|
|
spinner.Finish()
|
|
|
|
}
|
2023-07-06 22:43:04 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
latest = response
|
2023-07-13 01:18:06 +00:00
|
|
|
|
2023-10-02 18:50:55 +00:00
|
|
|
if wordWrap {
|
2023-09-22 20:36:08 +00:00
|
|
|
for _, ch := range response.Response {
|
|
|
|
if currentLineLength+1 > termWidth-5 {
|
|
|
|
// backtrack the length of the last word and clear to the end of the line
|
|
|
|
fmt.Printf("\x1b[%dD\x1b[K\n", len(wordBuffer))
|
|
|
|
fmt.Printf("%s%c", wordBuffer, ch)
|
|
|
|
currentLineLength = len(wordBuffer) + 1
|
|
|
|
} else {
|
|
|
|
fmt.Print(string(ch))
|
|
|
|
currentLineLength += 1
|
|
|
|
|
|
|
|
switch ch {
|
|
|
|
case ' ':
|
|
|
|
wordBuffer = ""
|
|
|
|
case '\n':
|
|
|
|
currentLineLength = 0
|
|
|
|
default:
|
|
|
|
wordBuffer += string(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Print(response.Response)
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-07 21:04:43 +00:00
|
|
|
|
2023-09-29 00:13:01 +00:00
|
|
|
if err := client.Generate(cancelCtx, &request, fn); err != nil {
|
2023-10-12 15:16:37 +00:00
|
|
|
if strings.Contains(err.Error(), "context canceled") && abort {
|
2023-09-29 00:13:01 +00:00
|
|
|
spinner.Finish()
|
|
|
|
return nil
|
2023-07-07 21:04:43 +00:00
|
|
|
}
|
2023-09-18 19:26:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if prompt != "" {
|
2023-07-07 17:12:58 +00:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println()
|
2023-09-18 19:26:56 +00:00
|
|
|
}
|
2023-07-13 01:18:06 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
if !latest.Done {
|
2023-09-29 00:13:01 +00:00
|
|
|
if abort {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-18 19:26:56 +00:00
|
|
|
return errors.New("unexpected end of response")
|
|
|
|
}
|
2023-07-13 01:18:06 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
verbose, err := cmd.Flags().GetBool("verbose")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-18 18:59:42 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
if verbose {
|
|
|
|
latest.Summary()
|
2023-07-07 17:12:58 +00:00
|
|
|
}
|
2023-07-06 20:49:31 +00:00
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
ctx = context.WithValue(ctx, generateContextKey("context"), latest.Context)
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
|
2023-07-06 20:49:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-13 01:18:06 +00:00
|
|
|
func generateInteractive(cmd *cobra.Command, model string) error {
|
2023-07-18 21:01:19 +00:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
// load the model
|
2023-10-02 18:50:55 +00:00
|
|
|
if err := generate(cmd, model, "", false); err != nil {
|
2023-09-18 19:26:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
completer := readline.NewPrefixCompleter(
|
|
|
|
readline.PcItem("/help"),
|
|
|
|
readline.PcItem("/list"),
|
|
|
|
readline.PcItem("/set",
|
|
|
|
readline.PcItem("history"),
|
|
|
|
readline.PcItem("nohistory"),
|
2023-09-22 20:36:08 +00:00
|
|
|
readline.PcItem("wordwrap"),
|
|
|
|
readline.PcItem("nowordwrap"),
|
2023-07-19 16:25:23 +00:00
|
|
|
readline.PcItem("verbose"),
|
|
|
|
readline.PcItem("quiet"),
|
2023-07-18 21:01:19 +00:00
|
|
|
),
|
2023-07-27 23:58:40 +00:00
|
|
|
readline.PcItem("/show",
|
|
|
|
readline.PcItem("license"),
|
2023-09-06 18:04:17 +00:00
|
|
|
readline.PcItem("modelfile"),
|
|
|
|
readline.PcItem("parameters"),
|
2023-07-27 23:58:40 +00:00
|
|
|
readline.PcItem("system"),
|
|
|
|
readline.PcItem("template"),
|
|
|
|
),
|
2023-07-18 21:01:19 +00:00
|
|
|
readline.PcItem("/exit"),
|
|
|
|
readline.PcItem("/bye"),
|
|
|
|
)
|
|
|
|
|
|
|
|
usage := func() {
|
2023-10-05 18:36:07 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Available Commands:")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set Set session variables")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show Show model information")
|
|
|
|
fmt.Fprintln(os.Stderr, " /bye Exit")
|
|
|
|
fmt.Fprintln(os.Stderr, " /?, /help Help for a command")
|
|
|
|
fmt.Fprintln(os.Stderr, "")
|
|
|
|
fmt.Fprintln(os.Stderr, "Use \"\"\" to begin a multi-line message.")
|
|
|
|
fmt.Fprintln(os.Stderr, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
usageSet := func() {
|
|
|
|
fmt.Fprintln(os.Stderr, "Available Commands:")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set history Enable history")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set nohistory Disable history")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set wordwrap Enable wordwrap")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set nowordwrap Disable wordwrap")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set verbose Show LLM stats")
|
|
|
|
fmt.Fprintln(os.Stderr, " /set quiet Disable LLM stats")
|
|
|
|
fmt.Fprintln(os.Stderr, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
usageShow := func() {
|
|
|
|
fmt.Fprintln(os.Stderr, "Available Commands:")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show license Show model license")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show modelfile Show Modelfile for this model")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show parameters Show parameters for this model")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show system Show system prompt")
|
|
|
|
fmt.Fprintln(os.Stderr, " /show template Show prompt template")
|
|
|
|
fmt.Fprintln(os.Stderr, "")
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 03:49:35 +00:00
|
|
|
var painter Painter
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
config := readline.Config{
|
2023-09-23 03:49:35 +00:00
|
|
|
Painter: &painter,
|
2023-07-18 21:01:19 +00:00
|
|
|
Prompt: ">>> ",
|
|
|
|
HistoryFile: filepath.Join(home, ".ollama", "history"),
|
|
|
|
AutoComplete: completer,
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner, err := readline.NewEx(&config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer scanner.Close()
|
|
|
|
|
2023-10-02 18:50:55 +00:00
|
|
|
var wordWrap bool
|
|
|
|
termType := os.Getenv("TERM")
|
|
|
|
if termType == "xterm-256color" {
|
|
|
|
wordWrap = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// override wrapping if the user turned it off
|
|
|
|
nowrap, err := cmd.Flags().GetBool("nowordwrap")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nowrap {
|
|
|
|
wordWrap = false
|
|
|
|
}
|
|
|
|
|
2023-07-29 20:35:23 +00:00
|
|
|
var multiLineBuffer string
|
|
|
|
var isMultiLine bool
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
for {
|
|
|
|
line, err := scanner.Readline()
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, io.EOF):
|
|
|
|
return nil
|
|
|
|
case errors.Is(err, readline.ErrInterrupt):
|
2023-07-20 07:53:08 +00:00
|
|
|
if line == "" {
|
2023-09-29 00:13:01 +00:00
|
|
|
fmt.Println("Use Ctrl-D or /bye to exit.")
|
2023-07-20 07:53:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
continue
|
|
|
|
case err != nil:
|
2023-07-06 20:49:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
line = strings.TrimSpace(line)
|
2023-07-06 20:49:31 +00:00
|
|
|
|
2023-07-18 21:01:19 +00:00
|
|
|
switch {
|
2023-07-29 20:35:23 +00:00
|
|
|
case isMultiLine:
|
|
|
|
if strings.HasSuffix(line, `"""`) {
|
|
|
|
isMultiLine = false
|
2023-09-24 00:20:30 +00:00
|
|
|
painter.IsMultiLine = isMultiLine
|
2023-07-29 20:35:23 +00:00
|
|
|
multiLineBuffer += strings.TrimSuffix(line, `"""`)
|
2023-09-23 04:27:24 +00:00
|
|
|
line = multiLineBuffer
|
2023-07-29 20:35:23 +00:00
|
|
|
multiLineBuffer = ""
|
|
|
|
scanner.SetPrompt(">>> ")
|
|
|
|
} else {
|
|
|
|
multiLineBuffer += line + " "
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(line, `"""`):
|
|
|
|
isMultiLine = true
|
2023-09-24 00:20:30 +00:00
|
|
|
painter.IsMultiLine = isMultiLine
|
2023-07-29 20:35:23 +00:00
|
|
|
multiLineBuffer = strings.TrimPrefix(line, `"""`) + " "
|
|
|
|
scanner.SetPrompt("... ")
|
|
|
|
continue
|
2023-07-18 21:01:19 +00:00
|
|
|
case strings.HasPrefix(line, "/list"):
|
|
|
|
args := strings.Fields(line)
|
2023-07-20 23:09:23 +00:00
|
|
|
if err := ListHandler(cmd, args[1:]); err != nil {
|
2023-07-18 21:01:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(line, "/set"):
|
|
|
|
args := strings.Fields(line)
|
|
|
|
if len(args) > 1 {
|
|
|
|
switch args[1] {
|
|
|
|
case "history":
|
|
|
|
scanner.HistoryEnable()
|
|
|
|
case "nohistory":
|
|
|
|
scanner.HistoryDisable()
|
2023-09-22 20:36:08 +00:00
|
|
|
case "wordwrap":
|
2023-10-02 18:50:55 +00:00
|
|
|
wordWrap = true
|
2023-09-22 20:36:08 +00:00
|
|
|
fmt.Println("Set 'wordwrap' mode.")
|
|
|
|
case "nowordwrap":
|
2023-10-02 18:50:55 +00:00
|
|
|
wordWrap = false
|
2023-09-22 20:36:08 +00:00
|
|
|
fmt.Println("Set 'nowordwrap' mode.")
|
2023-07-19 16:25:23 +00:00
|
|
|
case "verbose":
|
|
|
|
cmd.Flags().Set("verbose", "true")
|
2023-09-22 20:36:08 +00:00
|
|
|
fmt.Println("Set 'verbose' mode.")
|
2023-07-19 16:25:23 +00:00
|
|
|
case "quiet":
|
|
|
|
cmd.Flags().Set("verbose", "false")
|
2023-09-22 20:36:08 +00:00
|
|
|
fmt.Println("Set 'quiet' mode.")
|
2023-07-18 21:01:19 +00:00
|
|
|
case "mode":
|
|
|
|
if len(args) > 2 {
|
|
|
|
switch args[2] {
|
|
|
|
case "vim":
|
|
|
|
scanner.SetVimMode(true)
|
|
|
|
case "emacs", "default":
|
|
|
|
scanner.SetVimMode(false)
|
2023-07-27 23:58:40 +00:00
|
|
|
default:
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage()
|
|
|
|
}
|
2023-09-28 21:19:45 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])
|
2023-07-27 23:58:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-05 18:36:07 +00:00
|
|
|
usageSet()
|
2023-07-27 23:58:40 +00:00
|
|
|
}
|
|
|
|
case strings.HasPrefix(line, "/show"):
|
|
|
|
args := strings.Fields(line)
|
|
|
|
if len(args) > 1 {
|
2023-10-16 21:27:25 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error: couldn't connect to ollama server")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := client.Show(cmd.Context(), &api.ShowRequest{Name: model})
|
2023-08-22 04:56:56 +00:00
|
|
|
if err != nil {
|
2023-09-06 18:04:17 +00:00
|
|
|
fmt.Println("error: couldn't get model")
|
2023-09-28 21:25:22 +00:00
|
|
|
return err
|
2023-07-27 23:58:40 +00:00
|
|
|
}
|
2023-09-06 18:04:17 +00:00
|
|
|
|
2023-07-27 23:58:40 +00:00
|
|
|
switch args[1] {
|
|
|
|
case "license":
|
2023-10-02 21:34:52 +00:00
|
|
|
if resp.License == "" {
|
2023-10-05 15:11:04 +00:00
|
|
|
fmt.Print("No license was specified for this model.\n\n")
|
2023-10-02 21:34:52 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(resp.License)
|
|
|
|
}
|
2023-09-06 18:04:17 +00:00
|
|
|
case "modelfile":
|
|
|
|
fmt.Println(resp.Modelfile)
|
|
|
|
case "parameters":
|
2023-10-02 21:34:52 +00:00
|
|
|
if resp.Parameters == "" {
|
2023-10-05 15:11:04 +00:00
|
|
|
fmt.Print("No parameters were specified for this model.\n\n")
|
2023-10-02 21:34:52 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(resp.Parameters)
|
|
|
|
}
|
2023-07-27 23:58:40 +00:00
|
|
|
case "system":
|
2023-10-02 21:34:52 +00:00
|
|
|
if resp.System == "" {
|
2023-10-05 15:11:04 +00:00
|
|
|
fmt.Print("No system prompt was specified for this model.\n\n")
|
2023-10-02 21:34:52 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(resp.System)
|
|
|
|
}
|
2023-07-27 23:58:40 +00:00
|
|
|
case "template":
|
2023-10-02 21:34:52 +00:00
|
|
|
if resp.Template == "" {
|
2023-10-05 15:11:04 +00:00
|
|
|
fmt.Print("No prompt template was specified for this model.\n\n")
|
2023-10-02 21:34:52 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(resp.Template)
|
|
|
|
}
|
2023-07-27 23:58:40 +00:00
|
|
|
default:
|
2023-09-28 21:19:45 +00:00
|
|
|
fmt.Printf("Unknown command '/show %s'. Type /? for help\n", args[1])
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
2023-10-05 18:36:07 +00:00
|
|
|
} else {
|
|
|
|
usageShow()
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(line, "/help"), strings.HasPrefix(line, "/?"):
|
|
|
|
args := strings.Fields(line)
|
|
|
|
if len(args) > 1 {
|
|
|
|
switch args[1] {
|
|
|
|
case "set", "/set":
|
|
|
|
usageSet()
|
|
|
|
case "show", "/show":
|
|
|
|
usageShow()
|
|
|
|
}
|
2023-07-27 23:58:40 +00:00
|
|
|
} else {
|
|
|
|
usage()
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
|
|
|
case line == "/exit", line == "/bye":
|
|
|
|
return nil
|
2023-09-22 20:36:08 +00:00
|
|
|
case strings.HasPrefix(line, "/"):
|
|
|
|
args := strings.Fields(line)
|
|
|
|
fmt.Printf("Unknown command '%s'. Type /? for help\n", args[0])
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 19:26:56 +00:00
|
|
|
if len(line) > 0 && line[0] != '/' {
|
2023-10-02 18:50:55 +00:00
|
|
|
if err := generate(cmd, model, line, wordWrap); err != nil {
|
2023-09-18 19:26:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-07-18 21:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-06 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 01:18:06 +00:00
|
|
|
func generateBatch(cmd *cobra.Command, model string) error {
|
2023-07-06 20:49:31 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
prompt := scanner.Text()
|
|
|
|
fmt.Printf(">>> %s\n", prompt)
|
2023-10-02 18:50:55 +00:00
|
|
|
if err := generate(cmd, model, prompt, false); err != nil {
|
2023-07-06 20:49:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 21:17:53 +00:00
|
|
|
func RunServer(cmd *cobra.Command, _ []string) error {
|
2023-09-21 00:49:48 +00:00
|
|
|
host, port, err := net.SplitHostPort(os.Getenv("OLLAMA_HOST"))
|
|
|
|
if err != nil {
|
|
|
|
host, port = "127.0.0.1", "11434"
|
2023-09-22 17:40:54 +00:00
|
|
|
if ip := net.ParseIP(strings.Trim(os.Getenv("OLLAMA_HOST"), "[]")); ip != nil {
|
2023-09-21 00:49:48 +00:00
|
|
|
host = ip.String()
|
|
|
|
}
|
2023-07-04 04:47:00 +00:00
|
|
|
}
|
2023-08-07 19:55:20 +00:00
|
|
|
|
2023-09-21 00:49:48 +00:00
|
|
|
if err := initializeKeypair(); err != nil {
|
2023-08-11 17:58:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-21 00:49:48 +00:00
|
|
|
ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
|
2023-08-07 03:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-04 04:47:00 +00:00
|
|
|
|
2023-08-10 21:17:53 +00:00
|
|
|
var origins []string
|
|
|
|
if o := os.Getenv("OLLAMA_ORIGINS"); o != "" {
|
|
|
|
origins = strings.Split(o, ",")
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:46:35 +00:00
|
|
|
if noprune := os.Getenv("OLLAMA_NOPRUNE"); noprune == "" {
|
|
|
|
if err := server.PruneLayers(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-27 00:28:14 +00:00
|
|
|
|
|
|
|
manifestsPath, err := server.GetManifestPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.PruneDirectory(manifestsPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 18:46:35 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:27:03 +00:00
|
|
|
return server.Serve(ln, origins)
|
2023-07-04 04:47:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 17:58:23 +00:00
|
|
|
func initializeKeypair() error {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
|
|
|
|
pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")
|
|
|
|
|
|
|
|
_, err = os.Stat(privKeyPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("Couldn't find '%s'. Generating new private key.\n", privKeyPath)
|
|
|
|
_, privKey, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKeyBytes, err := format.OpenSSHPrivateKey(privKey, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:36:30 +00:00
|
|
|
err = os.MkdirAll(filepath.Dir(privKeyPath), 0o755)
|
2023-08-11 22:35:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create directory %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-22 04:56:56 +00:00
|
|
|
err = os.WriteFile(privKeyPath, pem.EncodeToMemory(privKeyBytes), 0o600)
|
2023-08-11 17:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sshPrivateKey, err := ssh.NewSignerFromKey(privKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKeyData := ssh.MarshalAuthorizedKey(sshPrivateKey.PublicKey())
|
|
|
|
|
2023-08-22 04:56:56 +00:00
|
|
|
err = os.WriteFile(pubKeyPath, pubKeyData, 0o644)
|
2023-08-11 17:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Your new public key is: \n\n%s\n", string(pubKeyData))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:38:10 +00:00
|
|
|
func startMacApp(client *api.Client) error {
|
2023-08-01 20:01:55 +00:00
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
link, err := os.Readlink(exe)
|
2023-07-31 21:38:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-01 17:48:48 +00:00
|
|
|
if !strings.Contains(link, "Ollama.app") {
|
|
|
|
return fmt.Errorf("could not find ollama app")
|
|
|
|
}
|
2023-07-31 21:38:10 +00:00
|
|
|
path := strings.Split(link, "Ollama.app")
|
|
|
|
if err := exec.Command("/usr/bin/open", "-a", path[0]+"Ollama.app").Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// wait for the server to start
|
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
tick := time.Tick(500 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timed out waiting for server to start")
|
|
|
|
case <-tick:
|
|
|
|
if err := client.Heartbeat(context.Background()); err == nil {
|
|
|
|
return nil // server has started
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 20:25:57 +00:00
|
|
|
func checkServerHeartbeat(_ *cobra.Command, _ []string) error {
|
2023-10-09 19:18:26 +00:00
|
|
|
client, err := api.ClientFromEnvironment()
|
2023-08-16 15:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-31 20:25:57 +00:00
|
|
|
if err := client.Heartbeat(context.Background()); err != nil {
|
2023-07-31 21:38:10 +00:00
|
|
|
if !strings.Contains(err.Error(), "connection refused") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
if err := startMacApp(client); err != nil {
|
2023-08-01 17:48:48 +00:00
|
|
|
return fmt.Errorf("could not connect to ollama app, is it running?")
|
2023-07-31 20:25:57 +00:00
|
|
|
}
|
2023-07-31 21:38:10 +00:00
|
|
|
} else {
|
2023-07-31 20:25:57 +00:00
|
|
|
return fmt.Errorf("could not connect to ollama server, run 'ollama serve' to start it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 19:22:44 +00:00
|
|
|
func NewCLI() *cobra.Command {
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
|
|
rootCmd := &cobra.Command{
|
2023-08-14 18:15:53 +00:00
|
|
|
Use: "ollama",
|
|
|
|
Short: "Large language model runner",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
2023-07-03 19:22:44 +00:00
|
|
|
CompletionOptions: cobra.CompletionOptions{
|
|
|
|
DisableDefaultCmd: true,
|
|
|
|
},
|
2023-08-22 01:24:42 +00:00
|
|
|
Version: version.Version,
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cobra.EnableCommandSorting = false
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
createCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "create MODEL",
|
|
|
|
Short: "Create a model from a Modelfile",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: CreateHandler,
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createCmd.Flags().StringP("file", "f", "Modelfile", "Name of the Modelfile (default \"Modelfile\")")
|
|
|
|
|
2023-09-06 18:04:17 +00:00
|
|
|
showCmd := &cobra.Command{
|
|
|
|
Use: "show MODEL",
|
|
|
|
Short: "Show information for a model",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: ShowHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
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("template", false, "Show template of a model")
|
|
|
|
showCmd.Flags().Bool("system", false, "Show system prompt of a model")
|
|
|
|
|
2023-07-03 19:22:44 +00:00
|
|
|
runCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "run MODEL [PROMPT]",
|
|
|
|
Short: "Run a model",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: RunHandler,
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 01:18:06 +00:00
|
|
|
runCmd.Flags().Bool("verbose", false, "Show timings for response")
|
2023-08-22 04:56:56 +00:00
|
|
|
runCmd.Flags().Bool("insecure", false, "Use an insecure registry")
|
2023-09-22 20:36:08 +00:00
|
|
|
runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically")
|
2023-07-13 01:18:06 +00:00
|
|
|
|
2023-07-03 19:22:44 +00:00
|
|
|
serveCmd := &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Aliases: []string{"start"},
|
|
|
|
Short: "Start ollama",
|
2023-07-06 20:49:31 +00:00
|
|
|
RunE: RunServer,
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
pullCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "pull MODEL",
|
|
|
|
Short: "Pull a model from a registry",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: PullHandler,
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
pullCmd.Flags().Bool("insecure", false, "Use an insecure registry")
|
|
|
|
|
2023-07-17 00:02:22 +00:00
|
|
|
pushCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "push MODEL",
|
|
|
|
Short: "Push a model to a registry",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: PushHandler,
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:42:19 +00:00
|
|
|
pushCmd.Flags().Bool("insecure", false, "Use an insecure registry")
|
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
listCmd := &cobra.Command{
|
2023-07-21 22:42:19 +00:00
|
|
|
Use: "list",
|
2023-07-20 22:28:27 +00:00
|
|
|
Aliases: []string{"ls"},
|
2023-07-21 22:42:19 +00:00
|
|
|
Short: "List models",
|
2023-07-31 20:25:57 +00:00
|
|
|
PreRunE: checkServerHeartbeat,
|
2023-07-21 22:42:19 +00:00
|
|
|
RunE: ListHandler,
|
2023-07-20 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 15:27:28 +00:00
|
|
|
copyCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "cp",
|
|
|
|
Short: "Copy a model",
|
|
|
|
Args: cobra.MinimumNArgs(2),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: CopyHandler,
|
2023-07-24 15:27:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:09:23 +00:00
|
|
|
deleteCmd := &cobra.Command{
|
2023-07-31 20:25:57 +00:00
|
|
|
Use: "rm",
|
|
|
|
Short: "Remove a model",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
PreRunE: checkServerHeartbeat,
|
|
|
|
RunE: DeleteHandler,
|
2023-07-18 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 19:22:44 +00:00
|
|
|
rootCmd.AddCommand(
|
|
|
|
serveCmd,
|
2023-07-17 00:02:22 +00:00
|
|
|
createCmd,
|
2023-09-06 18:04:17 +00:00
|
|
|
showCmd,
|
2023-07-03 21:14:20 +00:00
|
|
|
runCmd,
|
2023-07-17 00:02:22 +00:00
|
|
|
pullCmd,
|
|
|
|
pushCmd,
|
2023-07-18 16:09:45 +00:00
|
|
|
listCmd,
|
2023-07-24 15:27:28 +00:00
|
|
|
copyCmd,
|
2023-07-20 23:09:23 +00:00
|
|
|
deleteCmd,
|
2023-07-03 19:22:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return rootCmd
|
|
|
|
}
|