【电机控制器】ESP32-C3语言模型——通义千问

【电机控制器】ESP32-C3语言模型——通义千问 @TOC 前言 使用工具: 提示:以下是本篇文章正文内容,下面案例可供参考 一、简介 二、代码 #include #include #include // 1. Replace with your network credentials const char* ssid = "你需要改的地方"; const char* password = "你需要改的地方"; // 2. Replace with your OpenAI API key const char* apiKey = "你需要改的地方"; // Send request to OpenAI API String inputText = "你好,通义千问!"; String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"; String answer; String getGPTAnswer(String inputText) { HTTPClient http; http.setTimeout(10000); http.begin(apiUrl); http.addHeader("Content-Type", "application/json"); http.addHeader("Authorization", String(apiKey)); String payload = "{\"model\":\"qwen-turbo\",\"input\":{\"messages\":[{\"role\": \"system\",\"content\": \"你是鹏鹏的生活助手机器人,要求下面的回答严格控制在256字符以内。\"},{\"role\": \"user\",\"content\": \"" + inputText + "\"}]}}"; int httpResponseCode = http.POST(payload); if (httpResponseCode == 200) { String response = http.getString(); http.end(); Serial.println(response); // Parse JSON response DynamicJsonDocument jsonDoc(1024); deserializeJson(jsonDoc, response); String outputText = jsonDoc["output"]["text"]; return outputText; // Serial.println(outputText); } else { http.end(); Serial.printf("Error %i \n", httpResponseCode); return ""; } } void setup() { // Initialize Serial Serial.begin(115200); // Connect to Wi-Fi network WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); answer = getGPTAnswer(inputText); Serial.println("Answer: " + answer); Serial.println("Enter a prompt:"); } void loop() { // do nothing if (Serial.available()) { inputText = Serial.readStringUntil('\n'); // inputText.trim(); Serial.println("\n Input:"+inputText); answer = getGPTAnswer(inputText); Serial.println("Answer: " + answer); Serial.println("Enter a prompt:"); } // delay(2); } 三、实验结果 通义千问也抽风 四、参考资料 通过API使用通义千问 【ESP32接入语言大模型之通义千问】 总结 本文仅仅简单介绍了【电机控制器】ESP32-C3语言模型——通义千问,评论区欢迎讨论。

Mar 28, 2025 - 04:15
 0
【电机控制器】ESP32-C3语言模型——通义千问

【电机控制器】ESP32-C3语言模型——通义千问

@TOC

前言

使用工具:

提示:以下是本篇文章正文内容,下面案例可供参考

一、简介

二、代码

#include 
#include 
#include 

// 1. Replace with your network credentials
const char* ssid = "你需要改的地方";
const char* password = "你需要改的地方";

// 2. Replace with your OpenAI API key
const char* apiKey = "你需要改的地方";

// Send request to OpenAI API
String inputText = "你好,通义千问!";
String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

String answer;
String getGPTAnswer(String inputText) {
  HTTPClient http;
  http.setTimeout(10000);
  http.begin(apiUrl);
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Authorization", String(apiKey));
  String payload = "{\"model\":\"qwen-turbo\",\"input\":{\"messages\":[{\"role\": \"system\",\"content\": \"你是鹏鹏的生活助手机器人,要求下面的回答严格控制在256字符以内。\"},{\"role\": \"user\",\"content\": \"" + inputText + "\"}]}}";
  int httpResponseCode = http.POST(payload);
  if (httpResponseCode == 200) {
    String response = http.getString();
    http.end();
    Serial.println(response);

    // Parse JSON response
    DynamicJsonDocument jsonDoc(1024);
    deserializeJson(jsonDoc, response);
    String outputText = jsonDoc["output"]["text"];
    return outputText;
    // Serial.println(outputText);
  } else {
    http.end();
    Serial.printf("Error %i \n", httpResponseCode);
    return "";
  }
}

void setup() {
  // Initialize Serial
  Serial.begin(115200);

  // Connect to Wi-Fi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  answer = getGPTAnswer(inputText);
  Serial.println("Answer: " + answer);
  Serial.println("Enter a prompt:");

}

void loop() {
  // do nothing
  if (Serial.available()) {
    inputText = Serial.readStringUntil('\n');
    // inputText.trim();
    Serial.println("\n Input:"+inputText);

    answer = getGPTAnswer(inputText);
    Serial.println("Answer: " + answer);
    Serial.println("Enter a prompt:");
  }
  // delay(2);
}



三、实验结果

通义千问也抽风
在这里插入图片描述

四、参考资料

通过API使用通义千问
【ESP32接入语言大模型之通义千问】

总结

本文仅仅简单介绍了【电机控制器】ESP32-C3语言模型——通义千问,评论区欢迎讨论。