当前位置: 首页 > article >正文

Qt接入deepseekv3 API 提供openssl 1.1.1g安装包

1.获取api (有免费10元额度)

DeepSeek

记得复制api,避免丢失频繁创建。

2.qt调用https请求

配置网络模块

QT += core gui widgets network

直接上代码

拿到代码替换api,和修正qt组件输入和输出即可。

#ifndef DEEPSEEKCLIENT_H
#define DEEPSEEKCLIENT_H

#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include<QJsonArray>
#include <QSslSocket>

namespace Ui {
class DeepSeekClient;
}

class DeepSeekClient : public QWidget
{
    Q_OBJECT

public:
    explicit DeepSeekClient(QWidget *parent = nullptr);
    ~DeepSeekClient();
    void sendRequest(const QString &prompt);


    void handleResponse(QNetworkReply *reply);

signals:
    void responseReceived(const QString &response);

private slots:
    void on_pushButton_clicked();

private:
    QNetworkAccessManager *manager;
    QString api_key = "sk-xxxx"; // 替换为你的API Key
private:
    Ui::DeepSeekClient *ui;
};

#endif // DEEPSEEKCLIENT_H
#include "deepseekclient.h"
#include "ui_deepseekclient.h"
#include <QDebug>
#include"log/easylogging++.h"

DeepSeekClient::DeepSeekClient(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeepSeekClient)
{
    ui->setupUi(this);
    manager = new QNetworkAccessManager(this);
    
    // 设置样式表
    QString styleSheet = R"(
        QWidget {
            background-color: #f5f5f5;
            font-family: "Microsoft YaHei", Arial;
        }
        QTextEdit {
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 8px;
            font-size: 14px;
        }
        QLineEdit {
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 4px 12px;
            font-size: 14px;
        }
        QLineEdit:focus {
            border: 1px solid #4a90e2;
        }
        QPushButton {
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: bold;
        }
        QPushButton:hover {
            background-color: #357abd;
        }
        QPushButton:pressed {
            background-color: #2a5f96;
        }
    )";
    this->setStyleSheet(styleSheet);

    // 检查SSL支持
    qDebug() << "SSL Support:" << QSslSocket::supportsSsl();
    qDebug() << "Build Version:" << QSslSocket::sslLibraryBuildVersionString();
    qDebug() << "Runtime Version:" << QSslSocket::sslLibraryVersionString();

    qDebug() << manager->supportedSchemes();
}

DeepSeekClient::~DeepSeekClient()
{
    delete ui;
}

