add browse action

This commit is contained in:
Ettore Di Giacinto
2024-04-16 00:20:05 +02:00
parent 609b2a2b7c
commit ca57f4cd85
3 changed files with 73 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ const (
ActionGithubIssueSearcher = "github-issue-searcher" ActionGithubIssueSearcher = "github-issue-searcher"
ActionScraper = "scraper" ActionScraper = "scraper"
ActionWikipedia = "wikipedia" ActionWikipedia = "wikipedia"
ActionBrowse = "browse"
) )
var AvailableActions = []string{ var AvailableActions = []string{
@@ -27,6 +28,7 @@ var AvailableActions = []string{
ActionGithubIssueCloser, ActionGithubIssueCloser,
ActionGithubIssueSearcher, ActionGithubIssueSearcher,
ActionScraper, ActionScraper,
ActionBrowse,
ActionWikipedia, ActionWikipedia,
} }
@@ -58,6 +60,8 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions = append(actions, external.NewScraper(config)) actions = append(actions, external.NewScraper(config))
case ActionWikipedia: case ActionWikipedia:
actions = append(actions, external.NewWikipedia(config)) actions = append(actions, external.NewWikipedia(config))
case ActionBrowse:
actions = append(actions, external.NewBrowse(config))
} }
} }

68
external/browse.go vendored Normal file
View File

@@ -0,0 +1,68 @@
package external
import (
"context"
"fmt"
"io"
"net/http"
"github.com/mudler/local-agent-framework/action"
"github.com/sashabaranov/go-openai/jsonschema"
"jaytaylor.com/html2text"
)
func NewBrowse(config map[string]string) *BrowseAction {
return &BrowseAction{}
}
type BrowseAction struct{}
func (a *BrowseAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
result := struct {
URL string `json:"url"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
// download page with http.Client
client := &http.Client{}
req, err := http.NewRequest("GET", result.URL, nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
pagebyte, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
rendered, err := html2text.FromString(string(pagebyte), html2text.Options{PrettyTables: true})
if err != nil {
return "", err
}
return fmt.Sprintf("The webpage '%s' content is:\n%s", result.URL, rendered), nil
}
func (a *BrowseAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "browse",
Description: "Use this tool to visit an URL. It browse a website page and return the text content.",
Properties: map[string]jsonschema.Definition{
"url": {
Type: jsonschema.String,
Description: "The website URL.",
},
},
Required: []string{"url"},
}
}

View File

@@ -38,7 +38,7 @@ func (a *ScraperAction) Run(ctx context.Context, params action.ActionParams) (st
func (a *ScraperAction) Definition() action.ActionDefinition { func (a *ScraperAction) Definition() action.ActionDefinition {
return action.ActionDefinition{ return action.ActionDefinition{
Name: "scrape", Name: "scrape",
Description: "Scrapes a website and returns the site data.", Description: "Scrapes a full website content and returns the entire site data.",
Properties: map[string]jsonschema.Definition{ Properties: map[string]jsonschema.Definition{
"url": { "url": {
Type: jsonschema.String, Type: jsonschema.String,