-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (48 loc) · 1.18 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 (
"context"
"log"
"os"
"strings"
openai "github.com/sashabaranov/go-openai"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigFile(".env")
viper.ReadInConfig()
apiKey := viper.GetString("API_KEY")
if apiKey == "" {
panic("Missing API Key")
}
ctx := context.Background()
client := openai.NewClient(apiKey)
const inputFile = "./input_with_code.txt"
fileBytes, err := os.ReadFile(inputFile)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
msgPrefix := "Give me a list of libraries that are used in the code\n```python\n"
msgSuffix := "\n```"
msg := msgPrefix + string(fileBytes) + msgSuffix
resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: msg,
},
},
},
)
if err != nil {
log.Fatalln("Failed to create chat completion: %v", err)
}
output := strings.TrimSpace(resp.Choices[0].Message.Content)
const outputFile = "./output.txt"
err = os.WriteFile(outputFile, []byte(output), os.ModePerm)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
}