fix(providers): remove hardcoded max_tokens in anthropic-messages provider
- remove hardcoded max_tokens value (4096) from buildRequestBody - read max_tokens directly from options parameter - add error handling when max_tokens is missing from options - update test cases to include max_tokens in options This fix ensures the provider respects the config default value (32768) or system fallback (8192) instead of always using the hardcoded 4096.
This commit is contained in:
parent
ddfa437f9c
commit
94c7d879f8
2 changed files with 38 additions and 19 deletions
|
|
@ -140,15 +140,16 @@ func buildRequestBody(
|
|||
model string,
|
||||
options map[string]any,
|
||||
) (map[string]any, error) {
|
||||
result := map[string]any{
|
||||
"model": model,
|
||||
"max_tokens": int64(4096),
|
||||
"messages": []any{},
|
||||
// max_tokens is required and guaranteed by agent loop
|
||||
maxTokens, ok := asInt(options["max_tokens"])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("max_tokens is required in options")
|
||||
}
|
||||
|
||||
// Set max_tokens from options
|
||||
if mt, ok := asInt(options["max_tokens"]); ok {
|
||||
result["max_tokens"] = int64(mt)
|
||||
result := map[string]any{
|
||||
"model": model,
|
||||
"max_tokens": int64(maxTokens),
|
||||
"messages": []any{},
|
||||
}
|
||||
|
||||
// Set temperature from options
|
||||
|
|
|
|||
|
|
@ -27,11 +27,13 @@ func TestBuildRequestBody(t *testing.T) {
|
|||
messages: []Message{
|
||||
{Role: "user", Content: "Hello, world!"},
|
||||
},
|
||||
model: "test-model",
|
||||
options: map[string]any{},
|
||||
model: "test-model",
|
||||
options: map[string]any{
|
||||
"max_tokens": 8192,
|
||||
},
|
||||
want: map[string]any{
|
||||
"model": "test-model",
|
||||
"max_tokens": int64(4096),
|
||||
"max_tokens": int64(8192),
|
||||
"messages": []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
|
|
@ -46,11 +48,13 @@ func TestBuildRequestBody(t *testing.T) {
|
|||
{Role: "user", Content: "What is 2+2?"},
|
||||
{Role: "assistant", Content: "4"},
|
||||
},
|
||||
model: "test-model",
|
||||
options: map[string]any{},
|
||||
model: "test-model",
|
||||
options: map[string]any{
|
||||
"max_tokens": 8192,
|
||||
},
|
||||
want: map[string]any{
|
||||
"model": "test-model",
|
||||
"max_tokens": int64(4096),
|
||||
"max_tokens": int64(8192),
|
||||
"messages": []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
|
|
@ -74,11 +78,13 @@ func TestBuildRequestBody(t *testing.T) {
|
|||
{Role: "system", Content: "You are a helpful assistant."},
|
||||
{Role: "user", Content: "Hello"},
|
||||
},
|
||||
model: "test-model",
|
||||
options: map[string]any{},
|
||||
model: "test-model",
|
||||
options: map[string]any{
|
||||
"max_tokens": 8192,
|
||||
},
|
||||
want: map[string]any{
|
||||
"model": "test-model",
|
||||
"max_tokens": int64(4096),
|
||||
"max_tokens": int64(8192),
|
||||
"system": "You are a helpful assistant.",
|
||||
"messages": []any{
|
||||
map[string]any{
|
||||
|
|
@ -110,6 +116,16 @@ func TestBuildRequestBody(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing max_tokens returns error",
|
||||
messages: []Message{
|
||||
{Role: "user", Content: "Test"},
|
||||
},
|
||||
model: "test-model",
|
||||
options: map[string]any{},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "with tools",
|
||||
messages: []Message{
|
||||
|
|
@ -132,11 +148,13 @@ func TestBuildRequestBody(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
model: "test-model",
|
||||
options: map[string]any{},
|
||||
model: "test-model",
|
||||
options: map[string]any{
|
||||
"max_tokens": 8192,
|
||||
},
|
||||
want: map[string]any{
|
||||
"model": "test-model",
|
||||
"max_tokens": int64(4096),
|
||||
"max_tokens": int64(8192),
|
||||
"messages": []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue