//ChatGPT
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {
delay(500);
}
HTTPClient http;
http.begin("https://api.openai.com/v1/chat/completions");
http.addHeader("Authorization", "Bearer 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");
http.addHeader("Content-Type", "application/json");
//int httpCode = http.GET();
String body = "{\"model\": \"gpt-3.5-turbo\",\"messages\":[{\"role\": \"user\", \"content\": \"Say this is a test!\"}],\"temperature\": 0}"; //The body of the API request.
int httpCode=http.POST(body); //Post the body of the API request via HTTP.
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(payload);
DynamicJsonDocument doc(512);
deserializeJson(doc, payload);
Serial.print("Answer = ");
Serial.println(doc["choices"][0]["message"]["content"].as<String>());
}
else {
Serial.println("Error on HTTP request");
}
http.end();
}
void loop() {
}
/*
curl -X POST https://api.openai.com/v1/chat/completions -H "Authorization: Bearer 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" -H "Content-Type: application/json" -d "{\"model\": \"gpt-3.5-turbo\",\"messages\":[{\"role\": \"user\", \"content\": \"Say this is a test!\"}],\"temperature\": 0}"
*/