mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-14 20:23:46 +08:00
✨ add: add images api
This commit is contained in:
@@ -20,10 +20,13 @@ func CreateAzureProvider(c *gin.Context) *AzureProvider {
|
||||
Completions: "/completions",
|
||||
ChatCompletions: "/chat/completions",
|
||||
Embeddings: "/embeddings",
|
||||
AudioSpeech: "/audio/speech",
|
||||
AudioTranscriptions: "/audio/transcriptions",
|
||||
AudioTranslations: "/audio/translations",
|
||||
Context: c,
|
||||
ImagesGenerations: "/images/generations",
|
||||
// ImagesEdit: "/images/edit",
|
||||
// ImagesVariations: "/images/variations",
|
||||
Context: c,
|
||||
// AudioSpeech: "/audio/speech",
|
||||
},
|
||||
IsAzure: true,
|
||||
},
|
||||
|
||||
102
providers/azure/image_generations.go
Normal file
102
providers/azure/image_generations.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package azure
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/providers/openai"
|
||||
"one-api/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *ImageAzureResponse) ResponseHandler(resp *http.Response) (OpenAIResponse any, errWithCode *types.OpenAIErrorWithStatusCode) {
|
||||
if c.Status == "canceled" || c.Status == "failed" {
|
||||
errWithCode = &types.OpenAIErrorWithStatusCode{
|
||||
OpenAIError: types.OpenAIError{
|
||||
Message: c.Error.Message,
|
||||
Type: "one_api_error",
|
||||
Code: c.Error.Code,
|
||||
},
|
||||
StatusCode: resp.StatusCode,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
operation_location := resp.Header.Get("operation-location")
|
||||
if operation_location == "" {
|
||||
return nil, types.ErrorWrapper(errors.New("image url is empty"), "get_images_url_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
client := common.NewClient()
|
||||
req, err := client.NewRequest("GET", operation_location, common.WithHeader(c.Header))
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "get_images_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
getImageAzureResponse := ImageAzureResponse{}
|
||||
for i := 0; i < 3; i++ {
|
||||
// 休眠 2 秒
|
||||
time.Sleep(2 * time.Second)
|
||||
_, errWithCode = common.SendRequest(req, &getImageAzureResponse, false)
|
||||
fmt.Println("getImageAzureResponse", getImageAzureResponse)
|
||||
if errWithCode != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if getImageAzureResponse.Status == "canceled" || getImageAzureResponse.Status == "failed" {
|
||||
return nil, &types.OpenAIErrorWithStatusCode{
|
||||
OpenAIError: types.OpenAIError{
|
||||
Message: c.Error.Message,
|
||||
Type: "get_images_request_failed",
|
||||
Code: c.Error.Code,
|
||||
},
|
||||
StatusCode: resp.StatusCode,
|
||||
}
|
||||
}
|
||||
if getImageAzureResponse.Status == "succeeded" {
|
||||
return getImageAzureResponse.Result, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, types.ErrorWrapper(errors.New("get image Timeout"), "get_images_url_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (p *AzureProvider) ImageGenerationsAction(request *types.ImageRequest, isModelMapped bool, promptTokens int) (usage *types.Usage, errWithCode *types.OpenAIErrorWithStatusCode) {
|
||||
|
||||
requestBody, err := p.GetRequestBody(&request, isModelMapped)
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "json_marshal_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
fullRequestURL := p.GetFullRequestURL(p.ImagesGenerations, request.Model)
|
||||
headers := p.GetRequestHeaders()
|
||||
|
||||
client := common.NewClient()
|
||||
req, err := client.NewRequest(p.Context.Request.Method, fullRequestURL, common.WithBody(requestBody), common.WithHeader(headers))
|
||||
if err != nil {
|
||||
return nil, types.ErrorWrapper(err, "new_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if request.Model == "dall-e-2" {
|
||||
imageAzureResponse := &ImageAzureResponse{
|
||||
Header: headers,
|
||||
}
|
||||
errWithCode = p.SendRequest(req, imageAzureResponse, false)
|
||||
} else {
|
||||
openAIProviderImageResponseResponse := &openai.OpenAIProviderImageResponseResponse{}
|
||||
errWithCode = p.SendRequest(req, openAIProviderImageResponseResponse, true)
|
||||
}
|
||||
|
||||
if errWithCode != nil {
|
||||
return
|
||||
}
|
||||
|
||||
usage = &types.Usage{
|
||||
PromptTokens: promptTokens,
|
||||
CompletionTokens: 0,
|
||||
TotalTokens: promptTokens,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
21
providers/azure/type.go
Normal file
21
providers/azure/type.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package azure
|
||||
|
||||
import "one-api/types"
|
||||
|
||||
type ImageAzureResponse struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Created int64 `json:"created,omitempty"`
|
||||
Expires int64 `json:"expires,omitempty"`
|
||||
Result types.ImageResponse `json:"result,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Error ImageAzureError `json:"error,omitempty"`
|
||||
Header map[string]string `json:"header,omitempty"`
|
||||
}
|
||||
|
||||
type ImageAzureError struct {
|
||||
Code string `json:"code,omitempty"`
|
||||
Target string `json:"target,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Details []string `json:"details,omitempty"`
|
||||
InnerError any `json:"innererror,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user