-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
ea6043f
commit bf3f3a0
Showing
4 changed files
with
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package searxng | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/adrianliechti/llama/pkg/jsonschema" | ||
"github.com/adrianliechti/llama/pkg/tool" | ||
) | ||
|
||
var _ tool.Tool = &Tool{} | ||
|
||
type Tool struct { | ||
url string | ||
|
||
client *http.Client | ||
} | ||
|
||
func New(url string, options ...Option) (*Tool, error) { | ||
t := &Tool{ | ||
url: url, | ||
|
||
client: http.DefaultClient, | ||
} | ||
|
||
for _, option := range options { | ||
option(t) | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (t *Tool) Name() string { | ||
return "searxng" | ||
} | ||
|
||
func (t *Tool) Description() string { | ||
return "Search online if the requested information cannot be found in the language model or the information could be present in a time after the language model was trained." | ||
} | ||
|
||
func (*Tool) Parameters() jsonschema.Definition { | ||
return jsonschema.Definition{ | ||
Type: jsonschema.DataTypeObject, | ||
|
||
Properties: map[string]jsonschema.Definition{ | ||
"query": { | ||
Type: jsonschema.DataTypeString, | ||
Description: "the text to search online to get the necessary information", | ||
}, | ||
}, | ||
|
||
Required: []string{"query"}, | ||
} | ||
} | ||
|
||
func (t *Tool) Execute(ctx context.Context, parameters map[string]any) (any, error) { | ||
query, ok := parameters["query"].(string) | ||
|
||
if !ok { | ||
return nil, errors.New("missing query parameter") | ||
} | ||
|
||
url, _ := url.Parse(t.url) | ||
url = url.JoinPath("/search") | ||
|
||
values := url.Query() | ||
values.Set("q", query) | ||
values.Set("format", "json") | ||
values.Set("safesearch", "0") | ||
|
||
url.RawQuery = values.Encode() | ||
|
||
req, _ := http.NewRequestWithContext(ctx, "GET", url.String(), nil) | ||
|
||
resp, err := t.client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
var data SearchResponse | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { | ||
return nil, err | ||
} | ||
|
||
var results []Result | ||
|
||
for _, r := range data.Results { | ||
result := Result{ | ||
Title: r.Title, | ||
Content: r.Content, | ||
Location: r.URL, | ||
} | ||
|
||
results = append(results, result) | ||
} | ||
|
||
return results, nil | ||
} |
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,13 @@ | ||
package searxng | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type Option func(*Tool) | ||
|
||
func WithClient(client *http.Client) Option { | ||
return func(t *Tool) { | ||
t.client = client | ||
} | ||
} |
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,22 @@ | ||
package searxng | ||
|
||
type Result struct { | ||
Title string | ||
Content string | ||
Location string | ||
} | ||
|
||
type SearchResult struct { | ||
URL string `json:"url"` | ||
|
||
Engine string `json:"engine"` | ||
|
||
Title string `json:"title"` | ||
Content string `json:"content"` | ||
|
||
Score int `json:"score"` | ||
} | ||
|
||
type SearchResponse struct { | ||
Results []SearchResult `json:"results"` | ||
} |