Extract calculation of max tool output size.

This commit is contained in:
Bartosz Taudul
2025-05-20 01:21:43 +02:00
parent 98e9383c89
commit 6d8d461f2b
2 changed files with 16 additions and 5 deletions

View File

@@ -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;

View File

@@ -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 );