diff --git a/profiler/src/profiler/TracyLlmApi.cpp b/profiler/src/profiler/TracyLlmApi.cpp index ca5c57c6..e8407d8c 100644 --- a/profiler/src/profiler/TracyLlmApi.cpp +++ b/profiler/src/profiler/TracyLlmApi.cpp @@ -248,6 +248,30 @@ int TracyLlmApi::Tokenize( const std::string& text, int modelIdx ) return -1; } +nlohmann::json TracyLlmApi::SendMessage( const nlohmann::json& chat, int modelIdx ) +{ + assert( m_curl ); + + nlohmann::json req = { + { "model", m_models[modelIdx].name }, + { "messages", chat } + }; + + auto data = req.dump( -1, ' ', false, nlohmann::json::error_handler_t::replace ); + std::string buf; + auto res = PostRequest( m_url + "/v1/chat/completions", data, buf, true ); + if( res != 200 ) return {}; + + try + { + return nlohmann::json::parse( buf ); + } + catch( const std::exception& ) + { + return {}; + } +} + int64_t TracyLlmApi::GetRequest( const std::string& url, std::string& response ) { assert( m_curl ); diff --git a/profiler/src/profiler/TracyLlmApi.hpp b/profiler/src/profiler/TracyLlmApi.hpp index ddb1abf0..15cbdcac 100644 --- a/profiler/src/profiler/TracyLlmApi.hpp +++ b/profiler/src/profiler/TracyLlmApi.hpp @@ -35,6 +35,7 @@ public: bool ChatCompletion( const nlohmann::json& req, const std::function& callback, int modelIdx ); bool Embeddings( const nlohmann::json& req, nlohmann::json& response, bool separateConnection = false ); [[nodiscard]] int Tokenize( const std::string& text, int modelIdx ); + [[nodiscard]] nlohmann::json SendMessage( const nlohmann::json& chat, int modelIdx ); [[nodiscard]] bool IsConnected() const { return m_curl != nullptr; } [[nodiscard]] const std::vector& GetModels() const { return m_models; }