51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package openai
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"apigo.cc/ai"
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
func MakeImage(aiConf *ai.AIConfig, conf ai.ImageConfig) (ai.ImageResult, error) {
|
|
c := getClient(aiConf)
|
|
|
|
style := conf.Style
|
|
if style == "" && (strings.Contains(conf.Prompt, "natural") || strings.Contains(conf.Prompt, "自然")) {
|
|
style = openai.CreateImageStyleNatural
|
|
}
|
|
if style == "" {
|
|
style = openai.CreateImageStyleVivid
|
|
}
|
|
quality := conf.Quality
|
|
if quality == "" {
|
|
quality = openai.CreateImageQualityStandard
|
|
}
|
|
|
|
t1 := time.Now().UnixMilli()
|
|
r, err := c.CreateImage(context.Background(), openai.ImageRequest{
|
|
Prompt: conf.SystemPrompt + conf.Prompt,
|
|
Model: conf.Model,
|
|
Quality: quality,
|
|
Size: fmt.Sprintf("%dx%d", conf.Width, conf.Height),
|
|
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)
|
|
}
|
|
return ai.ImageResult{
|
|
Results: results,
|
|
UsedTime: t2,
|
|
}, nil
|
|
} else {
|
|
return ai.ImageResult{}, err
|
|
}
|
|
}
|