此项目主要使用ESP32-S3实现一个AI语音聊天助手,可以通过该项目熟悉ESP32-S3 arduino的开发,百度语音识别,语音合成API调用,百度文心一言大模型API的调用方法,音频的录制及播放,SD卡的读写,Wifi的配置(smartconfig方式)等基本开发方法。本项目的所有软硬件工程开源,并配备了详细的教程文档,和对应的视频教程,对零基础的同学非常适用,希望能够帮助到大家。
项目开源网址:
https://gitee.com/chging/esp32s3-ai-chat
新增 esp32实现deepseek api访问

在这里插入图片描述

硅基流动的deepseek R1 API免费访问

1、官网api 密钥获取

1.1官方网址

1.2账号登陆

点击右侧Log in,注册登录一个自己的账号。

输入手机号注册登录。

一般这里登录成功后,会直接跳入到模型广场界面。

1.3api 密钥获取步骤

在模型广场中,可以选择一个免费的蒸馏模型进行测试。如下图,选择模型点击进去。

点击在线体验

进入测试对话界面,可以直接输入提示词进行对话测试。

点击API密钥。

点击新建API密钥,填入描述信息,点击新建密钥。

API密钥生成成功。

2、arduino实现API访问

// 调用DeepSeek API
String DeepSeek_Get(const char* api_key, String query) {
  HTTPClient http;

  // 构建请求头
  http.begin("https://api.siliconflow.cn/v1/chat/completions");
  http.setTimeout(15000);
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Authorization", "Bearer " + String(api_key));

  // 构建JSON请求体
  DynamicJsonDocument doc(3072); // 根据响应长度可能需要调整大小
  doc["model"] = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B";
  
  JsonArray messages = doc.createNestedArray("messages");
  JsonObject message = messages.createNestedObject();
  message["role"] = "user";
  message["content"] = query; // 使用传入的查询参数

  doc["stream"] = false;
  doc["max_tokens"] = 512;
  doc["temperature"] = 0.7;
  doc["top_p"] = 0.7;
  doc["top_k"] = 50;
  doc["frequency_penalty"] = 0.5;
  doc["n"] = 1;

  JsonArray stop = doc.createNestedArray("stop");
  stop.add("null");

  JsonObject response_format = doc.createNestedObject("response_format");
  response_format["type"] = "text";

  // 序列化JSON
  String requestBody;
  serializeJson(doc, requestBody);
  Serial.println("Request Body:\n" + requestBody);

  // 发送POST请求
  int httpCode = http.POST(requestBody);
  String response = "";

  // 处理响应
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    Serial.println("Raw Response:\n" + payload);

    // 解析JSON响应
    DynamicJsonDocument resDoc(3072);
    DeserializationError error = deserializeJson(resDoc, payload);
    
    if (!error) {
      if (resDoc.containsKey("choices")) {
        response = resDoc["choices"][0]["message"]["content"].as<String>();
      } else {
        Serial.println("No 'choices' in response");
      }
    } else {
      Serial.print("JSON parse failed: ");
      Serial.println(error.c_str());
    }
  } else {
    Serial.printf("HTTP Error: %d - %s\n", httpCode, http.errorToString(httpCode).c_str());
  }

  http.end();
  return response;
}
Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