-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: generate name for OpenAPI tools when operation ID is blank (#…
…601) Signed-off-by: Grant Linville <[email protected]>
- Loading branch information
1 parent
a0013e4
commit d39962e
Showing
7 changed files
with
593 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package loader | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/gptscript-ai/gptscript/pkg/types" | ||
"github.com/hexops/autogold/v2" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadOpenAPI(t *testing.T) { | ||
numOpenAPITools := func(set types.ToolSet) int { | ||
num := 0 | ||
for _, v := range set { | ||
if v.IsOpenAPI() { | ||
num++ | ||
} | ||
} | ||
return num | ||
} | ||
|
||
prgv3 := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav3, err := os.ReadFile("testdata/openapi_v3.yaml") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv3, &source{Content: datav3}, "") | ||
require.NoError(t, err, "failed to read openapi v3") | ||
require.Equal(t, 3, numOpenAPITools(prgv3.ToolSet), "expected 3 openapi tools") | ||
|
||
prgv2json := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav2, err := os.ReadFile("testdata/openapi_v2.json") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv2json, &source{Content: datav2}, "") | ||
require.NoError(t, err, "failed to read openapi v2") | ||
require.Equal(t, 3, numOpenAPITools(prgv2json.ToolSet), "expected 3 openapi tools") | ||
|
||
prgv2yaml := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav2, err = os.ReadFile("testdata/openapi_v2.yaml") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv2yaml, &source{Content: datav2}, "") | ||
require.NoError(t, err, "failed to read openapi v2 (yaml)") | ||
require.Equal(t, 3, numOpenAPITools(prgv2yaml.ToolSet), "expected 3 openapi tools") | ||
|
||
require.EqualValuesf(t, prgv2json.ToolSet, prgv2yaml.ToolSet, "expected same toolset for openapi v2 json and yaml") | ||
} | ||
|
||
func TestOpenAPIv3(t *testing.T) { | ||
prgv3 := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav3, err := os.ReadFile("testdata/openapi_v3.yaml") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv3, &source{Content: datav3}, "") | ||
require.NoError(t, err) | ||
|
||
autogold.ExpectFile(t, prgv3.ToolSet, autogold.Dir("testdata/openapi")) | ||
} | ||
|
||
func TestOpenAPIv3NoOperationIDs(t *testing.T) { | ||
prgv3 := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav3, err := os.ReadFile("testdata/openapi_v3_no_operation_ids.yaml") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv3, &source{Content: datav3}, "") | ||
require.NoError(t, err) | ||
|
||
autogold.ExpectFile(t, prgv3.ToolSet, autogold.Dir("testdata/openapi")) | ||
} | ||
|
||
func TestOpenAPIv2(t *testing.T) { | ||
prgv2 := types.Program{ | ||
ToolSet: types.ToolSet{}, | ||
} | ||
datav2, err := os.ReadFile("testdata/openapi_v2.yaml") | ||
require.NoError(t, err) | ||
_, err = readTool(context.Background(), nil, &prgv2, &source{Content: datav2}, "") | ||
require.NoError(t, err) | ||
|
||
autogold.ExpectFile(t, prgv2.ToolSet, autogold.Dir("testdata/openapi")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
types.ToolSet{ | ||
":": types.Tool{ | ||
ToolDef: types.ToolDef{Parameters: types.Parameters{ | ||
Description: "This is a tool set for the Swagger Petstore OpenAPI spec", | ||
ModelName: "gpt-4o", | ||
Export: []string{ | ||
"listPets", | ||
"createPets", | ||
"showPetById", | ||
}, | ||
}}, | ||
ID: ":", | ||
ToolMapping: map[string][]types.ToolReference{ | ||
"createPets": {{ | ||
Reference: "createPets", | ||
ToolID: ":createPets", | ||
}}, | ||
"listPets": {{ | ||
Reference: "listPets", | ||
ToolID: ":listPets", | ||
}}, | ||
"showPetById": {{ | ||
Reference: "showPetById", | ||
ToolID: ":showPetById", | ||
}}, | ||
}, | ||
LocalTools: map[string]string{ | ||
"": ":", | ||
"createpets": ":createPets", | ||
"listpets": ":listPets", | ||
"showpetbyid": ":showPetById", | ||
}, | ||
}, | ||
":createPets": types.Tool{ | ||
ToolDef: types.ToolDef{ | ||
Parameters: types.Parameters{ | ||
Name: "createPets", | ||
Description: "Create a pet", | ||
ModelName: "gpt-4o", | ||
}, | ||
Instructions: `#!sys.openapi '{"server":"http://petstore.swagger.io/v1","path":"/pets","method":"POST","bodyContentMIME":"","apiKeyInfos":null,"queryParameters":null,"pathParameters":null,"headerParameters":null,"cookieParameters":null}'`, | ||
}, | ||
ID: ":createPets", | ||
ToolMapping: map[string][]types.ToolReference{}, | ||
LocalTools: map[string]string{ | ||
"": ":", | ||
"createpets": ":createPets", | ||
"listpets": ":listPets", | ||
"showpetbyid": ":showPetById", | ||
}, | ||
Source: types.ToolSource{LineNo: 2}, | ||
}, | ||
":listPets": types.Tool{ | ||
ToolDef: types.ToolDef{ | ||
Parameters: types.Parameters{ | ||
Name: "listPets", | ||
Description: "List all pets", | ||
ModelName: "gpt-4o", | ||
Arguments: &openapi3.Schema{ | ||
Type: &openapi3.Types{ | ||
"object", | ||
}, | ||
Required: []string{}, | ||
Properties: openapi3.Schemas{"limit": &openapi3.SchemaRef{Value: &openapi3.Schema{ | ||
Type: &openapi3.Types{"integer"}, | ||
Format: "int32", | ||
Description: "How many items to return at one time (max 100)", | ||
}}}, | ||
}, | ||
}, | ||
Instructions: `#!sys.openapi '{"server":"http://petstore.swagger.io/v1","path":"/pets","method":"GET","bodyContentMIME":"","apiKeyInfos":null,"queryParameters":[{"name":"limit","style":"","explode":null}],"pathParameters":null,"headerParameters":null,"cookieParameters":null}'`, | ||
}, | ||
ID: ":listPets", | ||
ToolMapping: map[string][]types.ToolReference{}, | ||
LocalTools: map[string]string{ | ||
"": ":", | ||
"createpets": ":createPets", | ||
"listpets": ":listPets", | ||
"showpetbyid": ":showPetById", | ||
}, | ||
Source: types.ToolSource{LineNo: 1}, | ||
}, | ||
":showPetById": types.Tool{ | ||
ToolDef: types.ToolDef{ | ||
Parameters: types.Parameters{ | ||
Name: "showPetById", | ||
Description: "Info for a specific pet", | ||
ModelName: "gpt-4o", | ||
Arguments: &openapi3.Schema{ | ||
Type: &openapi3.Types{"object"}, | ||
Required: []string{"petId"}, | ||
Properties: openapi3.Schemas{"petId": &openapi3.SchemaRef{Value: &openapi3.Schema{ | ||
Type: &openapi3.Types{"string"}, | ||
Description: "The id of the pet to retrieve", | ||
}}}, | ||
}, | ||
}, | ||
Instructions: `#!sys.openapi '{"server":"http://petstore.swagger.io/v1","path":"/pets/{petId}","method":"GET","bodyContentMIME":"","apiKeyInfos":null,"queryParameters":null,"pathParameters":[{"name":"petId","style":"","explode":null}],"headerParameters":null,"cookieParameters":null}'`, | ||
}, | ||
ID: ":showPetById", | ||
ToolMapping: map[string][]types.ToolReference{}, | ||
LocalTools: map[string]string{ | ||
"": ":", | ||
"createpets": ":createPets", | ||
"listpets": ":listPets", | ||
"showpetbyid": ":showPetById", | ||
}, | ||
Source: types.ToolSource{LineNo: 3}, | ||
}, | ||
} |
Oops, something went wrong.