-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (47 loc) · 1.16 KB
/
main.go
File metadata and controls
64 lines (47 loc) · 1.16 KB
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
package main
import (
"context"
"errors"
"log"
"os"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
)
func main() {
// call to an AI engine to create some content
// just write to the FS or cmd line
// content can be returned as markdown, CSV, JSON and then added to the FS maybe
// ....
if err := godotenv.Load(".env"); err != nil {
log.Fatalf("error loading env %s", err.Error())
}
err := GetAIContent()
if err != nil {
log.Fatalf("error from AI call %s", err.Error())
}
}
func GetAIContent() error {
apiKey := os.Getenv("OPENAI_API_KEY")
client := openai.NewClient(apiKey)
ctx := context.Background()
prompt, err := os.ReadFile("prompt.md")
if err != nil {
return err
}
promptStr := string(prompt)
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: openai.GPT4, // Or "gpt-3.5-turbo" if you are using GPT-3.5
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: promptStr,
},
},
})
if err != nil {
return errors.New("chat completion error:" + err.Error())
}
data := resp.Choices[0].Message.Content
log.Println(data)
return nil
}