Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions components/model/claude/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ func (cm *ChatModel) genMessageNewParams(input []*schema.Message, opts ...model.
}

messages := make([]anthropic.MessageParam, 0, len(msgs))
hasTools := len(tools) > 0
for _, msg := range msgs {
message, err := convSchemaMessage(msg)
message, err := convSchemaMessage(msg, claudeOptions, hasTools)
if err != nil {
return anthropic.MessageNewParams{}, fmt.Errorf("convert schema message fail: %w", err)
}
Expand Down Expand Up @@ -592,9 +593,34 @@ func (cm *ChatModel) IsCallbacksEnabled() bool {
return true
}

func convSchemaMessage(message *schema.Message) (mp anthropic.MessageParam, err error) {
func hasToolCalls(message *schema.Message) bool {
return len(message.ToolCalls) > 0
}

func convSchemaMessage(message *schema.Message, opts *options, hasTools bool) (mp anthropic.MessageParam, err error) {

var messageParams []anthropic.ContentBlockParamUnion

if message.Role == schema.Assistant {
shouldSendThinking := false

if opts != nil && opts.SendBackThinking != nil {
shouldSendThinking = *opts.SendBackThinking
} else if hasTools && hasToolCalls(message) {
shouldSendThinking = true
}

if shouldSendThinking {
thinkingContent, hasThinking := GetThinking(message)
if hasThinking && thinkingContent != "" {
signature, hasSignature := GetThinkingSignature(message)
if hasSignature && signature != "" {
messageParams = append(messageParams, anthropic.NewThinkingBlock(signature, thinkingContent))
}
}
}
}

if len(message.Content) > 0 {
if len(message.ToolCallID) > 0 {
messageParams = append(messageParams, anthropic.NewToolResultBlock(message.ToolCallID, message.Content, false))
Expand Down Expand Up @@ -693,6 +719,7 @@ func convContentBlockToEinoMsg(
case anthropic.ThinkingBlock:
setThinking(dstMsg, block.Thinking)
dstMsg.ReasoningContent = block.Thinking
SetThinkingSignature(dstMsg, block.Signature)
case anthropic.RedactedThinkingBlock:
default:
return fmt.Errorf("unknown anthropic content block type: %T", block)
Expand Down Expand Up @@ -756,6 +783,11 @@ func convStreamEvent(event anthropic.MessageStreamEventUnion, streamCtx *streamC
result.ToolCalls = append(result.ToolCalls,
toolEvent(false, "", "", delta.PartialJSON, streamCtx))
case anthropic.SignatureDelta:
if currentSig, hasSig := GetThinkingSignature(result); hasSig {
SetThinkingSignature(result, currentSig+delta.Signature)
} else {
SetThinkingSignature(result, delta.Signature)
}
}

return result, nil
Expand Down
24 changes: 23 additions & 1 deletion components/model/claude/message_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

const (
keyOfThinking = "_eino_claude_thinking"
keyOfThinking = "_eino_claude_thinking"
keyOfThinkingSignature = "_eino_claude_thinking_signature"
)

func GetThinking(msg *schema.Message) (string, bool) {
Expand All @@ -45,3 +46,24 @@ func setThinking(msg *schema.Message, reasoningContent string) {
}
msg.Extra[keyOfThinking] = reasoningContent
}

func GetThinkingSignature(msg *schema.Message) (string, bool) {
if msg == nil {
return "", false
}
signature, ok := msg.Extra[keyOfThinkingSignature].(string)
if !ok {
return "", false
}
return signature, true
}

func SetThinkingSignature(msg *schema.Message, signature string) {
if msg == nil {
return
}
if msg.Extra == nil {
msg.Extra = make(map[string]interface{})
}
msg.Extra[keyOfThinkingSignature] = signature
}
8 changes: 8 additions & 0 deletions components/model/claude/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type options struct {
Thinking *Thinking

DisableParallelToolUse *bool

SendBackThinking *bool
}

func WithTopK(k int32) model.Option {
Expand All @@ -46,3 +48,9 @@ func WithDisableParallelToolUse() model.Option {
o.DisableParallelToolUse = &b
})
}

func WithSendBackThinking(enabled bool) model.Option {
return model.WrapImplSpecificOptFn(func(o *options) {
o.SendBackThinking = &enabled
})
}