Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
ba2e9287
authored
Aug 27, 2026
by
Seefs
Committed by
GitHub
Aug 27, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(ollama): passthrough Claude Messages and OpenAI Responses (#7051)
parent
cae3676e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
20 deletions
+43
-20
relay/channel/ollama/adaptor.go
+43
-20
No files found.
relay/channel/ollama/adaptor.go
View file @
ba2e9287
...
@@ -2,11 +2,12 @@ package ollama
...
@@ -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
...
@@ -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) {
...
@@ -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
...
@@ -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
...
@@ -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,15 +105,24 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
...
@@ -91,15 +105,24 @@ 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
.
RelayFormat
{
case
types
.
RelayFormatClaude
:
adaptor
:=
claude
.
Adaptor
{}
return
adaptor
.
DoResponse
(
c
,
resp
,
info
)
default
:
switch
info
.
RelayMode
{
switch
info
.
RelayMode
{
case
relayconstant
.
RelayModeEmbeddings
:
case
relayconstant
.
RelayModeEmbeddings
:
return
ollamaEmbeddingHandler
(
c
,
info
,
resp
)
return
ollamaEmbeddingHandler
(
c
,
info
,
resp
)
case
relayconstant
.
RelayModeResponses
,
relayconstant
.
RelayModeResponsesCompact
:
adaptor
:=
openai
.
Adaptor
{}
return
adaptor
.
DoResponse
(
c
,
resp
,
info
)
default
:
default
:
if
info
.
IsStream
{
if
info
.
IsStream
{
return
ollamaStreamHandler
(
c
,
info
,
resp
)
return
ollamaStreamHandler
(
c
,
info
,
resp
)
}
}
return
ollamaChatHandler
(
c
,
info
,
resp
)
return
ollamaChatHandler
(
c
,
info
,
resp
)
}
}
}
}
}
func
(
a
*
Adaptor
)
GetModelList
()
[]
string
{
func
(
a
*
Adaptor
)
GetModelList
()
[]
string
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment