-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini.cpp
69 lines (56 loc) · 1.72 KB
/
gemini.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const String gemini_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent";
String api_key = ""; // Don't store API key directly in code
WiFiClient client;
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");
}
bool connectWithApiKey(const String& key) {
api_key = key;
return true;
}
String sendPrompt(const String& prompt) {
if (api_key.length() == 0) {
Serial.println("Error: Missing API key");
return "";
}
DynamicJsonDocument doc(4096);
JsonArray contentArray = doc.createNestedArray("contents");
JsonObject partsObject = contentArray.createNestedObject();
JsonArray partsArray = partsObject.createNestedArray("parts");
JsonObject textObject = partsArray.createNestedObject();
textObject["text"] = prompt;
String jsonBody;
serializeJson(doc, jsonBody);
HTTPClient http;
String requestUrl = gemini_url + "?key=" + api_key;
http.begin(client, requestUrl);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonBody);
String response = "";
if (httpResponseCode > 0) {
response = http.getString();
http.end();
Serial.println("Response:");
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
return response;
}
void loop() {
String prompt = "Write a story about a magic backpack";
String response = sendPrompt(prompt);
delay(5000);
}