-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatlib.go
126 lines (105 loc) · 2.68 KB
/
chatlib.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"image/png"
"os"
openai "github.com/sashabaranov/go-openai"
)
// GPT
// Initilize the GPT client
func gptInit() *openai.Client {
return openai.NewClient(os.Getenv("OPENAI_API_KEY"))
}
// Ask GPT something and return the reply to the user
func gptComplete(client *openai.Client, msg string) (string, error) {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: msg,
},
},
},
)
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
// ASk DALL-E to generate an image from a prompt
func dalleGenerate(client *openai.Client, msg string) {
ctx := context.Background()
// Sample image by link
reqUrl := openai.ImageRequest{
Prompt: msg,
Size: openai.CreateImageSize1024x1024,
ResponseFormat: openai.CreateImageResponseFormatURL,
N: 1,
}
respUrl, err := client.CreateImage(ctx, reqUrl)
if err != nil {
fmt.Printf("Image creation error: %v\n", err)
return
}
fmt.Println(respUrl.Data[0].URL)
// Example image as base64
reqBase64 := openai.ImageRequest{
Prompt: msg,
Size: openai.CreateImageSize1024x1024,
ResponseFormat: openai.CreateImageResponseFormatB64JSON,
N: 1,
}
respBase64, err := client.CreateImage(ctx, reqBase64)
if err != nil {
fmt.Printf("Image creation error: %v\n", err)
return
}
imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON)
if err != nil {
fmt.Printf("Base64 decode error: %v\n", err)
return
}
r := bytes.NewReader(imgBytes)
imgData, err := png.Decode(r)
if err != nil {
fmt.Printf("PNG decode error: %v\n", err)
return
}
var hexVal, _ = randomHex(7)
var fileName = "image_" + hexVal + ".png"
file, err := os.Create(fileName)
if err != nil {
fmt.Printf("File creation error: %v\n", err)
return
}
defer file.Close()
if err := png.Encode(file, imgData); err != nil {
fmt.Printf("PNG encode error: %v\n", err)
return
}
fmt.Println("The image was saved as " + fileName)
}
/* Helper function to create unique hex IDs */
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
/* Helper function to keep track of the command history for the current session */
func history(userInputHistory []string) {
fmt.Print("\n")
for _, element := range userInputHistory {
fmt.Println("\t" + element)
}
fmt.Print("\n")
}