-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (141 loc) · 3.83 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var configFile string
var rootCmd = &cobra.Command{
Use: "chatgpt-cli",
Short: "Interact with ChatGPT via CLI",
Run: startChat,
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ./config.yaml)")
}
func initConfig() {
if configFile != "" {
viper.SetConfigFile("configFile")
} else {
home, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(home)
viper.SetConfigName("config")
}
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Cant read config file", err)
}
}
func startChat(cmd *cobra.Command, args []string) {
apiKey := viper.GetString("api_key")
if apiKey == "" {
log.Fatal("API key not found in the config file")
}
fmt.Println("ChatGPT CLI - Type 'exit' to quit")
for {
fmt.Println("You: ")
userInput, err := getUserInput()
if err != nil {
fmt.Println(err)
}
if strings.ToLower(userInput) == "exit\n" {
fmt.Println("Ending chat....")
os.Exit(0)
}
res, err := getResFromGpt(apiKey, userInput)
if err != nil {
fmt.Println(err)
break
os.Exit(1)
}
fmt.Println("assistant: ", res)
}
}
func getUserInput() (string, error) {
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("error in getting input: %s", err)
}
return line, nil
}
func getResFromGpt(apiKey string, input string) (string, error) {
url := "https://api.openai.com/v1/chat/completions"
payload := map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]string{{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": input}},
"temperature": 0.7,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("error marshalling JSON payload: %s", err)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonPayload))
if err != nil {
return "", fmt.Errorf("error creating HTTP request: %s", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making HTTP request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %s", err)
}
var completion ChatCompletion
resErr := json.Unmarshal([]byte(body), &completion)
if resErr != nil {
return "", fmt.Errorf("Error unmarshaling JSON:", resErr)
}
messageContent := completion.Choices[0].Message.Content
return string(messageContent), nil
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
Logprobs interface{} `json:"logprobs"`
FinishReason string `json:"finish_reason"`
}
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
type ChatCompletion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
SystemFingerprint interface{} `json:"system_fingerprint"`
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}