///
/// dpseek发送数据
/// \param prompt
///
void DeepSeekClient::sendRequest(const QString &prompt) {

    // 显示思考状态
    QString userInput = ui->dpseek_input_edit->text();
    ui->dpseek_output_edit->append(QString("<div style='margin: 8px 0;'><b style='color: #4a90e2;'>您:</b> %1</div>").arg(userInput));
    ui->dpseek_output_edit->append(QString("<div style='margin: 8px 0; color: #666;'><i>AI正在思考中,请稍候...</i></div>"));
    
    // 禁用输入和发送按钮
    ui->dpseek_input_edit->setEnabled(false);
    ui->pushButton->setEnabled(false);

    QUrl url("https://api.deepseek.com/chat/completions");
    QNetworkRequest request(url);

    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setRawHeader("Authorization", ("Bearer " + api_key).toUtf8());

    QJsonObject body;
    body["model"] = "deepseek-chat";
    body["messages"] = QJsonArray{QJsonObject{{"role", "user"}, {"content", prompt}}};

    QNetworkReply *reply = manager->post(request, QJsonDocument(body).toJson());

    // 处理SSL错误
    connect(reply, &QNetworkReply::sslErrors, this, [reply]() {
        qDebug() << "SSL Errors occurred!";
        reply->ignoreSslErrors(); // 测试阶段忽略错误
    });

    connect(reply, &QNetworkReply::finished, [=]() {
        handleResponse(reply);
        reply->deleteLater();
    });
}
///
/// 接收数据,回调处理
/// \param reply
///
void  DeepSeekClient::handleResponse(QNetworkReply *reply) {
     // 重新启用输入和发送按钮
    ui->dpseek_input_edit->setEnabled(true);
    ui->pushButton->setEnabled(true);
    
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray response = reply->readAll();
        QJsonDocument doc = QJsonDocument::fromJson(response);
        QJsonObject json = doc.object();

        if (json.contains("choices")) {
            QString result = json["choices"].toArray()[0]
                .toObject()["message"].toObject()["content"].toString();
            
            // 删除"正在思考"的提示
            QTextCursor cursor = ui->dpseek_output_edit->textCursor();
            cursor.movePosition(QTextCursor::End);
            cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);
            cursor.removeSelectedText();
            cursor.deletePreviousChar(); // 删除多余的换行
            
            // 显示AI回复
            ui->dpseek_output_edit->append(QString("<div style='margin: 8px 0; background-color: #f8f9fa; padding: 8px; border-radius: 4px;'><b style='color: #28a745;'>AI:</b> %1</div>").arg(result));
            
            // 清空输入框
            ui->dpseek_input_edit->clear();
            
            emit responseReceived(result);
        }
    } else {
        // 删除"正在思考"的提示
        QTextCursor cursor = ui->dpseek_output_edit->textCursor();
        cursor.movePosition(QTextCursor::End);
        cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);
        cursor.removeSelectedText();
        cursor.deletePreviousChar(); // 删除多余的换行
        
        QString errorMessage = QString("<div style='color: #dc3545; margin: 8px 0;'><b>错误:</b> %1</div>").arg(reply->errorString());
        ui->dpseek_output_edit->append(errorMessage);
    }
}
void DeepSeekClient::on_pushButton_clicked()
{
    LOG(INFO)<<"INPUT: "<<ui->dpseek_input_edit->text().toStdString();
    sendRequest(ui->dpseek_input_edit->text());
}

3.测试

把qt的界面组件输入输出替换即可

坑点!!

ssl1.1.1g报错处理,主要就是openssl版本兼容性问题导致,必须使用一致的dll版本。导致ssl不支持

官网还找不到了openssl 1.1.1g的安装包。

这里我提供安装包

通过网盘分享的文件:OpenSSL 1.1.1g-所需dll动态库文件.zip
链接: https://pan.baidu.com/s/16m5mmyd6J2LTeIIwRdDp1w 提取码: u3s7 
--来自百度网盘超级会员v6的分享

把dll文件放到目录的build后面即可 

学习资料分享

0voice · GitHub


http://www.kler.cn/a/546390.html

相关文章:

  • win11 MBR 启动 如何把我的硬盘改 GPT win11 的 UEFI 启动
  • Vulhub靶机 ActiveMQ任意 文件写入(CVE-2016-3088)(渗透测试详解)
  • 使用爬虫获取1688商品分类:实战案例指南
  • PMP冲刺每日一题(8)
  • Java 语言深度剖析与实践应用
  • 一文深入了解DeepSeek-R1:模型架构
  • Baumer工业相机堡盟工业相机如何通过NEOAPI SDK实现一次触发控制三个光源开关分别采集三张图像(C#)
  • 基础网络详解4--HTTP CookieSession 思考 2
  • S4D480 S4HANA 基于PDF的表单打印
  • FFmpeg中时长的表示方式
  • 论文笔记:Multi-Head Mixture-of-Experts
  • 数据库开发常识(10.6)——考量使用临时表及表连接写法(3)
  • 聊一聊FutureTask源码中体现的“自旋锁”思想
  • 10G EPON光模块
  • 【Matlab算法】基于人工势场的多机器人协同运动与避障算法研究(附MATLAB完整代码)
  • 交叉编译foxy版ros2部署到ARM上运行
  • Linux入侵检查流程
  • filebeat抓取nginx日志
  • Python实现文件夹监控:自动捕获并处理新增Excel文件,支持子文件夹遍历
  • 【Linux】Ubuntu Linux 系统——Node.js 开发环境