2024-02-17 16:15:31 +00:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-06 12:48:22 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
2024-04-05 17:36:48 +00:00
|
|
|
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
|
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
2024-04-05 17:31:44 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Adaptor struct {
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
2024-02-18 08:17:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
2024-04-26 15:05:48 +00:00
|
|
|
version := helper.AssignOrDefault(meta.Config.APIVersion, config.GeminiVersion)
|
2024-02-17 16:15:31 +00:00
|
|
|
action := "generateContent"
|
|
|
|
if meta.IsStream {
|
|
|
|
action = "streamGenerateContent"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s/models/%s:%s", meta.BaseURL, version, meta.ActualModelName, action), nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
2024-02-17 16:15:31 +00:00
|
|
|
channelhelper.SetupCommonRequestHeader(c, req, meta)
|
|
|
|
req.Header.Set("x-goog-api-key", meta.APIKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
return ConvertRequest(*request), nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:09:54 +00:00
|
|
|
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
2024-02-17 16:15:31 +00:00
|
|
|
return channelhelper.DoRequestHelper(a, c, meta, requestBody)
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
2024-02-17 16:15:31 +00:00
|
|
|
if meta.IsStream {
|
|
|
|
var responseText string
|
|
|
|
err, responseText = StreamHandler(c, resp)
|
|
|
|
usage = openai.ResponseText2Usage(responseText, meta.ActualModelName, meta.PromptTokens)
|
|
|
|
} else {
|
|
|
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
|
|
return ModelList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
|
|
return "google gemini"
|
|
|
|
}
|