mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-10-06 12:05:35 -04:00
feat: Add tools, fabric and executable handler (#230)
This commit is contained in:
88
llmcore/BaseTool.cpp
Normal file
88
llmcore/BaseTool.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "BaseTool.hpp"
|
||||
|
||||
namespace QodeAssist::LLMCore {
|
||||
|
||||
BaseTool::BaseTool(QObject *parent)
|
||||
: QObject(parent)
|
||||
{}
|
||||
|
||||
QJsonObject BaseTool::getDefinition(ToolSchemaFormat format) const
|
||||
{
|
||||
QJsonObject properties;
|
||||
QJsonObject filenameProperty;
|
||||
filenameProperty["type"] = "string";
|
||||
filenameProperty["description"] = "The filename or relative path to read";
|
||||
properties["filename"] = filenameProperty;
|
||||
|
||||
QJsonObject definition;
|
||||
definition["type"] = "object";
|
||||
definition["properties"] = properties;
|
||||
|
||||
QJsonArray required;
|
||||
required.append("filename");
|
||||
definition["required"] = required;
|
||||
|
||||
switch (format) {
|
||||
case LLMCore::ToolSchemaFormat::OpenAI:
|
||||
definition = customizeForOpenAI(definition);
|
||||
break;
|
||||
case LLMCore::ToolSchemaFormat::Claude:
|
||||
definition = customizeForClaude(definition);
|
||||
break;
|
||||
case LLMCore::ToolSchemaFormat::Ollama:
|
||||
definition = customizeForOllama(definition);
|
||||
break;
|
||||
}
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
QJsonObject BaseTool::customizeForOpenAI(const QJsonObject &baseDefinition) const
|
||||
{
|
||||
QJsonObject function;
|
||||
function["name"] = name();
|
||||
function["description"] = description();
|
||||
function["parameters"] = baseDefinition;
|
||||
|
||||
QJsonObject tool;
|
||||
tool["type"] = "function";
|
||||
tool["function"] = function;
|
||||
|
||||
return tool;
|
||||
}
|
||||
|
||||
QJsonObject BaseTool::customizeForClaude(const QJsonObject &baseDefinition) const
|
||||
{
|
||||
QJsonObject tool;
|
||||
tool["name"] = name();
|
||||
tool["description"] = description();
|
||||
tool["input_schema"] = baseDefinition;
|
||||
|
||||
return tool;
|
||||
}
|
||||
|
||||
QJsonObject BaseTool::customizeForOllama(const QJsonObject &baseDefinition) const
|
||||
{
|
||||
return customizeForOpenAI(baseDefinition);
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::LLMCore
|
@ -19,31 +19,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QFuture>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
namespace QodeAssist::LLMCore {
|
||||
|
||||
class ITool : public QObject
|
||||
enum class ToolSchemaFormat { OpenAI, Claude, Ollama };
|
||||
|
||||
class BaseTool : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ITool(QObject *parent = nullptr)
|
||||
: QObject(parent)
|
||||
{}
|
||||
virtual ~ITool() = default;
|
||||
explicit BaseTool(QObject *parent = nullptr);
|
||||
~BaseTool() override = default;
|
||||
|
||||
virtual QString name() const = 0;
|
||||
virtual QString description() const = 0;
|
||||
virtual QJsonObject getDefinition() const = 0;
|
||||
virtual QString execute(const QJsonObject &input = QJsonObject()) = 0;
|
||||
virtual QJsonObject getDefinition(ToolSchemaFormat format) const;
|
||||
|
||||
signals:
|
||||
void toolStarted(const QString &toolName);
|
||||
void toolCompleted(const QString &toolName, const QString &result);
|
||||
void toolFailed(const QString &toolName, const QString &error);
|
||||
virtual QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) = 0;
|
||||
|
||||
protected:
|
||||
virtual QJsonObject customizeForOpenAI(const QJsonObject &baseDefinition) const;
|
||||
virtual QJsonObject customizeForClaude(const QJsonObject &baseDefinition) const;
|
||||
virtual QJsonObject customizeForOllama(const QJsonObject &baseDefinition) const;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::LLMCore
|
@ -17,8 +17,8 @@ add_library(LLMCore STATIC
|
||||
HttpClient.hpp HttpClient.cpp
|
||||
DataBuffers.hpp
|
||||
SSEBuffer.hpp SSEBuffer.cpp
|
||||
ITool.hpp
|
||||
IToolsFactory.hpp
|
||||
BaseTool.hpp
|
||||
BaseTool.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(LLMCore
|
||||
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QList>
|
||||
|
||||
#include "ITool.hpp"
|
||||
|
||||
namespace QodeAssist::LLMCore {
|
||||
|
||||
class IToolsFactory
|
||||
{
|
||||
public:
|
||||
virtual ~IToolsFactory() = default;
|
||||
virtual QList<ITool *> getAvailableTools() const = 0;
|
||||
virtual ITool *getToolByName(const QString &name) const = 0;
|
||||
virtual QJsonArray getToolsDefinitions() const = 0;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::LLMCore
|
Reference in New Issue
Block a user