2024-10-25 16:04:36 +08:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-31 15:27:22 +08:00
|
|
|
"fmt"
|
2024-10-25 16:04:36 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-10-31 15:27:22 +08:00
|
|
|
"apigo.cc/ai"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
|
|
)
|
2024-10-25 16:04:36 +08:00
|
|
|
|
2024-10-31 15:27:22 +08:00
|
|
|
func MakeImage(aiConf *ai.AIConfig, conf ai.ImageConfig) (ai.ImageResult, error) {
|
|
|
|
c := getClient(aiConf)
|
2024-10-25 16:04:36 +08:00
|
|
|
|
2024-10-31 15:27:22 +08:00
|
|
|
style := conf.Style
|
|
|
|
if style == "" && (strings.Contains(conf.Prompt, "natural") || strings.Contains(conf.Prompt, "自然")) {
|
2024-10-25 16:04:36 +08:00
|
|
|
style = openai.CreateImageStyleNatural
|
|
|
|
}
|
2024-10-31 15:27:22 +08:00
|
|
|
if style == "" {
|
|
|
|
style = openai.CreateImageStyleVivid
|
|
|
|
}
|
|
|
|
quality := conf.Quality
|
|
|
|
if quality == "" {
|
|
|
|
quality = openai.CreateImageQualityStandard
|
2024-10-25 16:04:36 +08:00
|
|
|
}
|
2024-10-31 15:27:22 +08:00
|
|
|
|
2024-10-25 16:04:36 +08:00
|
|
|
t1 := time.Now().UnixMilli()
|
|
|
|
r, err := c.CreateImage(context.Background(), openai.ImageRequest{
|
2024-10-31 15:27:22 +08:00
|
|
|
Prompt: conf.SystemPrompt + conf.Prompt,
|
|
|
|
Model: conf.Model,
|
2024-10-25 16:04:36 +08:00
|
|
|
Quality: quality,
|
2024-10-31 15:27:22 +08:00
|
|
|
Size: fmt.Sprintf("%dx%d", conf.Width, conf.Height),
|
2024-10-25 16:04:36 +08:00
|
|
|
Style: style,
|
|
|
|
ResponseFormat: openai.CreateImageResponseFormatURL,
|
|
|
|
})
|
|
|
|
t2 := time.Now().UnixMilli() - t1
|
|
|
|
if err == nil {
|
|
|
|
results := make([]string, 0)
|
|
|
|
for _, item := range r.Data {
|
|
|
|
results = append(results, item.URL)
|
|
|
|
}
|
2024-10-31 15:27:22 +08:00
|
|
|
return ai.ImageResult{
|
|
|
|
Results: results,
|
|
|
|
UsedTime: t2,
|
2024-10-25 16:04:36 +08:00
|
|
|
}, nil
|
|
|
|
} else {
|
2024-10-31 15:27:22 +08:00
|
|
|
return ai.ImageResult{}, err
|
2024-10-25 16:04:36 +08:00
|
|
|
}
|
|
|
|
}
|