Unverified Commit 779e196e authored by Dane Madsen's avatar Dane Madsen Committed by GitHub
Browse files

Merge branch 'jmorganca:main' into main

parents c1a52208 42386204
package cmd package cmd
import ( import (
"bufio"
"context" "context"
"crypto/ed25519" "crypto/ed25519"
"crypto/rand" "crypto/rand"
...@@ -350,34 +349,44 @@ func pull(model string, insecure bool) error { ...@@ -350,34 +349,44 @@ func pull(model string, insecure bool) error {
} }
func RunGenerate(cmd *cobra.Command, args []string) error { func RunGenerate(cmd *cobra.Command, args []string) error {
if len(args) > 1 { format, err := cmd.Flags().GetString("format")
// join all args into a single prompt if err != nil {
wordWrap := false return err
if term.IsTerminal(int(os.Stdout.Fd())) {
wordWrap = true
} }
nowrap, err := cmd.Flags().GetBool("nowordwrap") prompts := args[1:]
// prepend stdin to the prompt if provided
if !term.IsTerminal(int(os.Stdin.Fd())) {
in, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
return err return err
} }
if nowrap {
wordWrap = false prompts = append([]string{string(in)}, prompts...)
} }
format, err := cmd.Flags().GetString("format") // output is being piped
if !term.IsTerminal(int(os.Stdout.Fd())) {
return generate(cmd, args[0], strings.Join(prompts, " "), false, format)
}
wordWrap := os.Getenv("TERM") == "xterm-256color"
nowrap, err := cmd.Flags().GetBool("nowordwrap")
if err != nil { if err != nil {
return err return err
} }
if nowrap {
return generate(cmd, args[0], strings.Join(args[1:], " "), wordWrap, format) wordWrap = false
} }
if readline.IsTerminal(int(os.Stdin.Fd())) { // prompts are provided via stdin or args so don't enter interactive mode
return generateInteractive(cmd, args[0]) if len(prompts) > 0 {
return generate(cmd, args[0], strings.Join(prompts, " "), wordWrap, format)
} }
return generateBatch(cmd, args[0]) return generateInteractive(cmd, args[0], wordWrap, format)
} }
type generateContextKey string type generateContextKey string
...@@ -398,7 +407,7 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st ...@@ -398,7 +407,7 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st
generateContext = []int{} generateContext = []int{}
} }
termWidth, _, err := term.GetSize(int(0)) termWidth, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil { if err != nil {
wordWrap = false wordWrap = false
} }
...@@ -490,7 +499,7 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st ...@@ -490,7 +499,7 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st
return nil return nil
} }
func generateInteractive(cmd *cobra.Command, model string) error { func generateInteractive(cmd *cobra.Command, model string, wordWrap bool, format string) error {
// load the model // load the model
if err := generate(cmd, model, "", false, ""); err != nil { if err := generate(cmd, model, "", false, ""); err != nil {
return err return err
...@@ -542,22 +551,6 @@ func generateInteractive(cmd *cobra.Command, model string) error { ...@@ -542,22 +551,6 @@ func generateInteractive(cmd *cobra.Command, model string) error {
return err return err
} }
var format string
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
}
fmt.Print(readline.StartBracketedPaste) fmt.Print(readline.StartBracketedPaste)
defer fmt.Printf(readline.EndBracketedPaste) defer fmt.Printf(readline.EndBracketedPaste)
...@@ -711,19 +704,6 @@ func generateInteractive(cmd *cobra.Command, model string) error { ...@@ -711,19 +704,6 @@ func generateInteractive(cmd *cobra.Command, model string) error {
} }
} }
func generateBatch(cmd *cobra.Command, model string) error {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
prompt := scanner.Text()
fmt.Printf(">>> %s\n", prompt)
if err := generate(cmd, model, prompt, false, ""); err != nil {
return err
}
}
return nil
}
func RunServer(cmd *cobra.Command, _ []string) error { func RunServer(cmd *cobra.Command, _ []string) error {
host, port, err := net.SplitHostPort(os.Getenv("OLLAMA_HOST")) host, port, err := net.SplitHostPort(os.Getenv("OLLAMA_HOST"))
if err != nil { if err != nil {
......
...@@ -38,10 +38,10 @@ Generate a response for a given prompt with a provided model. This is a streamin ...@@ -38,10 +38,10 @@ Generate a response for a given prompt with a provided model. This is a streamin
- `model`: (required) the [model name](#model-names) - `model`: (required) the [model name](#model-names)
- `prompt`: the prompt to generate a response for - `prompt`: the prompt to generate a response for
- `format`: the format to return a response in. Currently the only accepted value is `json`
Advanced parameters (optional): Advanced parameters (optional):
- `format`: the format to return a response in. Currently the only accepted value is `json`
- `options`: additional model parameters listed in the documentation for the [Modelfile](./modelfile.md#valid-parameters-and-values) such as `temperature` - `options`: additional model parameters listed in the documentation for the [Modelfile](./modelfile.md#valid-parameters-and-values) such as `temperature`
- `system`: system prompt to (overrides what is defined in the `Modelfile`) - `system`: system prompt to (overrides what is defined in the `Modelfile`)
- `template`: the full prompt or prompt template (overrides what is defined in the `Modelfile`) - `template`: the full prompt or prompt template (overrides what is defined in the `Modelfile`)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment