File indexing completed on 2025-01-05 05:18:57
0001 // SPDX-FileCopyrightText: 2023 Loren Burkholder <computersemiexpert@outlook.com> 0002 // SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> 0003 // 0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 0006 #pragma once 0007 #include "kllmcore_export.h" 0008 #include <QDebug> 0009 #include <QJsonArray> 0010 0011 namespace KLLMCore 0012 { 0013 /** 0014 * @brief KLLMContext provides a representation of a conversation context. 0015 * 0016 * Most, if not all, LLMs have the concept of "context". This allows them to refer to previous messages in a conversation to 0017 * enhance their replies. In most scenarios, this is the preferred behavior. 0018 * 0019 * To use KLLMContext, you simply need to get the context from each KLLMReply and set it on the next KLLMReqeust that you 0020 * send. KLLMInterface will use this in KLLMInterface::getCompletion(). 0021 */ 0022 struct KLLMCORE_EXPORT KLLMContext { 0023 /** 0024 * @brief Converts the context to a JSON representation. 0025 * 0026 * Different LLM backends represent context in different ways; for example, while Ollama represents context as an array 0027 * of integer identifiers, OpenAI relies on a JSON array of all the messages in the conversation so far. Therefore, this 0028 * function exists to take any representation set on it for any backend and convert it to a JSON value suitable for 0029 * sending in a request. 0030 * 0031 * @return A JSON representation of the context. 0032 */ 0033 [[nodiscard]] QJsonValue toJson() const; 0034 0035 /** 0036 * @brief Sets an Ollama context as the current context. 0037 * @param context The context from Ollama. 0038 */ 0039 void setOllamaContext(const QJsonArray &context); 0040 0041 private: 0042 enum class Backend { 0043 Ollama, 0044 } m_backend; 0045 QVariant m_data; 0046 }; 0047 } 0048 KLLMCORE_EXPORT QDebug operator<<(QDebug d, const KLLMCore::KLLMContext &t);