Compare commits
4 Commits
04478cbbd1
...
4590f1db38
Author | SHA1 | Date | |
---|---|---|---|
4590f1db38 | |||
6465ce5146 | |||
ca45159ec3 | |||
5c6ec5e4e2 |
@ -8,9 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "lm",
|
Use: "lmcli",
|
||||||
Short: "Interact with Large Language Models",
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// execute `lm ls` by default
|
// 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{
|
var viewCmd = &cobra.Command{
|
||||||
Use: "view",
|
Use: "view",
|
||||||
Short: "View messages in a conversation",
|
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{
|
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?", "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
|
return
|
||||||
@ -105,9 +103,15 @@ 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: strings.Join(args, " "),
|
OriginalContent: message,
|
||||||
Role: "user",
|
Role: "user",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,14 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionRequest) {
|
func CreateChatCompletionRequest(system string, messages []Message) (*openai.ChatCompletionRequest) {
|
||||||
var chatCompletionMessages []openai.ChatCompletionMessage
|
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,
|
||||||
@ -17,7 +23,7 @@ func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionReque
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return openai.ChatCompletionRequest{
|
return &openai.ChatCompletionRequest{
|
||||||
Model: openai.GPT4,
|
Model: openai.GPT4,
|
||||||
MaxTokens: 256,
|
MaxTokens: 256,
|
||||||
Messages: chatCompletionMessages,
|
Messages: chatCompletionMessages,
|
||||||
@ -31,24 +37,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(messages),
|
*CreateChatCompletionRequest(system, messages),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("ChatCompletion error: %v\n", err)
|
return "", 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(messages)
|
req := CreateChatCompletionRequest(system, 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
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ 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
|
||||||
@ -34,8 +35,15 @@ 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 content == placeholder {
|
if placeholder != "" {
|
||||||
return "", nil
|
if content == placeholder {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// strip placeholder if content begins with it
|
||||||
|
if strings.HasPrefix(content, placeholder) {
|
||||||
|
content = content[len(placeholder):]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return content, nil
|
return content, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user