one-api/providers/openai/image_generations.go
2024-05-29 01:56:14 +08:00

53 lines
1.3 KiB
Go

package openai
import (
"net/http"
"one-api/common"
"one-api/common/config"
"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(config.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
}