I’m trying to call the gpt-3.5 api in arduino ide using esp8266 device, I managed to do the first request, but when doing the second request it always shows the error message “EmptyInput”… I’ve tried formatting the parameters in postman and there is no problem, can someone explain what causes it?
sorry if my English is a bit messy, I’m using machine translation.
here’s the code I used
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
const char* ssid = "Noerr";
const char* password = "sampai8saja";
const char* apiKey = "sk-Xxxxxxxxxxxxxxxxxxxxxxx";
const char* host = "api.openai.com";
const char* temperature = "0";
const char* max_tokens = "45";
const char* model = "gpt-3.5-turbo";
const int port = 443; // Port HTTPS
BearSSL::WiFiClientSecure client;
bool initialPrompt = true;
String payload = "";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Connecting...");
}
Serial.println("Connected!");
client.setInsecure();
if (!client.connect(host, port)) {
Serial.println("Connect error!");
return;
}
Serial.println("Connected!");
}
void loop() {
Serial.print("Question: ");
while (!Serial.available()) {
delay(100);
}
String inputText = Serial.readStringUntil('\n');
while (Serial.available()) {
char add = Serial.read();
inputText = inputText + add;
delay(1);
}
int len = inputText.length();
inputText = inputText.substring(0);
// inputText = "\"" + inputText + "\"";
inputText.trim();
Serial.println(inputText);
//==================================================
String url = "https://api.openai.com/v1/chat/completions";
if(initialPrompt)
{
payload = "{\"model\": \"gpt-3.5-turbo\",\"messages\": [{\"role\": \"user\", \"content\": \"" + inputText + "\"}]}";
initialPrompt = false;
}
else{
payload = payload + ",{\"role\": \"user\", \"content\": \"" + inputText + "\"}]}";
}
client.println("POST " + String(url) + " HTTP/1.1");
client.println("Host: api.openai.com");
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(payload.length()));
client.println("Authorization: Bearer sk-Xxxxxxxxxxxxxxxxxxxxxxx");
client.println("Connection: close");
client.println();
client.println(payload);
Serial.println("Content-Length: " + String(payload.length()));
Serial.println(payload);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
while (client.available()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String response = "";
while (client.available()) {
char c = client.read();
response += c;
}
Serial.println("Received response: " + response);
client.stop();
JsonDocument doc;
DeserializationError error = deserializeJson(doc, response);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
String outputText = doc["choices"][0]["message"]["content"];
outputText.remove(outputText.indexOf('\n'));
Serial.print("CHATGPT: ");
Serial.println(outputText);
String returnResponse = "{\"role\": \"assistant\", \"content\": \"" + outputText + "\"}";
payload = removeEndOfString(payload);
payload = payload + "," + returnResponse;
}
String removeEndOfString(String originalString)
{
int stringLength = originalString.length();
String newString = originalString.substring(0, stringLength - 2);
return(newString);
}
Summary
This text will be hidden
here’s the output
Connecting…
Connecting…
Connected!
Connected!
Question: give one title of an interesting book
Content-Length: 109
{“model”: “gpt-3.5-turbo”,“messages”: [{“role”: “user”, “content”: “give one title of an interesting book”}]}
Received response: {
“id”: “chatcmpl-970Sg7tH4RT35cxzuwkMATX0bjy5J”,
“object”: “chat.completion”,
“created”: 1711457098,
“model”: “gpt-3.5-turbo-0125”,
“choices”: [
{
“index”: 0,
“message”: {
“role”: “assistant”,
“content”: “"The Power of Now"”
},
“logprobs”: null,
“finish_reason”: “stop”
}
],
“usage”: {
“prompt_tokens”: 14,
“completion_tokens”: 5,
“total_tokens”: 19
},
“system_fingerprint”: “fp_3bc1b5746c”
}
CHATGPT: “The Power of Now”
Question: what is the synopsis
Content-Length: 216
{“model”: “gpt-3.5-turbo”,“messages”: [{“role”: “user”, “content”: “give one title of an interesting book”},{“role”: “assistant”, “content”: ““The Power of Now””},{“role”: “user”, “content”: “what is the synopsis”}]}
Received response:
deserializeJson() failed: EmptyInput
Question: