-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.go
62 lines (51 loc) · 2.03 KB
/
gpt.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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
var (
apiKey = os.Getenv("OPENAI_API_KEY")
systemAI = `
Respond with "No" (No is a pass condition in my script) or the go-fuzz test function only(Don't add explanation and how-to guide). Source: https://go.dev/doc/security/fuzz/.
Evaluate the function to check if it's even worth fuzzing.
Note: Good functions are those that perform some kind of parsing, decoding, or unmarshaling (this is not always true).
Don't create fuzz tests for functions (from the project I gave you) that rely on official Golang libraries (e.g., JSON, HTTP, net, etc.).
`
)
func processGPT(funcName string, functionCode string) {
if apiKey == "" {
fmt.Println(colorRed + "Error: OPENAI_API_KEY environment variable not set." + colorReset)
os.Exit(1)
}
prompt := fmt.Sprintf("Function name:\n%s\nFunction declaration:\n%s", funcName, functionCode)
client := openai.NewClient(
option.WithAPIKey(apiKey),
)
ctx := context.Background()
messages := openai.F([]openai.ChatCompletionMessageParamUnion{
openai.UserMessage(systemAI + "\n\n" + prompt),
})
completion, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
Messages: messages,
Model: openai.F(openai.ChatModelO1Mini),
Temperature: openai.F(1.000000),
TopP: openai.F(1.000000),
})
if err != nil {
fmt.Println(colorMagenta + strings.Repeat("-", 79) + "\n" + colorReset)
fmt.Println(colorRed + "Function Name: " + funcName + colorReset)
fmt.Printf(colorCyan+"ChatCompletion error: %v\n"+colorReset, err)
fmt.Println(colorMagenta + strings.Repeat("-", 79) + "\n" + colorReset)
return
}
if completion.Choices[0].Message.Content != "No" {
fmt.Println(colorMagenta + strings.Repeat("-", 79) + "\n" + colorReset)
fmt.Println(colorRed + "Function Name: " + funcName + colorReset)
fmt.Println(colorCyan + completion.Choices[0].Message.Content + colorReset)
fmt.Println(colorMagenta + strings.Repeat("-", 79) + "\n" + colorReset)
}
}