mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-07-21 10:40:57 -04:00
refactor: Restructure project into sources/tests layout and dissolve PluginLLMCore
This commit is contained in:
126
sources/templates/OpenAIResponses.hpp
Normal file
126
sources/templates/OpenAIResponses.hpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// Copyright (C) 2024-2026 Petr Mironychev
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Additional attribution terms under GPLv3 §7(b) apply — see LICENSE
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "templates/PromptTemplate.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace QodeAssist::Templates {
|
||||
|
||||
class OpenAIResponses : public Templates::PromptTemplate
|
||||
{
|
||||
public:
|
||||
Templates::TemplateType type() const noexcept override
|
||||
{
|
||||
return Templates::TemplateType::Chat;
|
||||
}
|
||||
|
||||
QString name() const override { return "OpenAI Responses"; }
|
||||
|
||||
QStringList stopWords() const override { return {}; }
|
||||
|
||||
bool supportsToolHistory() const override { return true; }
|
||||
|
||||
void prepareRequest(
|
||||
QJsonObject &request, const LLMCore::ContextData &context) const override
|
||||
{
|
||||
if (context.systemPrompt) {
|
||||
request["instructions"] = context.systemPrompt.value();
|
||||
}
|
||||
|
||||
if (!context.history || context.history->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonArray input;
|
||||
for (const auto &msg : context.history.value()) {
|
||||
if (msg.role == "system") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!msg.toolCalls.isEmpty()) {
|
||||
if (!msg.content.isEmpty()) {
|
||||
input.append(QJsonObject{{"role", "assistant"}, {"content", msg.content}});
|
||||
}
|
||||
for (const auto &call : msg.toolCalls) {
|
||||
input.append(QJsonObject{
|
||||
{"type", "function_call"},
|
||||
{"call_id", call.id},
|
||||
{"name", call.name},
|
||||
{"arguments",
|
||||
QString::fromUtf8(
|
||||
QJsonDocument(call.arguments).toJson(QJsonDocument::Compact))}});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (msg.role == "tool") {
|
||||
input.append(QJsonObject{
|
||||
{"type", "function_call_output"},
|
||||
{"call_id", msg.toolCallId},
|
||||
{"output", msg.content}});
|
||||
continue;
|
||||
}
|
||||
|
||||
QJsonObject message;
|
||||
message["role"] = msg.role;
|
||||
|
||||
const bool hasImages = msg.images && !msg.images->isEmpty();
|
||||
|
||||
if (!hasImages) {
|
||||
message["content"] = msg.content;
|
||||
} else {
|
||||
QJsonArray content;
|
||||
if (!msg.content.isEmpty()) {
|
||||
content.append(
|
||||
QJsonObject{{"type", "input_text"}, {"text", msg.content}});
|
||||
}
|
||||
|
||||
for (const auto &image : msg.images.value()) {
|
||||
QJsonObject imgObj{{"type", "input_image"}, {"detail", "auto"}};
|
||||
if (image.isUrl) {
|
||||
imgObj["image_url"] = image.data;
|
||||
} else {
|
||||
imgObj["image_url"]
|
||||
= QString("data:%1;base64,%2").arg(image.mediaType, image.data);
|
||||
}
|
||||
content.append(imgObj);
|
||||
}
|
||||
|
||||
message["content"] = content;
|
||||
}
|
||||
|
||||
input.append(message);
|
||||
}
|
||||
|
||||
request["input"] = input;
|
||||
}
|
||||
|
||||
QString description() const override
|
||||
{
|
||||
return "Template for OpenAI Responses API:\n\n"
|
||||
"Simple request:\n"
|
||||
"{\n"
|
||||
" \"input\": \"<user message>\"\n"
|
||||
"}\n\n"
|
||||
"Multi-turn conversation:\n"
|
||||
"{\n"
|
||||
" \"instructions\": \"<system prompt>\",\n"
|
||||
" \"input\": [\n"
|
||||
" {\"role\": \"user\", \"content\": \"<message>\"}\n"
|
||||
" ]\n"
|
||||
"}";
|
||||
}
|
||||
|
||||
bool isSupportProvider(Providers::ProviderID id) const noexcept override
|
||||
{
|
||||
return id == QodeAssist::Providers::ProviderID::OpenAIResponses;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Templates
|
||||
Reference in New Issue
Block a user