Skip to content

Commit

Permalink
add searxng
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianliechti committed Aug 1, 2024
1 parent ea6043f commit bf3f3a0
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/config_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/adrianliechti/llama/pkg/tool/bing"
"github.com/adrianliechti/llama/pkg/tool/custom"
"github.com/adrianliechti/llama/pkg/tool/duckduckgo"
"github.com/adrianliechti/llama/pkg/tool/searxng"
"github.com/adrianliechti/llama/pkg/tool/tavily"

"github.com/adrianliechti/llama/pkg/otel"
Expand Down Expand Up @@ -71,6 +72,9 @@ func createTool(cfg toolConfig) (tool.Tool, error) {
case "tavily":
return tavilyTool(cfg)

case "searxng":
return searxngTool(cfg)

case "custom":
return customTool(cfg)

Expand All @@ -91,6 +95,12 @@ func duckduckgoTool(cfg toolConfig) (tool.Tool, error) {
return duckduckgo.New(options...)
}

func searxngTool(cfg toolConfig) (tool.Tool, error) {
var options []searxng.Option

return searxng.New(cfg.URL, options...)
}

func tavilyTool(cfg toolConfig) (tool.Tool, error) {
var options []tavily.Option

Expand Down
105 changes: 105 additions & 0 deletions pkg/tool/searxng/client.go
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
}
13 changes: 13 additions & 0 deletions pkg/tool/searxng/config.go
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
}
}
22 changes: 22 additions & 0 deletions pkg/tool/searxng/models.go
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"`
}

0 comments on commit bf3f3a0

Please sign in to comment.