forked from projectdiscovery/dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm.go
48 lines (39 loc) · 793 Bytes
/
llm.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
package llm
import (
"context"
"errors"
"os"
"github.com/sashabaranov/go-openai"
)
var client *openai.Client
func init() {
openaiToken := os.Getenv("OPENAI_API_KEY")
if openaiToken != "" {
client = openai.NewClient(openaiToken)
}
}
func Query(prompt string) (string, error) {
if client == nil {
return "", errors.New("no token defined")
}
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
return "", err
}
if len(resp.Choices) == 0 {
return "", errors.New("no data")
}
data := resp.Choices[0].Message.Content
return data, nil
}