How do i add a model parameter to an API request using c++

I am trying to use GPT-3 with an esp32 module.
I am using the HTTPClient.h library, and managed to successfully authenticate, but i can’t figure out how to add the model parameter to the request.
any help would be appreciated!

here is the error i am getting:
{
“error”: {
“message”: “you must provide a model parameter”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}

Here is the code i am using:

#include “WiFi.h”
#include “HTTPClient.h”
#include “base64.h”

const char* ssid = “MYSSID”;
const char* password = “MYPASSWORD”;

String authUsername = “”;
String authPassword = “MY_API_KEY”;
void setup() {

Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}

Serial.println(“Connected to WiFi”);

}

void loop() {

if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status

HTTPClient http;

http.begin("https://api.openai.com/v1/completions");

String auth = base64::encode(authUsername + ":" + authPassword);
http.addHeader("Authorization", "Basic " + auth);
http.addHeader("Content-Type", "application/json");


int httpCode = http.GET(); 

if (httpCode > 0) { //Check for the returning code

    String payload = http.getString();
    Serial.println(httpCode);
    Serial.println(payload);
}

else {
  Serial.println("Error on HTTP request");
}

http.end();

}

delay(10000);

}

Hi There,

Were you ever able to solve this problem? I am planning to explore a similar workflow and am curious to hear about other’s results.

Thanks!

You need to send a JSON payload with that and other info like frequency_penalty, temperature, etc…

{“model”:“curie”,“prompt”:"Text: hello this is a nice bit of text. \n\n Keywords: "}

You miss Authorizatioin header in the request.

All API requests should include your API key in an Authorization HTTP header as follows:

Authorization: Bearer YOUR_API_KEY

A proper payload in the request can be found in the Playground.

I have!
this is how i did it:

String body = “{"model": "text-davinci-002","prompt": "” + input + “","max_tokens": 20,"temperature": 0.9}”; //The body of the API request.
http.POST(body); //Post the body of the API request via HTTP.

Thanks so much for the response, @noamthebuk! This worked very well.

Think I’m probably going to switch this project over to MicroPython for easier compatibility with a few other things, but this demo was an awesome proof of concept.

1 Like