ai-gateway/providers/base/common.go

165 lines
4.2 KiB
Go
Raw Permalink Normal View History

package base
2023-11-28 10:32:26 +00:00
import (
"encoding/json"
"fmt"
"io"
"net/http"
"one-api/common"
"one-api/model"
2023-11-28 10:32:26 +00:00
"one-api/types"
"strings"
"github.com/gin-gonic/gin"
)
var StopFinishReason = "stop"
2023-11-28 10:32:26 +00:00
type BaseProvider struct {
2023-11-28 10:32:26 +00:00
BaseURL string
Completions string
ChatCompletions string
Embeddings string
AudioSpeech string
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
Channel *model.Channel
2023-11-28 10:32:26 +00:00
}
// 获取基础URL
func (p *BaseProvider) GetBaseURL() string {
if p.Channel.GetBaseURL() != "" {
return p.Channel.GetBaseURL()
2023-11-28 10:32:26 +00:00
}
return p.BaseURL
}
func (p *BaseProvider) SetChannel(channel *model.Channel) {
p.Channel = channel
}
// 获取完整请求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)
}
// 获取请求头
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
}
}
// 发送请求
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-12-26 10:42:39 +00:00
resp, openAIErrorWithStatusCode := common.SendRequest(req, response, true, p.Channel.Proxy)
if openAIErrorWithStatusCode != nil {
return
2023-11-28 10:32:26 +00:00
}
defer resp.Body.Close()
openAIResponse, openAIErrorWithStatusCode := response.ResponseHandler(resp)
2023-11-28 10:32:26 +00:00
if openAIErrorWithStatusCode != nil {
return
}
if rawOutput {
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, "copy_response_body_failed", http.StatusInternalServerError)
}
} 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)
}
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-28 10:32:26 +00:00
return nil
}
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
// 发送请求
2023-12-26 10:42:39 +00:00
client := common.GetHttpClient(p.Channel.Proxy)
resp, err := client.Do(req)
2023-11-29 10:11:15 +00:00
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
}
2023-12-26 10:42:39 +00:00
common.PutHttpClient(client)
2023-11-29 10:11:15 +00:00
defer resp.Body.Close()
// 处理响应
if common.IsFailureStatusCode(resp) {
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
}
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 != ""
case common.RelayModeImagesEdits:
2023-12-01 09:20:22 +00:00
return p.ImagesEdit != ""
case common.RelayModeImagesVariations:
return p.ImagesVariations != ""
default:
return false
}
}