Skip to content

Commit 11a0301

Browse files
committed
swap our JSONSchema implementation for kin-openapi's
Signed-off-by: Grant Linville <[email protected]>
1 parent 64e4fd6 commit 11a0301

File tree

11 files changed

+73
-223
lines changed

11 files changed

+73
-223
lines changed

pkg/loader/loader_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,13 @@ func TestHelloWorld(t *testing.T) {
7575
"modelName": "gpt-4-turbo-preview",
7676
"internalPrompt": null,
7777
"arguments": {
78-
"type": "object",
7978
"properties": {
8079
"input": {
8180
"description": " Any string",
82-
"type": "string",
83-
"properties": null,
84-
"additionalProperties": {
85-
"Has": null,
86-
"Schema": null
87-
}
81+
"type": "string"
8882
}
8983
},
90-
"additionalProperties": null
84+
"type": "object"
9185
},
9286
"instructions": "echo \"${input}\"",
9387
"id": "https://get.gptscript.ai/echo.gpt:1",

pkg/loader/openapi.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
6767
Parameters: types.Parameters{
6868
Name: operation.OperationID,
6969
Description: toolDesc,
70-
Arguments: &types.JSONSchema{
70+
Arguments: &openapi3.Schema{
7171
Type: "object",
72-
Properties: map[string]types.JSONSchema{},
72+
Properties: openapi3.Schemas{},
7373
Required: []string{},
74-
Defs: map[string]types.JSONSchema{},
7574
},
7675
},
7776
Source: types.ToolSource{
@@ -91,7 +90,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
9190
return nil, err
9291
}
9392

94-
arg := types.JSONSchema{}
93+
arg := openapi3.Schema{}
9594
if err := json.Unmarshal(raw, &arg); err != nil {
9695
return nil, err
9796
}
@@ -101,7 +100,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
101100
}
102101

103102
// Add the new arg to the tool's arguments
104-
tool.Parameters.Arguments.Properties[param.Value.Name] = arg
103+
tool.Parameters.Arguments.Properties[param.Value.Name] = &openapi3.SchemaRef{Value: &arg}
105104

106105
// Check whether it is required
107106
if param.Value.Required {
@@ -141,7 +140,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
141140
return nil, err
142141
}
143142

144-
arg := types.JSONSchema{}
143+
arg := openapi3.Schema{}
145144
if err := json.Unmarshal(raw, &arg); err != nil {
146145
return nil, err
147146
}
@@ -152,7 +151,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
152151

153152
// Unfortunately, the request body doesn't contain any good descriptor for it,
154153
// so we just use "requestBodyContent" as the name of the arg.
155-
tool.Parameters.Arguments.Properties["requestBodyContent"] = arg
154+
tool.Parameters.Arguments.Properties["requestBodyContent"] = &openapi3.SchemaRef{Value: &arg}
156155
break
157156
}
158157

@@ -161,6 +160,12 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
161160
}
162161
}
163162

