Artificial Existences
Robots that hold endless conversations, using esp32 and Open AI API
Components and supplies
1
Cardboard
4
Mini LED 8x8 Matrix
1
PAM8302
1
esp32 WROOM32
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
API code
js
1#include <TTS.h> 2#include <WiFi.h> 3#include <HTTPClient.h> 4#include <ArduinoJson.h> 5 6TTS text2speech(25); 7 8const char* ssid = "your ssid"; 9const char* password = "your pass word"; 10const char* gptApiEndpoint = "https://api.openai.com/v1/chat/completions"; 11const char* apiKey = "your key"; 12String currentQuestion = "your question"; 13String chatHistory = "{\"model\": \"gpt-3.5-turbo-1106\", \"messages\": [{\"role\": \"user\", \"content\": \"What begins now is an infinite conversation. We're going to start a debate, like this. Your response to each of my questions should be opposite in stance.Your response structure should be different from mine. \"}, {\"role\": \"assistant\", \"content\": \"Got it.\"}, {\"role\": \"user\", \"content\": \"prompt\"}, {\"role\": \"assistant\", \"content\": \"prompt\"}, {\"role\": \"user\", \"content\": \"prompt\"}], \"temperature\": 0.7, \"max_tokens\": 2000}"; 14unsigned long lastRequestTime = 0; 15const unsigned long requestInterval = 20000; 16 17void setup() { 18 Serial.begin(115200); 19 delay(2000); 20 21 WiFi.begin(ssid, password); 22 Serial.print("Connecting to WiFi"); 23 while (WiFi.status() != WL_CONNECTED) { 24 delay(500); 25 Serial.print("."); 26 } 27 Serial.println("Connected to WiFi"); 28 29 Serial.println("Setup complete"); 30 31 sendQuestionToAPI(); 32} 33 34void loop() { 35 if (WiFi.status() == WL_CONNECTED) { 36 unsigned long currentMillis = millis(); 37 if (currentMillis - lastRequestTime >= requestInterval) { 38 sendQuestionToAPI(); 39 lastRequestTime = currentMillis; 40 } 41 } 42 43 delay(1000); 44} 45 46String extractContent(const String& data) { 47 int contentStart = data.indexOf("\"content\": \""); 48 if (contentStart == -1) { 49 return ""; // 未找到 content 字符串 50 } 51 contentStart += 13; 52 53 int contentEnd = data.indexOf("\"", contentStart); 54 if (contentEnd == -1) { 55 return ""; // 未找到結束引號 56 } 57 58 return data.substring(contentStart, contentEnd); 59} 60 61void sendQuestionToAPI() { 62 HTTPClient http; 63 http.begin(gptApiEndpoint); 64 http.addHeader("Content-Type", "application/json"); 65 http.addHeader("Authorization", "Bearer " + String(apiKey)); 66 67 String requestData = chatHistory; 68 69 int httpResponseCode = http.POST(requestData); 70 71 if (httpResponseCode > 0) { 72 Serial.print("HTTP Response code: "); 73 Serial.println(httpResponseCode); 74 75 String response = http.getString(); 76 Serial.println("Response:"); 77 Serial.println(response); 78 // Serial.println(typeof(response)); 79 String resep = extractContent(response); 80 81 const char *resp= resep.c_str(); 82 83 84 85 86 87 88 89 StaticJsonDocument<512> doc; 90 DeserializationError error = deserializeJson(doc, response); 91 if (!error) { 92 Serial.println(doc["choices"][0]["message"]["content"].as<String>()); 93 currentQuestion = doc["choices"][0]["message"]["content"].as<String>(); 94 } 95 else { 96 Serial.println("Error parsing JSON"); 97 } 98 } else { 99 Serial.print("Error on HTTP request: "); 100 Serial.println(httpResponseCode); 101 } 102 103 http.end(); 104}
Comments
Only logged in users can leave comments