commit 90dce3e8e42dce66070114b6f970d309932e2748 Author: olemorud Date: Fri May 26 17:47:36 2023 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/cmd/ask.go b/cmd/ask.go new file mode 100644 index 0000000..bc10501 --- /dev/null +++ b/cmd/ask.go @@ -0,0 +1,116 @@ +package main + +import ( + "bufio" + "context" + "flag" + "fmt" + "os" + "strings" + + util "github.com/olemorud/chatgpt-cli/v2" + openai "github.com/sashabaranov/go-openai" +) + +func main() { + env, err := util.ReadEnvFile(".env") + + if err != nil { + fmt.Println("failed to read .env", err) + } + + // parse command line arguments + token := env["OPENAI_API_KEY"] + + model := *flag.String("model", openai.GPT3Dot5Turbo, + "OpenAI Model to use.\n"+ + "List of models:\n"+ + "https://platform.openai.com/docs/models/overview\n") + + flag.Parse() + + args := flag.Args() + + if len(args) == 0 { + err = runInteractive(token, model) + + if err != nil { + fmt.Println(err) + } + } else { + query := strings.Join(args, " ") + err = askGpt(token, model, query) + + if err != nil { + panic(err) + } + } +} + +func askGpt(token string, model string, query string) error { + client := openai.NewClient("your token") + + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: model, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: query, + }, + }, + }, + ) + + if err != nil { + return err + } + + fmt.Println(resp.Choices[0].Message.Content) + + return nil +} + +func runInteractive(token string, model string) error { + client := openai.NewClient("your token") + messages := make([]openai.ChatCompletionMessage, 0) + + scanner := bufio.NewScanner(os.Stdin) + + fmt.Println("ChatGPT", model, "interactive mode") + + fmt.Println("->") + + for scanner.Scan() { + + text := scanner.Text() + + messages = append(messages, openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleUser, + Content: text, + }) + + resp, err := client.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: model, + Messages: messages, + }, + ) + + if err != nil { + return err + } + + fmt.Println(resp.Choices[0].Message.Content) + + fmt.Println("->") + } + + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "reading standard input:", err) + } + + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..43375f2 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/olemorud/chatgpt-cli/v2 + +go 1.18 + +require github.com/sashabaranov/go-openai v1.9.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4087dce --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/sashabaranov/go-openai v1.9.4 h1:KanoCEoowAI45jVXlenMCckutSRr39qOmSi9MyPBfZM= +github.com/sashabaranov/go-openai v1.9.4/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= diff --git a/util.go b/util.go new file mode 100644 index 0000000..646f96e --- /dev/null +++ b/util.go @@ -0,0 +1,44 @@ +package util + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func ReadEnvFile(path string) (map[string]string, error) { + f, err := os.Open(path) + + if err != nil { + fmt.Println("failed to open file: ", err) + return nil, fmt.Errorf("failed to open file: %v", err) + } + + defer f.Close() + + scanner := bufio.NewScanner(f) + + output := make(map[string]string) + + for scanner.Scan() { + text := scanner.Text() + line := strings.Split(text, "=") + + if len(line) != 2 { + continue + } + + key := line[0] + val := line[1] + + output[key] = val + } + + return output, nil +} + +// Creates a new chat session id and saves it to a file +func NewChat(path string) { + +}