huoshan/llm_unfinished/gc.go
2024-10-29 12:04:22 +08:00

88 lines
2.2 KiB
Go

package huoshan
import (
"errors"
"strings"
"time"
"apigo.cc/ai/llm/llm"
"github.com/ssgo/u"
"github.com/volcengine/volc-sdk-golang/service/visual/model"
)
func (lm *LLM) FastMakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
config.Model = ModelT2I14
if config.Ref != "" {
config.Model = ModelI2I14IP
}
return lm.MakeImage(prompt, config)
}
func (lm *LLM) BestMakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
config.Model = ModelT2IXL
if config.Ref != "" {
config.Model = ModelI2IXL
}
return lm.MakeImage(prompt, config)
}
func (lm *LLM) MakeImage(prompt string, config llm.GCConfig) ([]string, llm.Usage, error) {
config.SetDefault(&lm.config.GCConfig)
modelA := strings.SplitN(config.GetModel(), ":", 2)
sizeA := strings.SplitN(config.GetSize(), "x", 2)
if len(sizeA) == 1 {
sizeA = append(sizeA, sizeA[0])
}
ref := config.GetRef()
vis := lm.getGCClient()
data := map[string]any{
"req_key": modelA[0],
"prompt": prompt,
"width": u.Int(sizeA[0]),
"height": u.Int(sizeA[1]),
"return_url": true,
}
if len(modelA) > 1 {
data["model_version"] = modelA[1]
}
// TODO llm 支持动态额外参数
t1 := time.Now().UnixMilli()
var resp *model.VisualPubResult
var status int
var err error
if ref == "" {
resp, status, err = vis.Text2ImgXLSft(data)
} else {
if strings.Contains(ref, "://") {
data["image_url"] = []string{ref}
} else {
data["binary_data_base64"] = []string{ref}
}
resp, status, err = vis.Img2ImgXLSft(data)
}
t2 := time.Now().UnixMilli() - t1
if err != nil {
return nil, llm.Usage{}, err
}
if status != 200 {
return nil, llm.Usage{}, errors.New(resp.Message)
}
return resp.Data.ImageUrls, llm.Usage{
UsedTime: t2,
}, nil
}
func (lm *LLM) FastMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
return lm.MakeVideo(prompt, config)
}
func (lm *LLM) BestMakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
return lm.MakeVideo(prompt, config)
}
func (lm *LLM) MakeVideo(prompt string, config llm.GCConfig) ([]string, []string, llm.Usage, error) {
return nil, nil, llm.Usage{}, errors.New("not support")
}