Add search internet

This commit is contained in:
Ettore Di Giacinto
2024-04-05 23:56:03 +02:00
parent 5f29125bbb
commit f2c74e29e8
6 changed files with 59 additions and 22 deletions

View File

@@ -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"},
}
}

View File

@@ -12,6 +12,8 @@ import (
"github.com/donseba/go-htmx"
"github.com/donseba/go-htmx/sse"
actions "github.com/mudler/local-agent-framework/example/webui/actions"
. "github.com/mudler/local-agent-framework/agent"
)
@@ -63,6 +65,7 @@ func main() {
)
return true
}),
WithActions(actions.NewSearch()),
WithAgentResultCallback(func(state ActionState) {
text := fmt.Sprintf(`Reasoning: %s
Action taken: %+v
@@ -123,7 +126,7 @@ func main() {
// Server Sent Events
mux.Handle("GET /sse", http.HandlerFunc(app.SSE))
fmt.Print("Server started at :3210")
fmt.Print("Server started at http://localhost:3210")
err = http.ListenAndServe(":3210", mux)
log.Fatal(err)
}