feat: support searchapi (#1284)
This commit is contained in:
parent
c60dc6c316
commit
7fa7b0fa3b
@ -582,15 +582,15 @@ For more information, see [Custom Providers](https://github.com/yetone/avante.nv
|
||||
|
||||
## Web Search Engines
|
||||
|
||||
Avante's tools include some web search engines, currently support [tavily](https://tavily.com/), [serpapi](https://serpapi.com/) and google's [programmable search engine](https://developers.google.com/custom-search/v1/overview). The default is tavily, and can be changed through configuring `Config.web_search_engine.provider`:
|
||||
Avante's tools include some web search engines, currently support [tavily](https://tavily.com/), [serpapi](https://serpapi.com/), [searchapi](https://www.searchapi.io/) and google's [programmable search engine](https://developers.google.com/custom-search/v1/overview). The default is tavily, and can be changed through configuring `Config.web_search_engine.provider`:
|
||||
|
||||
```lua
|
||||
web_search_engine = {
|
||||
provider = "tavily", -- tavily, serpapi or google
|
||||
provider = "tavily", -- tavily, serpapi, searchapi or google
|
||||
}
|
||||
```
|
||||
|
||||
You need to set the environment variable `TAVILY_API_KEY` , `SERPAPI_API_KEY` to use tavily or serpapi.
|
||||
You need to set the environment variable `TAVILY_API_KEY` , `SERPAPI_API_KEY`, `SEARCHAPI_API_KEY` to use tavily or serpapi or searchapi.
|
||||
To use google, set the `GOOGLE_SEARCH_API_KEY` as the [API key](https://developers.google.com/custom-search/v1/overview), and `GOOGLE_SEARCH_ENGINE_ID` as the [search engine](https://programmablesearchengine.google.com) ID.
|
||||
|
||||
## Disable Tools
|
||||
|
@ -56,10 +56,39 @@ M._defaults = {
|
||||
title = result.title,
|
||||
link = result.link,
|
||||
snippet = result.snippet,
|
||||
date = result.date,
|
||||
}
|
||||
end
|
||||
)
|
||||
:take(5)
|
||||
:take(10)
|
||||
:totable()
|
||||
return vim.json.encode(jsn), nil
|
||||
end
|
||||
return "", nil
|
||||
end,
|
||||
},
|
||||
searchapi = {
|
||||
api_key_name = "SEARCHAPI_API_KEY",
|
||||
extra_request_body = {
|
||||
engine = "google",
|
||||
},
|
||||
---@type WebSearchEngineProviderResponseBodyFormatter
|
||||
format_response_body = function(body)
|
||||
if body.answer_box ~= nil then return body.answer_box.result, nil end
|
||||
if body.organic_results ~= nil then
|
||||
local jsn = vim
|
||||
.iter(body.organic_results)
|
||||
:map(
|
||||
function(result)
|
||||
return {
|
||||
title = result.title,
|
||||
link = result.link,
|
||||
snippet = result.snippet,
|
||||
date = result.date,
|
||||
}
|
||||
end
|
||||
)
|
||||
:take(10)
|
||||
:totable()
|
||||
return vim.json.encode(jsn), nil
|
||||
end
|
||||
@ -84,7 +113,7 @@ M._defaults = {
|
||||
}
|
||||
end
|
||||
)
|
||||
:take(5)
|
||||
:take(10)
|
||||
:totable()
|
||||
return vim.json.encode(jsn), nil
|
||||
end
|
||||
|
@ -340,6 +340,23 @@ function M.web_search(opts, on_log)
|
||||
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
|
||||
local jsn = vim.json.decode(resp.body)
|
||||
return search_engine.format_response_body(jsn)
|
||||
elseif provider_type == "searchapi" then
|
||||
local query_params = vim.tbl_deep_extend("force", {
|
||||
api_key = api_key,
|
||||
q = opts.query,
|
||||
}, search_engine.extra_request_body)
|
||||
local query_string = ""
|
||||
for key, value in pairs(query_params) do
|
||||
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
|
||||
end
|
||||
local resp = curl.get("https://searchapi.io/api/v1/search?" .. query_string, {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
},
|
||||
})
|
||||
if resp.status ~= 200 then return nil, "Error: " .. resp.body end
|
||||
local jsn = vim.json.decode(resp.body)
|
||||
return search_engine.format_response_body(jsn)
|
||||
elseif provider_type == "google" then
|
||||
local engine_id = os.getenv(search_engine.engine_id_name)
|
||||
if engine_id == nil or engine_id == "" then
|
||||
|
@ -20,6 +20,7 @@ Tools Usage Guide:
|
||||
- When attempting to modify a file that is not in the context, please first use the `list_files` tool and `search_files` tool to check if the file you want to modify exists, then use the `read_file` tool to read the file content. Don't modify blindly!
|
||||
- When generating files, first use `list_files` tool to read the directory structure, don't generate blindly!
|
||||
- When creating files, first check if the directory exists. If it doesn't exist, create the directory before creating the file.
|
||||
- After `web_search`, if you don't get detailed enough information, do not continue use `web_search`, just continue using the `fetch` tool to get more information you need from the links in the search results.
|
||||
|
||||
{% if system_info -%}
|
||||
Use the appropriate shell based on the user's system info:
|
||||
|
Loading…
x
Reference in New Issue
Block a user