mirror of https://gitee.com/namelin2022/ollama
Browse Source
afaik gpt-oss is the first model that meaningfully transforms tool function definitions in its template. We found that relatively common definitions that include `anyOf` were not working because the template was assuming that types were always defined via a `type` field. anyOf allows for fully recursive types, so I exposed a `toTypeScriptType()` function to handle this recursive logic in go and keep the templates cleaner. The gpt-oss templates will need to be updated to use this. We should keep building out our function definition support to more fully support the parts of json schema that make sense for this use case, but in the meantime this will unblock some users (e.g., zed's ollama integration w/ gpt-oss). Probably the most urgent is proper array supportdrifkin/print-template
7 changed files with 264 additions and 154 deletions
@ -0,0 +1,142 @@ |
|||
package api |
|||
|
|||
import ( |
|||
"testing" |
|||
) |
|||
|
|||
func TestToolParameterToTypeScriptType(t *testing.T) { |
|||
tests := []struct { |
|||
name string |
|||
param ToolProperty |
|||
expected string |
|||
}{ |
|||
{ |
|||
name: "single string type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"string"}, |
|||
}, |
|||
expected: "string", |
|||
}, |
|||
{ |
|||
name: "single number type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"number"}, |
|||
}, |
|||
expected: "number", |
|||
}, |
|||
{ |
|||
name: "integer maps to number", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"integer"}, |
|||
}, |
|||
expected: "number", |
|||
}, |
|||
{ |
|||
name: "boolean type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"boolean"}, |
|||
}, |
|||
expected: "boolean", |
|||
}, |
|||
{ |
|||
name: "array type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"array"}, |
|||
}, |
|||
expected: "any[]", |
|||
}, |
|||
{ |
|||
name: "object type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"object"}, |
|||
}, |
|||
expected: "Record<string, any>", |
|||
}, |
|||
{ |
|||
name: "null type", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"null"}, |
|||
}, |
|||
expected: "null", |
|||
}, |
|||
{ |
|||
name: "multiple types as union", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"string", "number"}, |
|||
}, |
|||
expected: "string | number", |
|||
}, |
|||
{ |
|||
name: "string or null union", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"string", "null"}, |
|||
}, |
|||
expected: "string | null", |
|||
}, |
|||
{ |
|||
name: "anyOf with single types", |
|||
param: ToolProperty{ |
|||
AnyOf: []ToolProperty{ |
|||
{Type: PropertyType{"string"}}, |
|||
{Type: PropertyType{"number"}}, |
|||
}, |
|||
}, |
|||
expected: "string | number", |
|||
}, |
|||
{ |
|||
name: "anyOf with multiple types in each branch", |
|||
param: ToolProperty{ |
|||
AnyOf: []ToolProperty{ |
|||
{Type: PropertyType{"string", "null"}}, |
|||
{Type: PropertyType{"number"}}, |
|||
}, |
|||
}, |
|||
expected: "string | null | number", |
|||
}, |
|||
{ |
|||
name: "nested anyOf", |
|||
param: ToolProperty{ |
|||
AnyOf: []ToolProperty{ |
|||
{Type: PropertyType{"boolean"}}, |
|||
{ |
|||
AnyOf: []ToolProperty{ |
|||
{Type: PropertyType{"string"}}, |
|||
{Type: PropertyType{"number"}}, |
|||
}, |
|||
}, |
|||
}, |
|||
}, |
|||
expected: "boolean | string | number", |
|||
}, |
|||
{ |
|||
name: "empty type returns any", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{}, |
|||
}, |
|||
expected: "any", |
|||
}, |
|||
{ |
|||
name: "unknown type maps to any", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"unknown_type"}, |
|||
}, |
|||
expected: "any", |
|||
}, |
|||
{ |
|||
name: "multiple types including array", |
|||
param: ToolProperty{ |
|||
Type: PropertyType{"string", "array", "null"}, |
|||
}, |
|||
expected: "string | any[] | null", |
|||
}, |
|||
} |
|||
|
|||
for _, tt := range tests { |
|||
t.Run(tt.name, func(t *testing.T) { |
|||
result := tt.param.ToTypeScriptType() |
|||
if result != tt.expected { |
|||
t.Errorf("ToTypeScriptType() = %q, want %q", result, tt.expected) |
|||
} |
|||
}) |
|||
} |
|||
} |
|||
Loading…
Reference in new issue