mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-09 10:13:42 +08:00
feat: support openai images edits api
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/client"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -30,7 +31,7 @@ func DoRequestHelper(a Adaptor, c *gin.Context, meta *meta.Meta, requestBody io.
|
||||
return nil, errors.Wrap(err, "new request failed")
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Set("Content-Type", c.GetString(ctxkey.ContentType))
|
||||
|
||||
err = a.SetupRequestHeader(c, req, meta)
|
||||
if err != nil {
|
||||
|
||||
@@ -93,10 +93,13 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met
|
||||
switch meta.Mode {
|
||||
case relaymode.ImagesGenerations:
|
||||
err, _ = ImageHandler(c, resp)
|
||||
case relaymode.ImagesEdits:
|
||||
err, _ = ImagesEditsHandler(c, resp)
|
||||
default:
|
||||
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,30 @@ package openai
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ImagesEditsHandler just copy response body to client
|
||||
//
|
||||
// https://platform.openai.com/docs/api-reference/images/createEdit
|
||||
func ImagesEditsHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
for k, v := range resp.Header {
|
||||
c.Writer.Header().Set(k, v[0])
|
||||
}
|
||||
|
||||
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||
return ErrorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
var imageResponse ImageResponse
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
@@ -57,7 +58,8 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus
|
||||
}
|
||||
|
||||
var requestBody io.Reader
|
||||
if isModelMapped || meta.ChannelType == channeltype.Azure { // make Azure channel request body
|
||||
if strings.ToLower(c.GetString(ctxkey.ContentType)) == "application/json" &&
|
||||
isModelMapped || meta.ChannelType == channeltype.Azure { // make Azure channel request body
|
||||
jsonStr, err := json.Marshal(imageRequest)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "marshal_image_request_failed", http.StatusInternalServerError)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package model
|
||||
|
||||
type ImageRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt" binding:"required"`
|
||||
N int `json:"n,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
Quality string `json:"quality,omitempty"`
|
||||
ResponseFormat string `json:"response_format,omitempty"`
|
||||
Style string `json:"style,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Model string `json:"model" form:"model"`
|
||||
Prompt string `json:"prompt" binding:"required" form:"prompt"`
|
||||
N int `json:"n,omitempty" form:"n"`
|
||||
Size string `json:"size,omitempty" form:"size"`
|
||||
Quality string `json:"quality,omitempty" form:"quality"`
|
||||
ResponseFormat string `json:"response_format,omitempty" form:"response_format"`
|
||||
Style string `json:"style,omitempty" form:"style"`
|
||||
User string `json:"user,omitempty" form:"user"`
|
||||
}
|
||||
|
||||
@@ -11,4 +11,5 @@ const (
|
||||
AudioSpeech
|
||||
AudioTranscription
|
||||
AudioTranslation
|
||||
ImagesEdits
|
||||
)
|
||||
|
||||
@@ -24,6 +24,9 @@ func GetByPath(path string) int {
|
||||
relayMode = AudioTranscription
|
||||
} else if strings.HasPrefix(path, "/v1/audio/translations") {
|
||||
relayMode = AudioTranslation
|
||||
} else if strings.HasPrefix(path, "/v1/images/edits") {
|
||||
relayMode = ImagesEdits
|
||||
}
|
||||
|
||||
return relayMode
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user