-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommands.go
138 lines (126 loc) · 4.25 KB
/
commands.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
package main
import (
"context"
"fmt"
"log"
"strings"
"sync"
"github.com/mrchi/lark-dalle3-bot/pkg/dispatcher"
larkee "github.com/mrchi/lark-dalle3-bot/pkg/larkee"
"github.com/sashabaranov/go-openai"
)
func createImageAndReply(prompt string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) {
// 判断 prompt 不为空
if prompt == "" {
larkeeClient.ReplyTextMessage("[Error]Prompt is empty", messageId, tanantKey)
return
}
// 提交创建请求
writingId, err := bingClient.CreateImage(prompt)
if err != nil {
larkeeClient.ReplyTextMessage(fmt.Sprintf("[Error]%s", err.Error()), messageId, tanantKey)
return
}
// 返回一些提示信息
messages := []string{"Request submitted\nWriting ID is " + writingId}
balance, err := bingClient.GetTokenBalance()
var balanceMsg string
if err != nil {
balanceMsg = fmt.Sprintf("[Error]Failed get token balance, %s", err.Error())
} else if balance == 0 {
balanceMsg = "Tokens are exhausted, generation will take longer and may fail"
} else {
balanceMsg = fmt.Sprintf("There are %d token(s) left", balance)
}
messages = append(messages, balanceMsg)
larkeeClient.ReplyTextMessage(strings.Join(messages, "\n"), messageId, tanantKey)
// 获取生成结果
imageUrls, err := bingClient.QueryResult(writingId, prompt)
if err != nil {
larkeeClient.ReplyTextMessage(fmt.Sprintf("[Error]%s", err.Error()), messageId, tanantKey)
return
}
var wg sync.WaitGroup
wg.Add(len(imageUrls))
imageKeys := make([]string, len(imageUrls))
for idx, imageUrl := range imageUrls {
go func(idx int, imageUrl string) {
defer wg.Done()
reader, err := bingClient.DownloadImage(imageUrl)
if err != nil {
log.Printf("Download image failed, %s", err.Error())
return
}
imageKey, err := larkeeClient.UploadImage(reader)
if err != nil {
log.Printf("Upload image failed, %s", err.Error())
return
}
imageKeys[idx] = imageKey
}(idx, imageUrl)
}
wg.Wait()
larkeeClient.ReplyImagesInteractiveMessage(prompt, imageKeys, messageId, tanantKey)
}
var commandBalance = dispatcher.Command{
Prefix: "/balance",
HelpMsg: "**/balance** Get tokens balance of Bing cookie",
Execute: func(prompt string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) {
balance, err := bingClient.GetTokenBalance()
var replyMsg string
if err != nil {
replyMsg = fmt.Sprintf("[Error]%s", err.Error())
} else if balance == 0 {
replyMsg = "Tokens are exhausted, generation will take longer and may fail"
} else {
replyMsg = fmt.Sprintf("There are %d token(s) left", balance)
}
larkeeClient.ReplyTextMessage(replyMsg, messageId, tanantKey)
},
}
var commandPrompt = dispatcher.Command{
Prefix: "/prompt",
HelpMsg: "**/prompt <Your prompt>** Create image with prompt",
Execute: createImageAndReply,
}
var commandPromptPro = dispatcher.Command{
Prefix: "/prompt_pro",
HelpMsg: `**/prompt_pro <Your prompt>** Create image with prompt revised by GPT`,
Execute: func(prompt string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) {
// 判断 prompt 不为空
if prompt == "" {
larkeeClient.ReplyTextMessage("[Error]Prompt is empty", messageId, tanantKey)
return
}
// Revise prompt by GPT
gptResp, err := gptClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf("Revise `%s` to a concrete DALL·E prompt", prompt),
},
},
},
)
if err == nil {
prompt = gptResp.Choices[0].Message.Content
larkeeClient.ReplyTextMessage(
fmt.Sprintf("GPT revising successful\n\n%s", prompt),
messageId, tanantKey,
)
} else {
larkeeClient.ReplyTextMessage(
fmt.Sprintf("GPT revising failed, fallback to original prompt\n[Error]%s", err),
messageId, tanantKey,
)
}
createImageAndReply(prompt, larkeeClient, messageId, tanantKey)
},
}
func commandHelpExecute(helpMsgs []string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) {
msg := "Welcome to use DALL·E 3 bot. We now support the following commands:\n\n" + strings.Join(helpMsgs, "\n")
larkeeClient.ReplyMarkdownMessage(msg, messageId, tanantKey)
}