123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659 |
- package cmd
- import (
- "errors"
- "fmt"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "regexp"
- "slices"
- "sort"
- "strings"
- "github.com/spf13/cobra"
- "github.com/ollama/ollama/api"
- "github.com/ollama/ollama/envconfig"
- "github.com/ollama/ollama/progress"
- "github.com/ollama/ollama/readline"
- "github.com/ollama/ollama/types/errtypes"
- )
- type MultilineState int
- const (
- MultilineNone MultilineState = iota
- MultilinePrompt
- MultilineSystem
- MultilineTemplate
- )
- func loadModel(cmd *cobra.Command, opts *runOptions) error {
- p := progress.NewProgress(os.Stderr)
- defer p.StopAndClear()
- spinner := progress.NewSpinner("")
- p.Add("", spinner)
- client, err := api.ClientFromEnvironment()
- if err != nil {
- return err
- }
- chatReq := &api.ChatRequest{
- Model: opts.Model,
- KeepAlive: opts.KeepAlive,
- }
- return client.Chat(cmd.Context(), chatReq, func(resp api.ChatResponse) error {
- p.StopAndClear()
- for _, msg := range opts.Messages {
- switch msg.Role {
- case "user":
- fmt.Printf(">>> %s\n", msg.Content)
- case "assistant":
- state := &displayResponseState{}
- displayResponse(msg.Content, opts.WordWrap, state)
- fmt.Println()
- fmt.Println()
- }
- }
- return nil
- })
- }
- func generateInteractive(cmd *cobra.Command, opts runOptions) error {
- err := loadModel(cmd, &opts)
- if err != nil {
- return err
- }
- usage := func() {
- 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, " /load <model> Load a session or model")
- fmt.Fprintln(os.Stderr, " /save <model> Save your current session")
- fmt.Fprintln(os.Stderr, " /clear Clear session context")
- fmt.Fprintln(os.Stderr, " /bye Exit")
- fmt.Fprintln(os.Stderr, " /?, /help Help for a command")
- fmt.Fprintln(os.Stderr, " /? shortcuts Help for keyboard shortcuts")
- fmt.Fprintln(os.Stderr, "")
- fmt.Fprintln(os.Stderr, "Use \"\"\" to begin a multi-line message.")
- if opts.MultiModal {
- fmt.Fprintf(os.Stderr, "Use %s to include .jpg or .png images.\n", filepath.FromSlash("/path/to/file"))
- }
- fmt.Fprintln(os.Stderr, "")
- }
- usageSet := func() {
- fmt.Fprintln(os.Stderr, "Available Commands:")
- fmt.Fprintln(os.Stderr, " /set parameter ... Set a parameter")
- fmt.Fprintln(os.Stderr, " /set system <string> Set system message")
- fmt.Fprintln(os.Stderr, " /set template <string> Set prompt template")
- 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 format json Enable JSON mode")
- fmt.Fprintln(os.Stderr, " /set noformat Disable formatting")
- fmt.Fprintln(os.Stderr, " /set verbose Show LLM stats")
- fmt.Fprintln(os.Stderr, " /set quiet Disable LLM stats")
- fmt.Fprintln(os.Stderr, "")
- }
- usageShortcuts := func() {
- fmt.Fprintln(os.Stderr, "Available keyboard shortcuts:")
- fmt.Fprintln(os.Stderr, " Ctrl + a Move to the beginning of the line (Home)")
- fmt.Fprintln(os.Stderr, " Ctrl + e Move to the end of the line (End)")
- fmt.Fprintln(os.Stderr, " Alt + b Move back (left) one word")
- fmt.Fprintln(os.Stderr, " Alt + f Move forward (right) one word")
- fmt.Fprintln(os.Stderr, " Ctrl + k Delete the sentence after the cursor")
- fmt.Fprintln(os.Stderr, " Ctrl + u Delete the sentence before the cursor")
- fmt.Fprintln(os.Stderr, " Ctrl + w Delete the word before the cursor")
- fmt.Fprintln(os.Stderr, "")
- fmt.Fprintln(os.Stderr, " Ctrl + l Clear the screen")
- fmt.Fprintln(os.Stderr, " Ctrl + c Stop the model from responding")
- fmt.Fprintln(os.Stderr, " Ctrl + d Exit ollama (/bye)")
- fmt.Fprintln(os.Stderr, "")
- }
- usageShow := func() {
- fmt.Fprintln(os.Stderr, "Available Commands:")
- fmt.Fprintln(os.Stderr, " /show info Show details for this model")
- 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 message")
- fmt.Fprintln(os.Stderr, " /show template Show prompt template")
- fmt.Fprintln(os.Stderr, "")
- }
- // only list out the most common parameters
- usageParameters := func() {
- fmt.Fprintln(os.Stderr, "Available Parameters:")
- fmt.Fprintln(os.Stderr, " /set parameter seed <int> Random number seed")
- fmt.Fprintln(os.Stderr, " /set parameter num_predict <int> Max number of tokens to predict")
- fmt.Fprintln(os.Stderr, " /set parameter top_k <int> Pick from top k num of tokens")
- fmt.Fprintln(os.Stderr, " /set parameter top_p <float> Pick token based on sum of probabilities")
- fmt.Fprintln(os.Stderr, " /set parameter num_ctx <int> Set the context size")
- fmt.Fprintln(os.Stderr, " /set parameter temperature <float> Set creativity level")
- fmt.Fprintln(os.Stderr, " /set parameter repeat_penalty <float> How strongly to penalize repetitions")
- fmt.Fprintln(os.Stderr, " /set parameter repeat_last_n <int> Set how far back to look for repetitions")
- fmt.Fprintln(os.Stderr, " /set parameter num_gpu <int> The number of layers to send to the GPU")
- fmt.Fprintln(os.Stderr, " /set parameter stop <string> <string> ... Set the stop parameters")
- fmt.Fprintln(os.Stderr, "")
- }
- scanner, err := readline.New(readline.Prompt{
- Prompt: ">>> ",
- AltPrompt: "... ",
- Placeholder: "Send a message (/? for help)",
- AltPlaceholder: `Use """ to end multi-line input`,
- })
- if err != nil {
- return err
- }
- if envconfig.NoHistory {
- scanner.HistoryDisable()
- }
- fmt.Print(readline.StartBracketedPaste)
- defer fmt.Printf(readline.EndBracketedPaste)
- var sb strings.Builder
- var multiline MultilineState
- for {
- line, err := scanner.Readline()
- switch {
- case errors.Is(err, io.EOF):
- fmt.Println()
- return nil
- case errors.Is(err, readline.ErrInterrupt):
- if line == "" {
- fmt.Println("\nUse Ctrl + d or /bye to exit.")
- }
- scanner.Prompt.UseAlt = false
- sb.Reset()
- continue
- case err != nil:
- return err
- }
- switch {
- case multiline != MultilineNone:
- // check if there's a multiline terminating string
- before, ok := strings.CutSuffix(line, `"""`)
- sb.WriteString(before)
- if !ok {
- fmt.Fprintln(&sb)
- continue
- }
- switch multiline {
- case MultilineSystem:
- opts.System = sb.String()
- opts.Messages = append(opts.Messages, api.Message{Role: "system", Content: opts.System})
- fmt.Println("Set system message.")
- sb.Reset()
- case MultilineTemplate:
- opts.Template = sb.String()
- fmt.Println("Set prompt template.")
- sb.Reset()
- }
- multiline = MultilineNone
- scanner.Prompt.UseAlt = false
- case strings.HasPrefix(line, `"""`):
- line := strings.TrimPrefix(line, `"""`)
- line, ok := strings.CutSuffix(line, `"""`)
- sb.WriteString(line)
- if !ok {
- // no multiline terminating string; need more input
- fmt.Fprintln(&sb)
- multiline = MultilinePrompt
- scanner.Prompt.UseAlt = true
- }
- case scanner.Pasting:
- fmt.Fprintln(&sb, line)
- continue
- case strings.HasPrefix(line, "/list"):
- args := strings.Fields(line)
- if err := ListHandler(cmd, args[1:]); err != nil {
- return err
- }
- case strings.HasPrefix(line, "/load"):
- args := strings.Fields(line)
- if len(args) != 2 {
- fmt.Println("Usage:\n /load <modelname>")
- continue
- }
- opts.Model = args[1]
- opts.Messages = []api.Message{}
- fmt.Printf("Loading model '%s'\n", opts.Model)
- if err := loadModel(cmd, &opts); err != nil {
- return err
- }
- continue
- case strings.HasPrefix(line, "/save"):
- args := strings.Fields(line)
- if len(args) != 2 {
- fmt.Println("Usage:\n /save <modelname>")
- continue
- }
- client, err := api.ClientFromEnvironment()
- if err != nil {
- fmt.Println("error: couldn't connect to ollama server")
- return err
- }
- req := &api.CreateRequest{
- Name: args[1],
- Modelfile: buildModelfile(opts),
- }
- fn := func(resp api.ProgressResponse) error { return nil }
- err = client.Create(cmd.Context(), req, fn)
- if err != nil {
- if strings.Contains(err.Error(), errtypes.InvalidModelNameErrMsg) {
- fmt.Printf("error: The model name '%s' is invalid\n", args[1])
- continue
- }
- return err
- }
- fmt.Printf("Created new model '%s'\n", args[1])
- continue
- case strings.HasPrefix(line, "/clear"):
- opts.Messages = []api.Message{}
- if opts.System != "" {
- newMessage := api.Message{Role: "system", Content: opts.System}
- opts.Messages = append(opts.Messages, newMessage)
- }
- fmt.Println("Cleared session context")
- continue
- case strings.HasPrefix(line, "/set"):
- args := strings.Fields(line)
- if len(args) > 1 {
- switch args[1] {
- case "history":
- scanner.HistoryEnable()
- case "nohistory":
- scanner.HistoryDisable()
- case "wordwrap":
- opts.WordWrap = true
- fmt.Println("Set 'wordwrap' mode.")
- case "nowordwrap":
- opts.WordWrap = false
- fmt.Println("Set 'nowordwrap' mode.")
- case "verbose":
- if err := cmd.Flags().Set("verbose", "true"); err != nil {
- return err
- }
- fmt.Println("Set 'verbose' mode.")
- case "quiet":
- if err := cmd.Flags().Set("verbose", "false"); err != nil {
- return err
- }
- fmt.Println("Set 'quiet' mode.")
- case "format":
- if len(args) < 3 || args[2] != "json" {
- fmt.Println("Invalid or missing format. For 'json' mode use '/set format json'")
- } else {
- opts.Format = args[2]
- fmt.Printf("Set format to '%s' mode.\n", args[2])
- }
- case "noformat":
- opts.Format = ""
- fmt.Println("Disabled format.")
- case "parameter":
- if len(args) < 4 {
- usageParameters()
- continue
- }
- params := args[3:]
- fp, err := api.FormatParams(map[string][]string{args[2]: params})
- if err != nil {
- fmt.Printf("Couldn't set parameter: %q\n", err)
- continue
- }
- fmt.Printf("Set parameter '%s' to '%s'\n", args[2], strings.Join(params, ", "))
- opts.Options[args[2]] = fp[args[2]]
- case "system", "template":
- if len(args) < 3 {
- usageSet()
- continue
- }
- if args[1] == "system" {
- multiline = MultilineSystem
- } else if args[1] == "template" {
- multiline = MultilineTemplate
- }
- line := strings.Join(args[2:], " ")
- line, ok := strings.CutPrefix(line, `"""`)
- if !ok {
- multiline = MultilineNone
- } else {
- // only cut suffix if the line is multiline
- line, ok = strings.CutSuffix(line, `"""`)
- if ok {
- multiline = MultilineNone
- }
- }
- sb.WriteString(line)
- if multiline != MultilineNone {
- scanner.Prompt.UseAlt = true
- continue
- }
- if args[1] == "system" {
- opts.System = sb.String() // for display in modelfile
- newMessage := api.Message{Role: "system", Content: sb.String()}
- // Check if the slice is not empty and the last message is from 'system'
- if len(opts.Messages) > 0 && opts.Messages[len(opts.Messages)-1].Role == "system" {
- // Replace the last message
- opts.Messages[len(opts.Messages)-1] = newMessage
- } else {
- opts.Messages = append(opts.Messages, newMessage)
- }
- fmt.Println("Set system message.")
- sb.Reset()
- } else if args[1] == "template" {
- opts.Template = sb.String()
- fmt.Println("Set prompt template.")
- sb.Reset()
- }
- sb.Reset()
- continue
- default:
- fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])
- }
- } else {
- usageSet()
- }
- case strings.HasPrefix(line, "/show"):
- args := strings.Fields(line)
- if len(args) > 1 {
- client, err := api.ClientFromEnvironment()
- if err != nil {
- fmt.Println("error: couldn't connect to ollama server")
- return err
- }
- req := &api.ShowRequest{
- Name: opts.Model,
- System: opts.System,
- Template: opts.Template,
- Options: opts.Options,
- }
- resp, err := client.Show(cmd.Context(), req)
- if err != nil {
- fmt.Println("error: couldn't get model")
- return err
- }
- switch args[1] {
- case "info":
- showInfo(resp)
- case "license":
- if resp.License == "" {
- fmt.Println("No license was specified for this model.")
- } else {
- fmt.Println(resp.License)
- }
- case "modelfile":
- fmt.Println(resp.Modelfile)
- case "parameters":
- if resp.Parameters == "" {
- fmt.Println("No parameters were specified for this model.")
- } else {
- if len(opts.Options) > 0 {
- fmt.Println("User defined parameters:")
- for k, v := range opts.Options {
- fmt.Printf("%-*s %v\n", 30, k, v)
- }
- fmt.Println()
- }
- fmt.Println("Model defined parameters:")
- fmt.Println(resp.Parameters)
- }
- case "system":
- switch {
- case opts.System != "":
- fmt.Println(opts.System + "\n")
- case resp.System != "":
- fmt.Println(resp.System + "\n")
- default:
- fmt.Println("No system message was specified for this model.")
- }
- case "template":
- switch {
- case opts.Template != "":
- fmt.Println(opts.Template + "\n")
- case resp.Template != "":
- fmt.Println(resp.Template)
- default:
- fmt.Println("No prompt template was specified for this model.")
- }
- default:
- fmt.Printf("Unknown command '/show %s'. Type /? for help\n", args[1])
- }
- } 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()
- case "shortcut", "shortcuts":
- usageShortcuts()
- }
- } else {
- usage()
- }
- case strings.HasPrefix(line, "/exit"), strings.HasPrefix(line, "/bye"):
- return nil
- case strings.HasPrefix(line, "/"):
- args := strings.Fields(line)
- isFile := false
- if opts.MultiModal {
- for _, f := range extractFileNames(line) {
- if strings.HasPrefix(f, args[0]) {
- isFile = true
- break
- }
- }
- }
- if !isFile {
- fmt.Printf("Unknown command '%s'. Type /? for help\n", args[0])
- continue
- }
- sb.WriteString(line)
- default:
- sb.WriteString(line)
- }
- if sb.Len() > 0 && multiline == MultilineNone {
- newMessage := api.Message{Role: "user", Content: sb.String()}
- if opts.MultiModal {
- msg, images, err := extractFileData(sb.String())
- if err != nil {
- return err
- }
- // clear all previous images for better responses
- if len(images) > 0 {
- for i := range opts.Messages {
- opts.Messages[i].Images = nil
- }
- }
- newMessage.Content = msg
- newMessage.Images = images
- }
- opts.Messages = append(opts.Messages, newMessage)
- assistant, err := chat(cmd, opts)
- if err != nil {
- return err
- }
- if assistant != nil {
- opts.Messages = append(opts.Messages, *assistant)
- }
- sb.Reset()
- }
- }
- }
- func buildModelfile(opts runOptions) string {
- var mf strings.Builder
- model := opts.ParentModel
- if model == "" {
- model = opts.Model
- }
- fmt.Fprintf(&mf, "FROM %s\n", model)
- if opts.System != "" {
- fmt.Fprintf(&mf, "SYSTEM \"\"\"%s\"\"\"\n", opts.System)
- }
- if opts.Template != "" {
- fmt.Fprintf(&mf, "TEMPLATE \"\"\"%s\"\"\"\n", opts.Template)
- }
- keys := make([]string, 0)
- for k := range opts.Options {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- for _, k := range keys {
- fmt.Fprintf(&mf, "PARAMETER %s %v\n", k, opts.Options[k])
- }
- fmt.Fprintln(&mf)
- for _, msg := range opts.Messages {
- fmt.Fprintf(&mf, "MESSAGE %s \"\"\"%s\"\"\"\n", msg.Role, msg.Content)
- }
- return mf.String()
- }
- func normalizeFilePath(fp string) string {
- // Define a map of escaped characters and their replacements
- replacements := map[string]string{
- "\\ ": " ", // Escaped space
- "\\(": "(", // Escaped left parenthesis
- "\\)": ")", // Escaped right parenthesis
- "\\[": "[", // Escaped left square bracket
- "\\]": "]", // Escaped right square bracket
- "\\{": "{", // Escaped left curly brace
- "\\}": "}", // Escaped right curly brace
- "\\$": "$", // Escaped dollar sign
- "\\&": "&", // Escaped ampersand
- "\\;": ";", // Escaped semicolon
- "\\'": "'", // Escaped single quote
- "\\\\": "\\", // Escaped backslash
- "\\*": "*", // Escaped asterisk
- "\\?": "?", // Escaped question mark
- }
- for escaped, actual := range replacements {
- fp = strings.ReplaceAll(fp, escaped, actual)
- }
- return fp
- }
- func extractFileNames(input string) []string {
- // Regex to match file paths starting with optional drive letter, / ./ \ or .\ and include escaped or unescaped spaces (\ or %20)
- // and followed by more characters and a file extension
- // This will capture non filename strings, but we'll check for file existence to remove mismatches
- regexPattern := `(?:[a-zA-Z]:)?(?:\./|/|\\)[\S\\ ]+?\.(?i:jpg|jpeg|png|svg)\b`
- re := regexp.MustCompile(regexPattern)
- return re.FindAllString(input, -1)
- }
- func extractFileData(input string) (string, []api.ImageData, error) {
- filePaths := extractFileNames(input)
- var imgs []api.ImageData
- for _, fp := range filePaths {
- nfp := normalizeFilePath(fp)
- data, err := getImageData(nfp)
- if err != nil {
- if os.IsNotExist(err) {
- continue
- }
- fmt.Fprintf(os.Stderr, "Couldn't process image: %q\n", err)
- return "", imgs, err
- }
- fmt.Fprintf(os.Stderr, "Added image '%s'\n", nfp)
- input = strings.ReplaceAll(input, fp, "")
- imgs = append(imgs, data)
- }
- return input, imgs, nil
- }
- func getImageData(filePath string) ([]byte, error) {
- file, err := os.Open(filePath)
- if err != nil {
- return nil, err
- }
- defer file.Close()
- buf := make([]byte, 512)
- _, err = file.Read(buf)
- if err != nil {
- return nil, err
- }
- contentType := http.DetectContentType(buf)
- allowedTypes := []string{"image/jpeg", "image/jpg", "image/png"}
- if !slices.Contains(allowedTypes, contentType) {
- return nil, fmt.Errorf("invalid image type: %s", contentType)
- }
- info, err := file.Stat()
- if err != nil {
- return nil, err
- }
- // Check if the file size exceeds 100MB
- var maxSize int64 = 100 * 1024 * 1024 // 100MB in bytes
- if info.Size() > maxSize {
- return nil, fmt.Errorf("file size exceeds maximum limit (100MB)")
- }
- buf = make([]byte, info.Size())
- _, err = file.Seek(0, 0)
- if err != nil {
- return nil, err
- }
- _, err = io.ReadFull(file, buf)
- if err != nil {
- return nil, err
- }
- return buf, nil
- }
|