From 7fa7b0fa3b2b846aab759633d45bd37edb03868e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 17 Feb 2025 00:36:00 +0800 Subject: [PATCH] feat: support searchapi (#1284) --- README.md | 6 ++--- lua/avante/config.lua | 33 +++++++++++++++++++++++++-- lua/avante/llm_tools.lua | 17 ++++++++++++++ lua/avante/templates/base.avanterules | 1 + 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 364f066..03e3ff3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 8791469..504f8fb 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -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 diff --git a/lua/avante/llm_tools.lua b/lua/avante/llm_tools.lua index 715244d..5dbec4d 100644 --- a/lua/avante/llm_tools.lua +++ b/lua/avante/llm_tools.lua @@ -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 diff --git a/lua/avante/templates/base.avanterules b/lua/avante/templates/base.avanterules index 2c361e4..c96886e 100644 --- a/lua/avante/templates/base.avanterules +++ b/lua/avante/templates/base.avanterules @@ -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: