diff --git a/gpt4all-chat/CHANGELOG.md b/gpt4all-chat/CHANGELOG.md index ee952b38..8ca1a2a6 100644 --- a/gpt4all-chat/CHANGELOG.md +++ b/gpt4all-chat/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +## [Unreleased] +- Fix the timeout error in code interpreter ([#3369](https://github.com/nomic-ai/gpt4all/pull/3369)) + ## [3.6.1] - 2024-12-20 ### Fixed diff --git a/gpt4all-chat/src/codeinterpreter.cpp b/gpt4all-chat/src/codeinterpreter.cpp index 027d0249..c33a40a3 100644 --- a/gpt4all-chat/src/codeinterpreter.cpp +++ b/gpt4all-chat/src/codeinterpreter.cpp @@ -26,7 +26,7 @@ QString CodeInterpreter::run(const QList ¶ms, qint64 timeout) workerThread.start(); bool timedOut = !workerThread.wait(timeout); if (timedOut) { - worker.interrupt(); // thread safe + worker.interrupt(timeout); // thread safe m_error = ToolEnums::Error::TimeoutError; } workerThread.quit(); @@ -96,14 +96,17 @@ void CodeInterpreterWorker::request(const QString &code) m_engine.globalObject().setProperty("console", consoleObject); const QJSValue result = m_engine.evaluate(code); - QString resultString = result.isUndefined() ? QString() : result.toString(); - // NOTE: We purposely do not set the m_error or m_errorString for the code interpreter since - // we *want* the model to see the response has an error so it can hopefully correct itself. The - // error member variables are intended for tools that have error conditions that cannot be corrected. - // For instance, a tool depending upon the network might set these error variables if the network - // is not available. - if (result.isError()) { + QString resultString; + + if (m_engine.isInterrupted()) { + resultString = QString("Error: code execution was timed out as it exceeded %1 ms. Code must be written to ensure execution does not timeout.").arg(m_timeout); + } else if (result.isError()) { + // NOTE: We purposely do not set the m_error or m_errorString for the code interpreter since + // we *want* the model to see the response has an error so it can hopefully correct itself. The + // error member variables are intended for tools that have error conditions that cannot be corrected. + // For instance, a tool depending upon the network might set these error variables if the network + // is not available. const QStringList lines = code.split('\n'); const int line = result.property("lineNumber").toInt(); const int index = line - 1; @@ -114,6 +117,8 @@ void CodeInterpreterWorker::request(const QString &code) .arg(lineContent); m_error = ToolEnums::Error::UnknownError; m_errorString = resultString; + } else { + resultString = result.isUndefined() ? QString() : result.toString(); } if (resultString.isEmpty()) diff --git a/gpt4all-chat/src/codeinterpreter.h b/gpt4all-chat/src/codeinterpreter.h index 41d2f983..0e88c463 100644 --- a/gpt4all-chat/src/codeinterpreter.h +++ b/gpt4all-chat/src/codeinterpreter.h @@ -42,7 +42,7 @@ public: QString response() const { return m_response; } void request(const QString &code); - void interrupt() { m_engine.setInterrupted(true); } + void interrupt(qint64 timeout) { m_timeout = timeout; m_engine.setInterrupted(true); } ToolEnums::Error error() const { return m_error; } QString errorString() const { return m_errorString; } @@ -50,6 +50,7 @@ Q_SIGNALS: void finished(); private: + qint64 m_timeout = 0; QJSEngine m_engine; QString m_response; ToolEnums::Error m_error = ToolEnums::Error::NoError;