Add dictionary support.

This commit is contained in:
Bartosz Taudul
2025-05-20 01:22:20 +02:00
parent 6d8d461f2b
commit 0f78084f8f
3 changed files with 41 additions and 2 deletions

View File

@@ -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."
}
]
},

View File

@@ -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<const std::string&>();
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;
}

View File

@@ -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<std::string, std::string> m_webCache;