1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-09 01:09:54 +00:00

better search tools

This commit is contained in:
Laura
2025-08-14 17:08:45 +02:00
parent 5d44980510
commit 0b51ee9dad
17 changed files with 335 additions and 102 deletions

103
search.go
View File

@@ -10,29 +10,56 @@ import (
"github.com/revrost/go-openrouter"
)
type SearchArguments struct {
Query string `json:"query"`
type SearchWebArguments struct {
Query string `json:"query"`
NumResults int `json:"num_results"`
}
var (
//go:embed prompts/search.txt
PromptSearch string
)
type FetchContentsArguments struct {
URLs []string `json:"urls"`
}
func GetSearchTool() []openrouter.Tool {
func GetSearchTools() []openrouter.Tool {
return []openrouter.Tool{
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "search_internet",
Description: "Search the internet for current information.",
Name: "search_web",
Description: "Search the web via Exa in auto mode. Returns up to 10 results with short summaries.",
Parameters: map[string]any{
"type": "object",
"required": []string{"query"},
"required": []string{"query", "num_results"},
"properties": map[string]any{
"query": map[string]string{
"query": map[string]any{
"type": "string",
"description": "A concise and specific query string.",
"description": "A concise, specific search query in natural language.",
},
"num_results": map[string]any{
"type": "integer",
"description": "Number of results to return (1-10). Default 10.",
"minimum": 1,
"maximum": 10,
},
},
"additionalProperties": false,
},
Strict: true,
},
},
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "fetch_contents",
Description: "Fetch page contents for one or more URLs via Exa /contents.",
Parameters: map[string]any{
"type": "object",
"required": []string{"urls"},
"properties": map[string]any{
"urls": map[string]any{
"type": "array",
"description": "List of URLs (1..N) to fetch.",
"items": map[string]any{"type": "string"},
"minItems": 1,
},
},
"additionalProperties": false,
@@ -43,8 +70,8 @@ func GetSearchTool() []openrouter.Tool {
}
}
func HandleSearchTool(ctx context.Context, tool *ToolCall) error {
var arguments SearchArguments
func HandleSearchWebTool(ctx context.Context, tool *ToolCall) error {
var arguments SearchWebArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
if err != nil {
@@ -55,30 +82,50 @@ func HandleSearchTool(ctx context.Context, tool *ToolCall) error {
return errors.New("no search query")
}
request := openrouter.ChatCompletionRequest{
Model: "perplexity/sonar",
Messages: []openrouter.ChatCompletionMessage{
openrouter.SystemMessage(PromptSearch),
openrouter.UserMessage(arguments.Query),
},
Temperature: 0.25,
MaxTokens: 2048,
}
response, err := OpenRouterRun(ctx, request)
results, err := ExaRunSearch(ctx, arguments)
if err != nil {
tool.Result = fmt.Sprintf("error: %v", err)
return nil
}
if len(response.Choices) == 0 {
tool.Result = "error: failed to perform search"
if len(results.Results) == 0 {
tool.Result = "error: no search results"
return nil
}
tool.Result = response.Choices[0].Message.Content.Text
tool.Result = results.String()
return nil
}
func HandleFetchContentsTool(ctx context.Context, tool *ToolCall) error {
var arguments FetchContentsArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
if err != nil {
return err
}
if len(arguments.URLs) == 0 {
return errors.New("no urls")
}
results, err := ExaRunContents(ctx, arguments)
if err != nil {
tool.Result = fmt.Sprintf("error: %v", err)
return nil
}
if len(results.Results) == 0 {
tool.Result = "error: no search results"
return nil
}
tool.Result = results.String()
return nil
}