-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
136 lines (115 loc) Β· 3.27 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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
const (
URL = "https://api.openai.com/v1/chat/completions"
)
type (
Prompt struct {
Model string `json:"model"`
Messages []Messages `json:"messages"`
}
Messages struct {
Role string `json:"role"`
Content string `json:"content"`
}
Response struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `json:"model"`
Usage Usage `json:"usage"`
Choices []Choices `json:"choices"`
}
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
Choices struct {
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
Index int `json:"index"`
}
)
func main() {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n", 33, "Hi, I'm Shell Mate. How can I help you?")
token := os.Getenv("OPENAPI_TOKEN")
if token == "" {
fmt.Println("I could not find the 'OPENAPI_TOKEN' value on your environment.")
fmt.Printf("Please provide your token: ")
fmt.Scanln(&token)
}
Start(os.Stdin, os.Stdout, token)
}
func Start(in io.Reader, out io.Writer, token string) {
scanner := bufio.NewScanner(in)
for {
fmt.Print("π€ >> ")
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()
if strings.TrimSpace(line) == "exit" {
fmt.Println("\nSee you soon! π")
os.Exit(0)
}
var prompt Prompt
prompt.Model = "gpt-3.5-turbo"
prompt.Messages = make([]Messages, 1)
prompt.Messages[0].Role = "user"
prompt.Messages[0].Content = line
jsonData, err := json.Marshal(prompt)
if err != nil {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n%s\n", 31, "Sorry, something went wrong π", err.Error())
os.Exit(1)
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n%s\n", 31, "Sorry, something went wrong π", err.Error())
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
bearer := fmt.Sprintf("Bearer %s", token)
req.Header.Set("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n%s\n", 31, "Sorry, something went wrong π", err.Error())
os.Exit(1)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n%s\n", 31, "Sorry, something went wrong π", err.Error())
os.Exit(1)
}
if resp.StatusCode == 200 {
var response Response
if err := json.Unmarshal(body, &response); err != nil {
fmt.Printf("\x1b[%dm%s\x1b[0m\n\n%s\n", 31, "Sorry, something went wrong π", err.Error())
os.Exit(1)
}
responseMessage := strings.Replace(strings.TrimSpace(response.Choices[0].Message.Content), "As an AI language model,", "You do know I'm just an AI right?", 1)
io.WriteString(out, fmt.Sprintf("\n\x1b[%dm%s\x1b[0m\n", 32, responseMessage))
io.WriteString(out, "\n")
} else {
fmt.Printf("\x1b[%dm%s\x1b[0m\n", 31, "Sorry, something went wrong π.")
fmt.Println(string(body))
os.Exit(1)
}
}
}