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
Commit
7160012f
authored
Feb 25, 2025
by
MartialBE
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add Claude 3.7 Sonnet thinking mode support
parent
c62276fc
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
5 deletions
+51
-5
common/model-ratio.go
+0
-0
relay/channel/claude/constants.go
+1
-0
relay/channel/claude/dto.go
+12
-3
relay/channel/claude/relay-claude.go
+38
-2
No files found.
common/model-ratio.go
View file @
7160012f
This diff is collapsed.
Click to expand it.
relay/channel/claude/constants.go
View file @
7160012f
...
...
@@ -12,6 +12,7 @@ var ModelList = []string{
"claude-3-5-sonnet-20240620"
,
"claude-3-5-sonnet-20241022"
,
"claude-3-7-sonnet-20250219"
,
"claude-3-7-sonnet-20250219-thinking"
,
}
var
ChannelName
=
"claude"
relay/channel/claude/dto.go
View file @
7160012f
...
...
@@ -11,6 +11,9 @@ type ClaudeMediaMessage struct {
Usage
*
ClaudeUsage
`json:"usage,omitempty"`
StopReason
*
string
`json:"stop_reason,omitempty"`
PartialJson
string
`json:"partial_json,omitempty"`
Thinking
string
`json:"thinking,omitempty"`
Signature
string
`json:"signature,omitempty"`
Delta
string
`json:"delta,omitempty"`
// tool_calls
Id
string
`json:"id,omitempty"`
Name
string
`json:"name,omitempty"`
...
...
@@ -54,9 +57,15 @@ type ClaudeRequest struct {
TopP
float64
`json:"top_p,omitempty"`
TopK
int
`json:"top_k,omitempty"`
//ClaudeMetadata `json:"metadata,omitempty"`
Stream
bool
`json:"stream,omitempty"`
Tools
[]
Tool
`json:"tools,omitempty"`
ToolChoice
any
`json:"tool_choice,omitempty"`
Stream
bool
`json:"stream,omitempty"`
Tools
[]
Tool
`json:"tools,omitempty"`
ToolChoice
any
`json:"tool_choice,omitempty"`
Thinking
*
Thinking
`json:"thinking,omitempty"`
}
type
Thinking
struct
{
Type
string
`json:"type,omitempty"`
BudgetTokens
int
`json:"budget_tokens,omitempty"`
}
type
ClaudeError
struct
{
...
...
relay/channel/claude/relay-claude.go
View file @
7160012f
...
...
@@ -92,6 +92,25 @@ func RequestOpenAI2ClaudeMessage(textRequest dto.GeneralOpenAIRequest) (*ClaudeR
Stream
:
textRequest
.
Stream
,
Tools
:
claudeTools
,
}
if
strings
.
HasSuffix
(
textRequest
.
Model
,
"-thinking"
)
{
if
claudeRequest
.
MaxTokens
==
0
{
claudeRequest
.
MaxTokens
=
8192
}
// 因为BudgetTokens 必须大于1024
if
claudeRequest
.
MaxTokens
<
1280
{
claudeRequest
.
MaxTokens
=
1280
}
// BudgetTokens 为 max_tokens 的 80%
claudeRequest
.
Thinking
=
&
Thinking
{
Type
:
"enabled"
,
BudgetTokens
:
int
(
float64
(
claudeRequest
.
MaxTokens
)
*
0.8
),
}
claudeRequest
.
Model
=
strings
.
TrimSuffix
(
textRequest
.
Model
,
"-thinking"
)
}
if
claudeRequest
.
MaxTokens
==
0
{
claudeRequest
.
MaxTokens
=
4096
}
...
...
@@ -308,12 +327,20 @@ func StreamResponseClaude2OpenAI(reqMode int, claudeResponse *ClaudeResponse) (*
if
claudeResponse
.
Delta
!=
nil
{
choice
.
Index
=
claudeResponse
.
Index
choice
.
Delta
.
SetContentString
(
claudeResponse
.
Delta
.
Text
)
if
claudeResponse
.
Delta
.
Type
==
"input_json_delta"
{
switch
claudeResponse
.
Delta
.
Type
{
case
"input_json_delta"
:
tools
=
append
(
tools
,
dto
.
ToolCall
{
Function
:
dto
.
FunctionCall
{
Arguments
:
claudeResponse
.
Delta
.
PartialJson
,
},
})
case
"signature_delta"
:
// 加密的不处理
signatureContent
:=
"
\n
"
choice
.
Delta
.
ReasoningContent
=
&
signatureContent
case
"thinking_delta"
:
thinkingContent
:=
claudeResponse
.
Delta
.
Thinking
choice
.
Delta
.
ReasoningContent
=
&
thinkingContent
}
}
}
else
if
claudeResponse
.
Type
==
"message_delta"
{
...
...
@@ -352,6 +379,8 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *ClaudeResponse) *dto.Ope
responseText
=
claudeResponse
.
Content
[
0
]
.
Text
}
tools
:=
make
([]
dto
.
ToolCall
,
0
)
thinkingContent
:=
""
if
reqMode
==
RequestModeCompletion
{
content
,
_
:=
json
.
Marshal
(
strings
.
TrimPrefix
(
claudeResponse
.
Completion
,
" "
))
choice
:=
dto
.
OpenAITextResponseChoice
{
...
...
@@ -367,7 +396,8 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *ClaudeResponse) *dto.Ope
}
else
{
fullTextResponse
.
Id
=
claudeResponse
.
Id
for
_
,
message
:=
range
claudeResponse
.
Content
{
if
message
.
Type
==
"tool_use"
{
switch
message
.
Type
{
case
"tool_use"
:
args
,
_
:=
json
.
Marshal
(
message
.
Input
)
tools
=
append
(
tools
,
dto
.
ToolCall
{
ID
:
message
.
Id
,
...
...
@@ -377,6 +407,11 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *ClaudeResponse) *dto.Ope
Arguments
:
string
(
args
),
},
})
case
"thinking"
:
// 加密的不管, 只输出明文的推理过程
thinkingContent
=
message
.
Thinking
case
"text"
:
responseText
=
message
.
Text
}
}
}
...
...
@@ -391,6 +426,7 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *ClaudeResponse) *dto.Ope
if
len
(
tools
)
>
0
{
choice
.
Message
.
SetToolCalls
(
tools
)
}
choice
.
Message
.
ReasoningContent
=
thinkingContent
fullTextResponse
.
Model
=
claudeResponse
.
Model
choices
=
append
(
choices
,
choice
)
fullTextResponse
.
Choices
=
choices
...
...
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