Compare commits

..

No commits in common. "4590f1db38b83aeb79b13378fea7e91de05500fd" and "04478cbbd13e7364fb11eb5fd4c6416bd15e5af2" have entirely different histories.

3 changed files with 25 additions and 43 deletions

View File

@ -8,9 +8,9 @@ import (
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "lmcli", Use: "lm",
Short: "Interact with Large Language Models", Short: "Interact with Large Language Models",
Long: `lmcli is a CLI tool to interact with Large Language Models.`, Long: `lm is a CLI tool to interact with OpenAI's GPT 3.5 and GPT 4.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// execute `lm ls` by default // execute `lm ls` by default
}, },
@ -45,6 +45,17 @@ var lsCmd = &cobra.Command{
}, },
} }
var msgCmd = &cobra.Command{
Use: "msg",
Short: "Send a message to active conversation",
Long: `Send a message to the active conversation and receive a message from the LLM in return.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Sending message to active conversation...")
// If no messsage provided via args, we should open an editor ala `git commit`
// After submitting the message, the
},
}
var viewCmd = &cobra.Command{ var viewCmd = &cobra.Command{
Use: "view", Use: "view",
Short: "View messages in a conversation", Short: "View messages in a conversation",
@ -54,21 +65,12 @@ var viewCmd = &cobra.Command{
}, },
} }
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...")
},
}
var newCmd = &cobra.Command{ var newCmd = &cobra.Command{
Use: "new", Use: "new",
Short: "Start a new conversation", Short: "Start a new conversation",
Long: `Start a new conversation with the Large Language Model.`, Long: `Start a new conversation with the Large Language Model.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md") messageContents, err := InputFromEditor("# What would you like to say?", "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 return
@ -103,15 +105,9 @@ var promptCmd = &cobra.Command{
Short: "Do a one-shot prompt", Short: "Do a one-shot prompt",
Long: `Prompt the Large Language Model and get a response.`, Long: `Prompt the Large Language Model and get a response.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
message := strings.Join(args, " ")
if len(strings.Trim(message, " \t\n")) == 0 {
Fatal("No message was provided.\n")
return
}
messages := []Message{ messages := []Message{
{ {
OriginalContent: message, OriginalContent: strings.Join(args, " "),
Role: "user", Role: "user",
}, },
} }

View File

@ -8,14 +8,8 @@ import (
openai "github.com/sashabaranov/go-openai" openai "github.com/sashabaranov/go-openai"
) )
func CreateChatCompletionRequest(system string, messages []Message) (*openai.ChatCompletionRequest) { func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionRequest) {
chatCompletionMessages := []openai.ChatCompletionMessage{ var chatCompletionMessages []openai.ChatCompletionMessage
{
Role: "system",
Content: system,
},
}
for _, m := range(messages) { for _, m := range(messages) {
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{ chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
Role: m.Role, Role: m.Role,
@ -23,7 +17,7 @@ func CreateChatCompletionRequest(system string, messages []Message) (*openai.Cha
}) })
} }
return &openai.ChatCompletionRequest{ return openai.ChatCompletionRequest{
Model: openai.GPT4, Model: openai.GPT4,
MaxTokens: 256, MaxTokens: 256,
Messages: chatCompletionMessages, Messages: chatCompletionMessages,
@ -37,24 +31,24 @@ func CreateChatCompletion(system string, messages []Message) (string, error) {
client := openai.NewClient(config.OpenAI.APIKey) client := openai.NewClient(config.OpenAI.APIKey)
resp, err := client.CreateChatCompletion( resp, err := client.CreateChatCompletion(
context.Background(), context.Background(),
*CreateChatCompletionRequest(system, messages), CreateChatCompletionRequest(messages),
) )
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("ChatCompletion error: %v\n", err)
} }
return resp.Choices[0].Message.Content, nil return resp.Choices[0].Message.Content, nil
} }
func CreateChatCompletionStream(system string, messages []Message, output io.Writer) error { func CreateChatCompletionStream(system string, messages []Message, output io.Writer) (error) {
client := openai.NewClient(config.OpenAI.APIKey) client := openai.NewClient(config.OpenAI.APIKey)
ctx := context.Background() ctx := context.Background()
req := CreateChatCompletionRequest(system, messages) req := CreateChatCompletionRequest(messages)
req.Stream = true req.Stream = true
stream, err := client.CreateChatCompletionStream(ctx, *req) stream, err := client.CreateChatCompletionStream(ctx, req)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,7 +3,6 @@ package cli
import ( import (
"os" "os"
"os/exec" "os/exec"
"strings"
) )
// InputFromEditor retrieves user input by opening an editor (one specified by // InputFromEditor retrieves user input by opening an editor (one specified by
@ -35,15 +34,8 @@ func InputFromEditor(placeholder string, pattern string) (string, error) {
bytes, _ := os.ReadFile(msgFile.Name()) bytes, _ := os.ReadFile(msgFile.Name())
content := string(bytes) content := string(bytes)
if placeholder != "" { if content == placeholder {
if content == placeholder { return "", nil
return "", nil
}
// strip placeholder if content begins with it
if strings.HasPrefix(content, placeholder) {
content = content[len(placeholder):]
}
} }
return content, nil return content, nil