2024-07-13 06:59:28 +00:00
|
|
|
package vertexai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor"
|
|
|
|
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
|
|
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
|
|
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adaptor.Adaptor = new(Adaptor)
|
|
|
|
|
|
|
|
const channelName = "vertexai"
|
|
|
|
|
2024-07-15 17:02:06 +00:00
|
|
|
type Adaptor struct{}
|
2024-07-13 06:59:28 +00:00
|
|
|
|
|
|
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
adaptor := GetAdaptor(request.Model)
|
|
|
|
if adaptor == nil {
|
|
|
|
return nil, errors.New("adaptor not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return adaptor.ConvertRequest(c, relayMode, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
2024-07-15 17:02:06 +00:00
|
|
|
adaptor := GetAdaptor(meta.ActualModelName)
|
2024-07-13 06:59:28 +00:00
|
|
|
if adaptor == nil {
|
|
|
|
return nil, &relaymodel.ErrorWithStatusCode{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
Error: relaymodel.Error{
|
|
|
|
Message: "adaptor not found",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adaptor.DoResponse(c, resp, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() (models []string) {
|
|
|
|
models = modelList
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
|
|
return channelName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
|
|
|
suffix := ""
|
|
|
|
if strings.HasPrefix(meta.ActualModelName, "gemini") {
|
|
|
|
if meta.IsStream {
|
2024-07-15 17:02:06 +00:00
|
|
|
suffix = "streamGenerateContent?alt=sse"
|
2024-07-13 06:59:28 +00:00
|
|
|
} else {
|
|
|
|
suffix = "generateContent"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if meta.IsStream {
|
2024-07-15 17:02:06 +00:00
|
|
|
suffix = "streamRawPredict?alt=sse"
|
2024-07-13 06:59:28 +00:00
|
|
|
} else {
|
|
|
|
suffix = "rawPredict"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 17:02:06 +00:00
|
|
|
if meta.BaseURL != "" {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s/v1/projects/%s/locations/%s/publishers/google/models/%s:%s",
|
|
|
|
meta.BaseURL,
|
|
|
|
meta.Config.VertexAIProjectID,
|
|
|
|
meta.Config.Region,
|
|
|
|
meta.ActualModelName,
|
|
|
|
suffix,
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:%s",
|
|
|
|
meta.Config.Region,
|
|
|
|
meta.Config.VertexAIProjectID,
|
|
|
|
meta.Config.Region,
|
|
|
|
meta.ActualModelName,
|
|
|
|
suffix,
|
|
|
|
), nil
|
2024-07-13 06:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
|
|
|
adaptor.SetupCommonRequestHeader(c, req, meta)
|
|
|
|
token, err := getToken(c, meta.ChannelId, meta.Config.VertexAIADC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
|
|
|
return channelhelper.DoRequestHelper(a, c, meta, requestBody)
|
|
|
|
}
|