diff --git a/profiler/CMakeLists.txt b/profiler/CMakeLists.txt index dfc5474d..6d538c9d 100644 --- a/profiler/CMakeLists.txt +++ b/profiler/CMakeLists.txt @@ -138,7 +138,7 @@ Embed(PROFILER_FILES FontNormal src/font/Roboto-Regular.ttf) Embed(PROFILER_FILES FontBold src/font/Roboto-Bold.ttf) Embed(PROFILER_FILES FontItalic src/font/Roboto-Italic.ttf) Embed(PROFILER_FILES FontBoldItalic src/font/Roboto-BoldItalic.ttf) -Embed(PROFILER_FILES Manual ../manual/tracy.tex) +Embed(PROFILER_FILES Manual ../manual/tracy.md) set(INCLUDES "${CMAKE_CURRENT_BINARY_DIR}") set(LIBS "") diff --git a/profiler/src/profiler/TracyLlmEmbeddings.cpp b/profiler/src/profiler/TracyLlmEmbeddings.cpp index 1fcb3abb..20fb0988 100644 --- a/profiler/src/profiler/TracyLlmEmbeddings.cpp +++ b/profiler/src/profiler/TracyLlmEmbeddings.cpp @@ -10,10 +10,10 @@ TracyLlmEmbeddings::TracyLlmEmbeddings( size_t length, size_t reserve ) if( reserve > 0 ) m_index.reserve( reserve ); } -void TracyLlmEmbeddings::Add( const char* chunk, size_t chunkLen, const std::vector& embedding ) +void TracyLlmEmbeddings::Add( int idx, const std::vector& embedding ) { m_index.add( m_data.size(), embedding.data() ); - m_data.emplace_back( Chunk { .str = chunk, .length = chunkLen } ); + m_data.emplace_back( idx ); } std::vector TracyLlmEmbeddings::Search( const std::vector& embedding, size_t k ) const diff --git a/profiler/src/profiler/TracyLlmEmbeddings.hpp b/profiler/src/profiler/TracyLlmEmbeddings.hpp index b957ec0b..c316c2fb 100644 --- a/profiler/src/profiler/TracyLlmEmbeddings.hpp +++ b/profiler/src/profiler/TracyLlmEmbeddings.hpp @@ -2,7 +2,6 @@ #define __TRACYLLMEMBEDDINGS_HPP__ #include -#include #include #include @@ -18,21 +17,15 @@ public: float distance; }; - struct Chunk - { - const char* str; - size_t length; - }; - explicit TracyLlmEmbeddings( size_t length, size_t reserve = 0 ); - void Add( const char* chunk, size_t chunkLen, const std::vector& embedding ); + void Add( int idx, const std::vector& embedding ); [[nodiscard]] std::vector Search( const std::vector& embedding, size_t k ) const; - [[nodiscard]] const Chunk& Get( size_t idx ) const { return m_data[idx]; } + [[nodiscard]] int Get( size_t idx ) const { return m_data[idx]; } private: unum::usearch::index_dense_t m_index; - std::vector m_data; + std::vector m_data; }; } diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index 088a4683..bbd3f8fb 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -159,9 +160,6 @@ void TracyLlmTools::BuildManualEmbeddings( const std::string& model, TracyLlmApi void TracyLlmTools::ManualEmbeddingsWorker( TracyLlmApi& api ) { - constexpr auto Chunk = 1000; - constexpr auto Overlap = 200; - size_t length; { nlohmann::json req; @@ -182,12 +180,91 @@ void TracyLlmTools::ManualEmbeddingsWorker( TracyLlmApi& api ) } if( !m_manual ) m_manual = Unembed( Manual ); + std::string_view manual( m_manual->data(), m_manual->size() ); const auto sz = (int)m_manual->size(); - const auto chunks = ( sz + Chunk - 1 ) / Chunk; + m_manualChunks.clear(); - m_manualEmbeddings = std::make_unique( length, chunks ); + std::vector> chunks; + std::vector levels = { 0 }; + std::vector chapterNames = { "Title Page" }; - for( int i=0; i( length, csz ); + + for( size_t i=0; idata() + start, end - start ); + req["input"] = std::move( chunks[i].first ); req["model"] = m_manualEmbeddingState.model; nlohmann::json response; @@ -211,12 +285,9 @@ void TracyLlmTools::ManualEmbeddingsWorker( TracyLlmApi& api ) std::vector embeddings; embeddings.reserve( length ); - for( auto& item : response["data"][0]["embedding"] ) - { - embeddings.emplace_back( item.get() ); - } + for( auto& item : response["data"][0]["embedding"] ) embeddings.emplace_back( item.get() ); - m_manualEmbeddings->Add( m_manual->data() + start, end - start, embeddings ); + m_manualEmbeddings->Add( chunks[i].second, embeddings ); } std::lock_guard lock( m_lock ); @@ -649,16 +720,24 @@ std::string TracyLlmTools::SearchManual( const std::string& query, TracyLlmApi& vec.reserve( embedding.size() ); for( auto& item : embedding ) vec.emplace_back( item.get() ); - auto results = m_manualEmbeddings->Search( vec, 5 ); + auto results = m_manualEmbeddings->Search( vec, 10 ); std::ranges::sort( results, []( const auto& a, const auto& b ) { return a.distance < b.distance; } ); - nlohmann::json json; + std::vector> chunks; + chunks.reserve( results.size() ); for( auto& item : results ) { - const auto& chunk = m_manualEmbeddings->Get( item.idx ); + const auto chunk = m_manualEmbeddings->Get( item.idx ); + if( std::ranges::find_if( chunks, [chunk]( const auto& v ) { return v.first == chunk; } ) == chunks.end() ) chunks.emplace_back( chunk, item.distance ); + } + if( chunks.size() > 5 ) chunks.resize( 5 ); + + nlohmann::json json; + for( auto& chunk : chunks ) + { nlohmann::json r; - r["distance"] = item.distance; - r["text"] = std::string( chunk.str, chunk.length ); + r["distance"] = chunk.second; + r["text"] = m_manualChunks[chunk.first]; json.emplace_back( std::move( r ) ); } diff --git a/profiler/src/profiler/TracyLlmTools.hpp b/profiler/src/profiler/TracyLlmTools.hpp index 5864bb52..5c12f185 100644 --- a/profiler/src/profiler/TracyLlmTools.hpp +++ b/profiler/src/profiler/TracyLlmTools.hpp @@ -68,7 +68,9 @@ private: bool m_cancel = false; EmbeddingState m_manualEmbeddingState; std::unique_ptr m_manualEmbeddings; + std::shared_ptr m_manual; + std::vector m_manualChunks; }; }