You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ESP32_AI_Connect is a powerful, flexible library designed to connect ESP32 microcontrollers to various Large Language Model (LLM) platforms. This library simplifies the process of integrating AI capabilities into your ESP32 projects, allowing you to create intelligent IoT devices, smart assistants, and interactive applications with minimal effort.
@@ -128,4 +128,9 @@ Each guide will include detailed explanations, code examples, and best practices
128
128
129
129
If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. Contributions are welcome through pull requests.
@@ -92,18 +93,38 @@ After establishing the WiFi connection, we configure the AI client with specific
92
93
93
94
```cpp:basic_example.ino
94
95
// Configure AI client parameters:
95
-
ai.setTemperature(0.7); // Set response creativity (0.0-2.0)
96
-
ai.setMaxTokens(200); // Limit response length (in tokens)
97
-
ai.setSystemRole("You are a helpful assistant"); // Set assistant behavior
96
+
ai.setChatTemperature(0.7); // Set response creativity (0.0-2.0)
97
+
ai.setChatMaxTokens(200); // Limit response length (in tokens)
98
+
ai.setChatSystemRole("You are a helpful assistant"); // Set assistant behavior
98
99
```
99
100
100
101
These configuration options allow you to customize the behavior of the AI:
101
102
102
-
-`setTemperature(0.7)`: Controls the randomness/creativity of the responses. Lower values (closer to 0) make responses more deterministic and focused, while higher values (up to 2.0) make them more creative and diverse.
103
-
-`setMaxTokens(200)`: Limits the maximum length of the AI's response in tokens (roughly 4 characters per token). This helps control response size and API costs.
104
-
-`setSystemRole("You are a helpful assistant")`: Sets the system message that defines the AI's behavior and personality.
103
+
-`setChatTemperature(0.7)`: Controls the randomness/creativity of the responses. Lower values (closer to 0) make responses more deterministic and focused, while higher values (up to 2.0) make them more creative and diverse.
104
+
-`setChatMaxTokens(200)`: Limits the maximum length of the AI's response in tokens (roughly 4 characters per token). This helps control response size and API costs.
105
+
-`setChatSystemRole("You are a helpful assistant")`: Sets the system message that defines the AI's behavior and personality.
105
106
106
-
## Step 6: Send a Message and Get a Response
107
+
## Step 6: Verifying Configuration with Getter Methods
108
+
109
+
You can verify your configuration settings using the corresponding getter methods:
110
+
111
+
```cpp:basic_example.ino
112
+
// Retrieve and display the current configuration
113
+
Serial.println("\nAI Configuration:");
114
+
Serial.print("System Role: ");
115
+
Serial.println(ai.getChatSystemRole());
116
+
Serial.print("Temperature: ");
117
+
Serial.println(ai.getChatTemperature());
118
+
Serial.print("Max Tokens: ");
119
+
Serial.println(ai.getChatMaxTokens());
120
+
```
121
+
122
+
These getter methods allow you to:
123
+
- Confirm that your settings were applied correctly
124
+
- Access current configuration values for logging or debugging
125
+
- Use configuration values in other parts of your application logic
126
+
127
+
## Step 7: Send a Message and Get a Response
107
128
108
129
Now we're ready to send a message to the AI and receive a response:
109
130
@@ -130,7 +151,7 @@ The key function here is `ai.chat()`, which:
130
151
131
152
We also include error checking to display any issues that might have occurred during the API call.
132
153
133
-
## Step 7: The Loop Function
154
+
## Step 8: The Loop Function
134
155
135
156
In this basic example, the `loop()` function is empty:
136
157
@@ -185,6 +206,26 @@ This modified `loop()` function:
185
206
4. Prints the AI's response
186
207
5. Checks for errors
187
208
209
+
## Resetting Chat Configuration
210
+
211
+
If you need to reset the chat configuration to default values, you can use the `chatReset()` method:
212
+
213
+
```cpp
214
+
// Reset chat configuration to defaults
215
+
ai.chatReset();
216
+
217
+
// Verify reset was successful
218
+
Serial.println("After reset:");
219
+
Serial.print("System Role: ");
220
+
Serial.println(ai.getChatSystemRole()); // Should be empty
221
+
Serial.print("Temperature: ");
222
+
Serial.println(ai.getChatTemperature()); // Should be -1.0 (default)
223
+
Serial.print("Max Tokens: ");
224
+
Serial.println(ai.getChatMaxTokens()); // Should be -1 (default)
225
+
```
226
+
227
+
This is useful when you want to start a fresh conversation with different settings or return to the default configuration.
228
+
188
229
## Switching Between AI Platforms
189
230
190
231
One of the key features of the ESP32_AI_Connect library is its ability to work with multiple AI platforms. To switch from OpenAI to Google Gemini or DeepSeek, you only need to change the platform identifier and model name:
This is useful for self-hosted models or alternative API providers that are compatible with the OpenAI API format.
221
262
263
+
## Accessing Raw API Responses
264
+
265
+
For advanced usage, you might want to access the complete raw JSON response from the API. The library provides methods to retrieve these:
266
+
267
+
```cpp
268
+
// Get the raw JSON response from the last chat request
269
+
String rawResponse = ai.getChatRawResponse();
270
+
Serial.println("Raw API Response:");
271
+
Serial.println(rawResponse);
272
+
273
+
// For tool calling, you can also get the raw response
274
+
String rawToolResponse = ai.getTCRawResponse();
275
+
```
276
+
277
+
These methods allow you to access the full API response data for custom processing or debugging.
278
+
222
279
## Troubleshooting
223
280
224
281
If you encounter issues with your AI chat application, here are some common problems and solutions:
@@ -243,4 +300,9 @@ You've now learned how to use the ESP32_AI_Connect library to conduct basic chat
243
300
244
301
In the next guide, we'll explore how to use the library's tool calling capabilities to enable your ESP32 to perform specific functions based on AI instructions.
This guide will walk you through the process of setting up and using tool calls (function calling) with Large Language Models (LLMs) using the ESP32_AI_Connect library. We'll use the `tool_calls_demo.ino` sketch stored in the examples folder as our reference implementation, explaining each component in detail so you can understand how to integrate AI function calling capabilities into your ESP32 projects.
@@ -141,20 +141,20 @@ You can customize the tool calling behavior using optional setter methods:
- Set a system role message that defines the AI's behavior (`setTCSystemRole`)
156
-
- Set the maximum number of tokens for the response (`setTCMaxToken`)
157
-
- Set the tool choice parameter (`setTCToolChoice`) which dictates how the AI selects tools
155
+
- Set a system role message that defines the AI's behavior (`setTCChatSystemRole`)
156
+
- Set the maximum number of tokens for the response (`setTCChatMaxTokens`)
157
+
- Set the tool choice parameter (`setTCChatToolChoice`) which dictates how the AI selects tools
158
158
159
159
Each setter also has a corresponding getter method to retrieve the current value.
160
160
@@ -228,7 +228,27 @@ Here's a table that summarizes the different finish reason values used by each p
228
228
229
229
This distinction is important when checking the `finishReason` in your code to determine if you need to execute tools based on the AI's response.
230
230
231
-
## Step 10: Parse and Execute Tool Calls
231
+
## Step 10: Accessing the Complete Raw API Response
232
+
233
+
For debugging or advanced scenarios, you may want to access the complete raw JSON response from the AI server:
234
+
235
+
```cpp
236
+
// Get the raw server response
237
+
String rawResponse = aiClient.getTCRawResponse();
238
+
Serial.println("\n--- Raw API Response ---");
239
+
Serial.println(rawResponse);
240
+
```
241
+
242
+
The `getTCRawResponse()` method returns the complete server response JSON for the last tool calling method executed (either `tcChat` or `tcReply`). This can be useful for:
243
+
244
+
- Debugging API interactions
245
+
- Accessing additional response information not exposed by the library
246
+
- Custom parsing of complex response data
247
+
- Monitoring token usage and other metadata
248
+
249
+
This raw response includes all fields returned by the AI platform, not just the extracted tool calls or content.
250
+
251
+
## Step 11: Parse and Execute Tool Calls
232
252
233
253
When you receive tool calls, you must parse the JSON response and execute the requested functions. A practical example of how to parse and handle tool calls can be found in the tool_calling_demo_2 example code located in the examples folder.
234
254
@@ -258,7 +278,7 @@ if (error) {
258
278
259
279
This code parses the tool calls JSON array and extracts the function name, ID, and arguments for each tool call.
260
280
261
-
## Step 11: Reset Tool Call Settings
281
+
## Step 12: Reset Tool Call Settings
262
282
263
283
After completing a tool call operation, you can reset the tool call settings to their default values:
264
284
@@ -268,12 +288,17 @@ Serial.println("\nResetting tool call settings to defaults...");
268
288
aiClient.tcChatReset();
269
289
270
290
Serial.println("\n--- Tool Call Configuration After Reset ---");
Serial.println("Raw response was cleared successfully");
298
+
}
274
299
```
275
300
276
-
The `tcChatReset()` method resets all tool call parameters to their default values.
301
+
The `tcChatReset()` method resets all tool call parameters to their default values and clears the stored raw response.
277
302
278
303
## Advanced: Using Multiple Tools
279
304
@@ -367,12 +392,19 @@ If you encounter issues with tool calls, here are some common problems and solut
367
392
368
393
5. **Tool Definition Too Large**: If your tool definition is too large, you'll get an error. Increase `AI_API_REQ_JSON_DOC_SIZE` in the configuration file.
369
394
395
+
6. **Debug with Raw Response**: If you're not sure why something isn't working, examine the raw response with `getTCRawResponse()` to see the complete server response.
396
+
370
397
## Conclusion
371
398
372
399
You've now learned how to use the ESP32_AI_Connect library to implement tool calls with LLMs. This powerful feature allows your ESP32 to act as an intelligent agent, executing functions based on natural language instructions and providing the results back to the AI for further processing.
373
400
374
-
With the new setter/getter methods and the `tcChatReset()` function, you have greater control over the tool calling behavior, allowing you to create more flexible and sophisticated AI-powered IoT applications.
401
+
With the setter/getter methods, the `tcChatReset()` function, and the raw response access methods, you have greater control over the tool calling behavior, allowing you to create more flexible and sophisticated AI-powered IoT applications.
375
402
376
403
Tool calls open up a world of possibilities for creating smart IoT devices that can interact with their environment based on AI-driven decisions. You can create weather stations, home automation systems, data loggers, and much more, all controlled through natural language.
0 commit comments