163+
// OpenAI will get upset if we have an object schema with no properties,
164+
// so we just nil this out if there were no properties added.
165+
if len(tool.Arguments.Properties) == 0 {
166+
tool.Arguments = nil
167+
}
168+
164169
var err error
165170
tool.Instructions, err = instructionString(operationServer, method, pathString, bodyMIME, queryParameters, pathParameters, headerParameters, cookieParameters)
166171
if err != nil {

pkg/openai/client.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,6 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
305305

306306
for _, tool := range messageRequest.Tools {
307307
params := tool.Function.Parameters
308-
if params != nil {
309-
params.Properties = recursiveSetProperties(params.Properties)
310-
if params.Items != nil {
311-
params.Items.Properties = recursiveSetProperties(params.Items.Properties)
312-
}
313-
}
314308

315309
request.Tools = append(request.Tools, openai.Tool{
316310
Type: openai.ToolTypeFunction,
@@ -364,22 +358,6 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
364358
return &result, nil
365359
}
366360

367-
// recursiveSetProperties ensures that the Properties map is not nil for each property.
368-
// OpenAI will get mad and reject the function schema if any of these maps are left nil.
369-
func recursiveSetProperties(p map[string]types.JSONSchema) map[string]types.JSONSchema {
370-
if p == nil {
371-
return map[string]types.JSONSchema{}
372-
}
373-
for k, v := range p {
374-
v.Properties = recursiveSetProperties(v.Properties)
375-
if v.Items != nil {
376-
v.Items.Properties = recursiveSetProperties(v.Items.Properties)
377-
}
378-
p[k] = v
379-
}
380-
return p
381-
}
382-
383361
func appendMessage(msg types.CompletionMessage, response openai.ChatCompletionStreamResponse) types.CompletionMessage {
384362
if len(response.Choices) == 0 {
385363
return msg

pkg/parser/parser.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010

11+
"github.com/getkin/kin-openapi/openapi3"
1112
"github.com/gptscript-ai/gptscript/pkg/types"
1213
)
1314

@@ -46,9 +47,9 @@ func csv(line string) (result []string) {
4647

4748
func addArg(line string, tool *types.Tool) error {
4849
if tool.Parameters.Arguments == nil {
49-
tool.Parameters.Arguments = &types.JSONSchema{
50+
tool.Parameters.Arguments = &openapi3.Schema{
5051
Type: "object",
51-
Properties: map[string]types.JSONSchema{},
52+
Properties: openapi3.Schemas{},
5253
}
5354
}
5455

@@ -57,9 +58,11 @@ func addArg(line string, tool *types.Tool) error {
5758
return fmt.Errorf("invalid arg format: %s", line)
5859
}
5960

60-
tool.Parameters.Arguments.Properties[key] = types.JSONSchema{
61-
Description: value,
62-
Type: "string",
61+
tool.Parameters.Arguments.Properties[key] = &openapi3.SchemaRef{
62+
Value: &openapi3.Schema{
63+
Description: value,
64+
Type: "string",
65+
},
6366
}
6467

6568
return nil

pkg/system/prompt.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/gptscript-ai/gptscript/pkg/types"
8+
"github.com/getkin/kin-openapi/openapi3"
99
)
1010

1111
// Suffix is default suffix of gptscript files
@@ -26,12 +26,14 @@ You don't move to the next step until you have a result.
2626
// to just send pure text but the interface required JSON (as that is the fundamental interface of tools in OpenAI)
2727
var DefaultPromptParameter = "defaultPromptParameter"
2828

29-
var DefaultToolSchema = types.JSONSchema{
29+
var DefaultToolSchema = openapi3.Schema{
3030
Type: "object",
31-
Properties: map[string]types.JSONSchema{
32-
DefaultPromptParameter: {
33-
Description: "Prompt to send to the tool or assistant. This may be instructions or question.",
34-
Type: "string",
31+
Properties: openapi3.Schemas{
32+
DefaultPromptParameter: &openapi3.SchemaRef{
33+
Value: &openapi3.Schema{
34+
Description: "Prompt to send to the tool or assistant. This may be instructions or question.",
35+
Type: "string",
36+
},
3537
},
3638
},
3739
Required: []string{DefaultPromptParameter},

pkg/tests/testdata/TestExport/call1.golden

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@
77
"toolID": "testdata/TestExport/parent.gpt:5",
88
"name": "frommain",
99
"parameters": {
10-
"type": "object",
1110
"properties": {
1211
"defaultPromptParameter": {
1312
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
14-
"type": "string",
15-
"properties": null,
16-
"additionalProperties": {
17-
"Has": null,
18-
"Schema": null
19-
}
13+
"type": "string"
2014
}
2115
},
2216
"required": [
2317
"defaultPromptParameter"
2418
],
25-
"additionalProperties": null
19+
"type": "object"
2620
}
2721
}
2822
},
@@ -31,22 +25,16 @@
3125
"toolID": "testdata/TestExport/parent.gpt:11",
3226
"name": "parent-local",
3327
"parameters": {
34-
"type": "object",
3528
"properties": {
3629
"defaultPromptParameter": {
3730
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
38-
"type": "string",
39-
"properties": null,
40-
"additionalProperties": {
41-
"Has": null,
42-
"Schema": null
43-
}
31+
"type": "string"
4432
}
4533
},
4634
"required": [
4735
"defaultPromptParameter"
4836
],
49-
"additionalProperties": null
37+
"type": "object"
5038
}
5139
}
5240
},
@@ -55,22 +43,16 @@
5543
"toolID": "testdata/TestExport/sub/child.gpt:8",
5644
"name": "transient",
5745
"parameters": {
58-
"type": "object",
5946
"properties": {
6047
"defaultPromptParameter": {
6148
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
62-
"type": "string",
63-
"properties": null,
64-
"additionalProperties": {
65-
"Has": null,
66-
"Schema": null
67-
}
49+
"type": "string"
6850
}
6951
},
7052
"required": [
7153
"defaultPromptParameter"
7254
],
73-
"additionalProperties": null
55+
"type": "object"
7456
}
7557
}
7658
}

pkg/tests/testdata/TestExport/call3.golden

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@
77
"toolID": "testdata/TestExport/parent.gpt:5",
88
"name": "frommain",
99
"parameters": {
10-
"type": "object",
1110
"properties": {
1211
"defaultPromptParameter": {
1312
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
14-
"type": "string",
15-
"properties": null,
16-
"additionalProperties": {
17-
"Has": null,
18-
"Schema": null
19-
}
13+
"type": "string"
2014
}
2115
},
2216
"required": [
2317
"defaultPromptParameter"
2418
],
25-
"additionalProperties": null
19+
"type": "object"
2620
}
2721
}
2822
},
@@ -31,22 +25,16 @@
3125
"toolID": "testdata/TestExport/parent.gpt:11",
3226
"name": "parent-local",
3327
"parameters": {
34-
"type": "object",
3528
"properties": {
3629
"defaultPromptParameter": {
3730
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
38-
"type": "string",
39-
"properties": null,
40-
"additionalProperties": {
41-
"Has": null,
42-
"Schema": null
43-
}
31+
"type": "string"
4432
}
4533
},
4634
"required": [
4735
"defaultPromptParameter"
4836
],
49-
"additionalProperties": null
37+
"type": "object"
5038
}
5139
}
5240
},
@@ -55,22 +43,16 @@
5543
"toolID": "testdata/TestExport/sub/child.gpt:8",
5644
"name": "transient",
5745
"parameters": {
58-
"type": "object",
5946
"properties": {
6047
"defaultPromptParameter": {
6148
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
62-
"type": "string",
63-
"properties": null,
64-
"additionalProperties": {
65-
"Has": null,
66-
"Schema": null
67-
}
49+
"type": "string"
6850
}
6951
},
7052
"required": [
7153
"defaultPromptParameter"
7254
],
73-
"additionalProperties": null
55+
"type": "object"
7456
}
7557
}
7658
}

pkg/types/completion.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/fatih/color"
8+
"github.com/getkin/kin-openapi/openapi3"
89
)
910

1011
type CompletionRequest struct {
@@ -24,11 +25,11 @@ type CompletionTool struct {
2425
}
2526

2627
type CompletionFunctionDefinition struct {
27-
ToolID string `json:"toolID,omitempty"`
28-
Name string `json:"name"`
29-
Description string `json:"description,omitempty"`
30-
Domain string `json:"domain,omitempty"`
31-
Parameters *JSONSchema `json:"parameters"`
28+
ToolID string `json:"toolID,omitempty"`
29+
Name string `json:"name"`
30+
Description string `json:"description,omitempty"`
31+
Domain string `json:"domain,omitempty"`
32+
Parameters *openapi3.Schema `json:"parameters"`
3233
}
3334

3435
// Chat message role defined by the OpenAI API.

0 commit comments

Comments
 (0)