-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.go
More file actions
79 lines (71 loc) · 1.69 KB
/
helper.go
File metadata and controls
79 lines (71 loc) · 1.69 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// GetResponse sends a request to the Grok 3 API and prints the response
func GetResponse(input string, params Params, isQuiet bool) {
if !isQuiet {
fmt.Print("Thinking")
go func() {
for {
fmt.Print(".")
time.Sleep(500 * time.Millisecond)
}
}()
}
// Prepare request payload
payload := map[string]interface{}{
"prompt": input,
"model": "grok-3", // Placeholder model name
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
PrintError(fmt.Sprintf("Error marshaling payload: %v", err))
return
}
// Create HTTP request
req, err := http.NewRequest("POST", params.Url, bytes.NewBuffer(payloadBytes))
if err != nil {
PrintError(fmt.Sprintf("Error creating request: %v", err))
return
}
req.Header.Set("Content-Type", "application/json")
if params.ApiKey != "" {
req.Header.Set("Authorization", "Bearer "+params.ApiKey)
}
// Send request
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if !isQuiet {
fmt.Println()
}
if err != nil {
PrintError(fmt.Sprintf("Error sending request: %v", err))
return
}
defer resp.Body.Close()
// Read response
body, err := io.ReadAll(resp.Body)
if err != nil {
PrintError(fmt.Sprintf("Error reading response: %v", err))
return
}
// Parse response (assuming JSON with a "response" field)
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
PrintError(fmt.Sprintf("Error parsing response: %v", err))
return
}
response, ok := result["response"].(string)
if !ok {
PrintError("Invalid response format")
return
}
// Print response
fmt.Println("\n" + response)
}