22 * Project ChatGPT Client
33 * Description: The official method using API Key for communication with ChatGPT
44 * Author: Eric Nam
5- * Date: 05-19 -2024
5+ * Date: 07-17 -2024
66 */
77
88#ifndef __CHATGPT__
@@ -22,7 +22,6 @@ class ChatGPT
2222 * - api_key_in: The API key for authentication.
2323 * - timeout_in: The timeout duration for requests.
2424 */
25- ChatGPT () {}
2625
2726 ChatGPT (T *_client,
2827 const char *api_version_in,
@@ -50,6 +49,7 @@ class ChatGPT
5049 * - max_tokens: The maximum number of tokens to generate.
5150 * - content_only: If true, extracts and returns only the content from the response.
5251 * - result: Stores the response from the API.
52+ * - mem_dynamic: Select Dynamic/Static Memory, (Default: Dyanmic Memory)
5353 *
5454 * Returns:
5555 * - True if the request is successful, false otherwise.
@@ -64,20 +64,46 @@ class ChatGPT
6464 const char *detail,
6565 int max_tokens,
6666 bool content_only,
67- String &result)
67+ String &result,
68+ bool mem_dynamic = true )
6869 {
69- int post_body_size = snprintf (nullptr , 0 ,
70- " {\" model\" : \" %s\" , \" messages\" : [{\" role\" : \" %s\" , \" content\" : [{\" type\" : \" %s\" , \" text\" : \" %s\" }, {\" type\" : \" %s\" , \" image_url\" : {\" url\" : \" %s\" , \" detail\" : \" %s\" }}]}], \" max_tokens\" : %d}" ,
71- model, role, type, text, image_type, image_url, detail, max_tokens) +
72- 1 ;
70+ char *post_body = nullptr ; // Initialize post_body pointer
71+ int post_body_size = 0 ;
72+ if (mem_dynamic)
73+ {
74+ // Calculate the required size for dynamic allocation
75+ post_body_size = snprintf (nullptr , 0 ,
76+ " {\" model\" : \" %s\" , \" messages\" : [{\" role\" : \" %s\" , \" content\" : [{\" type\" : \" %s\" , \" text\" : \" %s\" }, {\" type\" : \" %s\" , \" image_url\" : {\" url\" : \" %s\" , \" detail\" : \" %s\" }}]}], \" max_tokens\" : %d}" ,
77+ model, role, type, text, image_type, image_url, detail, max_tokens) + 1 ;
78+ post_body = new char [post_body_size];
79+ if (post_body == nullptr )
80+ {
81+ result = " [ERR] Memory allocation failed!" ;
82+ return false ;
83+ }
84+ }
85+ else
86+ {
87+ // Use a static buffer with a fixed size
88+ static const int static_buffer_size = 512 ;
89+ char static_post_body[static_buffer_size];
90+ post_body_size = static_buffer_size;
91+ post_body = static_post_body;
92+ }
7393
74- char *post_body = new char [post_body_size];
94+ // Format the post_body string
7595 snprintf (post_body, post_body_size,
7696 " {\" model\" : \" %s\" , \" messages\" : [{\" role\" : \" %s\" , \" content\" : [{\" type\" : \" %s\" , \" text\" : \" %s\" }, {\" type\" : \" %s\" , \" image_url\" : {\" url\" : \" %s\" , \" detail\" : \" %s\" }}]}], \" max_tokens\" : %d}" ,
7797 model, role, type, text, image_type, image_url, detail, max_tokens);
7898
99+ // Call the _post function
79100 bool success = _postStream (post_body, content_only, result);
80- delete[] post_body;
101+
102+ // Free dynamic memory if allocated
103+ if (mem_dynamic)
104+ {
105+ delete[] post_body;
106+ }
81107 return success;
82108 }
83109
@@ -92,6 +118,7 @@ class ChatGPT
92118 * - max_tokens: The maximum number of tokens to generate.
93119 * - content_only: If true, extracts and returns only the content from the response.
94120 * - result: Stores the response from the API.
121+ * - mem_dynamic: Select Dynamic/Static Memory, (Default: Dyanmic Memory)
95122 *
96123 * Returns:
97124 * - True if the request is successful, false otherwise.
@@ -102,17 +129,43 @@ class ChatGPT
102129 const char *content,
103130 int max_tokens,
104131 bool content_only,
105- String &result)
132+ String &result,
133+ bool mem_dynamic = true )
106134 {
107- int post_body_size = snprintf (nullptr , 0 , " {\" model\" : \" %s\" , \" max_tokens\" : %d, \" messages\" : [{\" role\" : \" %s\" , \" content\" : \" %s\" }]}" , model, max_tokens, role, content) + 1 ;
108- char *post_body = new char [post_body_size];
135+ char *post_body = nullptr ; // Initialize post_body pointer
136+ int post_body_size = 0 ;
137+
138+ if (mem_dynamic)
139+ {
140+ // Calculate the required size for dynamic allocation
141+ post_body_size = snprintf (nullptr , 0 , " {\" model\" : \" %s\" , \" max_tokens\" : %d, \" messages\" : [{\" role\" : \" %s\" , \" content\" : \" %s\" }]}" , model, max_tokens, role, content) + 1 ;
142+ post_body = new char [post_body_size];
143+ if (post_body == nullptr )
144+ {
145+ result = " [ERR] Memory allocation failed!" ;
146+ return false ;
147+ }
148+ }
149+ else
150+ {
151+ // Use a static buffer with a fixed size
152+ static const int static_buffer_size = 256 ;
153+ char static_post_body[static_buffer_size];
154+ post_body_size = static_buffer_size;
155+ post_body = static_post_body;
156+ }
109157
110158 // Format the post_body string
111159 snprintf (post_body, post_body_size, " {\" model\" : \" %s\" , \" max_tokens\" : %d, \" messages\" : [{\" role\" : \" %s\" , \" content\" : \" %s\" }]}" , model, max_tokens, role, content);
112160
113161 // Call the _post function
114162 bool success = _postStream (post_body, content_only, result);
115- delete[] post_body;
163+
164+ // Free dynamic memory if allocated
165+ if (mem_dynamic)
166+ {
167+ delete[] post_body;
168+ }
116169 return success;
117170 }
118171
@@ -148,15 +201,15 @@ class ChatGPT
148201 return false ;
149202 }
150203
151- int payload_length = strlen (post_body);
204+ size_t payload_length = strlen (post_body);
152205 String auth_header = _get_auth_header (api_key);
153206 String http_request = " POST /" + api_version + " /chat/completions HTTP/1.1\r\n " + auth_header + " \r\n " + " Host: " + host + " \r\n " + " Cache-control: no-cache\r\n " + " User-Agent: ESP32 ChatGPT\r\n " + " Content-Type: application/json\r\n " + " Content-Length: " + String (payload_length) + " \r\n " + " Connection: close\r\n " + " \r\n " ;
154207
155208 // Send the HTTP request headers
156209 client->print (http_request);
157210
158211 // Send the HTTP request body in chunks
159- int bytes_sent = 0 ;
212+ size_t bytes_sent = 0 ;
160213 while (bytes_sent < payload_length)
161214 {
162215 size_t chunk_size = minimum (payload_length - bytes_sent, static_cast <size_t >(1024 )); // Adjust chunk size as needed
0 commit comments