Compare commits

..

4 Commits

3 changed files with 43 additions and 25 deletions

View File

@ -8,9 +8,9 @@ import (
)
var rootCmd = &cobra.Command{
Use: "lm",
Use: "lmcli",
Short: "Interact with Large Language Models",
Long: `lm is a CLI tool to interact with OpenAI's GPT 3.5 and GPT 4.`,
Long: `lmcli is a CLI tool to interact with Large Language Models.`,
Run: func(cmd *cobra.Command, args []string) {
// execute `lm ls` by default
},
@ -45,17 +45,6 @@ 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",
@ -65,12 +54,21 @@ 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?", "message.*.md")
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
if err != nil {
Fatal("Failed to get input: %v\n", err)
return
@ -105,9 +103,15 @@ 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: strings.Join(args, " "),
OriginalContent: message,
Role: "user",
},
}

View File

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

View File

@ -3,6 +3,7 @@ package cli
import (
"os"
"os/exec"
"strings"
)
// InputFromEditor retrieves user input by opening an editor (one specified by
@ -34,9 +35,16 @@ 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):]
}
}
return content, nil
}