Implement persistence for lmcli new
This commit is contained in:
parent
78bcc11a4b
commit
3d518efd6f
@ -12,3 +12,7 @@ func Fatal(format string, args ...any) {
|
|||||||
fmt.Fprintf(os.Stderr, format, args...)
|
fmt.Fprintf(os.Stderr, format, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Warn(format string, args ...any) {
|
||||||
|
fmt.Fprintf(os.Stderr, format, args...)
|
||||||
|
}
|
||||||
|
@ -71,29 +71,53 @@ var newCmd = &cobra.Command{
|
|||||||
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
|
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("Failed to get input: %v\n", err)
|
Fatal("Failed to get input: %v\n", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if messageContents == "" {
|
if messageContents == "" {
|
||||||
Fatal("No message was provided.\n")
|
Fatal("No message was provided.\n")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("> %s\n", messageContents)
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
messages := []Message{
|
message := Message{
|
||||||
{
|
ConversationID: conversation.ID,
|
||||||
Role: "user",
|
Role: "user",
|
||||||
OriginalContent: messageContents,
|
OriginalContent: messageContents,
|
||||||
},
|
}
|
||||||
|
err = store.SaveMessage(&message)
|
||||||
|
if err != nil {
|
||||||
|
Warn("Could not save message: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const system = "You are a helpful assistant."
|
||||||
|
fmt.Printf("<System>\n\n%s\n\n", system)
|
||||||
|
fmt.Printf("<You>\n\n%s\n\n", messageContents)
|
||||||
|
fmt.Print("<Assistant>\n\n")
|
||||||
|
|
||||||
receiver := make(chan string)
|
receiver := make(chan string)
|
||||||
go HandleDelayedResponse(receiver)
|
response := make(chan string)
|
||||||
err = CreateChatCompletionStream("You are a helpful assistant.", messages, receiver)
|
go func() {
|
||||||
|
response <- HandleDelayedResponse(receiver)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = CreateChatCompletionStream(system, []Message{message}, receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
return
|
}
|
||||||
|
|
||||||
|
reply := Message{
|
||||||
|
ConversationID: conversation.ID,
|
||||||
|
Role: "assistant",
|
||||||
|
OriginalContent: <-response,
|
||||||
|
}
|
||||||
|
err = store.SaveMessage(&reply)
|
||||||
|
if err != nil {
|
||||||
|
Fatal("Could not save reply: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
@ -108,7 +132,6 @@ var promptCmd = &cobra.Command{
|
|||||||
message := strings.Join(args, " ")
|
message := strings.Join(args, " ")
|
||||||
if len(strings.Trim(message, " \t\n")) == 0 {
|
if len(strings.Trim(message, " \t\n")) == 0 {
|
||||||
Fatal("No message was provided.\n")
|
Fatal("No message was provided.\n")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
messages := []Message{
|
messages := []Message{
|
||||||
@ -123,7 +146,6 @@ var promptCmd = &cobra.Command{
|
|||||||
err := CreateChatCompletionStream("You are a helpful assistant.", messages, receiver)
|
err := CreateChatCompletionStream("You are a helpful assistant.", messages, receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
Loading…
Reference in New Issue
Block a user