feat: support searchapi (#1284)
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user