2023-11-29 08:07:09 +00:00
|
|
|
package base
|
2023-11-28 10:32:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"one-api/common"
|
|
|
|
"one-api/types"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
var StopFinishReason = "stop"
|
2023-11-28 10:32:26 +00:00
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
type BaseProvider struct {
|
2023-11-28 10:32:26 +00:00
|
|
|
BaseURL string
|
|
|
|
Completions string
|
|
|
|
ChatCompletions string
|
|
|
|
Embeddings string
|
|
|
|
AudioSpeech string
|
2023-11-29 08:54:37 +00:00
|
|
|
Moderation string
|
2023-11-28 10:32:26 +00:00
|
|
|
AudioTranscriptions string
|
|
|
|
AudioTranslations string
|
2023-12-01 09:20:22 +00:00
|
|
|
ImagesGenerations string
|
|
|
|
ImagesEdit string
|
|
|
|
ImagesVariations string
|
2023-11-28 10:32:26 +00:00
|
|
|
Proxy string
|
|
|
|
Context *gin.Context
|
|
|
|
}
|
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
// 获取基础URL
|
|
|
|
func (p *BaseProvider) GetBaseURL() string {
|
2023-11-28 10:32:26 +00:00
|
|
|
if p.Context.GetString("base_url") != "" {
|
|
|
|
return p.Context.GetString("base_url")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.BaseURL
|
|
|
|
}
|
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
// 获取完整请求URL
|
|
|
|
func (p *BaseProvider) GetFullRequestURL(requestURL string, modelName string) string {
|
2023-11-28 10:32:26 +00:00
|
|
|
baseURL := strings.TrimSuffix(p.GetBaseURL(), "/")
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s", baseURL, requestURL)
|
|
|
|
}
|
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
// 获取请求头
|
|
|
|
func (p *BaseProvider) CommonRequestHeaders(headers map[string]string) {
|
|
|
|
headers["Content-Type"] = p.Context.Request.Header.Get("Content-Type")
|
|
|
|
headers["Accept"] = p.Context.Request.Header.Get("Accept")
|
|
|
|
if headers["Content-Type"] == "" {
|
|
|
|
headers["Content-Type"] = "application/json"
|
2023-11-28 10:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发送请求
|
2023-11-30 05:49:35 +00:00
|
|
|
func (p *BaseProvider) SendRequest(req *http.Request, response ProviderResponseHandler, rawOutput bool) (openAIErrorWithStatusCode *types.OpenAIErrorWithStatusCode) {
|
2023-12-01 02:54:07 +00:00
|
|
|
defer req.Body.Close()
|
2023-11-28 10:32:26 +00:00
|
|
|
|
2023-11-30 05:49:35 +00:00
|
|
|
resp, openAIErrorWithStatusCode := common.SendRequest(req, response, true)
|
|
|
|
if openAIErrorWithStatusCode != nil {
|
|
|
|
return
|
2023-11-28 10:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-11-29 08:07:09 +00:00
|
|
|
openAIResponse, openAIErrorWithStatusCode := response.ResponseHandler(resp)
|
2023-11-28 10:32:26 +00:00
|
|
|
if openAIErrorWithStatusCode != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-30 05:49:35 +00:00
|
|
|
if rawOutput {
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
p.Context.Writer.Header().Set(k, v[0])
|
|
|
|
}
|
2023-11-29 08:07:09 +00:00
|
|
|
|
2023-11-30 05:49:35 +00:00
|
|
|
p.Context.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
_, err := io.Copy(p.Context.Writer, resp.Body)
|
|
|
|
if err != nil {
|
2023-12-01 19:28:18 +00:00
|
|
|
return common.ErrorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError)
|
2023-11-30 05:49:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
jsonResponse, err := json.Marshal(openAIResponse)
|
|
|
|
if err != nil {
|
2023-12-01 19:28:18 +00:00
|
|
|
return common.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError)
|
2023-11-30 05:49:35 +00:00
|
|
|
}
|
|
|
|
p.Context.Writer.Header().Set("Content-Type", "application/json")
|
|
|
|
p.Context.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
_, err = p.Context.Writer.Write(jsonResponse)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-12-01 19:28:18 +00:00
|
|
|
return common.ErrorWrapper(err, "write_response_body_failed", http.StatusInternalServerError)
|
2023-11-30 05:49:35 +00:00
|
|
|
}
|
2023-11-29 08:07:09 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 10:32:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-11-29 08:07:09 +00:00
|
|
|
|
2023-11-29 10:11:15 +00:00
|
|
|
func (p *BaseProvider) SendRequestRaw(req *http.Request) (openAIErrorWithStatusCode *types.OpenAIErrorWithStatusCode) {
|
2023-12-01 02:54:07 +00:00
|
|
|
defer req.Body.Close()
|
2023-11-29 10:11:15 +00:00
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
resp, err := common.HttpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-12-01 19:28:18 +00:00
|
|
|
return common.ErrorWrapper(err, "http_request_failed", http.StatusInternalServerError)
|
2023-11-29 10:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// 处理响应
|
|
|
|
if common.IsFailureStatusCode(resp) {
|
2023-11-30 05:49:35 +00:00
|
|
|
return common.HandleErrorResp(resp)
|
2023-11-29 10:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
p.Context.Writer.Header().Set(k, v[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Context.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
|
|
_, err = io.Copy(p.Context.Writer, resp.Body)
|
|
|
|
if err != nil {
|
2023-12-01 19:28:18 +00:00
|
|
|
return common.ErrorWrapper(err, "write_response_body_failed", http.StatusInternalServerError)
|
2023-11-29 10:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-29 08:54:37 +00:00
|
|
|
func (p *BaseProvider) SupportAPI(relayMode int) bool {
|
|
|
|
switch relayMode {
|
|
|
|
case common.RelayModeChatCompletions:
|
|
|
|
return p.ChatCompletions != ""
|
|
|
|
case common.RelayModeCompletions:
|
|
|
|
return p.Completions != ""
|
|
|
|
case common.RelayModeEmbeddings:
|
|
|
|
return p.Embeddings != ""
|
|
|
|
case common.RelayModeAudioSpeech:
|
|
|
|
return p.AudioSpeech != ""
|
|
|
|
case common.RelayModeAudioTranscription:
|
|
|
|
return p.AudioTranscriptions != ""
|
|
|
|
case common.RelayModeAudioTranslation:
|
|
|
|
return p.AudioTranslations != ""
|
|
|
|
case common.RelayModeModerations:
|
|
|
|
return p.Moderation != ""
|
2023-12-01 09:20:22 +00:00
|
|
|
case common.RelayModeImagesGenerations:
|
|
|
|
return p.ImagesGenerations != ""
|
2023-12-01 10:25:05 +00:00
|
|
|
case common.RelayModeImagesEdits:
|
2023-12-01 09:20:22 +00:00
|
|
|
return p.ImagesEdit != ""
|
|
|
|
case common.RelayModeImagesVariations:
|
|
|
|
return p.ImagesVariations != ""
|
2023-11-29 08:54:37 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|