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{
Use: "lmcli",
Use: "lm",
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) {
// 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{
Use: "view",
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{
Use: "new",
Short: "Start a new conversation",
Long: `Start a new conversation with the Large Language Model.`,
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 {
Fatal("Failed to get input: %v\n", err)
return
@ -103,15 +105,9 @@ var promptCmd = &cobra.Command{
Short: "Do a one-shot prompt",
Long: `Prompt the Large Language Model and get a response.`,
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{
{
OriginalContent: message,
OriginalContent: strings.Join(args, " "),
Role: "user",
},
}

View File

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

View File

@ -3,7 +3,6 @@ package cli
import (
"os"
"os/exec"
"strings"
)
// 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())
content := string(bytes)
if placeholder != "" {
if content == placeholder {
return "", nil
}
// strip placeholder if content begins with it
if strings.HasPrefix(content, placeholder) {
content = content[len(placeholder):]
}
if content == placeholder {
return "", nil
}
return content, nil