-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
235 lines (207 loc) · 6.22 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (C) 2023 Asim Ihsan
* SPDX-License-Identifier: AGPL-3.0-only
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>
*/
package openai
import (
"context"
_ "embed"
"encoding/base64"
"errors"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog"
goopenai "github.com/sashabaranov/go-openai"
"go.uber.org/ratelimit"
"strconv"
"strings"
"time"
)
var (
FailedToCompletePrompt = errors.New("failed to complete prompt")
//go:embed initial_prompt_01.txt
initialPrompt string
)
type OpenAI struct {
client *goopenai.Client
initialPrompt string
limiter ratelimit.Limiter
}
func NewOpenAI(token string) *OpenAI {
client := goopenai.NewClient(token)
limiter := ratelimit.New(1)
return &OpenAI{
client: client,
initialPrompt: initialPrompt,
limiter: limiter,
}
}
type ChatMessage struct {
FromHuman bool
Text string
}
// GetCurrentDate returns the current date e.g. 2023-02-04.
func GetCurrentDate() string {
now := time.Now().Unix()
tm := time.Unix(now, 0)
return tm.Format("2006-01-02")
}
func (o *OpenAI) CompleteChat(messages []*ChatMessage, ctx context.Context, zlog *zerolog.Logger) (string, error) {
o.limiter.Take()
var resultErr error
requestMessages := make([]goopenai.ChatCompletionMessage, 0, len(messages))
for i := 0; i < len(messages); i++ {
message := messages[i]
if message.FromHuman {
requestMessages = append(requestMessages, goopenai.ChatCompletionMessage{
Role: "user",
Content: message.Text,
})
} else {
requestMessages = append(requestMessages, goopenai.ChatCompletionMessage{
Role: "assistant",
Content: message.Text,
})
}
}
completion, err := o.ChatComplete(requestMessages, ctx, zlog)
if err != nil {
zlog.Error().Err(err).Msg("Failed to complete prompt")
resultErr = multierror.Append(resultErr, err)
return "", resultErr
}
zlog.Debug().Interface("requestMessages", requestMessages).Msgf("completion: %s", completion)
return completion, nil
}
func (o *OpenAI) ChatComplete(
messages []goopenai.ChatCompletionMessage,
ctx context.Context,
zlog *zerolog.Logger,
) (string, error) {
o.limiter.Take()
var resultErr error
completion, err := o.client.CreateChatCompletion(ctx, goopenai.ChatCompletionRequest{
Model: goopenai.GPT4,
Messages: messages,
MaxTokens: 4096,
Temperature: 0.0,
TopP: 1.0,
Stream: false,
Stop: []string{"<|endoftext|>"},
})
if err != nil {
zlog.Error().Err(err).Msg("Failed to complete chat")
resultErr = multierror.Append(resultErr, err, FailedToCompletePrompt)
return "", resultErr
}
return completion.Choices[0].Message.Content, resultErr
}
func (o *OpenAI) Complete(prompt string, ctx context.Context, zlog *zerolog.Logger) (string, error) {
o.limiter.Take()
var resultErr error
completion, err := o.client.CreateCompletion(ctx, goopenai.CompletionRequest{
Model: goopenai.GPT3TextDavinci003,
MaxTokens: 2048,
Prompt: prompt,
Temperature: 0.0,
TopP: 1.0,
Stop: []string{"<|endoftext|>"},
})
if err != nil {
zlog.Error().Err(err).Msg("Failed to complete prompt")
resultErr = multierror.Append(resultErr, err, FailedToCompletePrompt)
return "", resultErr
}
return completion.Choices[0].Text, resultErr
}
type CreateImageResponse struct {
Images []Image `json:"images"`
}
type Image struct {
Data []byte `json:"data"`
}
func (o *OpenAI) CreateImage(prompt string, ctx context.Context, zlog *zerolog.Logger) (*CreateImageResponse, error) {
o.limiter.Take()
resp, err := o.client.CreateImage(ctx, goopenai.ImageRequest{
Prompt: prompt,
N: 1,
Size: goopenai.CreateImageSize1024x1024,
ResponseFormat: goopenai.CreateImageResponseFormatB64JSON,
})
if err != nil {
zlog.Error().Err(err).Msg("Failed to create image")
return nil, err
}
result := CreateImageResponse{Images: make([]Image, 0, len(resp.Data))}
for _, data := range resp.Data {
imageData, err := base64.StdEncoding.DecodeString(data.B64JSON)
if err != nil {
zlog.Error().Err(err).Msg("Failed to decode image data")
return nil, err
}
result.Images = append(result.Images, Image{Data: imageData})
}
return &result, nil
}
func (o *OpenAI) Close(*zerolog.Logger) error {
//o.client.HTTPClient.CloseIdleConnections()
return nil
}
func (o *OpenAI) Summarize(
content string,
words int,
ctx context.Context,
zlog *zerolog.Logger,
) (string, error) {
o.limiter.Take()
var promptBuilder strings.Builder
promptBuilder.WriteString(o.initialPrompt)
promptBuilder.WriteString(GetCurrentDate())
promptBuilder.WriteString("\n\n")
promptBuilder.WriteString("Summarize the following message into less than ")
promptBuilder.WriteString(strconv.Itoa(words))
promptBuilder.WriteString(" words:\n\n")
promptBuilder.WriteString(content)
prompt := promptBuilder.String()
completion, err := o.client.CreateCompletion(ctx, goopenai.CompletionRequest{
Model: goopenai.GPT3TextDavinci003,
MaxTokens: 16,
Prompt: prompt,
Stop: []string{"<|endoftext|>"},
})
if err != nil {
zlog.Error().Err(err).Msg("Failed to complete prompt")
return "", err
}
// trim space from summary
summary := strings.TrimSpace(completion.Choices[0].Text)
// trim punctuation from summary
summary = strings.TrimRight(summary, ".")
// Discord only allows up to 100 characters. Split the string on spaces, and then join chunks in a string builder
// until you hit < 100 characters.
if len(summary) > 100 {
var sb strings.Builder
words := strings.Split(summary, " ")
for i, word := range words {
if sb.Len()+len(word)+1 >= 100 {
break
}
sb.WriteString(word)
if i != len(words)-1 {
sb.WriteString(" ")
}
}
summary = sb.String()
}
return summary, err
}