Skip to content

feat: add metadata and type fields to tools #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
57 changes: 57 additions & 0 deletions gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,42 @@ func TestParseSimpleFile(t *testing.T) {
}
}

func TestParseFileWithMetadata(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Error getting working directory: %v", err)
}

tools, err := g.Parse(context.Background(), wd+"/test/parse-with-metadata.gpt")
if err != nil {
t.Errorf("Error parsing file: %v", err)
}

if len(tools) != 2 {
t.Fatalf("Unexpected number of tools: %d", len(tools))
}

if tools[0].ToolNode == nil {
t.Fatalf("No tool node found")
}

if !strings.Contains(tools[0].ToolNode.Tool.Instructions, "requests.get(") {
t.Errorf("Unexpected instructions: %s", tools[0].ToolNode.Tool.Instructions)
}

if tools[0].ToolNode.Tool.MetaData["requirements.txt"] != "requests" {
t.Errorf("Unexpected metadata: %s", tools[0].ToolNode.Tool.MetaData["requirements.txt"])
}

if tools[1].TextNode == nil {
t.Fatalf("No text node found")
}

if tools[1].TextNode.Fmt != "metadata:foo:requirements.txt" {
t.Errorf("Unexpected text: %s", tools[1].TextNode.Fmt)
}
}

func TestParseTool(t *testing.T) {
tools, err := g.ParseTool(context.Background(), "echo hello")
if err != nil {
Expand Down Expand Up @@ -1069,3 +1105,24 @@ func TestGetEnv(t *testing.T) {
})
}
}

func TestRunPythonWithMetadata(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Error getting working directory: %v", err)
}

run, err := g.Run(context.Background(), wd+"/test/parse-with-metadata.gpt", Options{IncludeEvents: true})
if err != nil {
t.Fatalf("Error executing file: %v", err)
}

out, err := run.Text()
if err != nil {
t.Fatalf("Error reading output: %v", err)
}

if out != "200" {
t.Errorf("Unexpected output: %s", out)
}
}
12 changes: 12 additions & 0 deletions test/parse-with-metadata.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Name: foo

#!/usr/bin/env python3
import requests


resp = requests.get("https://google.com")
print(resp.status_code, end="")

---
!metadata:foo:requirements.txt
requests
2 changes: 2 additions & 0 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ToolDef struct {
Agents []string `json:"agents,omitempty"`
Credentials []string `json:"credentials,omitempty"`
Instructions string `json:"instructions,omitempty"`
Type string `json:"type,omitempty"`
}

func ObjectSchema(kv ...string) *openapi3.Schema {
Expand Down Expand Up @@ -86,6 +87,7 @@ type Tool struct {
ID string `json:"id,omitempty"`
Arguments *openapi3.Schema `json:"arguments,omitempty"`
ToolMapping map[string][]ToolReference `json:"toolMapping,omitempty"`
MetaData map[string]string `json:"metadata,omitempty"`
LocalTools map[string]string `json:"localTools,omitempty"`
Source ToolSource `json:"source,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Expand Down