mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-12-05 00:42:50 -05:00
feat: Add OpenAI Responses API (#282)
* feat: Add OpenAI Responses API * fix: Make temperature optional * chore: Increase default value of max tokens
This commit is contained in:
135
templates/OpenAIResponses.hpp
Normal file
135
templates/OpenAIResponses.hpp
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (C) 2024-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 "llmcore/PromptTemplate.hpp"
|
||||
#include "providers/OpenAIResponsesRequestBuilder.hpp"
|
||||
|
||||
namespace QodeAssist::Templates {
|
||||
|
||||
class OpenAIResponses : public LLMCore::PromptTemplate
|
||||
{
|
||||
public:
|
||||
LLMCore::TemplateType type() const noexcept override
|
||||
{
|
||||
return LLMCore::TemplateType::Chat;
|
||||
}
|
||||
|
||||
QString name() const override { return "OpenAI Responses"; }
|
||||
|
||||
QStringList stopWords() const override { return {}; }
|
||||
|
||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
||||
{
|
||||
using namespace QodeAssist::OpenAIResponses;
|
||||
RequestBuilder builder;
|
||||
|
||||
if (context.systemPrompt) {
|
||||
builder.setInstructions(context.systemPrompt.value());
|
||||
}
|
||||
|
||||
if (!context.history || context.history->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &history = context.history.value();
|
||||
|
||||
for (const auto &msg : history) {
|
||||
if (msg.role == "system") {
|
||||
continue;
|
||||
}
|
||||
|
||||
Message message;
|
||||
message.role = roleFromString(msg.role);
|
||||
|
||||
if (msg.images && !msg.images->isEmpty()) {
|
||||
const auto &images = msg.images.value();
|
||||
message.content.reserve(1 + images.size());
|
||||
|
||||
if (!msg.content.isEmpty()) {
|
||||
message.content.append(MessageContent(InputText{msg.content}));
|
||||
}
|
||||
|
||||
for (const auto &image : images) {
|
||||
InputImage imgInput;
|
||||
imgInput.detail = "auto";
|
||||
|
||||
if (image.isUrl) {
|
||||
imgInput.imageUrl = image.data;
|
||||
} else {
|
||||
imgInput.imageUrl
|
||||
= QString("data:%1;base64,%2").arg(image.mediaType, image.data);
|
||||
}
|
||||
|
||||
message.content.append(MessageContent(std::move(imgInput)));
|
||||
}
|
||||
} else {
|
||||
message.content.append(MessageContent(msg.content));
|
||||
}
|
||||
|
||||
builder.addMessage(std::move(message));
|
||||
}
|
||||
|
||||
const QJsonObject builtRequest = builder.toJson();
|
||||
for (auto it = builtRequest.constBegin(); it != builtRequest.constEnd(); ++it) {
|
||||
request[it.key()] = it.value();
|
||||
}
|
||||
}
|
||||
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"
|
||||
"}\n\n"
|
||||
"Uses type-safe RequestBuilder for OpenAI Responses API.";
|
||||
}
|
||||
bool isSupportProvider(LLMCore::ProviderID id) const noexcept override
|
||||
{
|
||||
return id == QodeAssist::LLMCore::ProviderID::OpenAIResponses;
|
||||
}
|
||||
|
||||
private:
|
||||
static QodeAssist::OpenAIResponses::Role roleFromString(const QString &roleStr) noexcept
|
||||
{
|
||||
using namespace QodeAssist::OpenAIResponses;
|
||||
|
||||
if (roleStr == "user")
|
||||
return Role::User;
|
||||
if (roleStr == "assistant")
|
||||
return Role::Assistant;
|
||||
if (roleStr == "system")
|
||||
return Role::System;
|
||||
if (roleStr == "developer")
|
||||
return Role::Developer;
|
||||
|
||||
return Role::User;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Templates
|
||||
|
||||
@ -29,6 +29,7 @@
|
||||
#include "templates/Ollama.hpp"
|
||||
#include "templates/OpenAI.hpp"
|
||||
#include "templates/OpenAICompatible.hpp"
|
||||
#include "templates/OpenAIResponses.hpp"
|
||||
// #include "templates/CustomFimTemplate.hpp"
|
||||
// #include "templates/DeepSeekCoderFim.hpp"
|
||||
#include "templates/GoogleAI.hpp"
|
||||
@ -49,6 +50,7 @@ inline void registerTemplates()
|
||||
templateManager.registerTemplate<CodeLlamaFim>();
|
||||
templateManager.registerTemplate<Claude>();
|
||||
templateManager.registerTemplate<OpenAI>();
|
||||
templateManager.registerTemplate<OpenAIResponses>();
|
||||
templateManager.registerTemplate<MistralAIFim>();
|
||||
templateManager.registerTemplate<MistralAIChat>();
|
||||
templateManager.registerTemplate<CodeLlamaQMLFim>();
|
||||
|
||||
Reference in New Issue
Block a user