2023-11-04 12:20:13 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-11-04 16:56:22 -06:00
|
|
|
|
2023-11-04 12:20:13 -06:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
2023-11-04 16:37:18 -06:00
|
|
|
Use: "lmcli",
|
2023-11-04 12:20:13 -06:00
|
|
|
Short: "Interact with Large Language Models",
|
2023-11-04 16:37:18 -06:00
|
|
|
Long: `lmcli is a CLI tool to interact with Large Language Models.`,
|
2023-11-04 12:20:13 -06:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
// execute `lm ls` by default
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var lsCmd = &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Short: "List existing conversations",
|
|
|
|
Long: `List all existing conversations in descending order of recent activity.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println("Listing conversations...")
|
|
|
|
// Example output, asterisk to indicate current converation
|
|
|
|
|
|
|
|
// $ lm ls
|
|
|
|
// last hour:
|
|
|
|
// 98sg - 12 minutes ago - Project discussion
|
|
|
|
// last day:
|
|
|
|
// tj3l - 10 hours ago - Deep learning concepts
|
|
|
|
// last week:
|
|
|
|
// bwfm - 2 days ago - Machine learning study
|
|
|
|
// * 8n3h - 3 days ago - Weekend plans
|
|
|
|
// f3n7 - 6 days ago - CLI development
|
|
|
|
// last month:
|
|
|
|
// 5hn2 - 8 days ago - Book club discussion
|
|
|
|
// b7ze - 20 days ago - Gardening tips and tricks
|
|
|
|
// last 6 months:
|
|
|
|
// 3jn2 - 30 days ago - Web development best practices
|
|
|
|
// 43jk - 2 months ago - Longboard maintenance
|
|
|
|
// g8d9 - 3 months ago - History book club
|
|
|
|
// 4lk3 - 4 months ago - Local events and meetups
|
|
|
|
// 43jn - 6 months ago - Mobile photography techniques
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var viewCmd = &cobra.Command{
|
|
|
|
Use: "view",
|
|
|
|
Short: "View messages in a conversation",
|
|
|
|
Long: `Displays all the messages in a coversation.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println("Displaying conversation messages...")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:37:18 -06:00
|
|
|
var replyCmd = &cobra.Command{
|
|
|
|
Use: "reply",
|
|
|
|
Short: "Send a reply to a conversation",
|
|
|
|
Long: `Sends a reply to conversation and writes the response to stdout.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println("Replying to a conversation...")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:20:13 -06:00
|
|
|
var newCmd = &cobra.Command{
|
|
|
|
Use: "new",
|
|
|
|
Short: "Start a new conversation",
|
2023-11-04 16:56:22 -06:00
|
|
|
Long: `Start a new conversation with the Large Language Model.`,
|
2023-11-04 12:20:13 -06:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-11-04 16:53:09 -06:00
|
|
|
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
|
2023-11-04 12:20:13 -06:00
|
|
|
if err != nil {
|
2023-11-04 13:58:48 -06:00
|
|
|
Fatal("Failed to get input: %v\n", err)
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if messageContents == "" {
|
2023-11-04 13:58:48 -06:00
|
|
|
Fatal("No message was provided.\n")
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:41:43 -06:00
|
|
|
// TODO: set title if --title provided, otherwise defer for later(?)
|
|
|
|
conversation := Conversation{}
|
|
|
|
err = store.SaveConversation(&conversation)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Could not save new conversation: %v\n", err)
|
|
|
|
}
|
2023-11-04 12:20:13 -06:00
|
|
|
|
2023-11-05 01:54:12 -06:00
|
|
|
const system = "You are a helpful assistant."
|
|
|
|
messages := []Message{
|
|
|
|
{
|
|
|
|
ConversationID: conversation.ID,
|
|
|
|
Role: "system",
|
|
|
|
OriginalContent: system,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ConversationID: conversation.ID,
|
|
|
|
Role: "user",
|
|
|
|
OriginalContent: messageContents,
|
|
|
|
},
|
2023-11-05 01:41:43 -06:00
|
|
|
}
|
2023-11-05 01:50:07 -07:00
|
|
|
for _, message := range messages {
|
2023-11-05 01:54:12 -06:00
|
|
|
err = store.SaveMessage(&message)
|
|
|
|
if err != nil {
|
2023-11-05 01:50:07 -07:00
|
|
|
Warn("Could not save %s message: %v\n", message.Role, err)
|
2023-11-05 01:54:12 -06:00
|
|
|
}
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:50:07 -07:00
|
|
|
for _, message := range messages {
|
|
|
|
message.RenderTTY(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := Message{
|
|
|
|
ConversationID: conversation.ID,
|
|
|
|
Role: "assistant",
|
|
|
|
}
|
|
|
|
reply.RenderTTY(false)
|
2023-11-05 01:41:43 -06:00
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
receiver := make(chan string)
|
2023-11-05 01:41:43 -06:00
|
|
|
response := make(chan string)
|
|
|
|
go func() {
|
|
|
|
response <- HandleDelayedResponse(receiver)
|
|
|
|
}()
|
2023-11-05 11:19:30 -07:00
|
|
|
|
2023-11-05 01:54:12 -06:00
|
|
|
err = CreateChatCompletionStream(messages, receiver)
|
2023-11-04 12:20:13 -06:00
|
|
|
if err != nil {
|
2023-11-04 13:58:48 -06:00
|
|
|
Fatal("%v\n", err)
|
2023-11-05 01:41:43 -06:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:50:07 -07:00
|
|
|
reply.OriginalContent = <-response
|
|
|
|
|
2023-11-05 01:41:43 -06:00
|
|
|
err = store.SaveMessage(&reply)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Could not save reply: %v\n", err)
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var promptCmd = &cobra.Command{
|
2023-11-04 16:56:22 -06:00
|
|
|
Use: "prompt",
|
2023-11-04 12:20:13 -06:00
|
|
|
Short: "Do a one-shot prompt",
|
2023-11-04 16:56:22 -06:00
|
|
|
Long: `Prompt the Large Language Model and get a response.`,
|
2023-11-04 12:20:13 -06:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-11-04 16:53:09 -06:00
|
|
|
message := strings.Join(args, " ")
|
|
|
|
if len(strings.Trim(message, " \t\n")) == 0 {
|
|
|
|
Fatal("No message was provided.\n")
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:54:12 -06:00
|
|
|
const system = "You are a helpful assistant."
|
2023-11-04 12:20:13 -06:00
|
|
|
messages := []Message{
|
2023-11-05 01:54:12 -06:00
|
|
|
{
|
|
|
|
Role: "system",
|
|
|
|
OriginalContent: system,
|
|
|
|
},
|
2023-11-04 12:20:13 -06:00
|
|
|
{
|
2023-11-04 16:56:22 -06:00
|
|
|
Role: "user",
|
2023-11-04 16:53:09 -06:00
|
|
|
OriginalContent: message,
|
2023-11-04 12:20:13 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:44:06 -06:00
|
|
|
receiver := make(chan string)
|
|
|
|
go HandleDelayedResponse(receiver)
|
2023-11-05 01:54:12 -06:00
|
|
|
err := CreateChatCompletionStream(messages, receiver)
|
2023-11-04 12:20:13 -06:00
|
|
|
if err != nil {
|
2023-11-04 13:58:48 -06:00
|
|
|
Fatal("%v\n", err)
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRootCmd() *cobra.Command {
|
2023-11-05 00:55:38 -06:00
|
|
|
rootCmd.AddCommand(
|
|
|
|
newCmd,
|
|
|
|
promptCmd,
|
|
|
|
)
|
2023-11-04 16:56:22 -06:00
|
|
|
return rootCmd
|
2023-11-04 12:20:13 -06:00
|
|
|
}
|