-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (123 loc) · 3.7 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"github.com/joho/godotenv"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/youtube/v3"
)
func main() {
//Define Flags
query := flag.String("query", "Code broken :( y?", "The error you are facing.")
ask := flag.Bool("ask", true, "Whether to ask ChatGPT for help.")
video := flag.Bool("video", false, "Whether to display the video")
video_topic := ""
//Get + Parse Flags & Environment variables
flag.Parse()
load_env := godotenv.Load(".env")
if load_env != nil {
fmt.Println("Error loading .env file", load_env)
return
}
if *ask {
//Set up ChatGPT API
url := "https://api.openai.com/v1/chat/completions"
//Define ChatGPT call
body := map[string]interface{}{
"model": "gpt-4",
"messages": []map[string]string{
{
"role": "user",
"content": fmt.Sprintf("I have this error: %s. How can I solve it?", *query),
},
},
}
//Send call and parse ChatGPT response
jsonBody, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + os.Getenv("OPENAI_KEY")},
}
client := &http.Client{}
res, _ := client.Do(req)
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
fmt.Printf("Impossible to read OPENAI response: %s", err)
}
var responseBody map[string]interface{}
_ = json.Unmarshal(resBody, &responseBody)
choices := responseBody["choices"].([]interface{})
if len(choices) > 0 {
message := choices[0].(map[string]interface{})["message"].(map[string]interface{})
content := message["content"].(string)
video_topic = content
}
fmt.Printf("Search Query: %s", video_topic)
}
if *video {
//Set up ChatGPT API
url := "https://api.openai.com/v1/chat/completions"
//Define ChatGPT call
body := map[string]interface{}{
"model": "gpt-4",
"messages": []map[string]string{
{
"role": "user",
"content": fmt.Sprintf("I have this question: %s. Can you give a search phrase to find a YouTube video to help me fix it", *query),
},
},
}
//Send call and parse ChatGPT response
jsonBody, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + os.Getenv("OPENAI_KEY")},
}
client := &http.Client{}
res, _ := client.Do(req)
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
fmt.Printf("Impossible to read OPENAI response: %s", err)
}
var responseBody map[string]interface{}
_ = json.Unmarshal(resBody, &responseBody)
choices := responseBody["choices"].([]interface{})
if len(choices) > 0 {
message := choices[0].(map[string]interface{})["message"].(map[string]interface{})
content := message["content"].(string)
video_topic = content
}
fmt.Printf("Search Query: %s", video_topic)
// Make call to Youtube API to get search URL
var developerKey = os.Getenv("YOUTUBE_KEY")
client2 := &http.Client{
Transport: &transport.APIKey{Key: developerKey},
}
service, err := youtube.New(client2)
if err != nil {
log.Fatalf("Error creating new YouTube client: %v", err)
}
call := service.Search.List([]string{}).Q(video_topic).MaxResults(1)
response, err := call.Do()
if err != nil {
log.Fatalf("Error making youtube search API call: %v", err)
}
video_id := response.Items[0].Id.VideoId
//Define video URL
videoURL := "https://www.youtube.com/watch?v=" + video_id
//Play Video
cmd := exec.Command("mpv", videoURL)
cmd.Run()
}
}