Skip to content

Commit 1847020

Browse files
authored
Merge pull request #264 from wenerme/wener
feat: default to dall-e-3, support more image res, add image style
2 parents 05b12e3 + 3b5fb83 commit 1847020

File tree

6 files changed

+111
-21
lines changed

6 files changed

+111
-21
lines changed

code/handlers/card_pic_action.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func NewPicResolutionHandler(cardMsg CardMsg, m MessageHandler) CardHandlerFunc
1717
CommonProcessPicResolution(cardMsg, cardAction, m.sessionCache)
1818
return nil, nil
1919
}
20+
if cardMsg.Kind == PicStyleKind {
21+
CommonProcessPicStyle(cardMsg, cardAction, m.sessionCache)
22+
return nil, nil
23+
}
2024
return nil, ErrNextHandler
2125
}
2226
}
@@ -57,13 +61,25 @@ func CommonProcessPicResolution(msg CardMsg,
5761
&msg.MsgId)
5862
}
5963

64+
func CommonProcessPicStyle(msg CardMsg,
65+
cardAction *larkcard.CardAction,
66+
cache services.SessionServiceCacheInterface) {
67+
option := cardAction.Action.Option
68+
fmt.Println(larkcore.Prettify(msg))
69+
cache.SetPicStyle(msg.SessionId, services.PicStyle(option))
70+
//send text
71+
replyMsg(context.Background(), "已更新图片风格为"+option,
72+
&msg.MsgId)
73+
}
74+
6075
func (m MessageHandler) CommonProcessPicMore(msg CardMsg) {
6176
resolution := m.sessionCache.GetPicResolution(msg.SessionId)
77+
style := m.sessionCache.GetPicStyle(msg.SessionId)
6278

6379
logger.Debugf("resolution: %v", resolution)
6480
logger.Debug("msg: %v", msg)
6581
question := msg.Value.(string)
66-
bs64, _ := m.gpt.GenerateOneImage(question, resolution)
82+
bs64, _ := m.gpt.GenerateOneImage(question, resolution, style)
6783
replayImageCardByBase64(context.Background(), bs64, &msg.MsgId,
6884
&msg.SessionId, question)
6985
}

