Skip to content

Commit c90355f

Browse files
Add sys.http.html2text
1 parent c596301 commit c90355f

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

examples/search.gpt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tools: search
2+
3+
Search the web for vacation spots in Florida, for each
4+
vacation spot give the name and description.
5+
6+
---
7+
name: search
8+
description: Searches the internet for content
9+
args: query: The query to search for
10+
tools: sys.http.text?
11+
12+
First download the content of "https://html.duckduckgo.com/html/?q=${query}".
13+
Look for the first 5 search results. Download each search result and look for content
14+
that would best answer the query ${query}.
15+
16+
With all that information try your best to provide an answer or useful context to ${query}.

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/adrg/xdg v0.4.0
88
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
99
github.com/hexops/autogold/v2 v2.1.0
10+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
1011
github.com/sashabaranov/go-openai v1.18.3
1112
github.com/sirupsen/logrus v1.9.3
1213
github.com/spf13/cobra v1.8.0
@@ -34,16 +35,19 @@ require (
3435
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3536
github.com/mattn/go-colorable v0.1.13 // indirect
3637
github.com/mattn/go-isatty v0.0.17 // indirect
38+
github.com/mattn/go-runewidth v0.0.9 // indirect
3739
github.com/nightlyone/lockfile v1.0.0 // indirect
40+
github.com/olekukonko/tablewriter v0.0.5 // indirect
3841
github.com/pmezard/go-difflib v1.0.0 // indirect
3942
github.com/rogpeppe/go-internal v1.11.0 // indirect
4043
github.com/samber/lo v1.38.1 // indirect
4144
github.com/samber/slog-logrus v1.0.0 // indirect
4245
github.com/spf13/pflag v1.0.5 // indirect
46+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
4347
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
4448
golang.org/x/mod v0.14.0 // indirect
49+
golang.org/x/net v0.19.0 // indirect
4550
golang.org/x/sys v0.16.0 // indirect
46-
golang.org/x/text v0.14.0 // indirect
4751
golang.org/x/tools v0.16.0 // indirect
4852
gopkg.in/yaml.v3 v3.0.1 // indirect
4953
k8s.io/klog/v2 v2.110.1 // indirect

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/hexops/valast v1.4.3 h1:oBoGERMJh6UZdRc6cduE1CTPK+VAdXA59Y1HFgu3sm0=
4444
github.com/hexops/valast v1.4.3/go.mod h1:Iqx2kLj3Jn47wuXpj3wX40xn6F93QNFBHuiKBerkTGA=
4545
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
4646
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
47+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
48+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
4749
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
4850
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
4951
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -60,8 +62,12 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
6062
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6163
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
6264
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
65+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
66+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
6367
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
6468
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
69+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
70+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
6571
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
6672
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
6773
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
@@ -86,6 +92,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
8692
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
8793
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8894
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
95+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
96+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
8997
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9098
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9199
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=

pkg/builtin/builtin.go

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/acorn-io/gptscript/pkg/types"
17+
"github.com/jaytaylor/html2text"
1718
)
1819

1920
var Tools = map[string]types.Tool{
@@ -36,6 +37,12 @@ var Tools = map[string]types.Tool{
3637
"url", "The URL to download"),
3738
BuiltinFunc: SysHTTPGet,
3839
},
40+
"sys.http.html2text": {
41+
Description: "Download the contents of a http or https URL returning the content as rendered text converted from HTML",
42+
Arguments: types.ObjectSchema(
43+
"url", "The URL to download"),
44+
BuiltinFunc: SysHTTPHtml2Text,
45+
},
3946
"sys.abort": {
4047
Description: "Aborts execution",
4148
Arguments: types.ObjectSchema(
@@ -91,10 +98,21 @@ func ListTools() (result []types.Tool) {
9198
}
9299

93100
func Builtin(name string) (types.Tool, bool) {
101+
name, dontFail := strings.CutSuffix(name, "?")
94102
t, ok := Tools[name]
95103
t.Name = name
96104
t.ID = name
97105
t.Instructions = "#!" + name
106+
if ok && dontFail {
107+
orig := t.BuiltinFunc
108+
t.BuiltinFunc = func(ctx context.Context, env []string, input string) (string, error) {
109+
s, err := orig(ctx, env, input)
110+
if err != nil {
111+
return fmt.Sprintf("ERROR: %s", err.Error()), nil
112+
}
113+
return s, err
114+
}
115+
}
98116
return t, ok
99117
}
100118

@@ -218,6 +236,16 @@ func SysHTTPGet(ctx context.Context, env []string, input string) (string, error)
218236
return string(data), nil
219237
}
220238

239+
func SysHTTPHtml2Text(ctx context.Context, env []string, input string) (string, error) {
240+
content, err := SysHTTPGet(ctx, env, input)
241+
if err != nil {
242+
return "", err
243+
}
244+
return html2text.FromString(content, html2text.Options{
245+
PrettyTables: true,
246+
})
247+
}
248+
221249
func SysHTTPPost(ctx context.Context, env []string, input string) (string, error) {
222250
var params struct {
223251
URL string `json:"url,omitempty"`

pkg/types/tool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (t Tool) String() string {
6969
sort.Strings(keys)
7070
for _, key := range keys {
7171
prop := t.Arguments.Properties[key]
72-
_, _ = fmt.Fprintf(buf, "Arg: %s: %s\n", key, prop.Description)
72+
_, _ = fmt.Fprintf(buf, "Args: %s: %s\n", key, prop.Description)
7373
}
7474
}
7575
if t.Instructions != "" && t.BuiltinFunc == nil {

0 commit comments

Comments
 (0)