feat: support openai images edits api

This commit is contained in:
Laisky.Cai
2024-04-25 03:02:20 +00:00
parent da0842272c
commit f71e4ef151
17 changed files with 87 additions and 52 deletions

View File

@@ -2,6 +2,7 @@ package ctxkey
const (
Id = "id"
RequestId = "X-Oneapi-Request-Id"
Username = "username"
Role = "role"
Status = "status"
@@ -14,6 +15,7 @@ const (
Group = "group"
ModelMapping = "model_mapping"
ChannelName = "channel_name"
ContentType = "content_type"
TokenId = "token_id"
TokenName = "token_name"
BaseURL = "base_url"

View File

@@ -2,10 +2,10 @@ package common
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"io"
"strings"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
const KeyRequestBody = "key_request_body"
@@ -29,18 +29,16 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error {
if err != nil {
return err
}
contentType := c.Request.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/json") {
err = json.Unmarshal(requestBody, &v)
} else {
// skip for now
// TODO: someday non json request have variant model, we will need to implementation this
}
if err != nil {
return err
}
// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
defer func() {
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}()
if err = c.Bind(v); err != nil {
return errors.Wrap(err, "bind request body failed")
}
return nil
}

View File

@@ -1,7 +1,3 @@
package logger
const (
RequestIdKey = "X-Oneapi-Request-Id"
)
var LogDir string

View File

@@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
)
@@ -87,7 +88,7 @@ func logHelper(ctx context.Context, level string, msg string) {
if level == loggerINFO {
writer = gin.DefaultWriter
}
id := ctx.Value(RequestIdKey)
id := ctx.Value(ctxkey.RequestId)
if id == nil {
id = helper.GenRequestID()
}