mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-30 07:06:38 +08:00
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package openai
|
|
|
|
import (
|
|
"net/http"
|
|
"one-api/common"
|
|
"one-api/types"
|
|
)
|
|
|
|
func (p *OpenAIProvider) CreateImageGenerations(request *types.ImageRequest) (*types.ImageResponse, *types.OpenAIErrorWithStatusCode) {
|
|
if !IsWithinRange(request.Model, request.N) {
|
|
return nil, common.StringErrorWrapper("n_not_within_range", "n_not_within_range", http.StatusBadRequest)
|
|
}
|
|
|
|
req, errWithCode := p.GetRequestTextBody(common.RelayModeImagesGenerations, request.Model, request)
|
|
if errWithCode != nil {
|
|
return nil, errWithCode
|
|
}
|
|
defer req.Body.Close()
|
|
|
|
response := &OpenAIProviderImageResponse{}
|
|
// 发送请求
|
|
_, errWithCode = p.Requester.SendRequest(req, response, false)
|
|
if errWithCode != nil {
|
|
return nil, errWithCode
|
|
}
|
|
|
|
// 检测是否错误
|
|
openaiErr := ErrorHandle(&response.OpenAIErrorResponse)
|
|
if openaiErr != nil {
|
|
errWithCode = &types.OpenAIErrorWithStatusCode{
|
|
OpenAIError: *openaiErr,
|
|
StatusCode: http.StatusBadRequest,
|
|
}
|
|
return nil, errWithCode
|
|
}
|
|
|
|
p.Usage.TotalTokens = p.Usage.PromptTokens
|
|
|
|
return &response.ImageResponse, nil
|
|
|
|
}
|
|
|
|
func IsWithinRange(element string, value int) bool {
|
|
if _, ok := common.DalleGenerationImageAmounts[element]; !ok {
|
|
return false
|
|
}
|
|
min := common.DalleGenerationImageAmounts[element][0]
|
|
max := common.DalleGenerationImageAmounts[element][1]
|
|
|
|
return value >= min && value <= max
|
|
}
|