#include <WiFi.
h>
#include <HTTPClient.h>
const char* ssid = "nandhu yadav";
const char* password = "nandhu56";
const char* chatgpt_token = "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";
const char* chatgpt_Q = "\"Who are you\"";
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
while(!Serial);
// Wait for WiFi connection
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin("https://api.openai.com/v1/completions")) { // HTTPS
https.addHeader("Content-Type", "application/json");
String token_key = String("Bearer ") + chatgpt_token;
https.addHeader("Authorization", token_key);
String payload = String("{\"model\": \"gpt-3.5-turbo-instruct\", \"prompt\": ")
+ chatgpt_Q + String(", \"temperature\": 0, \"max_tokens\": 7}"); // JSON Payload
Serial.print("[HTTPS] POST...\n");
// Start connection and send HTTP header
int httpCode = https.POST(payload);
// httpCode will be negative on error
// File found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String response = https.getString();
Serial.println(response);
} else {
Serial.printf("[HTTPS] POST... failed, error: %s\n",
https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
Serial.println("Wait 10s before next round...");
delay(10000);