96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package openai
|
|
|
|
import (
|
|
"context"
|
|
"github.com/sashabaranov/go-openai"
|
|
"strings"
|
|
)
|
|
|
|
func (ag *Agent) FastMakeImage(prompt, size, refImage string) ([]string, error) {
|
|
return ag.MakeImage(ModelDallE3Std, prompt, size, refImage)
|
|
}
|
|
|
|
func (ag *Agent) BestMakeImage(prompt, size, refImage string) ([]string, error) {
|
|
return ag.MakeImage(ModelDallE3HD, prompt, size, refImage)
|
|
}
|
|
|
|
func (ag *Agent) MakeImage(model, prompt, size, refImage string) ([]string, error) {
|
|
openaiConf := openai.DefaultConfig(ag.config.ApiKey)
|
|
if ag.config.Endpoint != "" {
|
|
openaiConf.BaseURL = ag.config.Endpoint
|
|
}
|
|
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
|
|
if strings.HasSuffix(model, "-hd") {
|
|
quality = openai.CreateImageQualityHD
|
|
model = model[0 : len(model)-3]
|
|
}
|
|
r, err := c.CreateImage(context.Background(), openai.ImageRequest{
|
|
Prompt: prompt,
|
|
Model: model,
|
|
Quality: quality,
|
|
Size: size,
|
|
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 (ag *Agent) FastMakeVideo(prompt, size, refImage string) ([]string, []string, error) {
|
|
return ag.MakeVideo("", prompt, size, refImage)
|
|
}
|
|
|
|
func (ag *Agent) BestMakeVideo(prompt, size, refImage string) ([]string, []string, error) {
|
|
return ag.MakeVideo("", prompt, size, refImage)
|
|
}
|
|
|
|
func (ag *Agent) MakeVideo(model, prompt, size, refImage string) ([]string, []string, error) {
|
|
//c, err := openai.NewClient(openai.WithAPIKey(ag.config.ApiKey), openai.WithBaseURL(ag.config.Endpoint))
|
|
//if err != nil {
|
|
// return nil, nil, err
|
|
//}
|
|
//
|
|
//cc := c.VideoGeneration(model).SetPrompt(prompt)
|
|
//if refImage != "" {
|
|
// cc.SetImageURL(refImage)
|
|
//}
|
|
//
|
|
//if resp, err := cc.Do(context.Background()); err == nil {
|
|
// for i := 0; i < 1200; i++ {
|
|
// r, err := c.AsyncResult(resp.ID).Do(context.Background())
|
|
// if err != nil {
|
|
// return nil, nil, err
|
|
// }
|
|
// if r.TaskStatus == openai.VideoGenerationTaskStatusSuccess {
|
|
// covers := make([]string, 0)
|
|
// results := make([]string, 0)
|
|
// for _, item := range r.VideoResult {
|
|
// results = append(results, item.URL)
|
|
// covers = append(covers, item.CoverImageURL)
|
|
// }
|
|
// return results, covers, nil
|
|
// }
|
|
// if r.TaskStatus == openai.VideoGenerationTaskStatusFail {
|
|
// return nil, nil, errors.New("fail on task " + resp.ID)
|
|
// }
|
|
// time.Sleep(3 * time.Second)
|
|
// }
|
|
// return nil, nil, errors.New("timeout on task " + resp.ID)
|
|
//} else {
|
|
// return nil, nil, err
|
|
//}
|
|
return nil, nil, nil
|
|
}
|