Add user_manual tool.

This commit is contained in:
Bartosz Taudul
2025-05-23 19:00:34 +02:00
parent 0520d12b24
commit c5422e0264
4 changed files with 49 additions and 3 deletions

View File

@@ -97,6 +97,15 @@ These are the tools you can use. *You have no access to any other tools or means
"description": "Search query."
}
]
},
{
"name": "user_manual",
"description": "Search the Tracy Profiler user manual with given query.",
"parameters": [
{
"description": "Search query *in English*."
}
]
}
]
```

View File

@@ -800,7 +800,7 @@ bool TracyLlm::OnResponse( const nlohmann::json& json )
auto tool = lines[0];
lines.erase( lines.begin() );
lock.unlock();
const auto reply = m_tools.HandleToolCalls( tool, lines, m_api->GetModels()[m_modelIdx].contextSize );
const auto reply = m_tools.HandleToolCalls( tool, lines, *m_api, m_api->GetModels()[m_modelIdx].contextSize, m_embedIdx >= 0 );
const auto output = "<tool_output>\n" + reply.reply;
m_usedCtx += output.size() / 4;
lock.lock();

View File

@@ -52,7 +52,7 @@ TracyLlmTools::~TracyLlmTools()
CancelManualEmbeddings();
}
TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name, const std::vector<std::string>& args, int contextSize )
TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name, const std::vector<std::string>& args, TracyLlmApi& api, int contextSize, bool hasEmbeddingsModel )
{
m_ctxSize = contextSize;
@@ -84,6 +84,11 @@ TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name
if( args.empty() ) return { .reply = "Missing search term argument" };
return { .reply = SearchWeb( args[0] ) };
}
if( name == "user_manual" )
{
if( args.empty() ) return { .reply = "Missing search term argument" };
return { .reply = SearchManual( args[0], api, hasEmbeddingsModel ) };
}
return { .reply = "Unknown tool call: " + name };
}
@@ -427,4 +432,35 @@ std::string TracyLlmTools::SearchWeb( std::string query )
return json.dump( 2 );
}
std::string TracyLlmTools::SearchManual( const std::string& query, TracyLlmApi& api, bool hasEmbeddingsModel )
{
if( !hasEmbeddingsModel ) return "Searching the user manual requires vector embeddings model to be selected. You must inform the user that he should download such a model using their LLM provider software, so you can use this tool.";
if( !m_manualEmbeddingState.done ) return "User manual embedding vectors are not calculated. You must inform the user that he should click the \"Learn manual\" button, so you can use this tool.";
nlohmann::json req;
req["input"] = query;
req["model"] = m_manualEmbeddingState.model;
nlohmann::json response;
api.Embeddings( req, response, true );
auto& embedding = response["data"][0]["embedding"];
std::vector<float> vec;
vec.reserve( embedding.size() );
for( auto& item : embedding ) vec.emplace_back( item.get<float>() );
auto results = m_manualEmbeddings->Search( vec, 5 );
std::ranges::sort( results, []( const auto& a, const auto& b ) { return a.distance < b.distance; } );
nlohmann::json json;
for( auto& item : results )
{
nlohmann::json r;
r["distance"] = item.distance;
r["text"] = m_manualEmbeddings->Get( item.idx );
json.emplace_back( std::move( r ) );
}
return json.dump( 2 );
}
}

View File

@@ -34,7 +34,7 @@ public:
~TracyLlmTools();
ToolReply HandleToolCalls( const std::string& name, const std::vector<std::string>& args, int contextSize );
ToolReply HandleToolCalls( const std::string& name, const std::vector<std::string>& args, TracyLlmApi& api, int contextSize, bool hasEmbeddingsModel );
std::string GetCurrentTime();
[[nodiscard]] EmbeddingState GetManualEmbeddingsState() const;
@@ -52,6 +52,7 @@ 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 SearchManual( const std::string& query, TracyLlmApi& api, bool hasEmbeddingsModel );
void ManualEmbeddingsWorker( TracyLlmApi& api );