-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenai.go
45 lines (36 loc) · 1.08 KB
/
openai.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
package openai
import (
"context"
_ "embed"
"fmt"
"github.com/sashabaranov/go-openai"
)
//go:embed assets/review.txt
var PromptReview string
const (
PromptDescribeChanges = "Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix\n%s\n"
PromptOverallDescribe = "Below comments are generated by AI, Generate a GitHub pull request description based on the following comments without basic prefix in markdown format with ### Description and ### Changes blocks:\n%s\n"
)
type Client struct {
client *openai.Client
}
func NewClient(token string) *Client {
return &Client{
client: openai.NewClient(token),
}
}
func (o *Client) ChatCompletion(ctx context.Context, messages []openai.ChatCompletionMessage) (string, error) {
resp, err := o.client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: messages,
Temperature: 0.1,
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return "", err
}
return resp.Choices[0].Message.Content, nil
}