Skip to content

Commit 59a320a

Browse files
committed
fix: concat agentic messages (#604)
1 parent 20c0568 commit 59a320a

20 files changed

+1896
-737
lines changed

components/agentic/callback_extra.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ type Config struct {
2626
// Model is the model name.
2727
Model string
2828
// Temperature is the temperature, which controls the randomness of the model.
29-
Temperature float32
29+
Temperature float64
3030
// TopP is the top p, which controls the diversity of the model.
31-
TopP float32
31+
TopP float64
3232
}
3333

3434
// CallbackInput is the input for the model callback.

components/agentic/option.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ type Options struct {
3030
TopP *float64
3131
// Tools is a list of tools the model may call.
3232
Tools []*schema.ToolInfo
33-
// ToolChoice controls which tool is called by the model.
33+
// ToolChoice controls how the model call the tools.
3434
ToolChoice *schema.ToolChoice
35+
// AllowedTools is a list of allowed tools the model may call.
36+
AllowedTools []*schema.AllowedTool
3537
}
3638

3739
// Option is the call option for ChatModel component.
@@ -81,10 +83,11 @@ func WithTools(tools []*schema.ToolInfo) Option {
8183
}
8284

8385
// WithToolChoice is the option to set tool choice for the model.
84-
func WithToolChoice(toolChoice schema.ToolChoice) Option {
86+
func WithToolChoice(toolChoice schema.ToolChoice, allowedTools ...*schema.AllowedTool) Option {
8587
return Option{
8688
apply: func(opts *Options) {
8789
opts.ToolChoice = &toolChoice
90+
opts.AllowedTools = allowedTools
8891
},
8992
}
9093
}

components/agentic/option_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestCommon(t *testing.T) {
2929
WithTools([]*schema.ToolInfo{{Name: "test"}}),
3030
WithModel("test"),
3131
WithTemperature(0.1),
32-
WithToolChoice(schema.ToolChoiceAllowed),
32+
WithToolChoice(schema.ToolChoiceAllowed, []*schema.AllowedTool{{FunctionToolName: "test"}}...),
3333
WithTopP(0.1),
3434
)
3535
assert.Len(t, o.Tools, 1)

components/model/callback_extra.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ type TokenUsage struct {
3030
PromptTokenDetails PromptTokenDetails
3131
// CompletionTokens is the number of completion tokens.
3232
CompletionTokens int
33+
// CompletionTokensDetails is a breakdown of the completion tokens.
34+
CompletionTokensDetails CompletionTokensDetails
3335
// TotalTokens is the total number of tokens.
3436
TotalTokens int
35-
// CompletionTokensDetails is breakdown of completion tokens.
36-
CompletionTokensDetails CompletionTokensDetails `json:"completion_token_details"`
3737
}
3838

3939
type CompletionTokensDetails struct {
4040
// ReasoningTokens tokens generated by the model for reasoning.
4141
// This is currently supported by OpenAI, Gemini, ARK and Qwen chat models.
4242
// For other models, this field will be 0.
43-
ReasoningTokens int `json:"reasoning_tokens,omitempty"`
43+
ReasoningTokens int
4444
}
4545

4646
// PromptTokenDetails provides a breakdown of prompt token usage.

compose/tools_node_agentic.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func agenticMessageToToolCallMessage(input *schema.AgenticMessage) *schema.Messa
7070
Name: block.FunctionToolCall.Name,
7171
Arguments: block.FunctionToolCall.Arguments,
7272
},
73+
Extra: block.Extra,
7374
})
7475
}
7576
return &schema.Message{
@@ -87,8 +88,8 @@ func toolMessageToAgenticMessage(input []*schema.Message) []*schema.AgenticMessa
8788
CallID: m.ToolCallID,
8889
Name: m.ToolName,
8990
Result: m.Content,
90-
Extra: m.Extra,
9191
},
92+
Extra: m.Extra,
9293
})
9394
}
9495
return []*schema.AgenticMessage{{
@@ -110,9 +111,9 @@ func streamToolMessageToAgenticMessage(input *schema.StreamReader[[]*schema.Mess
110111
CallID: m.ToolCallID,
111112
Name: m.ToolName,
112113
Result: m.Content,
113-
Extra: m.Extra,
114114
},
115-
StreamMeta: &schema.StreamMeta{Index: int64(i)},
115+
StreamingMeta: &schema.StreamingMeta{Index: i},
116+
Extra: m.Extra,
116117
})
117118
}
118119
return []*schema.AgenticMessage{{

compose/tools_node_agentic_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"testing"
2222

23+
"github.com/bytedance/sonic"
2324
"github.com/stretchr/testify/assert"
2425

2526
"github.com/cloudwego/eino/schema"
@@ -155,13 +156,14 @@ func TestStreamToolMessageToAgenticMessage(t *testing.T) {
155156
nil,
156157
},
157158
{
159+
nil,
158160
{
159161
Role: schema.Tool,
160-
Content: "content1-2",
162+
Content: "content2-2",
161163
ToolName: "name2",
162164
ToolCallID: "2",
163165
},
164-
nil, nil,
166+
nil,
165167
},
166168
{
167169
nil, nil,
@@ -172,16 +174,6 @@ func TestStreamToolMessageToAgenticMessage(t *testing.T) {
172174
ToolCallID: "3",
173175
},
174176
},
175-
{
176-
nil,
177-
{
178-
Role: schema.Tool,
179-
Content: "content2-2",
180-
ToolName: "name2",
181-
ToolCallID: "2",
182-
},
183-
nil,
184-
},
185177
{
186178
nil, nil,
187179
{
@@ -204,7 +196,11 @@ func TestStreamToolMessageToAgenticMessage(t *testing.T) {
204196
}
205197
result, err := schema.ConcatAgenticMessagesArray(chunks)
206198
assert.NoError(t, err)
207-
assert.Equal(t, []*schema.AgenticMessage{
199+
200+
actualStr, err := sonic.MarshalString(result)
201+
assert.NoError(t, err)
202+
203+
expected := []*schema.AgenticMessage{
208204
{
209205
Role: schema.AgenticRoleTypeUser,
210206
ContentBlocks: []*schema.ContentBlock{
@@ -213,32 +209,31 @@ func TestStreamToolMessageToAgenticMessage(t *testing.T) {
213209
FunctionToolResult: &schema.FunctionToolResult{
214210
CallID: "1",
215211
Name: "name1",
216-
Result: "content1-1content1-2",
217-
Extra: map[string]interface{}{},
212+
Result: "content1-1",
218213
},
219-
StreamMeta: &schema.StreamMeta{Index: 0},
220214
},
221215
{
222216
Type: schema.ContentBlockTypeFunctionToolResult,
223217
FunctionToolResult: &schema.FunctionToolResult{
224218
CallID: "2",
225219
Name: "name2",
226220
Result: "content2-1content2-2",
227-
Extra: map[string]interface{}{},
228221
},
229-
StreamMeta: &schema.StreamMeta{Index: 1},
230222
},
231223
{
232224
Type: schema.ContentBlockTypeFunctionToolResult,
233225
FunctionToolResult: &schema.FunctionToolResult{
234226
CallID: "3",
235227
Name: "name3",
236228
Result: "content3-1content3-2",
237-
Extra: map[string]interface{}{},
238229
},
239-
StreamMeta: &schema.StreamMeta{Index: 2},
240230
},
241231
},
242232
},
243-
}, result)
233+
}
234+
235+
expectedStr, err := sonic.MarshalString(expected)
236+
assert.NoError(t, err)
237+
238+
assert.Equal(t, expectedStr, actualStr)
244239
}

0 commit comments

Comments
 (0)