Skip to content

Commit 9e607e1

Browse files
committed
Add jq/get intents
1 parent 1d041ab commit 9e607e1

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/PuerkitoBio/goquery v1.8.1
77
github.com/go-skynet/llama-cli v0.0.0-20230325001050-abee34f60a2a
88
github.com/gocolly/colly/v2 v2.1.0
9+
github.com/itchyny/gojq v0.12.12
910
github.com/onsi/ginkgo/v2 v2.9.2
1011
github.com/onsi/gomega v1.27.5
1112
github.com/rs/zerolog v1.29.0
@@ -23,6 +24,7 @@ require (
2324
github.com/golang/protobuf v1.5.3 // indirect
2425
github.com/google/go-cmp v0.5.9 // indirect
2526
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
27+
github.com/itchyny/timefmt-go v0.1.5 // indirect
2628
github.com/kennygrant/sanitize v1.2.4 // indirect
2729
github.com/mattn/go-colorable v0.1.13 // indirect
2830
github.com/mattn/go-isatty v0.0.17 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
6464
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
6565
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
6666
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
67+
github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA=
68+
github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE=
69+
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
70+
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
6771
github.com/jawher/mow.cli v1.1.0/go.mod h1:aNaQlc7ozF3vw6IJ2dHjp2ZFiA4ozMIYY6PyuRJwlUg=
6872
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
6973
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=

intents/get.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package intents
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"strings"
7+
8+
intent "github.com/go-skynet/intents/core/intent"
9+
client "github.com/go-skynet/llama-cli/client"
10+
)
11+
12+
func NewWebGet(s string) *ScrapeWeb {
13+
return &ScrapeWeb{input: StringIntent(s)}
14+
}
15+
16+
func Get() *ScrapeWeb {
17+
return &ScrapeWeb{}
18+
}
19+
20+
type WebGet struct {
21+
input intent.IntentInput
22+
}
23+
24+
func getURL(url string) (string, error) {
25+
// Make sure the URL starts with http:// or https://
26+
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
27+
url = "http://" + url
28+
}
29+
30+
// Make a GET request to the URL
31+
response, err := http.Get(url)
32+
if err != nil {
33+
return "", err
34+
}
35+
defer response.Body.Close()
36+
37+
// Read the response body
38+
body, err := ioutil.ReadAll(response.Body)
39+
if err != nil {
40+
return "", err
41+
}
42+
43+
return string(body), nil
44+
}
45+
46+
func (s *WebGet) SetInput(ii intent.IntentInput) intent.IntentInput {
47+
s.input = ii
48+
return s
49+
}
50+
51+
func (i *WebGet) Execute(c *client.Client, opts ...client.InputOption) (string, error) {
52+
var inputresult string
53+
var err error
54+
if i.input != nil {
55+
inputresult, err = i.input.Execute(c, opts...)
56+
if err != nil {
57+
return "", err
58+
}
59+
}
60+
61+
return getURL(inputresult)
62+
}

intents/get_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package intents_test
2+
3+
import (
4+
intents "github.com/go-skynet/intents/intents"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("Get intent", func() {
11+
Context("retrieve pages", func() {
12+
It("should return a web page summary", func() {
13+
str, err := intents.NewWebGet("https://mysafeinfo.com/api/data?list=englishmonarchs&format=json").Execute(nil)
14+
Expect(err).ToNot(HaveOccurred())
15+
Expect(str).To(ContainSubstring("Edward the Elder"))
16+
})
17+
})
18+
})

intents/jq.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package intents
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
8+
intent "github.com/go-skynet/intents/core/intent"
9+
client "github.com/go-skynet/llama-cli/client"
10+
"github.com/itchyny/gojq"
11+
)
12+
13+
func jq(q string, a any) (string, error) {
14+
query, err := gojq.Parse(q)
15+
if err != nil {
16+
return "", err
17+
}
18+
iter := query.Run(a) // or query.RunWithContext
19+
res := ""
20+
for {
21+
v, ok := iter.Next()
22+
if !ok {
23+
break
24+
}
25+
if err, ok := v.(error); ok {
26+
return "", err
27+
}
28+
res += fmt.Sprint(v)
29+
}
30+
31+
return res, nil
32+
}
33+
34+
type JQ struct {
35+
input intent.IntentInput
36+
segment string
37+
}
38+
39+
func NewJQ(segment string) *JQ {
40+
return &JQ{segment: segment}
41+
}
42+
43+
func (s *JQ) SetInput(ii intent.IntentInput) intent.IntentInput {
44+
s.input = ii
45+
return s
46+
}
47+
48+
func (i *JQ) Execute(c *client.Client, opts ...client.InputOption) (string, error) {
49+
input := []any{}
50+
51+
var inputresult string
52+
var err error
53+
if i.input != nil {
54+
inputresult, err = i.input.Execute(c, opts...)
55+
if err != nil {
56+
return "", err
57+
}
58+
}
59+
log.Print("Unmarshallling", inputresult)
60+
if err := json.Unmarshal([]byte(inputresult), &input); err != nil {
61+
input := map[string]any{}
62+
if err := json.Unmarshal([]byte(inputresult), &input); err != nil {
63+
return "", err
64+
}
65+
}
66+
67+
return jq(i.segment, input)
68+
}

intents/jq_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package intents_test
2+
3+
import (
4+
"os"
5+
6+
. "github.com/go-skynet/intents/core/chain"
7+
8+
intents "github.com/go-skynet/intents/intents"
9+
client "github.com/go-skynet/llama-cli/client"
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
"github.com/rs/zerolog"
13+
"github.com/rs/zerolog/log"
14+
)
15+
16+
var _ = Describe("JQ intent", func() {
17+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
18+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
19+
Context("JQ", func() {
20+
It("Can filter json", func() {
21+
chain := &Chain{}
22+
chain.Add(intents.NewWebGet("https://mysafeinfo.com/api/data?list=englishmonarchs&format=json"))
23+
chain.Add(intents.NewJQ(".[0].Name"))
24+
a, err := chain.Execute(nil, client.WithTokens(99999))
25+
Expect(err).ToNot(HaveOccurred())
26+
Expect(a).To(Equal("Edward the Elder"))
27+
})
28+
})
29+
})

0 commit comments

Comments
 (0)