@@ -8,21 +8,34 @@ void AIChatbot::begin(long baudRate) {
88}
99
1010bool AIChatbot::connectWiFi (const char * ssid, const char * password, unsigned long timeoutMs) {
11+ Serial.printf (" Connecting to WiFi: %s\n " , ssid);
1112 WiFi.begin (ssid, password);
12- unsigned long startAttemptTime = millis ();
1313
14+ unsigned long startAttemptTime = millis ();
1415 while (WiFi.status () != WL_CONNECTED && millis () - startAttemptTime < timeoutMs) {
15- delay (1000 );
16- Serial.println ( " Connecting to WiFi.. ." );
16+ delay (500 );
17+ Serial.print ( " ." );
1718 }
1819
1920 if (WiFi.status () == WL_CONNECTED) {
20- Serial.println (" Connected to WiFi" );
21+ Serial.println (" \n WiFi Connected!" );
22+ Serial.printf (" IP Address: %s\n " , WiFi.localIP ().toString ().c_str ());
2123 return true ;
2224 } else {
23- Serial.println (" Failed to connect to WiFi" );
25+ Serial.println (" \n WiFi Connection Failed." );
26+ return false ;
27+ }
28+ }
29+
30+ bool AIChatbot::validateKeys () {
31+ if (selectedAI == " chatgpt" && chatGPTApiKey.isEmpty ()) {
32+ Serial.println (" Error: ChatGPT API key is not set." );
33+ return false ;
34+ } else if (selectedAI == " huggingface" && huggingFaceApiKey.isEmpty ()) {
35+ Serial.println (" Error: Hugging Face API key is not set." );
2436 return false ;
2537 }
38+ return true ;
2639}
2740
2841void AIChatbot::update () {
@@ -46,6 +59,15 @@ void AIChatbot::setKey(const String& key, const String& aiName) {
4659void AIChatbot::selectAI (const String& aiName, const String& aiVersion) {
4760 selectedAI = aiName;
4861 selectedAIVersion = (aiVersion != " null" && aiVersion != " none" ) ? aiVersion : " gpt-3.5-turbo" ;
62+
63+ if (selectedAI == " chatgpt" && (selectedAIVersion != " gpt-3.5-turbo" && selectedAIVersion != " gpt-4" )) {
64+ Serial.println (" Warning: Unsupported ChatGPT version. Defaulting to gpt-3.5-turbo." );
65+ selectedAIVersion = " gpt-3.5-turbo" ;
66+ }
67+ if (selectedAI == " huggingface" && selectedAIVersion.isEmpty ()) {
68+ Serial.println (" Warning: Hugging Face version not set. Defaulting to the google/flan-t5-small." );
69+ selectedAIVersion = " google/flan-t5-small" ;
70+ }
4971}
5072
5173String AIChatbot::getResponse (const String& message) {
@@ -58,6 +80,24 @@ String AIChatbot::getResponse(const String& message) {
5880 }
5981}
6082
83+ String AIChatbot::sanitizeInput (const String& input) {
84+ String sanitized = input;
85+ sanitized.replace (" \\ " , " \\\\ " );
86+ sanitized.replace (" \" " , " \\\" " );
87+ sanitized.replace (" \n " , " \\ n" );
88+ sanitized.replace (" \r " , " \\ r" );
89+
90+ for (int i = 0 ; i < sanitized.length (); i++) {
91+ if (sanitized[i] < 32 ) { // ASCII 32
92+ sanitized.remove (i, 1 );
93+ i--;
94+ }
95+ }
96+
97+ return sanitized;
98+ }
99+
100+
61101String AIChatbot::sendToChatGPT (const String& message) {
62102 if (chatGPTApiKey.isEmpty ()) {
63103 return " ChatGPT API key not set." ;
@@ -74,30 +114,34 @@ String AIChatbot::sendToHuggingFace(const String& message) {
74114 return " Hugging Face API key not set." ;
75115 }
76116
117+ String sanitizedMessage = sanitizeInput (message);
77118 String url = " https://api-inference.huggingface.co/models/" + selectedAIVersion;
78- String payload = " {\" inputs\" : \" " + message + " \" }" ;
119+ String payload = " {\" inputs\" : \" " + sanitizedMessage + " \" }" ;
79120
80121 return makeHttpRequest (url, payload, huggingFaceApiKey);
81122}
82123
124+
83125String AIChatbot::makeHttpRequest (const String& url, const String& payload, const String& apiKey) {
84126 HTTPClient http;
85-
86- #ifdef ESP32
87127 WiFiClientSecure client;
88128 client.setInsecure ();
89- http.begin (client, url);
90- #elif defined(ESP8266)
91- WiFiClientSecure client;
92- client.setInsecure ();
93129 http.begin (client, url);
94- #endif
95130
96131 http.addHeader (" Content-Type" , " application/json" );
97132 http.addHeader (" Authorization" , " Bearer " + apiKey);
98133
99134 int httpResponseCode = http.POST (payload);
100- String response = httpResponseCode > 0 ? http.getString () : " Error on sending POST: " + String (httpResponseCode);
135+ String response = " " ;
136+
137+ if (httpResponseCode > 0 ) {
138+ response = http.getString ();
139+ Serial.printf (" HTTP Response code: %d\n Response body: %s\n " , httpResponseCode, response.c_str ());
140+ } else {
141+ response = " HTTP POST failed: " + String (http.errorToString (httpResponseCode).c_str ());
142+ Serial.println (response);
143+ Serial.printf (" Payload: %s\n " , payload.c_str ());
144+ }
101145
102146 http.end ();
103147 return response;
0 commit comments