Created
April 14, 2024 07:28
-
-
Save eggfly/ad2f6b7d195f83329aee9f28f13095a8 to your computer and use it in GitHub Desktop.
MiniMax.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include <ArduinoJson.h> | |
// flash mode 要改dio | |
// 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 = "你好,minimax!"; | |
String apiUrl = "https://api.minimax.chat/v1/text/chatcompletion_v2"; | |
String answer; | |
String getGPTAnswer(String inputText) { | |
HTTPClient http; | |
http.setTimeout(10000); | |
http.begin(apiUrl); | |
http.addHeader("Content-Type", "application/json"); | |
String token_key = String("Bearer ") + apiKey; | |
http.addHeader("Authorization", token_key); | |
String payload = "{\"model\":\"abab5.5s-chat\",\"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["choices"][0]["message"]["content"]; | |
return outputText; | |
// Serial.println(outputText); | |
} else { | |
http.end(); | |
Serial.printf("Error %i \n", httpResponseCode); | |
return "<error>"; | |
} | |
} | |
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); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment