(ctx context.Context, session *chatSession, in io.Reader, out io.Writer)
| 9 | ) |
| 10 | |
| 11 | func runTerminalChat(ctx context.Context, session *chatSession, in io.Reader, out io.Writer) error { |
| 12 | scanner := bufio.NewScanner(in) |
| 13 | scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024) |
| 14 | |
| 15 | if err := writeChat(out, "LocalAI chat (%s)\n", session.CurrentModel()); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | if err := writeChat(out, "Type /exit to quit, /clear to reset the conversation, /models to list models.\n"); err != nil { |
| 19 | return err |
| 20 | } |
| 21 | |
| 22 | for { |
| 23 | if err := writeChat(out, "\n> "); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | if !scanner.Scan() { |
| 27 | break |
| 28 | } |
| 29 | |
| 30 | prompt := strings.TrimSpace(scanner.Text()) |
| 31 | switch prompt { |
| 32 | case "": |
| 33 | continue |
| 34 | case "/bye", "/exit", "/quit": |
| 35 | return writeChat(out, "bye\n") |
| 36 | case "/clear": |
| 37 | session.Clear() |
| 38 | if err := writeChat(out, "conversation cleared\n"); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | continue |
| 42 | case "/models": |
| 43 | if err := printChatModels(out, session.Models(), session.CurrentModel()); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | continue |
| 47 | } |
| 48 | |
| 49 | if nextModel, ok := strings.CutPrefix(prompt, "/model "); ok { |
| 50 | nextModel = strings.TrimSpace(nextModel) |
| 51 | if nextModel == "" { |
| 52 | if err := writeChat(out, "usage: /model <name>\n"); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | continue |
| 56 | } |
| 57 | if err := session.SwitchModel(nextModel); err != nil { |
| 58 | if writeErr := writeChat(out, "%s\n", err); writeErr != nil { |
| 59 | return writeErr |
| 60 | } |
| 61 | continue |
| 62 | } |
| 63 | if err := writeChat(out, "switched to %s; conversation cleared\n", session.CurrentModel()); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | continue |
| 67 | } |
| 68 |
no test coverage detected