diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index a439d8b8..11a13b8c 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -515,33 +515,45 @@ static void ReplaceAll( std::string& str, std::string_view from, std::string_vie std::string TracyLlmTools::SearchWeb( std::string query ) { NetworkCheck; - query = UrlEncode( query ); if( !s_config.llmSearchApiKey.empty() && !s_config.llmSearchIdentifier.empty() ) { - const auto response = FetchWebPage( "https://customsearch.googleapis.com/customsearch/v1?key=" + s_config.llmSearchApiKey + "&cx=" + s_config.llmSearchIdentifier + "&q=" + query ); - try - { - auto json = nlohmann::json::parse( response ); - if( json.contains( "items" ) && json["items"].size() != 0 ) - { - nlohmann::json results; - for( size_t i = 0; i < json["items"].size(); i++ ) - { - auto& item = json["items"][i]; - nlohmann::json result; - result["title"] = RemoveNewline( item["title"].get_ref() ); - result["preview"] = RemoveNewline( item["snippet"].get_ref() ); - result["url"] = RemoveNewline( item["link"].get_ref() ); - results[i] = result; - } - return results.dump( -1, ' ', false, nlohmann::json::error_handler_t::replace ); - } - } - catch( const nlohmann::json::exception& e ) {} + const auto result = SearchWebGoogle( query ); + if( !result.starts_with( "Error:" ) && !result.starts_with( "No results" ) ) return result; } + return SearchWebDuckDuckGo( query ); +} + +std::string TracyLlmTools::SearchWebGoogle( std::string query ) +{ + const auto response = FetchWebPage( "https://customsearch.googleapis.com/customsearch/v1?key=" + s_config.llmSearchApiKey + "&cx=" + s_config.llmSearchIdentifier + "&q=" + query ); + try + { + auto json = nlohmann::json::parse( response ); + if( json.contains( "items" ) && json["items"].size() != 0 ) + { + nlohmann::json results; + for( size_t i = 0; i < json["items"].size(); i++ ) + { + auto& item = json["items"][i]; + nlohmann::json result; + result["title"] = RemoveNewline( item["title"].get_ref() ); + result["preview"] = RemoveNewline( item["snippet"].get_ref() ); + result["url"] = RemoveNewline( item["link"].get_ref() ); + results[i] = result; + } + return results.dump( -1, ' ', false, nlohmann::json::error_handler_t::replace ); + } + } + catch( const nlohmann::json::exception& e ) {} + + return "Error: Google search failed"; +} + +std::string TracyLlmTools::SearchWebDuckDuckGo( std::string query ) +{ auto response = FetchWebPage( "https://lite.duckduckgo.com/lite?q=" + query ); if( response.starts_with( "Error:" ) ) return response; diff --git a/profiler/src/profiler/TracyLlmTools.hpp b/profiler/src/profiler/TracyLlmTools.hpp index ac78132d..28fa1a2e 100644 --- a/profiler/src/profiler/TracyLlmTools.hpp +++ b/profiler/src/profiler/TracyLlmTools.hpp @@ -55,6 +55,8 @@ private: std::string GetWikipedia( std::string page, const std::string& lang ); std::string GetDictionary( std::string word, const std::string& lang ); std::string SearchWeb( std::string query ); + std::string SearchWebGoogle( std::string query ); + std::string SearchWebDuckDuckGo( std::string query ); std::string GetWebpage( const std::string& url ); std::string SearchManual( const std::string& query, TracyLlmApi& api, bool hasEmbeddingsModel ); std::string SourceFile( const std::string& file, uint32_t line, uint32_t context, uint32_t contextBack ) const;