mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-11-30 22:43:00 -05:00
feat: Add ollama thinking feature (#275)
This commit is contained in:
@ -70,6 +70,22 @@ void OllamaMessage::handleToolCall(const QJsonObject &toolCall)
|
||||
LOG_MESSAGE(
|
||||
QString("OllamaMessage: Structured tool call detected - name=%1, id=%2").arg(name, toolId));
|
||||
}
|
||||
|
||||
void OllamaMessage::handleThinkingDelta(const QString &thinking)
|
||||
{
|
||||
LLMCore::ThinkingContent *thinkingContent = getOrCreateThinkingContent();
|
||||
thinkingContent->appendThinking(thinking);
|
||||
}
|
||||
|
||||
void OllamaMessage::handleThinkingComplete(const QString &signature)
|
||||
{
|
||||
if (m_currentThinkingContent) {
|
||||
m_currentThinkingContent->setSignature(signature);
|
||||
LOG_MESSAGE(QString("OllamaMessage: Set thinking signature, length=%1")
|
||||
.arg(signature.length()));
|
||||
}
|
||||
}
|
||||
|
||||
void OllamaMessage::handleDone(bool done)
|
||||
{
|
||||
m_done = done;
|
||||
@ -216,6 +232,7 @@ QJsonObject OllamaMessage::toProviderFormat() const
|
||||
|
||||
QString textContent;
|
||||
QJsonArray toolCalls;
|
||||
QString thinkingContent;
|
||||
|
||||
for (auto block : m_currentBlocks) {
|
||||
if (!block)
|
||||
@ -228,9 +245,15 @@ QJsonObject OllamaMessage::toProviderFormat() const
|
||||
toolCall["type"] = "function";
|
||||
toolCall["function"] = QJsonObject{{"name", tool->name()}, {"arguments", tool->input()}};
|
||||
toolCalls.append(toolCall);
|
||||
} else if (auto thinking = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
||||
thinkingContent += thinking->thinking();
|
||||
}
|
||||
}
|
||||
|
||||
if (!thinkingContent.isEmpty()) {
|
||||
message["thinking"] = thinkingContent;
|
||||
}
|
||||
|
||||
if (!textContent.isEmpty()) {
|
||||
message["content"] = textContent;
|
||||
}
|
||||
@ -275,6 +298,17 @@ QList<LLMCore::ToolUseContent *> OllamaMessage::getCurrentToolUseContent() const
|
||||
return toolBlocks;
|
||||
}
|
||||
|
||||
QList<LLMCore::ThinkingContent *> OllamaMessage::getCurrentThinkingContent() const
|
||||
{
|
||||
QList<LLMCore::ThinkingContent *> thinkingBlocks;
|
||||
for (auto block : m_currentBlocks) {
|
||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
||||
thinkingBlocks.append(thinkingContent);
|
||||
}
|
||||
}
|
||||
return thinkingBlocks;
|
||||
}
|
||||
|
||||
void OllamaMessage::startNewContinuation()
|
||||
{
|
||||
LOG_MESSAGE(QString("OllamaMessage: Starting new continuation"));
|
||||
@ -284,6 +318,7 @@ void OllamaMessage::startNewContinuation()
|
||||
m_done = false;
|
||||
m_state = LLMCore::MessageState::Building;
|
||||
m_contentAddedToTextBlock = false;
|
||||
m_currentThinkingContent = nullptr;
|
||||
}
|
||||
|
||||
void OllamaMessage::updateStateFromDone()
|
||||
@ -309,4 +344,22 @@ LLMCore::TextContent *OllamaMessage::getOrCreateTextContent()
|
||||
return addCurrentContent<LLMCore::TextContent>();
|
||||
}
|
||||
|
||||
LLMCore::ThinkingContent *OllamaMessage::getOrCreateThinkingContent()
|
||||
{
|
||||
if (m_currentThinkingContent) {
|
||||
return m_currentThinkingContent;
|
||||
}
|
||||
|
||||
for (auto block : m_currentBlocks) {
|
||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
||||
m_currentThinkingContent = thinkingContent;
|
||||
return m_currentThinkingContent;
|
||||
}
|
||||
}
|
||||
|
||||
m_currentThinkingContent = addCurrentContent<LLMCore::ThinkingContent>();
|
||||
LOG_MESSAGE(QString("OllamaMessage: Created new ThinkingContent block"));
|
||||
return m_currentThinkingContent;
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
|
||||
@ -31,6 +31,8 @@ public:
|
||||
|
||||
void handleContentDelta(const QString &content);
|
||||
void handleToolCall(const QJsonObject &toolCall);
|
||||
void handleThinkingDelta(const QString &thinking);
|
||||
void handleThinkingComplete(const QString &signature);
|
||||
void handleDone(bool done);
|
||||
|
||||
QJsonObject toProviderFormat() const;
|
||||
@ -38,6 +40,7 @@ public:
|
||||
|
||||
LLMCore::MessageState state() const { return m_state; }
|
||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||
QList<LLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
||||
QList<LLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
||||
|
||||
void startNewContinuation();
|
||||
@ -48,11 +51,13 @@ private:
|
||||
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
||||
QString m_accumulatedContent;
|
||||
bool m_contentAddedToTextBlock = false;
|
||||
LLMCore::ThinkingContent *m_currentThinkingContent = nullptr;
|
||||
|
||||
void updateStateFromDone();
|
||||
bool tryParseToolCall();
|
||||
bool isLikelyToolCallJson(const QString &content) const;
|
||||
LLMCore::TextContent *getOrCreateTextContent();
|
||||
LLMCore::ThinkingContent *getOrCreateThinkingContent();
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T *addCurrentContent(Args &&...args)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright (C) 2024-2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
@ -104,12 +104,31 @@ void OllamaProvider::prepareRequest(
|
||||
request["keep_alive"] = settings.ollamaLivetime();
|
||||
};
|
||||
|
||||
auto applyThinkingMode = [&request]() {
|
||||
request["enable_thinking"] = true;
|
||||
QJsonObject options = request["options"].toObject();
|
||||
options["temperature"] = 1.0;
|
||||
request["options"] = options;
|
||||
};
|
||||
|
||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
||||
applySettings(Settings::codeCompletionSettings());
|
||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
||||
applySettings(Settings::quickRefactorSettings());
|
||||
const auto &qrSettings = Settings::quickRefactorSettings();
|
||||
applySettings(qrSettings);
|
||||
|
||||
if (isThinkingEnabled) {
|
||||
applyThinkingMode();
|
||||
LOG_MESSAGE(QString("OllamaProvider: Thinking mode enabled for QuickRefactoring"));
|
||||
}
|
||||
} else {
|
||||
applySettings(Settings::chatAssistantSettings());
|
||||
const auto &chatSettings = Settings::chatAssistantSettings();
|
||||
applySettings(chatSettings);
|
||||
|
||||
if (isThinkingEnabled) {
|
||||
applyThinkingMode();
|
||||
LOG_MESSAGE(QString("OllamaProvider: Thinking mode enabled for Chat"));
|
||||
}
|
||||
}
|
||||
|
||||
if (isToolsEnabled) {
|
||||
@ -247,6 +266,11 @@ bool OllamaProvider::supportImage() const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OllamaProvider::supportThinking() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void OllamaProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
||||
{
|
||||
LOG_MESSAGE(QString("OllamaProvider: Cancelling request %1").arg(requestId));
|
||||
@ -405,12 +429,48 @@ void OllamaProvider::processStreamData(const QString &requestId, const QJsonObje
|
||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
||||
}
|
||||
|
||||
if (data.contains("thinking")) {
|
||||
QString thinkingDelta = data["thinking"].toString();
|
||||
if (!thinkingDelta.isEmpty()) {
|
||||
message->handleThinkingDelta(thinkingDelta);
|
||||
LOG_MESSAGE(QString("OllamaProvider: Received thinking delta, length=%1")
|
||||
.arg(thinkingDelta.length()));
|
||||
}
|
||||
}
|
||||
|
||||
if (data.contains("message")) {
|
||||
QJsonObject messageObj = data["message"].toObject();
|
||||
|
||||
if (messageObj.contains("thinking")) {
|
||||
QString thinkingDelta = messageObj["thinking"].toString();
|
||||
if (!thinkingDelta.isEmpty()) {
|
||||
message->handleThinkingDelta(thinkingDelta);
|
||||
LOG_MESSAGE(QString("OllamaProvider: Received thinking delta from message.thinking, length=%1")
|
||||
.arg(thinkingDelta.length()));
|
||||
|
||||
if (!m_thinkingStarted.contains(requestId)) {
|
||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
||||
if (!thinkingBlocks.isEmpty() && thinkingBlocks.first()) {
|
||||
QString currentThinking = thinkingBlocks.first()->thinking();
|
||||
QString displayThinking = currentThinking.length() > 50
|
||||
? QString("%1...").arg(currentThinking.left(50))
|
||||
: currentThinking;
|
||||
|
||||
emit thinkingBlockReceived(requestId, displayThinking, "");
|
||||
LOG_MESSAGE(QString("Emitted initial thinking indicator for request %1, length=%2")
|
||||
.arg(requestId)
|
||||
.arg(currentThinking.length()));
|
||||
m_thinkingStarted.insert(requestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (messageObj.contains("content")) {
|
||||
QString content = messageObj["content"].toString();
|
||||
if (!content.isEmpty()) {
|
||||
emitThinkingBlocks(requestId, message);
|
||||
|
||||
message->handleContentDelta(content);
|
||||
|
||||
bool hasTextContent = false;
|
||||
@ -460,6 +520,13 @@ void OllamaProvider::processStreamData(const QString &requestId, const QJsonObje
|
||||
}
|
||||
|
||||
if (data["done"].toBool()) {
|
||||
if (data.contains("signature")) {
|
||||
QString signature = data["signature"].toString();
|
||||
message->handleThinkingComplete(signature);
|
||||
LOG_MESSAGE(QString("OllamaProvider: Set thinking signature, length=%1")
|
||||
.arg(signature.length()));
|
||||
}
|
||||
|
||||
message->handleDone(true);
|
||||
handleMessageComplete(requestId);
|
||||
}
|
||||
@ -472,6 +539,8 @@ void OllamaProvider::handleMessageComplete(const QString &requestId)
|
||||
|
||||
OllamaMessage *message = m_messages[requestId];
|
||||
|
||||
emitThinkingBlocks(requestId, message);
|
||||
|
||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
||||
LOG_MESSAGE(QString("Ollama message requires tool execution for %1").arg(requestId));
|
||||
|
||||
@ -517,6 +586,32 @@ void OllamaProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
||||
m_dataBuffers.remove(requestId);
|
||||
m_requestUrls.remove(requestId);
|
||||
m_originalRequests.remove(requestId);
|
||||
m_thinkingEmitted.remove(requestId);
|
||||
m_thinkingStarted.remove(requestId);
|
||||
m_toolsManager->cleanupRequest(requestId);
|
||||
}
|
||||
|
||||
void OllamaProvider::emitThinkingBlocks(const QString &requestId, OllamaMessage *message)
|
||||
{
|
||||
if (!message || m_thinkingEmitted.contains(requestId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
||||
if (thinkingBlocks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto thinkingContent : thinkingBlocks) {
|
||||
emit thinkingBlockReceived(
|
||||
requestId, thinkingContent->thinking(), thinkingContent->signature());
|
||||
LOG_MESSAGE(QString("Emitted thinking block for request %1, thinking length=%2, signature "
|
||||
"length=%3")
|
||||
.arg(requestId)
|
||||
.arg(thinkingContent->thinking().length())
|
||||
.arg(thinkingContent->signature().length()));
|
||||
}
|
||||
m_thinkingEmitted.insert(requestId);
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Providers
|
||||
|
||||
@ -55,6 +55,7 @@ public:
|
||||
|
||||
bool supportsTools() const override;
|
||||
bool supportImage() const override;
|
||||
bool supportThinking() const override;
|
||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
||||
|
||||
public slots:
|
||||
@ -73,10 +74,13 @@ private:
|
||||
void processStreamData(const QString &requestId, const QJsonObject &data);
|
||||
void handleMessageComplete(const QString &requestId);
|
||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
||||
void emitThinkingBlocks(const QString &requestId, OllamaMessage *message);
|
||||
|
||||
QHash<QodeAssist::LLMCore::RequestID, OllamaMessage *> m_messages;
|
||||
QHash<QodeAssist::LLMCore::RequestID, QUrl> m_requestUrls;
|
||||
QHash<QodeAssist::LLMCore::RequestID, QJsonObject> m_originalRequests;
|
||||
QSet<QString> m_thinkingEmitted;
|
||||
QSet<QString> m_thinkingStarted;
|
||||
Tools::ToolsManager *m_toolsManager;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user