2024-09-17 18:44:21 +08:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"apigo.cc/ai/ai/llm"
|
|
|
|
"context"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// func (lm *LLM) FastMakeImage(prompt, size, refImage string) ([]string, error) {
|
|
|
|
// return lm.MakeImage(ModelDallE3Std, prompt, size, refImage)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func (lm *LLM) BestMakeImage(prompt, size, refImage string) ([]string, error) {
|
|
|
|
// return lm.MakeImage(ModelDallE3HD, prompt, size, refImage)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func (lm *LLM) MakeImage(model, prompt, size, refImage string) ([]string, error) {
|
|
|
|
func (lm *LLM) FastMakeImage(prompt string, config llm.GCConfig) ([]string, error) {
|
|
|
|
config.Model = ModelDallE3Std
|
|
|
|
return lm.MakeImage(prompt, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LLM) BestMakeImage(prompt string, config llm.GCConfig) ([]string, error) {
|
|
|
|
config.Model = ModelDallE3HD
|
|
|
|
return lm.MakeImage(prompt, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LLM) MakeImage(prompt string, config llm.GCConfig) ([]string, error) {
|
|
|
|
openaiConf := openai.DefaultConfig(lm.config.ApiKey)
|
|
|
|
if lm.config.Endpoint != "" {
|
|
|
|
openaiConf.BaseURL = lm.config.Endpoint
|
|
|
|
}
|
2024-09-18 18:29:21 +08:00
|
|
|
config.SetDefault(&lm.config.GCConfig)
|
2024-09-17 18:44:21 +08:00
|
|
|
c := openai.NewClientWithConfig(openaiConf)
|
|
|
|
style := openai.CreateImageStyleVivid
|
|
|
|
if (!strings.Contains(prompt, "vivid") || !strings.Contains(prompt, "生动的")) && (strings.Contains(prompt, "natural") || strings.Contains(prompt, "自然的")) {
|
|
|
|
style = openai.CreateImageStyleNatural
|
|
|
|
}
|
|
|
|
quality := openai.CreateImageQualityStandard
|
2024-09-18 18:29:21 +08:00
|
|
|
model := config.GetModel()
|
|
|
|
if strings.HasSuffix(model, "-hd") {
|
2024-09-17 18:44:21 +08:00
|
|
|
quality = openai.CreateImageQualityHD
|
2024-09-18 18:29:21 +08:00
|
|
|
model = model[0 : len(model)-3]
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
r, err := c.CreateImage(context.Background(), openai.ImageRequest{
|
|
|
|
Prompt: prompt,
|
2024-09-18 18:29:21 +08:00
|
|
|
Model: model,
|
2024-09-17 18:44:21 +08:00
|
|
|
Quality: quality,
|
2024-09-18 18:29:21 +08:00
|
|
|
Size: config.GetSize(),
|
2024-09-17 18:44:21 +08:00
|
|
|
Style: style,
|
|
|
|
ResponseFormat: openai.CreateImageResponseFormatURL,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
results := make([]string, 0)
|
|
|
|
for _, item := range r.Data {
|
|
|
|
results = append(results, item.URL)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LLM) FastMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, error) {
|
|
|
|
return lm.MakeVideo(prompt, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LLM) BestMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, error) {
|
|
|
|
return lm.MakeVideo(prompt, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LLM) MakeVideo(prompt string, config llm.GCConfig) ([]string, []string, error) {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|