-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvestigate.go
100 lines (85 loc) · 1.91 KB
/
investigate.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type RequestBody struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
}
func investigation(input string) bool {
if input == "skip" {
fmt.Println("Skipping OpenAI call.")
return true
}
apiKeyBytes, err := ioutil.ReadFile("openaikey.pem")
if err != nil {
fmt.Println("Error reading API key:", err)
}
apiKey := strings.TrimSpace(string(apiKeyBytes))
body := RequestBody{
Model: "gpt-3.5-turbo",
Messages: []Message{
{
Role: "system",
Content: prompt,
},
{
Role: "user",
Content: input,
},
},
}
bodyBytes, err := json.Marshal(body)
if err != nil {
fmt.Println("Error marshalling body:", err)
return false
}
req, _ := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(bodyBytes))
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return false
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return false
}
type Response struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
var respContent Response
err = json.Unmarshal(responseBody, &respContent)
if err != nil {
fmt.Println("Error unmarshalling response body:", err)
return false
}
scoreStr := respContent.Choices[0].Message.Content
score, err := strconv.Atoi(scoreStr)
if err != nil {
fmt.Println("Error converting score to integer:", err)
return false
}
if score > 50 {
return false
}
return true
}