From c5422e026440a165fdf89d71f97d06aa0a6b36bd Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 23 May 2025 19:00:34 +0200 Subject: [PATCH] Add user_manual tool. --- profiler/src/llm/system.prompt | 9 ++++++ profiler/src/profiler/TracyLlm.cpp | 2 +- profiler/src/profiler/TracyLlmTools.cpp | 38 ++++++++++++++++++++++++- profiler/src/profiler/TracyLlmTools.hpp | 3 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/profiler/src/llm/system.prompt b/profiler/src/llm/system.prompt index 8368e256..48872996 100644 --- a/profiler/src/llm/system.prompt +++ b/profiler/src/llm/system.prompt @@ -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*." + } + ] } ] ``` diff --git a/profiler/src/profiler/TracyLlm.cpp b/profiler/src/profiler/TracyLlm.cpp index ef2716ee..cbf892e1 100644 --- a/profiler/src/profiler/TracyLlm.cpp +++ b/profiler/src/profiler/TracyLlm.cpp @@ -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 = "\n" + reply.reply; m_usedCtx += output.size() / 4; lock.lock(); diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index b0e2854c..d9fcbb35 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -52,7 +52,7 @@ TracyLlmTools::~TracyLlmTools() CancelManualEmbeddings(); } -TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name, const std::vector& args, int contextSize ) +TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& name, const std::vector& 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 vec; + vec.reserve( embedding.size() ); + for( auto& item : embedding ) vec.emplace_back( item.get() ); + + 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 ); +} + } diff --git a/profiler/src/profiler/TracyLlmTools.hpp b/profiler/src/profiler/TracyLlmTools.hpp index 4430bef9..46be242b 100644 --- a/profiler/src/profiler/TracyLlmTools.hpp +++ b/profiler/src/profiler/TracyLlmTools.hpp @@ -34,7 +34,7 @@ public: ~TracyLlmTools(); - ToolReply HandleToolCalls( const std::string& name, const std::vector& args, int contextSize ); + ToolReply HandleToolCalls( const std::string& name, const std::vector& 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 );