|
|
@@ -0,0 +1,59 @@
|
|
|
+package ollama
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+ "one-api/dto"
|
|
|
+ "one-api/relay/channel"
|
|
|
+ "one-api/relay/channel/openai"
|
|
|
+ relaycommon "one-api/relay/common"
|
|
|
+ "one-api/service"
|
|
|
+)
|
|
|
+
|
|
|
+type Adaptor struct {
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) {
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|
|
+ return fmt.Sprintf("%s/api/chat", info.BaseUrl), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
|
|
|
+ channel.SetupApiRequestHeader(info, c, req)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.GeneralOpenAIRequest) (any, error) {
|
|
|
+ if request == nil {
|
|
|
+ return nil, errors.New("request is nil")
|
|
|
+ }
|
|
|
+ return requestOpenAI2Ollama(*request), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
|
|
|
+ return channel.DoApiRequest(a, c, info, requestBody)
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
|
|
|
+ if info.IsStream {
|
|
|
+ var responseText string
|
|
|
+ err, responseText = openai.OpenaiStreamHandler(c, resp, info.RelayMode)
|
|
|
+ usage = service.ResponseText2Usage(responseText, info.UpstreamModelName, info.PromptTokens)
|
|
|
+ } else {
|
|
|
+ err, usage = openai.OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) GetModelList() []string {
|
|
|
+ return ModelList
|
|
|
+}
|
|
|
+
|
|
|
+func (a *Adaptor) GetChannelName() string {
|
|
|
+ return ChannelName
|
|
|
+}
|