-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-go.go
81 lines (60 loc) · 1.75 KB
/
chat-go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
openai "github.com/sashabaranov/go-openai"
)
var KEY string = os.Getenv("OPENAI_API_KEY")
var client = openai.NewClient(KEY)
func prompt() string {
fmt.Printf(">>> ")
rdr := bufio.NewReader(os.Stdin)
input, err := rdr.ReadString('\n')
if err != nil {
panic(err)
}
return input
}
var convo = []openai.ChatCompletionMessage{}
func createChat(convo []openai.ChatCompletionMessage) openai.ChatCompletionResponse {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: convo,
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
panic(err)
}
return resp
}
func main() {
if len(KEY) < 1 {
panic("Missing OPEN AI API Key. Make sure to export OPENAI_API_KEY variable. eg export OPENAI_API_KEY=sk-fIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXsk")
os.Exit(1)
}
// This allows the CLI to not be interactive so you can pipe the output eg $ chat-go write a bash loop > loop.sh
if len(os.Args) > 1 {
resp := createChat([]openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleUser, Content: strings.Join(os.Args[1:], " ")}})
fmt.Println(resp.Choices[0].Message.Content)
os.Exit(0)
}
for {
userInput := prompt()
if userInput == "exit\n" || userInput == "\\q\n" {
os.Exit(0)
}
convo = append(convo, openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser, Content: userInput})
resp := createChat(convo)
convo = append(convo, resp.Choices[0].Message)
fmt.Println("------------------------------------------------------")
fmt.Println(resp.Choices[0].Message.Content)
fmt.Println("------------------------------------------------------")
}
}