Skip to content

Commit 4341fa1

Browse files
authoredJun 5, 2024··
Merge pull request #26 from thedadams/remove-to-string
chore: remove ToString method for tools
2 parents 7a845df + 41f191d commit 4341fa1

File tree

6 files changed

+94
-159
lines changed

6 files changed

+94
-159
lines changed
 

‎frame.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ type Call struct {
8484
}
8585

8686
type CallContext struct {
87-
ID string `json:"id"`
88-
Tool Tool `json:"tool"`
89-
DisplayText string `json:"displayText"`
90-
InputContext []InputContext `json:"inputContext"`
91-
ToolCategory ToolCategory `json:"toolCategory,omitempty"`
92-
ToolName string `json:"toolName,omitempty"`
93-
ParentID string `json:"parentID,omitempty"`
87+
ID string `json:"id"`
88+
Tool Tool `json:"tool"`
89+
AgentGroup []ToolReference `json:"agentGroup,omitempty"`
90+
DisplayText string `json:"displayText"`
91+
InputContext []InputContext `json:"inputContext"`
92+
ToolCategory ToolCategory `json:"toolCategory,omitempty"`
93+
ToolName string `json:"toolName,omitempty"`
94+
ParentID string `json:"parentID,omitempty"`
9495
}
9596

9697
type InputContext struct {

‎gptscript.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const relativeToBinaryPath = "<me>"
2828

2929
type GPTScript interface {
3030
Run(context.Context, string, Options) (*Run, error)
31-
Evaluate(context.Context, Options, ...fmt.Stringer) (*Run, error)
31+
Evaluate(context.Context, Options, ...ToolDef) (*Run, error)
3232
Parse(context.Context, string) ([]Node, error)
3333
ParseTool(context.Context, string) ([]Node, error)
3434
Version(context.Context) (string, error)
@@ -124,13 +124,13 @@ func (g *gptscript) Close() {
124124
}
125125
}
126126

127-
func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...fmt.Stringer) (*Run, error) {
127+
func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
128128
return (&Run{
129129
url: g.url,
130130
requestPath: "evaluate",
131131
state: Creating,
132132
opts: opts,
133-
content: concatTools(tools),
133+
tools: tools,
134134
}).NextChat(ctx, opts.Input)
135135
}
136136

‎gptscript_test.go

+29-33
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestListModels(t *testing.T) {
8181
}
8282

