From 0f78084f8ffb104267c3fac2a60064d62719605a Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 20 May 2025 01:22:20 +0200 Subject: [PATCH] Add dictionary support. --- profiler/src/llm/system.prompt | 16 +++++++++++++-- profiler/src/profiler/TracyLlmTools.cpp | 26 +++++++++++++++++++++++++ profiler/src/profiler/TracyLlmTools.hpp | 1 + 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/profiler/src/llm/system.prompt b/profiler/src/llm/system.prompt index afed70dd..301cd6e5 100644 --- a/profiler/src/llm/system.prompt +++ b/profiler/src/llm/system.prompt @@ -59,7 +59,7 @@ These are the tools you can use. *You have no access to any other tools or means "description": "The search terms in the language matching the second parameter." }, { - "description": "The language code matching the search query. For example, `en` for English or `pl` for Polish." + "description": "Language code matching the search query. For example, `en` for English or `pl` for Polish." } ] }, @@ -71,7 +71,19 @@ These are the tools you can use. *You have no access to any other tools or means "description": "The `key` field from the search response, specifying the topic you want to retrieve.", }, { - "description": "The language code." + "description": "Language code." + } + ] + }, + { + "name": "get_dictionary", + "description": "Retrieve description of a word from dictionary.", + "parameters": [ + { + "description": "Word to describe." + }, + { + "description": "Language code." } ] }, diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index e276d375..6244c3c4 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -64,6 +64,12 @@ TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name if( args.size() < 2 ) return { .reply = "Missing language argument" }; return { .reply = GetWikipedia( args[0], args[1] ) }; } + if( name == "get_dictionary" ) + { + if( args.empty() ) return { .reply = "Missing word argument" }; + if( args.size() < 2 ) return { .reply = "Missing language argument" }; + return { .reply = GetDictionary( args[0], args[1] ) }; + } if( name == "search_web" ) { if( args.empty() ) return { .reply = "Missing search term argument" }; @@ -210,6 +216,26 @@ std::string TracyLlmTools::GetWikipedia( std::string page, const std::string& la return res; } +std::string TracyLlmTools::GetDictionary( std::string word, const std::string& lang ) +{ + if( !m_netAccess ) return "Internet access is disabled by the user."; + + std::ranges::replace( word, ' ', '+' ); + const auto response = FetchWebPage( "https://" + lang + ".wiktionary.org/w/rest.php/v1/search/page?q=" + UrlEncode( word ) + "&limit=1" ); + + auto json = nlohmann::json::parse( response ); + if( !json.contains( "pages" ) ) return "No results found"; + + auto& page = json["pages"]; + if( page.size() == 0 ) return "No results found"; + + auto& page0 = page[0]; + if( !page0.contains( "key" ) ) return "No results found"; + + const auto key = page0["key"].get_ref(); + auto res = FetchWebPage( "https://" + lang + ".wiktionary.org/w/rest.php/v1/page/" + key ); + + const auto maxSize = CalcMaxSize(); if( res.size() > maxSize ) res = res.substr( 0, maxSize ); return res; } diff --git a/profiler/src/profiler/TracyLlmTools.hpp b/profiler/src/profiler/TracyLlmTools.hpp index 5edc3b7f..2f6d0dca 100644 --- a/profiler/src/profiler/TracyLlmTools.hpp +++ b/profiler/src/profiler/TracyLlmTools.hpp @@ -31,6 +31,7 @@ private: std::string FetchWebPage( const std::string& url ); ToolReply SearchWikipedia( std::string query, const std::string& lang ); 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 ); unordered_flat_map m_webCache;