Add search internet
This commit is contained in:
@@ -1,39 +1,55 @@
|
||||
package action2
|
||||
package action
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mudler/local-agent-framework/action"
|
||||
"github.com/sap-nocops/duckduckgogo/client"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
// NewIntention creates a new intention action
|
||||
// The inention action is special as it tries to identify
|
||||
// a tool to use and a reasoning over to use it
|
||||
func NewSearch(s ...string) *SearchAction {
|
||||
return &SearchAction{tools: s}
|
||||
func NewSearch() *SearchAction {
|
||||
return &SearchAction{}
|
||||
}
|
||||
|
||||
type SearchAction struct {
|
||||
tools []string
|
||||
}
|
||||
type SearchAction struct{}
|
||||
|
||||
func (a *SearchAction) Run(action.ActionParams) (string, error) {
|
||||
return "no-op", nil
|
||||
func (a *SearchAction) Run(params action.ActionParams) (string, error) {
|
||||
result := struct {
|
||||
Query string `json:"query"`
|
||||
}{}
|
||||
err := params.Unmarshal(&result)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
|
||||
return "", err
|
||||
}
|
||||
ddg := client.NewDuckDuckGoSearchClient()
|
||||
res, err := ddg.SearchLimited(result.Query, 10)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("error: %v", err)
|
||||
fmt.Printf(msg)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
results := ""
|
||||
for i, r := range res {
|
||||
results += fmt.Sprintf("*********** RESULT %d\nurl: %s\ntitle: %s\nsnippet: %s\n", i, r.FormattedUrl, r.Title, r.Snippet)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (a *SearchAction) Definition() action.ActionDefinition {
|
||||
return action.ActionDefinition{
|
||||
Name: "intent",
|
||||
Description: "detect user intent",
|
||||
Name: "search_internet",
|
||||
Description: "Search the internet for something.",
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"reasoning": {
|
||||
"query": {
|
||||
Type: jsonschema.String,
|
||||
Description: "A detailed reasoning on why you want to call this tool.",
|
||||
},
|
||||
"tool": {
|
||||
Type: jsonschema.String,
|
||||
Enum: a.tools,
|
||||
Description: "The query to search for.",
|
||||
},
|
||||
},
|
||||
Required: []string{"tool", "reasoning"},
|
||||
Required: []string{"query"},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user