mirror of
https://github.com/ollama/ollama.git
synced 2025-05-11 18:36:41 +02:00
types: add any type and validation for ToolFunction enum (#10166)
This commit is contained in:
parent
ccc8c6777b
commit
6747099d71
4 changed files with 71 additions and 10 deletions
|
@ -217,7 +217,7 @@ type ToolFunction struct {
|
||||||
Properties map[string]struct {
|
Properties map[string]struct {
|
||||||
Type PropertyType `json:"type"`
|
Type PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
} `json:"parameters"`
|
} `json:"parameters"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,6 +232,67 @@ func TestMessage_UnmarshalJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToolFunction_UnmarshalJSON(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid enum with same types",
|
||||||
|
input: `{
|
||||||
|
"name": "test",
|
||||||
|
"description": "test function",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["test"],
|
||||||
|
"properties": {
|
||||||
|
"test": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "test prop",
|
||||||
|
"enum": ["a", "b", "c"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
wantErr: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty enum array",
|
||||||
|
input: `{
|
||||||
|
"name": "test",
|
||||||
|
"description": "test function",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["test"],
|
||||||
|
"properties": {
|
||||||
|
"test": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "test prop",
|
||||||
|
"enum": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
wantErr: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var tf ToolFunction
|
||||||
|
err := json.Unmarshal([]byte(tt.input), &tf)
|
||||||
|
|
||||||
|
if tt.wantErr != "" {
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), tt.wantErr)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPropertyType_UnmarshalJSON(t *testing.T) {
|
func TestPropertyType_UnmarshalJSON(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
|
@ -285,7 +285,7 @@ func TestChatMiddleware(t *testing.T) {
|
||||||
Properties map[string]struct {
|
Properties map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
}{
|
}{
|
||||||
Type: "object",
|
Type: "object",
|
||||||
|
@ -293,7 +293,7 @@ func TestChatMiddleware(t *testing.T) {
|
||||||
Properties: map[string]struct {
|
Properties: map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
}{
|
}{
|
||||||
"location": {
|
"location": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
|
@ -301,7 +301,7 @@ func TestChatMiddleware(t *testing.T) {
|
||||||
},
|
},
|
||||||
"unit": {
|
"unit": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
Enum: []string{"celsius", "fahrenheit"},
|
Enum: []any{"celsius", "fahrenheit"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -374,7 +374,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
Properties map[string]struct {
|
Properties map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
}{
|
}{
|
||||||
Type: "object",
|
Type: "object",
|
||||||
|
@ -382,7 +382,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
Properties: map[string]struct {
|
Properties: map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
}{
|
}{
|
||||||
"location": {
|
"location": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
|
@ -390,7 +390,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
},
|
},
|
||||||
"unit": {
|
"unit": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
Enum: []string{"celsius", "fahrenheit"},
|
Enum: []any{"celsius", "fahrenheit"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -471,7 +471,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
Properties map[string]struct {
|
Properties map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
}{
|
}{
|
||||||
Type: "object",
|
Type: "object",
|
||||||
|
@ -479,7 +479,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
Properties: map[string]struct {
|
Properties: map[string]struct {
|
||||||
Type api.PropertyType `json:"type"`
|
Type api.PropertyType `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Enum []string `json:"enum,omitempty"`
|
Enum []any `json:"enum,omitempty"`
|
||||||
}{
|
}{
|
||||||
"location": {
|
"location": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
|
@ -487,7 +487,7 @@ func TestGenerateChat(t *testing.T) {
|
||||||
},
|
},
|
||||||
"unit": {
|
"unit": {
|
||||||
Type: api.PropertyType{"string"},
|
Type: api.PropertyType{"string"},
|
||||||
Enum: []string{"celsius", "fahrenheit"},
|
Enum: []any{"celsius", "fahrenheit"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue