-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (59 loc) · 1.86 KB
/
main.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
package main
import (
"bufio"
"context"
//"fmt"
"os"
"strings"
openai "github.com/sashabaranov/go-openai"
"github.com/fatih/color"
)
var (
promptStyle = color.New(color.FgHiMagenta, color.Bold)
userInputStyle = color.New(color.FgHiCyan)
goGPTResponseStyle = color.New(color.FgHiGreen)
errorStyle = color.New(color.FgHiRed, color.Bold)
)
func printStyled(c *color.Color, format string, a ...interface{}) {
c.PrintfFunc()(format, a...)
}
func main() {
client := openai.NewClient("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")
reader := bufio.NewReader(os.Stdin)
printStyled(promptStyle, "Welcome to GoGPT! Please choose a role: writer, programmer, or teacher\n")
role, _ := reader.ReadString('\n')
role = strings.TrimSpace(strings.ToLower(role))
// Initialize an empty slice to store the conversation history in memory
var messages []openai.ChatCompletionMessage
for {
printStyled(promptStyle, "Enter your message (type 'exit' to quit):\n")
userInput, _ := reader.ReadString('\n')
userInput = strings.TrimSpace(userInput)
if userInput == "exit" {
break
}
// Add user message to the history
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: userInput,
})
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: messages,
},
)
if err != nil {
printStyled(errorStyle, "ChatCompletion error: %v\n", err)
return
}
printStyled(userInputStyle, "You: %s\n", userInput)
printStyled(goGPTResponseStyle, "GoGPT (%s): %s\n", role, resp.Choices[0].Message.Content)
// Add GPT-3 response to the history
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: resp.Choices[0].Message.Content,
})
}
}