forked from AstroWa3l/seadog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseadog.go
257 lines (215 loc) · 5.83 KB
/
seadog.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
type Response struct {
Answer struct {
Text string `json:"text"`
} `json:"answer"`
Sources []struct {
Content string `json:"content"`
Date any `json:"date"`
ID int `json:"id"`
Link string `json:"link"`
SourceName any `json:"source_name"`
Text string `json:"text"`
} `json:"sources"`
MessageID int `json:"message_id"`
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// comment the line below out if you are not using .env file
apiKey := os.Getenv("MENDABLE_API_KEY")
// uncomment the line below and add your api key if you are not using .env file
// apiKey := "MENDABLE_API_KEY"
// Parse command-line arguments
cmd := flag.String("cmd", "", "Command to execute")
// if the user types -h or --help or help, print the help menu
// help := flag.String("h", "", "help")
help := flag.Bool("h", false, "help")
flag.Parse()
// add help case as -h or --help or help
if *help == true {
fmt.Println("Usage: go run seadog.go [command] [arguments]")
fmt.Println("Commands:")
fmt.Println(" -cmd [arguments] - Command followed by argument to execute")
fmt.Println(" -h - help")
// Arguemnts commands
fmt.Println("Arguments:")
fmt.Println(" ask - Ask a question")
fmt.Println(" ingest - Ingest data")
// Exit the program
os.Exit(0)
}
switch *cmd {
case "ask":
response, err := newConversation(apiKey)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// printResponse(response)
// Get the conversation_id from the response
conversationID := response["conversation_id"]
// create a scanner to read user input
scanner := bufio.NewScanner(os.Stdin)
// loop until exit condition is met
for {
// get the question from the user
fmt.Print("Ask a question (type 'quit' to exit): ")
scanner.Scan()
questionString := scanner.Text()
// check if user wants to exit
if questionString == "quit" {
break
}
url := "https://api.mendable.ai/v0/mendableChat"
data := map[string]interface{}{
"api_key": apiKey,
"question": questionString,
"history": []interface{}{},
"conversation_id": conversationID,
"shouldStream": false,
}
payload, err := json.Marshal(data)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
panic(err)
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var result Response
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer
fmt.Println("Can not unmarshal JSON")
}
fmt.Println(result.Answer.Text)
fmt.Println("Sources:" + "\n" + "--------")
for _, source := range result.Sources {
fmt.Println(source.Link)
fmt.Println()
}
}
case "ingest":
scanner := bufio.NewScanner(os.Stdin)
// Loop until exit condition is met
for {
// get the data source from the user
fmt.Print("Enter a data source url (type 'quit' to exit): ")
scanner.Scan()
dataSource := scanner.Text()
if dataSource == "quit" {
break
}
if dataSource == "" {
fmt.Fprintln(os.Stderr, "url and type are required")
os.Exit(1)
}
// get type of ingestion from the user
fmt.Print("Enter the type of data ingestion (url only for now): ")
scanner.Scan()
dataType := scanner.Text()
if dataType == "" {
fmt.Fprintln(os.Stderr, "url and type are required")
os.Exit(1)
}
response, err := ingestData(apiKey, dataSource, dataType)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
printResponse(response)
}
default:
fmt.Fprintln(os.Stderr, "Invalid command")
os.Exit(1)
}
}
func newConversation(apiKey string) (map[string]interface{}, error) {
url := "https://api.mendable.ai/v0/newConversation"
// Create the request body
requestBody := map[string]interface{}{
"api_key": apiKey,
}
requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
// Send the HTTP request
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBodyBytes))
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
var response map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return response, nil
}
func ingestData(apiKey string, dataSource string, dataType string) (map[string]interface{}, error) {
url := "https://api.mendable.ai/v0/ingestData"
// Create the request body
requestBody := map[string]interface{}{
"api_key": apiKey,
"url": dataSource,
"type": dataType,
}
requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
// Send the HTTP request
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBodyBytes))
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
var response map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return response, nil
}
func printResponse(response map[string]interface{}) {
// Convert the response map into a JSON string and print the response
responseJSON, err := json.Marshal(response)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(string(responseJSON))
}
// PrettyPrint to print struct in a readable way
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}