8383
func TestAbortRun(t *testing.T) {
84-
tool := &ToolDef{Instructions: "What is the capital of the united states?"}
84+
tool := ToolDef{Instructions: "What is the capital of the united states?"}
8585

8686
run, err := g.Evaluate(context.Background(), Options{DisableCache: true, IncludeEvents: true}, tool)
8787
if err != nil {
@@ -105,7 +105,7 @@ func TestAbortRun(t *testing.T) {
105105
}
106106

107107
func TestSimpleEvaluate(t *testing.T) {
108-
tool := &ToolDef{Instructions: "What is the capital of the united states?"}
108+
tool := ToolDef{Instructions: "What is the capital of the united states?"}
109109

110110
run, err := g.Evaluate(context.Background(), Options{}, tool)
111111
if err != nil {
@@ -142,7 +142,7 @@ func TestEvaluateWithContext(t *testing.T) {
142142
t.Fatalf("Error getting current working directory: %v", err)
143143
}
144144

145-
tool := &ToolDef{
145+
tool := ToolDef{
146146
Instructions: "What is the capital of the united states?",
147147
Context: []string{
148148
wd + "/test/acorn-labs-context.gpt",
@@ -165,7 +165,7 @@ func TestEvaluateWithContext(t *testing.T) {
165165
}
166166

167167
func TestEvaluateComplexTool(t *testing.T) {
168-
tool := &ToolDef{
168+
tool := ToolDef{
169169
JSONResponse: true,
170170
Instructions: `
171171
Create three short graphic artist descriptions and their muses.
@@ -202,18 +202,16 @@ func TestEvaluateWithToolList(t *testing.T) {
202202
if runtime.GOOS == "windows" {
203203
shebang = "#!/usr/bin/env powershell.exe"
204204
}
205-
tools := []fmt.Stringer{
206-
&ToolDef{
205+
tools := []ToolDef{
206+
{
207207
Tools: []string{"echo"},
208208
Instructions: "echo hello there",
209209
},
210-
&ToolDef{
211-
Name: "echo",
212-
Tools: []string{"sys.exec"},
213-
Description: "Echoes the input",
214-
Args: map[string]string{
215-
"input": "The string input to echo",
216-
},
210+
{
211+
Name: "echo",
212+
Tools: []string{"sys.exec"},
213+
Description: "Echoes the input",
214+
Arguments: ObjectSchema("input", "The string input to echo"),
217215
Instructions: shebang + "\n echo ${input}",
218216
},
219217
}
@@ -238,23 +236,21 @@ func TestEvaluateWithToolListAndSubTool(t *testing.T) {
238236
if runtime.GOOS == "windows" {
239237
shebang = "#!/usr/bin/env powershell.exe"
240238
}
241-
tools := []fmt.Stringer{
242-
&ToolDef{
239+
tools := []ToolDef{
240+
{
243241
Tools: []string{"echo"},
244242
Instructions: "echo 'hello there'",
245243
},
246-
&ToolDef{
244+
{
247245
Name: "other",
248246
Tools: []string{"echo"},
249247
Instructions: "echo 'hello somewhere else'",
250248
},
251-
&ToolDef{
252-
Name: "echo",
253-
Tools: []string{"sys.exec"},
254-
Description: "Echoes the input",
255-
Args: map[string]string{
256-
"input": "The string input to echo",
257-
},
249+
{
250+
Name: "echo",
251+
Tools: []string{"sys.exec"},
252+
Description: "Echoes the input",
253+
Arguments: ObjectSchema("input", "The string input to echo"),
258254
Instructions: shebang + "\n echo ${input}",
259255
},
260256
}
@@ -276,7 +272,7 @@ func TestEvaluateWithToolListAndSubTool(t *testing.T) {
276272

277273
func TestStreamEvaluate(t *testing.T) {
278274
var eventContent string
279-
tool := &ToolDef{Instructions: "What is the capital of the united states?"}
275+
tool := ToolDef{Instructions: "What is the capital of the united states?"}
280276

281277
run, err := g.Evaluate(context.Background(), Options{IncludeEvents: true}, tool)
282278
if err != nil {
@@ -536,7 +532,7 @@ echo hello there
536532
}
537533

538534
func TestToolChat(t *testing.T) {
539-
tool := &ToolDef{
535+
tool := ToolDef{
540536
Chat: true,
541537
Instructions: "You are a chat bot. Don't finish the conversation until I say 'bye'.",
542538
Tools: []string{"sys.chat.finish"},
@@ -688,8 +684,8 @@ func TestToolWithGlobalTools(t *testing.T) {
688684

689685
func TestConfirm(t *testing.T) {
690686
var eventContent string
691-
tools := []fmt.Stringer{
692-
&ToolDef{
687+
tools := []ToolDef{
688+
{
693689
Instructions: "List the files in the current directory",
694690
Tools: []string{"sys.exec"},
695691
},
@@ -759,8 +755,8 @@ func TestConfirm(t *testing.T) {
759755

760756
func TestConfirmDeny(t *testing.T) {
761757
var eventContent string
762-
tools := []fmt.Stringer{
763-
&ToolDef{
758+
tools := []ToolDef{
759+
{
764760
Instructions: "List the files in the current directory",
765761
Tools: []string{"sys.exec"},
766762
},
@@ -831,8 +827,8 @@ func TestConfirmDeny(t *testing.T) {
831827

832828
func TestPrompt(t *testing.T) {
833829
var eventContent string
834-
tools := []fmt.Stringer{
835-
&ToolDef{
830+
tools := []ToolDef{
831+
{
836832
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
837833
Tools: []string{"sys.prompt"},
838834
},
@@ -914,8 +910,8 @@ func TestPrompt(t *testing.T) {
914910
}
915911

916912
func TestPromptWithoutPromptAllowed(t *testing.T) {
917-
tools := []fmt.Stringer{
918-
&ToolDef{
913+
tools := []ToolDef{
914+
{
919915
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
920916
Tools: []string{"sys.prompt"},
921917
},

‎run.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
var errAbortRun = errors.New("run aborted")
1919

2020
type Run struct {
21-
url, requestPath, toolPath, content string
22-
opts Options
23-
state RunState
24-
chatState string
25-
cancel context.CancelCauseFunc
26-
err error
27-
wait func()
28-
basicCommand bool
21+
url, requestPath, toolPath string
22+
tools []ToolDef
23+
opts Options
24+
state RunState
25+
chatState string
26+
cancel context.CancelCauseFunc
27+
err error
28+
wait func()
29+
basicCommand bool
2930

3031
program *Program
3132
callsLock sync.RWMutex
@@ -163,7 +164,7 @@ func (r *Run) NextChat(ctx context.Context, input string) (*Run, error) {
163164
requestPath: r.requestPath,
164165
state: Creating,
165166
toolPath: r.toolPath,
166-
content: r.content,
167+
tools: r.tools,
167168
opts: r.opts,
168169
}
169170

@@ -175,11 +176,11 @@ func (r *Run) NextChat(ctx context.Context, input string) (*Run, error) {
175176
}
176177

177178
var payload any
178-
if r.content != "" {
179+
if len(r.tools) != 0 {
179180
payload = requestPayload{
180-
Content: run.content,
181-
Input: input,
182-
Options: run.opts,
181+
ToolDefs: r.tools,
182+
Input: input,
183+
Options: run.opts,
183184
}
184185
} else if run.toolPath != "" {
185186
payload = requestPayload{
@@ -412,8 +413,8 @@ const (
412413
)
413414

414415
type requestPayload struct {
415-
Content string `json:"content"`
416-
File string `json:"file"`
417-
Input string `json:"input"`
418-
Options `json:",inline"`
416+
Options `json:",inline"`
417+
File string `json:"file"`
418+
Input string `json:"input"`
419+
ToolDefs []ToolDef `json:"toolDefs,inline"`
419420
}

‎run_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ func TestRestartingErrorRun(t *testing.T) {
1111
if runtime.GOOS == "windows" {
1212
instructions = "#!/usr/bin/env powershell.exe\n\n$e = $env:EXIT_CODE;\nif ($e) { Exit 1; }"
1313
}
14-
tool := &ToolDef{
14+
tool := ToolDef{
1515
Context: []string{"my-context"},
1616
Instructions: "Say hello",
1717
}
18-
contextTool := &ToolDef{
18+
contextTool := ToolDef{
1919
Name: "my-context",
2020
Instructions: instructions,
2121
}

‎tool.go

+34-97
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,49 @@
11
package gptscript
22

33
import (
4-
"fmt"
5-
"strings"
6-
74
"github.com/getkin/kin-openapi/openapi3"
85
)
96

107
// ToolDef struct represents a tool with various configurations.
118
type ToolDef struct {
12-
Name string `json:"name,omitempty"`
13-
Description string `json:"description,omitempty"`
14-
MaxTokens int `json:"maxTokens,omitempty"`
15-
ModelName string `json:"modelName,omitempty"`
16-
ModelProvider bool `json:"modelProvider,omitempty"`
17-
JSONResponse bool `json:"jsonResponse,omitempty"`
18-
Chat bool `json:"chat,omitempty"`
19-
Temperature *float32 `json:"temperature,omitempty"`
20-
Cache *bool `json:"cache,omitempty"`
21-
InternalPrompt *bool `json:"internalPrompt"`
22-
Args map[string]string `json:"args,omitempty"`
23-
Tools []string `json:"tools,omitempty"`
24-
GlobalTools []string `json:"globalTools,omitempty"`
25-
GlobalModelName string `json:"globalModelName,omitempty"`
26-
Context []string `json:"context,omitempty"`
27-
ExportContext []string `json:"exportContext,omitempty"`
28-
Export []string `json:"export,omitempty"`
29-
Credentials []string `json:"credentials,omitempty"`
30-
Instructions string `json:"instructions,omitempty"`
9+
Name string `json:"name,omitempty"`
10+
Description string `json:"description,omitempty"`
11+
MaxTokens int `json:"maxTokens,omitempty"`
12+
ModelName string `json:"modelName,omitempty"`
13+
ModelProvider bool `json:"modelProvider,omitempty"`
14+
JSONResponse bool `json:"jsonResponse,omitempty"`
15+
Chat bool `json:"chat,omitempty"`
16+
Temperature *float32 `json:"temperature,omitempty"`
17+
Cache *bool `json:"cache,omitempty"`
18+
InternalPrompt *bool `json:"internalPrompt"`
19+
Arguments *openapi3.Schema `json:"arguments,omitempty"`
20+
Tools []string `json:"tools,omitempty"`
21+
GlobalTools []string `json:"globalTools,omitempty"`
22+
GlobalModelName string `json:"globalModelName,omitempty"`
23+
Context []string `json:"context,omitempty"`
24+
ExportContext []string `json:"exportContext,omitempty"`
25+
Export []string `json:"export,omitempty"`
26+
Agents []string `json:"agents,omitempty"`
27+
Credentials []string `json:"credentials,omitempty"`
28+
Instructions string `json:"instructions,omitempty"`
3129
}
3230

33-
// String method returns the string representation of ToolDef.
34-
func (t *ToolDef) String() string {
35-
var sb strings.Builder
36-
37-
if t.Name != "" {
38-
sb.WriteString(fmt.Sprintf("Name: %s\n", t.Name))
39-
}
40-
if t.Description != "" {
41-
sb.WriteString(fmt.Sprintf("Description: %s\n", t.Description))
42-
}
43-
if len(t.Tools) > 0 {
44-
sb.WriteString(fmt.Sprintf("Tools: %s\n", strings.Join(t.Tools, ", ")))
45-
}
46-
if t.MaxTokens != 0 {
47-
sb.WriteString(fmt.Sprintf("Max tokens: %d\n", t.MaxTokens))
48-
}
49-
if t.ModelName != "" {
50-
sb.WriteString(fmt.Sprintf("Model: %s\n", t.ModelName))
51-
}
52-
if t.Cache != nil && !*t.Cache {
53-
sb.WriteString("Cache: false\n")
54-
}
55-
if len(t.Context) > 0 {
56-
sb.WriteString(fmt.Sprintf("Context: %s\n", strings.Join(t.Context, ", ")))
57-
}
58-
if len(t.ExportContext) > 0 {
59-
sb.WriteString(fmt.Sprintf("Export Context: %s\n", strings.Join(t.ExportContext, ", ")))
60-
}
61-
if len(t.Export) > 0 {
62-
sb.WriteString(fmt.Sprintf("Export: %s\n", strings.Join(t.Export, ", ")))
63-
}
64-
if len(t.GlobalTools) > 0 {
65-
sb.WriteString(fmt.Sprintf("Global Tools: %s\n", strings.Join(t.GlobalTools, ", ")))
66-
}
67-
if t.Temperature != nil {
68-
sb.WriteString(fmt.Sprintf("Temperature: %f\n", *t.Temperature))
69-
}
70-
if t.JSONResponse {
71-
sb.WriteString("JSON Response: true\n")
72-
}
73-
if len(t.Args) > 0 {
74-
for arg, desc := range t.Args {
75-
sb.WriteString(fmt.Sprintf("Args: %s: %s\n", arg, desc))
31+
func ObjectSchema(kv ...string) *openapi3.Schema {
32+
s := &openapi3.Schema{
33+
Type: "object",
34+
Properties: openapi3.Schemas{},
35+
}
36+
for i, v := range kv {
37+
if i%2 == 1 {
38+
s.Properties[kv[i-1]] = &openapi3.SchemaRef{
39+
Value: &openapi3.Schema{
40+
Description: v,
41+
Type: "string",
42+
},
43+
}
7644
}
7745
}
78-
if t.Chat {
79-
sb.WriteString("Chat: true\n")
80-
}
81-
if t.InternalPrompt != nil && *t.InternalPrompt {
82-
sb.WriteString("Internal prompt: true\n")
83-
}
84-
if t.Instructions != "" {
85-
sb.WriteString(t.Instructions)
86-
}
87-
88-
return sb.String()
89-
}
90-
91-
type ToolDefs []ToolDef
92-
93-
func (t ToolDefs) String() string {
94-
resp := make([]string, 0, len(t))
95-
for _, tool := range t {
96-
resp = append(resp, tool.String())
97-
}
98-
return strings.Join(resp, "\n---\n")
46+
return s
9947
}
10048

10149
type Document struct {
@@ -145,14 +93,3 @@ type Repo struct {
14593
Name string
14694
Revision string
14795
}
148-
149-
func concatTools(tools []fmt.Stringer) string {
150-
var sb strings.Builder
151-
for i, tool := range tools {
152-
sb.WriteString(tool.String())
153-
if i < len(tools)-1 {
154-
sb.WriteString("\n---\n")
155-
}
156-
}
157-
return sb.String()
158-
}

0 commit comments

Comments
 (0)
Please sign in to comment.