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
9 changes: 9 additions & 0 deletions libs/acl/openai/chat_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func (c *Client) genRequest(ctx context.Context, in []*schema.Message, opts ...m
Name: t.Function.Name,
Description: t.Function.Description,
Parameters: t.Function.Parameters,
Strict: t.Function.Strict,
},
}
}
Expand Down Expand Up @@ -1291,11 +1292,19 @@ func toTools(tis []*schema.ToolInfo) ([]tool, error) {

sortArrayFields(paramsJSONSchema)

var strict bool
if v, ok := ti.Extra["strict"]; ok {
if b, ok := v.(bool); ok {
strict = b
}
}

tools[i] = tool{
Function: &functionDefinition{
Name: ti.Name,
Description: ti.Desc,
Parameters: paramsJSONSchema,
Strict: strict,
},
}
}
Expand Down
79 changes: 79 additions & 0 deletions libs/acl/openai/chat_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,85 @@ func TestToTools(t *testing.T) {
})
}

func TestToToolsStrict(t *testing.T) {
mockey.PatchConvey("strict via Extra", t, func() {
mockTools := []*schema.ToolInfo{
{
Name: "strict_tool",
Desc: "a tool with strict mode",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"query": {
Type: schema.String,
Required: true,
},
}),
Extra: map[string]any{"strict": true},
},
{
Name: "non_strict_tool",
Desc: "a tool without strict mode",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"query": {
Type: schema.String,
Required: true,
},
}),
},
{
Name: "invalid_strict_tool",
Desc: "a tool with invalid strict value",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"query": {
Type: schema.String,
Required: true,
},
}),
Extra: map[string]any{"strict": "yes"},
},
}

tools, err := toTools(mockTools)
assert.Nil(t, err)
assert.Len(t, tools, 3)

assert.True(t, tools[0].Function.Strict, "strict_tool should have Strict=true")
assert.False(t, tools[1].Function.Strict, "non_strict_tool should have Strict=false")
assert.False(t, tools[2].Function.Strict, "invalid strict value should default to false")
})

mockey.PatchConvey("strict propagated to genRequest", t, func() {
ctx := context.Background()
strictTools := []*schema.ToolInfo{
{
Name: "strict_tool",
Desc: "a tool with strict mode",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"queries": {
Type: schema.Array,
Required: true,
},
}),
Extra: map[string]any{"strict": true},
},
}

c := &Client{
config: &Config{Model: "test-model"},
}

nc, err := c.WithToolsForClient(strictTools)
assert.Nil(t, err)

req, _, _, _, err := nc.genRequest(ctx, []*schema.Message{
{Role: schema.User, Content: "hello"},
})
assert.Nil(t, err)
assert.Len(t, req.Tools, 1)
assert.True(t, req.Tools[0].Function.Strict, "FunctionDefinition.Strict should be true")
assert.Equal(t, "strict_tool", req.Tools[0].Function.Name)
})
}

func TestBuildMessages(t *testing.T) {
t.Run("buildMessageFromAssistantGenMultiContent", func(t *testing.T) {
t.Run("success with audio", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions libs/acl/openai/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ type functionDefinition struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters *jsonschema.Schema `json:"parameters"`
Strict bool `json:"strict,omitempty"`
}
Loading