2024-09-17 18:44:21 +08:00
|
|
|
package zhipu
|
|
|
|
|
|
|
|
import (
|
2024-09-29 21:20:28 +08:00
|
|
|
"apigo.cc/ai/ai/interface/llm"
|
2024-09-17 18:44:21 +08:00
|
|
|
"apigo.cc/ai/ai/llm/zhipu/zhipu"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) FastMakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
config.Model = ModelCogView3Plus
|
|
|
|
return lm.MakeImage(prompt, config)
|
|
|
|
}
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) BestMakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
config.Model = ModelCogView3
|
|
|
|
return lm.MakeImage(prompt, config)
|
|
|
|
}
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) MakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
c, err := zhipu.NewClient(zhipu.WithAPIKey(lm.config.ApiKey), zhipu.WithBaseURL(lm.config.Endpoint))
|
|
|
|
if err != nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, llm.Usage{}, err
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
config.SetDefault(&lm.config.GCConfig)
|
2024-09-17 18:44:21 +08:00
|
|
|
cc := c.ImageGeneration(config.Model).SetPrompt(prompt)
|
2024-09-18 18:29:21 +08:00
|
|
|
cc.SetSize(config.GetSize())
|
2024-09-17 18:44:21 +08:00
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
t1 := time.Now().UnixMilli()
|
2024-09-17 18:44:21 +08:00
|
|
|
if r, err := cc.Do(context.Background()); err == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
t2 := time.Now().UnixMilli() - t1
|
2024-09-17 18:44:21 +08:00
|
|
|
results := make([]string, 0)
|
|
|
|
for _, item := range r.Data {
|
|
|
|
results = append(results, item.URL)
|
|
|
|
}
|
2024-09-29 21:20:28 +08:00
|
|
|
return results, llm.Usage{
|
|
|
|
UsedTime: t2,
|
|
|
|
}, nil
|
2024-09-17 18:44:21 +08:00
|
|
|
} else {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, llm.Usage{}, err
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) FastMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
config.Model = ModelCogVideoX
|
|
|
|
return lm.MakeVideo(prompt, config)
|
|
|
|
}
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) BestMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
config.Model = ModelCogVideoX
|
|
|
|
return lm.MakeVideo(prompt, config)
|
|
|
|
}
|
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
func (lm *LLM) MakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
|
2024-09-17 18:44:21 +08:00
|
|
|
c, err := zhipu.NewClient(zhipu.WithAPIKey(lm.config.ApiKey), zhipu.WithBaseURL(lm.config.Endpoint))
|
|
|
|
if err != nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, nil, llm.Usage{}, err
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
config.SetDefault(&lm.config.GCConfig)
|
2024-09-17 18:44:21 +08:00
|
|
|
cc := c.VideoGeneration(config.Model).SetPrompt(prompt)
|
2024-09-18 18:29:21 +08:00
|
|
|
cc.SetImageURL(config.GetRef())
|
2024-09-17 18:44:21 +08:00
|
|
|
|
2024-09-29 21:20:28 +08:00
|
|
|
t1 := time.Now().UnixMilli()
|
2024-09-17 18:44:21 +08:00
|
|
|
if resp, err := cc.Do(context.Background()); err == nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
t2 := time.Now().UnixMilli() - t1
|
2024-09-17 18:44:21 +08:00
|
|
|
for i := 0; i < 1200; i++ {
|
|
|
|
r, err := c.AsyncResult(resp.ID).Do(context.Background())
|
|
|
|
if err != nil {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, nil, llm.Usage{}, err
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
if r.TaskStatus == zhipu.VideoGenerationTaskStatusSuccess {
|
|
|
|
covers := make([]string, 0)
|
|
|
|
results := make([]string, 0)
|
|
|
|
for _, item := range r.VideoResult {
|
|
|
|
results = append(results, item.URL)
|
|
|
|
covers = append(covers, item.CoverImageURL)
|
|
|
|
}
|
2024-09-29 21:20:28 +08:00
|
|
|
return results, covers, llm.Usage{
|
|
|
|
UsedTime: t2,
|
|
|
|
}, nil
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
if r.TaskStatus == zhipu.VideoGenerationTaskStatusFail {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, nil, llm.Usage{}, errors.New("fail on task " + resp.ID)
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, nil, llm.Usage{}, errors.New("timeout on task " + resp.ID)
|
2024-09-17 18:44:21 +08:00
|
|
|
} else {
|
2024-09-29 21:20:28 +08:00
|
|
|
return nil, nil, llm.Usage{}, err
|
2024-09-17 18:44:21 +08:00
|
|
|
}
|
|
|
|
}
|