Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ali image model cannot be found and the error with the incorrect response_format #1470

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ COPY ./VERSION .
COPY ./web .

WORKDIR /web/default
RUN npm config set registry https://registry.npmmirror.com
RUN npm install
RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build

WORKDIR /web/berry
RUN npm install
RUN npm install --force
RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build

WORKDIR /web/air
RUN npm install
RUN npm install --force
RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat VERSION) npm run build

FROM golang:alpine AS builder2
Expand All @@ -22,7 +23,8 @@ RUN apk add --no-cache g++

ENV GO111MODULE=on \
CGO_ENABLED=1 \
GOOS=linux
GOOS=linux \
GOPROXY=https://goproxy.cn,direct

WORKDIR /build
ADD go.mod go.sum ./
Expand Down
11 changes: 9 additions & 2 deletions relay/adaptor/aiproxy/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/songquanpeng/one-api/relay/model"
"io"
"net/http"
"strings"
)

type Adaptor struct {
Expand All @@ -20,12 +21,18 @@ func (a *Adaptor) Init(meta *meta.Meta) {
}

func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
return fmt.Sprintf("%s/api/library/ask", meta.BaseURL), nil
baseURL := meta.BaseURL
if strings.HasSuffix(meta.APIKey, "#vip") {
baseURL = "https://apivip.aiproxy.io"
}
return fmt.Sprintf("%s/api/library/ask", baseURL), nil
}

func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
adaptor.SetupCommonRequestHeader(c, req, meta)
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
apiKey := meta.APIKey
apiKey = strings.TrimSuffix(apiKey, "#vip")
req.Header.Set("Authorization", "Bearer "+apiKey)
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion relay/adaptor/ali/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/relay/adaptor/openai"
Expand All @@ -19,7 +20,11 @@ import (
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
apiKey := c.Request.Header.Get("Authorization")
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
responseFormat := c.GetString("response_format")

var responseFormat string
if req, exists := c.Get(ctxkey.ConvertedRequest); exists {
responseFormat = req.(*ImageRequest).ResponseFormat
}

var aliTaskResponse TaskResponse
responseBody, err := io.ReadAll(resp.Body)
Expand Down
2 changes: 1 addition & 1 deletion relay/adaptor/ali/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingReque
func ConvertImageRequest(request model.ImageRequest) *ImageRequest {
var imageRequest ImageRequest
imageRequest.Input.Prompt = request.Prompt
imageRequest.Model = request.Model
imageRequest.Model = strings.TrimPrefix(request.Model, "ali-")
imageRequest.Parameters.Size = strings.Replace(request.Size, "x", "*", -1)
imageRequest.Parameters.N = request.N
imageRequest.ResponseFormat = request.ResponseFormat
Expand Down
11 changes: 10 additions & 1 deletion relay/adaptor/openai/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
return minimax.GetRequestURL(meta)
case channeltype.Doubao:
return doubao.GetRequestURL(meta)
case channeltype.AIProxy:
if strings.HasSuffix(meta.APIKey, "#vip") {
return GetFullRequestURL("https://apivip.aiproxy.io", meta.RequestURLPath, meta.ChannelType), nil
}
fallthrough
case channeltype.Novita:
return novita.GetRequestURL(meta)
default:
Expand All @@ -63,7 +68,11 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *me
req.Header.Set("api-key", meta.APIKey)
return nil
}
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
apiKey := meta.APIKey
if meta.ChannelType == channeltype.AIProxy {
apiKey = strings.TrimSuffix(apiKey, "#vip")
}
req.Header.Set("Authorization", "Bearer "+apiKey)
if meta.ChannelType == channeltype.OpenRouter {
req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api")
req.Header.Set("X-Title", "One API")
Expand Down
2 changes: 0 additions & 2 deletions relay/billing/ratio/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,4 @@ var ImagePromptLengthLimitations = map[string]int{
}

var ImageOriginModelName = map[string]string{
"ali-stable-diffusion-xl": "stable-diffusion-xl",
"ali-stable-diffusion-v1.5": "stable-diffusion-v1.5",
}
1 change: 1 addition & 0 deletions relay/controller/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus
if err != nil {
return openai.ErrorWrapper(err, "marshal_image_request_failed", http.StatusInternalServerError)
}
c.Set(ctxkey.ConvertedRequest, finalRequest)
requestBody = bytes.NewBuffer(jsonStr)
}

Expand Down
3 changes: 1 addition & 2 deletions relay/controller/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/songquanpeng/one-api/relay/adaptor"
"io"
"net/http"

"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/relay"
"github.com/songquanpeng/one-api/relay/adaptor"
"github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/songquanpeng/one-api/relay/apitype"
"github.com/songquanpeng/one-api/relay/billing"
Expand Down Expand Up @@ -54,7 +54,6 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
}
adaptor.Init(meta)

// get request body
requestBody, err := getRequestBody(c, meta, textRequest, adaptor)
if err != nil {
return openai.ErrorWrapper(err, "convert_request_failed", http.StatusInternalServerError)
Expand Down