diff --git a/profiler/src/profiler/TracyLlm.cpp b/profiler/src/profiler/TracyLlm.cpp index c6ebd35d..f36e855a 100644 --- a/profiler/src/profiler/TracyLlm.cpp +++ b/profiler/src/profiler/TracyLlm.cpp @@ -1,5 +1,6 @@ -#include #include +#include +#include #include "TracyConfig.hpp" #include "TracyImGui.hpp" @@ -213,7 +214,7 @@ void TracyLlm::Draw() const auto posStart = ImGui::GetCursorPos().x; const auto& role = line["role"].get_ref(); - if( role == "system" ) continue; + if( role == "system" || role == "tool" ) continue; const auto isUser = role == "user"; const auto isError = role == "error"; @@ -566,8 +567,22 @@ bool TracyLlm::OnResponse( const ollama::response& response ) if( message.contains( "tool_calls" ) ) back["tool_calls"] = message["tool_calls"]; if( json["done"] ) { - m_responding = false; - m_focusInput = true; + if( back.contains( "tool_calls" ) ) + { + HandleToolCalls( back["tool_calls"] ); + + m_jobs.emplace_back( WorkItem { + .task = Task::SendMessage, + .callback = nullptr, + .chat = std::make_unique( *m_chat ) + } ); + m_cv.notify_all(); + } + else + { + m_responding = false; + m_focusInput = true; + } return false; } @@ -673,4 +688,38 @@ void TracyLlm::CleanContext( LineContext& ctx) } } +void TracyLlm::HandleToolCalls( const nlohmann::json& calls ) +{ + std::string response; + + for( auto& call : calls ) + { + auto& func = call["function"]; + auto& name = func["name"].get_ref(); + auto& args = func["arguments"]; + + std::string result = "### Result of calling function " + name + ":\n"; + + if( name == "get_current_time" ) result += GetCurrentTime(); + else result = "### Unknown function: " + name + "\n"; + + result += "\n\n"; + + response += result; + } + + m_chat->emplace_back( ollama::message( "tool", response ) ); +} + +std::string TracyLlm::GetCurrentTime() +{ + auto t = time( nullptr ); + auto tm = localtime( &t ); + + char buffer[64]; + std::strftime( buffer, sizeof( buffer ), "%Y-%m-%d %H:%M:%S", tm ); + + return buffer; +} + } diff --git a/profiler/src/profiler/TracyLlm.hpp b/profiler/src/profiler/TracyLlm.hpp index 09fa5f34..f0fa63ec 100644 --- a/profiler/src/profiler/TracyLlm.hpp +++ b/profiler/src/profiler/TracyLlm.hpp @@ -92,6 +92,10 @@ private: void PrintLine( LineContext& ctx, const std::string& str, int num ); void CleanContext( LineContext& ctx); + void HandleToolCalls( const nlohmann::json& calls ); + + std::string GetCurrentTime(); + std::unique_ptr m_ollama; mutable std::mutex m_modelsLock;