fix: enhance web search functionality with proxy support (#1823)
* fix: enhance web search functionality with proxy support - Remove unnecessary blank line in README.md - Add missing closing details tag in both README.md and README_zh.md - Add proxy support to the web search engine configuration in config.lua - Ensure the web search function uses the proxy setting when available in init.lua - Fix a potential nil access in the response body format check in config.lua Signed-off-by: 范立洲 <fanlizhou@yunqilaohe.com> * [pre-commit.ci lite] apply automatic fixes --------- Signed-off-by: 范立洲 <fanlizhou@yunqilaohe.com> Co-authored-by: 范立洲 <fanlizhou@yunqilaohe.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
@@ -47,6 +47,7 @@ M._defaults = {
|
||||
},
|
||||
web_search_engine = {
|
||||
provider = "tavily",
|
||||
proxy = nil,
|
||||
providers = {
|
||||
tavily = {
|
||||
api_key_name = "TAVILY_API_KEY",
|
||||
|
||||
@@ -212,6 +212,7 @@ end
|
||||
---@type AvanteLLMToolFunc<{ query: string }>
|
||||
function M.web_search(opts, on_log)
|
||||
local provider_type = Config.web_search_engine.provider
|
||||
local proxy = Config.web_search_engine.proxy
|
||||
if provider_type == nil then return nil, "Search engine provider is not set" end
|
||||
if on_log then on_log("provider: " .. provider_type) end
|
||||
if on_log then on_log("query: " .. opts.query) end
|
||||
@@ -223,7 +224,7 @@ function M.web_search(opts, on_log)
|
||||
return nil, "Environment variable " .. search_engine.api_key_name .. " is not set"
|
||||
end
|
||||
if provider_type == "tavily" then
|
||||
local resp = curl.post("https://api.tavily.com/search", {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
["Authorization"] = "Bearer " .. api_key,
|
||||
@@ -231,7 +232,9 @@ function M.web_search(opts, on_log)
|
||||
body = vim.json.encode(vim.tbl_deep_extend("force", {
|
||||
query = opts.query,
|
||||
}, search_engine.extra_request_body)),
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.post("https://api.tavily.com/search", curl_opts)
|
||||
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)
|
||||
@@ -244,11 +247,13 @@ function M.web_search(opts, on_log)
|
||||
for key, value in pairs(query_params) do
|
||||
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
|
||||
end
|
||||
local resp = curl.get("https://serpapi.com/search?" .. query_string, {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
},
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.get("https://serpapi.com/search?" .. query_string, curl_opts)
|
||||
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)
|
||||
@@ -261,11 +266,13 @@ function M.web_search(opts, on_log)
|
||||
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, {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
},
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.get("https://searchapi.io/api/v1/search?" .. query_string, curl_opts)
|
||||
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)
|
||||
@@ -283,11 +290,13 @@ function M.web_search(opts, on_log)
|
||||
for key, value in pairs(query_params) do
|
||||
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
|
||||
end
|
||||
local resp = curl.get("https://www.googleapis.com/customsearch/v1?" .. query_string, {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
},
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.get("https://www.googleapis.com/customsearch/v1?" .. query_string, curl_opts)
|
||||
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)
|
||||
@@ -299,12 +308,14 @@ function M.web_search(opts, on_log)
|
||||
for key, value in pairs(query_params) do
|
||||
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
|
||||
end
|
||||
local resp = curl.get("https://kagi.com/api/v0/search?" .. query_string, {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Authorization"] = "Bot " .. api_key,
|
||||
["Content-Type"] = "application/json",
|
||||
},
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.get("https://kagi.com/api/v0/search?" .. query_string, curl_opts)
|
||||
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)
|
||||
@@ -316,12 +327,14 @@ function M.web_search(opts, on_log)
|
||||
for key, value in pairs(query_params) do
|
||||
query_string = query_string .. key .. "=" .. vim.uri_encode(value) .. "&"
|
||||
end
|
||||
local resp = curl.get("https://api.search.brave.com/res/v1/web/search?" .. query_string, {
|
||||
local curl_opts = {
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
["X-Subscription-Token"] = api_key,
|
||||
},
|
||||
})
|
||||
}
|
||||
if proxy then curl_opts.proxy = proxy end
|
||||
local resp = curl.get("https://api.search.brave.com/res/v1/web/search?" .. query_string, curl_opts)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user