-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
56 lines (48 loc) · 1.26 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// Preencha com o seu API key
apiKey := "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"
// Dados da mensagem para o ChatGPT
input := "Canal do Youtube HunCoding"
// Constrói a requisição HTTP POST
requestBody, err := json.Marshal(map[string]interface{}{
"model": "text-davinci-003",
"prompt": input,
"max_tokens": 4000,
"temperature": 1.0,
})
if err != nil {
fmt.Println("Erro ao construir corpo da requisição: ", err.Error())
return
}
req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(requestBody))
if err != nil {
fmt.Println("Erro ao criar requisição: ", err.Error())
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
// Executa a requisição
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Erro ao executar requisição: ", err.Error())
return
}
defer resp.Body.Close()
// Lê a resposta
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Erro ao ler resposta: ", err.Error())
return
}
// Exibe a resposta em formato JSON
fmt.Println(string(body))
}