zhipu/gc.go

100 lines
2.5 KiB
Go
Raw Permalink Normal View History

2024-10-25 16:04:43 +08:00
package zhipu
import (
"context"
"errors"
"time"
2024-10-29 12:33:18 +08:00
2024-10-31 15:25:04 +08:00
"apigo.cc/ai"
2024-10-29 12:33:18 +08:00
"github.com/yankeguo/zhipu"
2024-10-25 16:04:43 +08:00
)
2024-10-31 15:25:04 +08:00
func MakeImage(aiConf *ai.AIConfig, conf ai.ImageConfig) (ai.ImageResult, error) {
c, err := getClient(aiConf)
2024-10-25 16:04:43 +08:00
if err != nil {
2024-10-31 15:25:04 +08:00
return ai.ImageResult{}, err
2024-10-25 16:04:43 +08:00
}
2024-10-31 15:25:04 +08:00
cc := c.ImageGeneration(conf.Model).SetPrompt(conf.SystemPrompt + conf.Prompt)
2024-10-25 16:04:43 +08:00
//cc.SetSize(config.GetSize())
t1 := time.Now().UnixMilli()
if r, err := cc.Do(context.Background()); err == nil {
t2 := time.Now().UnixMilli() - t1
results := make([]string, 0)
for _, item := range r.Data {
results = append(results, item.URL)
}
2024-10-31 15:25:04 +08:00
if len(results) == 0 {
results = append(results, "")
}
return ai.ImageResult{
Results: results,
2024-10-25 16:04:43 +08:00
UsedTime: t2,
}, nil
} else {
2024-10-31 15:25:04 +08:00
return ai.ImageResult{}, err
2024-10-25 16:04:43 +08:00
}
}
2024-10-31 15:25:04 +08:00
func MakeVideo(aiConf *ai.AIConfig, conf ai.VideoConfig) (string, error) {
c, err := getClient(aiConf)
if err != nil {
return "", err
}
cc := c.VideoGeneration(conf.Model).SetPrompt(conf.SystemPrompt + conf.Prompt)
if len(conf.Ref) > 0 {
cc.SetImageURL(conf.Ref[0])
}
2024-10-25 16:04:43 +08:00
2024-10-31 15:25:04 +08:00
if resp, err := cc.Do(context.Background()); err == nil {
return resp.ID, nil
} else {
return "", err
}
2024-10-25 16:04:43 +08:00
}
2024-10-31 15:25:04 +08:00
func GetVideoResult(aiConf *ai.AIConfig, taskId string, waitSeconds int) (ai.VideoResult, error) {
c, err := getClient(aiConf)
2024-10-25 16:04:43 +08:00
if err != nil {
2024-10-31 15:25:04 +08:00
return ai.VideoResult{}, err
2024-10-25 16:04:43 +08:00
}
2024-10-31 15:25:04 +08:00
var r zhipu.AsyncResultResponse
for i := 0; i < waitSeconds; i += 3 {
r, err = c.AsyncResult(taskId).Do(context.Background())
if err != nil {
return ai.VideoResult{}, err
}
if r.TaskStatus == zhipu.VideoGenerationTaskStatusSuccess {
results := make([]string, 0)
previews := make([]string, 0)
for _, item := range r.VideoResult {
results = append(results, item.URL)
previews = append(previews, item.CoverImageURL)
2024-10-25 16:04:43 +08:00
}
2024-10-31 15:25:04 +08:00
if len(results) == 0 {
results = append(results, "")
previews = append(previews, "")
2024-10-25 16:04:43 +08:00
}
2024-10-31 15:25:04 +08:00
return ai.VideoResult{
Results: results,
Previews: previews,
UsedTime: 0,
}, nil
}
if r.TaskStatus == zhipu.VideoGenerationTaskStatusFail {
return ai.VideoResult{}, errors.New("fail on video task " + taskId)
} else if r.TaskStatus == zhipu.VideoGenerationTaskStatusProcessing {
if waitSeconds == 0 {
return ai.VideoResult{IsProcessing: true}, nil
2024-10-25 16:04:43 +08:00
}
time.Sleep(3 * time.Second)
2024-10-31 15:25:04 +08:00
} else {
return ai.VideoResult{}, errors.New("unknow status " + r.TaskStatus + " on video task " + taskId)
2024-10-25 16:04:43 +08:00
}
}
2024-10-31 15:25:04 +08:00
return ai.VideoResult{IsProcessing: true}, errors.New("timeout on video task " + taskId)
2024-10-25 16:04:43 +08:00
}