diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index dfcc67b0..e276d375 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -83,6 +83,16 @@ std::string TracyLlmTools::GetCurrentTime() return buffer; } +int TracyLlmTools::CalcMaxSize() const +{ + // Limit the size of the response to avoid exceeding the context size + // Assume average token size is 4 bytes. Make space for 3 articles to be retrieved. + assert( m_modelMaxContext != 0 ); + const auto ctxSize = std::min( m_modelMaxContext, s_config.llmContext ); + const auto maxSize = ( ctxSize * 4 ) / 3; + return maxSize; +} + static size_t WriteFn( void* _data, size_t size, size_t num, void* ptr ) { const auto data = (unsigned char*)_data; @@ -195,11 +205,10 @@ std::string TracyLlmTools::GetWikipedia( std::string page, const std::string& la std::ranges::replace( page, ' ', '_' ); auto res = FetchWebPage( "https://" + lang + ".wikipedia.org/w/rest.php/v1/page/" + page ); - // Limit the size of the response to avoid exceeding the context size - // Assume average token size is 4 bytes. Make space for 3 articles to be retrieved. - assert( m_modelMaxContext != 0 ); - const auto ctxSize = std::min( m_modelMaxContext, s_config.llmContext ); - const auto maxSize = ( ctxSize * 4 ) / 3; + const auto maxSize = CalcMaxSize(); + if( res.size() > maxSize ) res = res.substr( 0, maxSize ); + return res; +} 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 b95297fc..5edc3b7f 100644 --- a/profiler/src/profiler/TracyLlmTools.hpp +++ b/profiler/src/profiler/TracyLlmTools.hpp @@ -26,6 +26,8 @@ public: bool m_netAccess = true; private: + [[nodiscard]] int CalcMaxSize() const; + 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 );