code/handlers/event_pic_action.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (*PicAction) Execute(a *ActionInfo) bool {
2929
a.handler.sessionCache.SetMode(*a.info.sessionId,
3030
services.ModePicCreate)
3131
a.handler.sessionCache.SetPicResolution(*a.info.sessionId,
32-
services.Resolution256)
32+
services.Resolution1024)
3333
sendPicCreateInstructionCard(*a.ctx, a.info.sessionId,
3434
a.info.msgId)
3535
return false
@@ -92,8 +92,10 @@ func (*PicAction) Execute(a *ActionInfo) bool {
9292
if mode == services.ModePicCreate {
9393
resolution := a.handler.sessionCache.GetPicResolution(*a.
9494
info.sessionId)
95+
style := a.handler.sessionCache.GetPicStyle(*a.
96+
info.sessionId)
9597
bs64, err := a.handler.gpt.GenerateOneImage(a.info.qParsed,
96-
resolution)
98+
resolution, style)
9799
if err != nil {
98100
replyMsg(*a.ctx, fmt.Sprintf(
99101
"🤖️:图片生成失败,请稍后再试~\n错误信息: %v", err), a.info.msgId)

code/handlers/msg.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
ClearCardKind = CardKind("clear") // 清空上下文
2525
PicModeChangeKind = CardKind("pic_mode_change") // 切换图片创作模式
2626
PicResolutionKind = CardKind("pic_resolution") // 图片分辨率调整
27+
PicStyleKind = CardKind("pic_style") // 图片风格调整
2728
PicTextMoreKind = CardKind("pic_text_more") // 重新根据文本生成图片
2829
PicVarMoreKind = CardKind("pic_var_more") // 变量图片
2930
RoleTagsChooseKind = CardKind("role_tags_choose") // 内置角色所属标签选择
@@ -324,29 +325,56 @@ func withOneBtn(btn *larkcard.MessageCardEmbedButton) larkcard.
324325

325326
func withPicResolutionBtn(sessionID *string) larkcard.
326327
MessageCardElement {
327-
cancelMenu := newMenu("默认分辨率",
328+
resolutionMenu := newMenu("默认分辨率",
328329
map[string]interface{}{
329330
"value": "0",
330331
"kind": PicResolutionKind,
331332
"sessionId": *sessionID,
332333
"msgId": *sessionID,
333334
},
335+
// dall-e-2 256, 512, 1024
336+
//MenuOption{
337+
// label: "256x256",
338+
// value: string(services.Resolution256),
339+
//},
340+
//MenuOption{
341+
// label: "512x512",
342+
// value: string(services.Resolution512),
343+
//},
344+
// dall-e-3
334345
MenuOption{
335-
label: "256x256",
336-
value: string(services.Resolution256),
346+
label: "1024x1024",
347+
value: string(services.Resolution1024),
337348
},
338349
MenuOption{
339-
label: "512x512",
340-
value: string(services.Resolution512),
350+
label: "1024x1792",
351+
value: string(services.Resolution10241792),
341352
},
342353
MenuOption{
343-
label: "1024x1024",
344-
value: string(services.Resolution1024),
354+
label: "1792x1024",
355+
value: string(services.Resolution17921024),
356+
},
357+
)
358+
359+
styleMenu := newMenu("风格",
360+
map[string]interface{}{
361+
"value": "0",
362+
"kind": PicStyleKind,
363+
"sessionId": *sessionID,
364+
"msgId": *sessionID,
365+
},
366+
MenuOption{
367+
label: "生动风格",
368+
value: string(services.PicStyleVivid),
369+
},
370+
MenuOption{
371+
label: "自然风格",
372+
value: string(services.PicStyleNatural),
345373
},
346374
)
347375

348376
actions := larkcard.NewMessageCardAction().
349-
Actions([]larkcard.MessageCardActionElement{cancelMenu}).
377+
Actions([]larkcard.MessageCardActionElement{resolutionMenu, styleMenu}).
350378
Layout(larkcard.MessageCardActionLayoutFlow.Ptr()).
351379
Build()
352380
return actions

code/services/openai/gpt3_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestGenerateOneImage(t *testing.T) {
2828
gpt := NewChatGPT(*config)
2929
prompt := "a red apple"
3030
size := "256x256"
31-
imageURL, err := gpt.GenerateOneImage(prompt, size)
31+
imageURL, err := gpt.GenerateOneImage(prompt, size, "")
3232
if err != nil {
3333
t.Errorf("TestGenerateOneImage failed with error: %v", err)
3434
}

code/services/openai/picture.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type ImageGenerationRequestBody struct {
1616
N int `json:"n"`
1717
Size string `json:"size"`
1818
ResponseFormat string `json:"response_format"`
19+
Model string `json:"model,omitempty"`
20+
Style string `json:"style,omitempty"`
1921
}
2022

2123
type ImageResponseBody struct {
@@ -33,12 +35,14 @@ type ImageVariantRequestBody struct {
3335
}
3436

3537
func (gpt *ChatGPT) GenerateImage(prompt string, size string,
36-
n int) ([]string, error) {
38+
n int, style string) ([]string, error) {
3739
requestBody := ImageGenerationRequestBody{
3840
Prompt: prompt,
3941
N: n,
4042
Size: size,
4143
ResponseFormat: "b64_json",
44+
Model: "dall-e-3",
45+
Style: style,
4246
}
4347

4448
imageResponseBody := &ImageResponseBody{}
@@ -57,8 +61,8 @@ func (gpt *ChatGPT) GenerateImage(prompt string, size string,
5761
}
5862

5963
func (gpt *ChatGPT) GenerateOneImage(prompt string,
60-
size string) (string, error) {
61-
b64s, err := gpt.GenerateImage(prompt, size, 1)
64+
size string, style string) (string, error) {
65+
b64s, err := gpt.GenerateImage(prompt, size, 1, style)
6266
if err != nil {
6367
return "", err
6468
}
@@ -67,7 +71,8 @@ func (gpt *ChatGPT) GenerateOneImage(prompt string,
6771

6872
func (gpt *ChatGPT) GenerateOneImageWithDefaultSize(
6973
prompt string) (string, error) {
70-
return gpt.GenerateOneImage(prompt, "512x512")
74+
// works for dall-e 2&3
75+
return gpt.GenerateOneImage(prompt, "1024x1024", "")
7176
}
7277

7378
func (gpt *ChatGPT) GenerateImageVariation(images string,

code/services/sessionCache.go

+44-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ type SessionService struct {
1313
}
1414
type PicSetting struct {
1515
resolution Resolution
16+
style PicStyle
1617
}
1718
type Resolution string
19+
type PicStyle string
1820

1921
type SessionMeta struct {
2022
Mode SessionMode `json:"mode"`
@@ -24,9 +26,15 @@ type SessionMeta struct {
2426
}
2527

2628
const (
27-
Resolution256 Resolution = "256x256"
28-
Resolution512 Resolution = "512x512"
29-
Resolution1024 Resolution = "1024x1024"
29+
Resolution256 Resolution = "256x256"
30+
Resolution512 Resolution = "512x512"
31+
Resolution1024 Resolution = "1024x1024"
32+
Resolution10241792 Resolution = "1024x1792"
33+
Resolution17921024 Resolution = "1792x1024"
34+
)
35+
const (
36+
PicStyleVivid PicStyle = "vivid"
37+
PicStyleNatural PicStyle = "natural"
3038
)
3139
const (
3240
ModePicCreate SessionMode = "pic_create"
@@ -44,7 +52,9 @@ type SessionServiceCacheInterface interface {
4452
GetAIMode(sessionId string) openai.AIMode
4553
SetAIMode(sessionId string, aiMode openai.AIMode)
4654
SetPicResolution(sessionId string, resolution Resolution)
55+
SetPicStyle(sessionId string, resolution PicStyle)
4756
GetPicResolution(sessionId string) string
57+
GetPicStyle(sessionId string) string
4858
Clear(sessionId string)
4959
}
5060

@@ -141,16 +151,45 @@ func (s *SessionService) SetMsg(sessionId string, msg []openai.Messages) {
141151
s.cache.Set(sessionId, sessionMeta, maxCacheTime)
142152
}
143153

154+
func (s *SessionService) SetPicStyle(sessionId string, style PicStyle) {
155+
maxCacheTime := time.Hour * 12
156+
157+
switch style {
158+
case PicStyleVivid, PicStyleNatural:
159+
default:
160+
style = PicStyleVivid
161+
}
162+
163+
sessionContext, ok := s.cache.Get(sessionId)
164+
if !ok {
165+
sessionMeta := &SessionMeta{PicSetting: PicSetting{style: style}}
166+
s.cache.Set(sessionId, sessionMeta, maxCacheTime)
167+
return
168+
}
169+
sessionMeta := sessionContext.(*SessionMeta)
170+
sessionMeta.PicSetting.style = style
171+
s.cache.Set(sessionId, sessionMeta, maxCacheTime)
172+
}
173+
174+
func (s *SessionService) GetPicStyle(sessionId string) string {
175+
sessionContext, ok := s.cache.Get(sessionId)
176+
if !ok {
177+
return string(PicStyleVivid)
178+
}
179+
sessionMeta := sessionContext.(*SessionMeta)
180+
return string(sessionMeta.PicSetting.style)
181+
}
182+
144183
func (s *SessionService) SetPicResolution(sessionId string,
145184
resolution Resolution) {
146185
maxCacheTime := time.Hour * 12
147186

148187
//if not in [Resolution256, Resolution512, Resolution1024] then set
149188
//to Resolution256
150189
switch resolution {
151-
case Resolution256, Resolution512, Resolution1024:
190+
case Resolution256, Resolution512, Resolution1024, Resolution10241792, Resolution17921024:
152191
default:
153-
resolution = Resolution256
192+
resolution = Resolution1024
154193
}
155194

156195
sessionContext, ok := s.cache.Get(sessionId)

0 commit comments

Comments
 (0)