【电机控制器】ESP32-C3语言模型——DeepSeek
文章目录
- @[TOC](文章目录)
- 前言
- 一、简介
- 二、代码
- 三、实验结果
- 四、参考资料
- 总结
文章目录
- @[TOC](文章目录)
- 前言
- 一、简介
- 二、代码
- 三、实验结果
- 四、参考资料
- 总结
前言
使用工具:
提示:以下是本篇文章正文内容,下面案例可供参考
一、简介
二、代码
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
// 替换为您的 WiFi 凭据
const char *ssid = "你需要改的地方";
const char *password = "你需要改的地方";
// 替换为您的 DeepSeek API 密钥
const char* apiKey = "你需要改的地方";
// DeepSeek API 端点
const char* host = "api.deepseek.com";
const int httpsPort = 443;
// 创建 WiFiClientSecure 对象
WiFiClientSecure client;
// 设置超时时间 (单位:毫秒)
const unsigned long timeout = 10000;
// 对话历史
const int maxHistory = 10; // 最大对话轮次
String conversationHistory[maxHistory]; // 存储对话历史
int historyIndex = 0; // 当前对话历史索引
// 函数声明
void connectToWiFi();
String askDeepSeek(String question);
void printResponse(String response);
void addToHistory(String role, String content);
void printHistory();
void setup() {
Serial.begin(115200);
// 连接到 WiFi
connectToWiFi();
// 关闭证书鉴权
client.setInsecure();
Serial.println("初始化完成,请输入您的问题:");
}
void loop() {
// 检查串口是否有输入
if (Serial.available()) {
String question = Serial.readStringUntil('\n');
question.trim(); // 去除换行符和空格
if (question.length() > 0) {
// 将用户问题添加到对话历史
addToHistory("user", question);
Serial.println("正在向 DeepSeek 提问...");
String response = askDeepSeek(question);
printResponse(response);
// 将模型回复添加到对话历史
addToHistory("assistant", response);
// 打印当前对话历史
printHistory();
Serial.println("\n请输入下一个问题:");
}
}
}
// 连接到 WiFi
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("正在连接到 WiFi...");
}
Serial.println("已连接到 WiFi");
}
// 向 DeepSeek 提问
String askDeepSeek(String question) {
String response = "";
// 连接到 DeepSeek API
if (!client.connect(host, httpsPort)) {
Serial.println("连接失败");
return "连接失败";
}
// 构建请求
String request = "POST /v1/chat/completions HTTP/1.1\r\n";
request += "Host: " + String(host) + "\r\n";
request += "Authorization: Bearer " + String(apiKey) + "\r\n";
request += "Content-Type: application/json\r\n";
request += "Connection: close\r\n";
// 构建请求体
DynamicJsonDocument doc(1024);
doc["model"] = "deepseek-chat";
doc["stream"] = true;
// 添加对话历史
JsonArray messages = doc.createNestedArray("messages");
for (int i = 0; i < historyIndex; i++) {
JsonObject message = messages.createNestedObject();
message["role"] = i % 2 == 0 ? "user" : "assistant"; // 交替用户和助手角色
message["content"] = conversationHistory[i];
}
// 添加当前问题
JsonObject newMessage = messages.createNestedObject();
newMessage["role"] = "user";
newMessage["content"] = question;
String requestBody;
serializeJson(doc, requestBody);
request += "Content-Length: " + String(requestBody.length()) + "\r\n\r\n";
request += requestBody;
// 发送请求
client.print(request);
// 记录开始时间
unsigned long startTime = millis();
// 流式接收响应
while (client.connected()) {
// 检查超时
if (millis() - startTime > timeout) {
Serial.println("响应超时");
break;
}
// 读取数据
while (client.available()) {
String line = client.readStringUntil('\n');
if (line.startsWith("data: ")) {
String jsonData = line.substring(6);
DynamicJsonDocument doc(1024);
deserializeJson(doc, jsonData);
// 提取回复内容
if (doc.containsKey("choices")) {
String content = doc["choices"][0]["delta"]["content"];
response += content;
}
// 提取思维链内容(假设字段为 "reasoning")
if (doc.containsKey("choices") && doc["choices"][0].containsKey("delta") && doc["choices"][0]["delta"].containsKey("reasoning")) {
String reasoning = doc["choices"][0]["delta"]["reasoning"];
Serial.println("思维链: " + reasoning);
}
}
}
}
// 断开连接
client.stop();
return response;
}
// 打印回复内容
void printResponse(String response) {
Serial.println("DeepSeek 回复:");
Serial.println(response);
}
// 添加对话历史
void addToHistory(String role, String content) {
if (historyIndex < maxHistory) {
conversationHistory[historyIndex] = content;
historyIndex++;
} else {
// 如果历史记录已满,移除最早的记录
for (int i = 0; i < maxHistory - 1; i++) {
conversationHistory[i] = conversationHistory[i + 1];
}
conversationHistory[maxHistory - 1] = content;
}
}
// 打印对话历史
void printHistory() {
Serial.println("\n当前对话历史:");
for (int i = 0; i < historyIndex; i++) {
Serial.println((i % 2 == 0 ? "用户: " : "助手: ") + conversationHistory[i]);
}
}
三、实验结果
回复结果为空,deepseek最近的服务器看起来情况不太好啊 - -
四、参考资料
【ESP32接入国产大模型之Deepseek】
立创开发板入门ESP32C3第八课 修改AI大模型接口为deepseek3接口
总结
本文仅仅简单介绍了【电机控制器】ESP32-C3语言模型——DeepSeek,评论区欢迎讨论。