This project is inspired by olrea/openai-cpp, rewritten using Qt libraries. No need to configure curl - achieve cross-platform functionality with the powerful Qt framework.
本项目参考了olrea/openai-cpp,基于Qt库重写,无需配置curl,即可基于强大的Qt库实现跨平台。
*
Simple chat program: You need to configure token and base_url before calling. You can set the OPENAI_API_KEY environment variable before use (or set it directly in the code).
简单的聊天程序:调用前需要配置token和base_url,可在使用之前设置OPENAI_API_KEY环境变量(也可以直接在代码中设置)
#include "qopenai.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString token = ""; // your token
QString api_base_url = ""; // the base url like https://api.openai.com/v1/
QOpenAI ai(token, "QOpenAI", api_base_url);
QVariantMap request{
{ "model", "Qwen/Qwen3-8B" }, // the model name
{ "temperature", 0.7 },
};
QVariantList messages;
QVariantMap systemMsg;
systemMsg["role"] = "system";
systemMsg["content"] = R"(Say this is a test)";
messages.append(systemMsg);
QVariantMap userMsg;
userMsg["role"] = "user";
userMsg["content"] = "hello";
messages.append(userMsg);
request["messages"] = messages;
QVariantMap response = ai.chat.create(request);
qDebug().noquote() << response;
return a.exec();
}
Add to your CMakeLists.txt file:
add_subdirectory(QOpenAI)
include_directories(QOpenAI)
add_executable(${PROJECT_NAME}
main.cpp
)
target_link_libraries(${PROJECT_NAME} QOpenAI)
Include the header file:
#include "qopenai.h"
git clone --recursive https://github.com/s12mmm3/QOpenAI.git
cd QCloudMusicApi
cmake -B build
cmake --build build -j