-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) |