feat: add openai sdk for kling
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/model"
|
"one-api/model"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bytedance/gopkg/util/logger"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -303,14 +305,6 @@ func (a *TaskAdaptor) createJWTToken() (string, error) {
|
|||||||
return a.createJWTTokenWithKey(a.apiKey)
|
return a.createJWTTokenWithKey(a.apiKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
|
|
||||||
// parts := strings.Split(apiKey, "|")
|
|
||||||
// if len(parts) != 2 {
|
|
||||||
// return "", fmt.Errorf("invalid API key format, expected 'access_key,secret_key'")
|
|
||||||
// }
|
|
||||||
// return a.createJWTTokenWithKey(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
|
func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
|
||||||
if isNewAPIRelay(apiKey) {
|
if isNewAPIRelay(apiKey) {
|
||||||
return apiKey, nil // new api relay
|
return apiKey, nil // new api relay
|
||||||
@@ -369,3 +363,50 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
|
|||||||
func isNewAPIRelay(apiKey string) bool {
|
func isNewAPIRelay(apiKey string) bool {
|
||||||
return strings.HasPrefix(apiKey, "sk-")
|
return strings.HasPrefix(apiKey, "sk-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *TaskAdaptor) ConvertToOpenAIVideo(originTask *model.Task) (*relaycommon.OpenAIVideo, error) {
|
||||||
|
var klingResp responsePayload
|
||||||
|
if err := json.Unmarshal(originTask.Data, &klingResp); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "unmarshal kling task data failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
convertProgress := func(progress string) int {
|
||||||
|
progress = strings.TrimSuffix(progress, "%")
|
||||||
|
p, err := strconv.Atoi(progress)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("convert progress failed, progress: %s, err: %v", progress, err)
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
openAIVideo := &relaycommon.OpenAIVideo{
|
||||||
|
ID: klingResp.Data.TaskId,
|
||||||
|
Object: "video",
|
||||||
|
//Model: "kling-v1", //todo save model
|
||||||
|
Status: string(originTask.Status),
|
||||||
|
CreatedAt: klingResp.Data.CreatedAt,
|
||||||
|
CompletedAt: klingResp.Data.UpdatedAt,
|
||||||
|
Metadata: make(map[string]any),
|
||||||
|
Progress: convertProgress(originTask.Progress),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频 URL
|
||||||
|
if len(klingResp.Data.TaskResult.Videos) > 0 {
|
||||||
|
video := klingResp.Data.TaskResult.Videos[0]
|
||||||
|
if video.Url != "" {
|
||||||
|
openAIVideo.Metadata["url"] = video.Url
|
||||||
|
}
|
||||||
|
if video.Duration != "" {
|
||||||
|
openAIVideo.Seconds = video.Duration
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if klingResp.Code != 0 && klingResp.Message != "" {
|
||||||
|
openAIVideo.Error = &relaycommon.OpenAIVideoError{
|
||||||
|
Message: klingResp.Message,
|
||||||
|
Code: fmt.Sprintf("%d", klingResp.Code),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return openAIVideo, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -553,7 +553,7 @@ func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOther
|
|||||||
|
|
||||||
type OpenAIVideo struct {
|
type OpenAIVideo struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
TaskID string `json:"task_id,omitempty"` //兼容旧接口
|
TaskID string `json:"task_id,omitempty"` //兼容旧接口 待废弃
|
||||||
Object string `json:"object"`
|
Object string `json:"object"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
@@ -564,8 +564,10 @@ type OpenAIVideo struct {
|
|||||||
Seconds string `json:"seconds,omitempty"`
|
Seconds string `json:"seconds,omitempty"`
|
||||||
Size string `json:"size,omitempty"`
|
Size string `json:"size,omitempty"`
|
||||||
RemixedFromVideoID string `json:"remixed_from_video_id,omitempty"`
|
RemixedFromVideoID string `json:"remixed_from_video_id,omitempty"`
|
||||||
Error *struct {
|
Error *OpenAIVideoError `json:"error,omitempty"`
|
||||||
|
Metadata map[string]any `json:"metadata,omitempty"`
|
||||||
|
}
|
||||||
|
type OpenAIVideoError struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
} `json:"error,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user