Skip to content

Commit

Permalink
Add jq/get intents
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Mar 29, 2023
1 parent 1d041ab commit 9e607e1
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/go-skynet/llama-cli v0.0.0-20230325001050-abee34f60a2a
github.com/gocolly/colly/v2 v2.1.0
github.com/itchyny/gojq v0.12.12
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.5
github.com/rs/zerolog v1.29.0
Expand All @@ -23,6 +24,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA=
github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/jawher/mow.cli v1.1.0/go.mod h1:aNaQlc7ozF3vw6IJ2dHjp2ZFiA4ozMIYY6PyuRJwlUg=
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
Expand Down
62 changes: 62 additions & 0 deletions intents/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package intents

import (
"io/ioutil"
"net/http"
"strings"

intent "github.com/go-skynet/intents/core/intent"
client "github.com/go-skynet/llama-cli/client"
)

func NewWebGet(s string) *ScrapeWeb {
return &ScrapeWeb{input: StringIntent(s)}
}

func Get() *ScrapeWeb {
return &ScrapeWeb{}
}

type WebGet struct {
input intent.IntentInput
}

func getURL(url string) (string, error) {
// Make sure the URL starts with http:// or https://
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "http://" + url
}

// Make a GET request to the URL
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()

// Read the response body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}

return string(body), nil
}

func (s *WebGet) SetInput(ii intent.IntentInput) intent.IntentInput {
s.input = ii
return s
}

func (i *WebGet) Execute(c *client.Client, opts ...client.InputOption) (string, error) {
var inputresult string
var err error
if i.input != nil {
inputresult, err = i.input.Execute(c, opts...)
if err != nil {
return "", err
}
}

return getURL(inputresult)
}
18 changes: 18 additions & 0 deletions intents/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package intents_test

import (
intents "github.com/go-skynet/intents/intents"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Get intent", func() {
Context("retrieve pages", func() {
It("should return a web page summary", func() {
str, err := intents.NewWebGet("https://mysafeinfo.com/api/data?list=englishmonarchs&format=json").Execute(nil)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(ContainSubstring("Edward the Elder"))
})
})
})
68 changes: 68 additions & 0 deletions intents/jq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package intents

import (
"encoding/json"
"fmt"
"log"

intent "github.com/go-skynet/intents/core/intent"
client "github.com/go-skynet/llama-cli/client"
"github.com/itchyny/gojq"
)

func jq(q string, a any) (string, error) {
query, err := gojq.Parse(q)
if err != nil {
return "", err
}
iter := query.Run(a) // or query.RunWithContext
res := ""
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return "", err
}
res += fmt.Sprint(v)
}

return res, nil
}

type JQ struct {
input intent.IntentInput
segment string
}

func NewJQ(segment string) *JQ {
return &JQ{segment: segment}
}

func (s *JQ) SetInput(ii intent.IntentInput) intent.IntentInput {
s.input = ii
return s
}

func (i *JQ) Execute(c *client.Client, opts ...client.InputOption) (string, error) {
input := []any{}

var inputresult string
var err error
if i.input != nil {
inputresult, err = i.input.Execute(c, opts...)
if err != nil {
return "", err
}
}
log.Print("Unmarshallling", inputresult)
if err := json.Unmarshal([]byte(inputresult), &input); err != nil {
input := map[string]any{}
if err := json.Unmarshal([]byte(inputresult), &input); err != nil {
return "", err
}
}

return jq(i.segment, input)
}
29 changes: 29 additions & 0 deletions intents/jq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package intents_test

import (
"os"

. "github.com/go-skynet/intents/core/chain"

intents "github.com/go-skynet/intents/intents"
client "github.com/go-skynet/llama-cli/client"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

var _ = Describe("JQ intent", func() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.DebugLevel)
Context("JQ", func() {
It("Can filter json", func() {
chain := &Chain{}
chain.Add(intents.NewWebGet("https://mysafeinfo.com/api/data?list=englishmonarchs&format=json"))
chain.Add(intents.NewJQ(".[0].Name"))
a, err := chain.Execute(nil, client.WithTokens(99999))
Expect(err).ToNot(HaveOccurred())
Expect(a).To(Equal("Edward the Elder"))
})
})
})

0 comments on commit 9e607e1

Please sign in to comment.