feat(ollama): passthrough Claude Messages and OpenAI Responses (#7051)
This commit is contained in:
@@ -2,11 +2,12 @@ package ollama
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/QuantumNous/new-api/relay/channel"
|
"github.com/QuantumNous/new-api/relay/channel"
|
||||||
|
"github.com/QuantumNous/new-api/relay/channel/claude"
|
||||||
"github.com/QuantumNous/new-api/relay/channel/openai"
|
"github.com/QuantumNous/new-api/relay/channel/openai"
|
||||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||||
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
||||||
@@ -24,16 +25,8 @@ func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
||||||
openaiAdaptor := openai.Adaptor{}
|
adaptor := claude.Adaptor{}
|
||||||
openaiRequest, err := openaiAdaptor.ConvertClaudeRequest(c, info, request)
|
return adaptor.ConvertClaudeRequest(c, info, request)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
openaiRequest.(*dto.GeneralOpenAIRequest).StreamOptions = &dto.StreamOptions{
|
|
||||||
IncludeUsage: true,
|
|
||||||
}
|
|
||||||
// map to ollama chat request (Claude -> OpenAI -> Ollama chat)
|
|
||||||
return openAIChatToOllamaChat(c, openaiRequest.(*dto.GeneralOpenAIRequest))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||||
@@ -48,18 +41,37 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
||||||
if info.RelayMode == relayconstant.RelayModeEmbeddings {
|
switch info.RelayFormat {
|
||||||
return info.ChannelBaseUrl + "/api/embed", nil
|
case types.RelayFormatClaude:
|
||||||
|
return (&claude.Adaptor{}).GetRequestURL(info)
|
||||||
|
default:
|
||||||
|
switch info.RelayMode {
|
||||||
|
case relayconstant.RelayModeEmbeddings:
|
||||||
|
return fmt.Sprintf("%s/api/embed", info.ChannelBaseUrl), nil
|
||||||
|
case relayconstant.RelayModeResponses:
|
||||||
|
return fmt.Sprintf("%s/v1/responses", info.ChannelBaseUrl), nil
|
||||||
|
case relayconstant.RelayModeResponsesCompact:
|
||||||
|
return fmt.Sprintf("%s/v1/responses/compact", info.ChannelBaseUrl), nil
|
||||||
|
case relayconstant.RelayModeCompletions:
|
||||||
|
return fmt.Sprintf("%s/api/generate", info.ChannelBaseUrl), nil
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%s/api/chat", info.ChannelBaseUrl), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if strings.Contains(info.RequestURLPath, "/v1/completions") || info.RelayMode == relayconstant.RelayModeCompletions {
|
|
||||||
return info.ChannelBaseUrl + "/api/generate", nil
|
|
||||||
}
|
|
||||||
return info.ChannelBaseUrl + "/api/chat", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
||||||
channel.SetupApiRequestHeader(info, c, req)
|
channel.SetupApiRequestHeader(info, c, req)
|
||||||
req.Set("Authorization", "Bearer "+info.ApiKey)
|
req.Set("Authorization", "Bearer "+info.ApiKey)
|
||||||
|
switch info.RelayFormat {
|
||||||
|
case types.RelayFormatClaude:
|
||||||
|
claude.CommonClaudeHeadersOperation(c, req, info)
|
||||||
|
anthropicVersion := c.Request.Header.Get("anthropic-version")
|
||||||
|
if anthropicVersion == "" {
|
||||||
|
anthropicVersion = "2023-06-01"
|
||||||
|
}
|
||||||
|
req.Set("anthropic-version", anthropicVersion)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,11 +79,12 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
|
|||||||
if request == nil {
|
if request == nil {
|
||||||
return nil, errors.New("request is nil")
|
return nil, errors.New("request is nil")
|
||||||
}
|
}
|
||||||
// decide generate or chat
|
switch info.RelayMode {
|
||||||
if strings.Contains(info.RequestURLPath, "/v1/completions") || info.RelayMode == relayconstant.RelayModeCompletions {
|
case relayconstant.RelayModeCompletions:
|
||||||
return openAIToGenerate(c, request)
|
return openAIToGenerate(c, request)
|
||||||
|
default:
|
||||||
|
return openAIChatToOllamaChat(c, request)
|
||||||
}
|
}
|
||||||
return openAIChatToOllamaChat(c, request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
||||||
@@ -83,7 +96,8 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
||||||
return nil, errors.New("not implemented")
|
adaptor := openai.Adaptor{}
|
||||||
|
return adaptor.ConvertOpenAIResponsesRequest(c, info, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
||||||
@@ -91,14 +105,23 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
switch info.RelayMode {
|
switch info.RelayFormat {
|
||||||
case relayconstant.RelayModeEmbeddings:
|
case types.RelayFormatClaude:
|
||||||
return ollamaEmbeddingHandler(c, info, resp)
|
adaptor := claude.Adaptor{}
|
||||||
|
return adaptor.DoResponse(c, resp, info)
|
||||||
default:
|
default:
|
||||||
if info.IsStream {
|
switch info.RelayMode {
|
||||||
return ollamaStreamHandler(c, info, resp)
|
case relayconstant.RelayModeEmbeddings:
|
||||||
|
return ollamaEmbeddingHandler(c, info, resp)
|
||||||
|
case relayconstant.RelayModeResponses, relayconstant.RelayModeResponsesCompact:
|
||||||
|
adaptor := openai.Adaptor{}
|
||||||
|
return adaptor.DoResponse(c, resp, info)
|
||||||
|
default:
|
||||||
|
if info.IsStream {
|
||||||
|
return ollamaStreamHandler(c, info, resp)
|
||||||
|
}
|
||||||
|
return ollamaChatHandler(c, info, resp)
|
||||||
}
|
}
|
||||||
return ollamaChatHandler(c, info, resp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user