-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.go
161 lines (145 loc) · 4.01 KB
/
console.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/atotto/clipboard"
"github.com/sashabaranov/go-openai"
)
func (agent *Agent) console() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
_, err := fmt.Println("Welcome")
if err != nil {
fmt.Println(err)
} else {
for {
select {
case <-interrupt:
fmt.Println("Enter 'q' or 'quit' to exit!")
continue
default:
fmt.Print("\nUser:\n")
input := gettextinput()
text := agent.process_text(input)
if text == "" {
continue
}
query := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: text,
}
agent.req.Messages = append(agent.req.Messages, query)
for {
// retries response until it works
response, err := agent.getresponse()
if err != nil {
fmt.Println(err)
continue
}
estcost := (float64(agent.tokencount) / 1000) * callcost
fmt.Println("\nAssistant:")
fmt.Println(response.Message.Content)
fmt.Println("\nTokencount: ", agent.tokencount, " Est. Cost: ", estcost)
break
}
}
}
}
}
func (agent *Agent) process_text(text string) string {
switch text {
case "q", "quit":
fmt.Println("\nQuitting...")
os.Exit(0)
case "del", "delete", "!":
agent.setprompt()
fmt.Println("\nChat cleared!")
return ""
case "reset":
agent.reset()
fmt.Println("\nChat reset!")
return ""
case "paste":
text, err := clipboard.ReadAll()
if err != nil {
fmt.Println(err)
return ""
}
fmt.Println("\nPasted text!")
return text
case "copy":
response := agent.req.Messages[len(agent.req.Messages)].Content
err := clipboard.WriteAll(response)
if err != nil {
fmt.Println(err)
}
fmt.Println("\nCopied text!")
return ""
case "@", "sel", "select":
agent.printnumberlines()
fmt.Println("\nWhich lines would you like to delete?")
editchoice := gettextinput()
if editchoice == "" {
return ""
}
agent.deletelines(editchoice)
agent.printnumberlines()
fmt.Println("Lines deleted!")
return ""
case "save":
_, err := agent.savefile(agent.req.Messages, "Chats")
if err != nil {
fmt.Println(err)
}
return ""
case "load":
_, err := getsavefilelist("Chats")
if err != nil {
fmt.Println(err)
}
fmt.Println("What file would you like to load?")
filename := gettextinput()
if filename == "" {
return ""
}
_, err = agent.loadfile("Chats", filename)
if err != nil {
fmt.Println(err)
}
return ""
case "prompt":
fmt.Println("\nEnter new prompt:")
input := gettextinput()
if input == "paste" {
text, err := clipboard.ReadAll()
if err != nil {
fmt.Println(err)
return ""
}
agent.prompt.Parameters = text
} else {
text := input
agent.prompt.Parameters = text
}
agent.setprompt()
fmt.Println("\nPrompt edited!")
return ""
case "help":
fmt.Println("• Typing 'copy' will copy the last output from the bot\n• Typing 'paste' will paste your clipboard as a query - this way you can craft prompts in a text editor for multi-line queries\n• 'prompt' will let you enter in a new prompt ('paste' command works here)\n• 'save' will save the chat into a json file with the filename YYYYMMDDHHMM.txt\n• 'load <filename>' will load files\n• '@', 'sel', or 'select' will allow you to select lines to delete (handy if the chat is getting a bit long and you want to save on costs)\n• '!', 'del', or 'delete' will clear the chat log and start fresh\n'q' or 'quit' will quit the program")
return ""
default:
// Nothing will happen
}
return text
}
func (agent *Agent) printnumberlines() {
for i, msg := range agent.req.Messages {
if msg.Role == openai.ChatMessageRoleUser {
fmt.Printf("%d. User: %s\n", i, msg.Content)
} else if msg.Role == openai.ChatMessageRoleAssistant {
fmt.Printf("%d. Assistant: %s\n", i, msg.Content)
}
}
}