Skip to content

Commit 7f23621

Browse files
committed
Added LLM Raw Response Feature. New LLM Handling Setters and Getters. Unified Method Names. Updated User Guides. Etc.
1 parent 270382f commit 7f23621

File tree

16 files changed

+440
-159
lines changed

16 files changed

+440
-159
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,17 @@ void setup() {
113113
Serial.println(WiFi.localIP());
114114

115115
// Configure AI client parameters:
116-
ai.setTemperature(0.7); // Set response creativity (0.0-2.0)
117-
ai.setMaxTokens(200); // Limit response length (in tokens)
118-
ai.setSystemRole("You are a helpful assistant"); // Set assistant behavior
116+
ai.setChatTemperature(0.7); // Set response creativity (0.0-2.0)
117+
ai.setChatMaxTokens(200); // Limit response length (in tokens)
118+
ai.setChatSystemRole("You are a helpful assistant"); // Set assistant behavior
119+
120+
// You can retrieve current settings using getter methods:
121+
Serial.print("Current temperature: ");
122+
Serial.println(ai.getChatTemperature());
123+
Serial.print("Maximum tokens: ");
124+
Serial.println(ai.getChatMaxTokens());
125+
Serial.print("System role: ");
126+
Serial.println(ai.getChatSystemRole());
119127

120128
// Send a test message to the AI and get response
121129
Serial.println("\nSending message to AI...");

doc/User Guides/1 Introduction.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ESP32_AI_Connect Library User Guide - 1 Introduction
2-
2+
> **Version 0.0.2** • Revised: May 09, 2025 • Author: AvantMaker • [https://www.AvantMaker.com](https://www.AvantMaker.com)
33
## Overview
44

55
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
128128
129129
If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. Contributions are welcome through pull requests.
130130
131-
Happy building with ESP32 and AI!
131+
Happy building with ESP32 and AI!
132+
133+
---
134+
>🚀 **Explore our GitHub** for more projects:
135+
>- [ESP32_AI_Connect GitHub Repo](https://github.com/AvantMaker/ESP32_AI_Connect)
136+
>- [AvantMaker GitHub](https://github.com/AvantMaker/)

doc/User Guides/2 Basic LLM Chat Implementation.md

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# ESP32_AI_Connect Library User Guide - 2 Basic LLM Chat Implementation
2+
> **Version 0.0.2** • Revised: May 09, 2025 • Author: AvantMaker • [https://www.AvantMaker.com](https://www.AvantMaker.com)
23
34
## Overview
45

@@ -92,18 +93,38 @@ After establishing the WiFi connection, we configure the AI client with specific
9293

9394
```cpp:basic_example.ino
9495
// 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
9899
```
99100

100101
These configuration options allow you to customize the behavior of the AI:
101102

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.
105106

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
107128

108129
Now we're ready to send a message to the AI and receive a response:
109130

@@ -130,7 +151,7 @@ The key function here is `ai.chat()`, which:
130151

131152
We also include error checking to display any issues that might have occurred during the API call.
132153

133-
## Step 7: The Loop Function
154+
## Step 8: The Loop Function
134155

135156
In this basic example, the `loop()` function is empty:
136157

@@ -185,6 +206,26 @@ This modified `loop()` function:
185206
4. Prints the AI's response
186207
5. Checks for errors
187208

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+
188229
## Switching Between AI Platforms
189230

190231
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:
@@ -219,6 +260,22 @@ ESP32_AI_Connect ai("openai-compatible", apiKey, "model-name", customEndpoint);
219260
220261
This is useful for self-hosted models or alternative API providers that are compatible with the OpenAI API format.
221262
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+
222279
## Troubleshooting
223280

224281
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
243300
244301
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.
245302
246-
Happy building with ESP32 and AI!
303+
Happy building with ESP32 and AI!
304+
305+
---
306+
>🚀 **Explore our GitHub** for more projects:
307+
>- [ESP32_AI_Connect GitHub Repo](https://github.com/AvantMaker/ESP32_AI_Connect)
308+
>- [AvantMaker GitHub](https://github.com/AvantMaker/)

doc/User Guides/3 Tool Calls Implementation Basics.md

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ESP32_AI_Connect Library User Guide - 3 Tool Calls Implementation Basics
2-
2+
> **Version 0.0.2** • Revised: May 09, 2025 • Author: AvantMaker • [https://www.AvantMaker.com](https://www.AvantMaker.com)
33
## Overview
44

55
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:
141141

142142
```cpp
143143
// --- Demonstrate Configuration Methods (Optional) ---
144-
aiClient.setTCSystemRole("You are a weather assistant.");
145-
aiClient.setTCMaxToken(300);
146-
aiClient.setTCToolChoice("auto");
144+
aiClient.setTCChatSystemRole("You are a weather assistant.");
145+
aiClient.setTCChatMaxTokens(300);
146+
aiClient.setTCChatToolChoice("auto");
147147

148148
Serial.println("\n--- Tool Call Configuration ---");
149-
Serial.println("System Role: " + aiClient.getTCSystemRole());
150-
Serial.println("Max Tokens: " + String(aiClient.getTCMaxToken()));
151-
Serial.println("Tool Choice: " + aiClient.getTCToolChoice());
149+
Serial.println("System Role: " + aiClient.getTCChatSystemRole());
150+
Serial.println("Max Tokens: " + String(aiClient.getTCChatMaxTokens()));
151+
Serial.println("Tool Choice: " + aiClient.getTCChatToolChoice());
152152
```
153153

154154
These optional methods allow you to:
155-
- 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
158158

159159
Each setter also has a corresponding getter method to retrieve the current value.
160160

@@ -228,7 +228,27 @@ Here's a table that summarizes the different finish reason values used by each p
228228

229229
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.
230230

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
232252

233253
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.
234254

@@ -258,7 +278,7 @@ if (error) {
258278
259279
This code parses the tool calls JSON array and extracts the function name, ID, and arguments for each tool call.
260280
261-
## Step 11: Reset Tool Call Settings
281+
## Step 12: Reset Tool Call Settings
262282
263283
After completing a tool call operation, you can reset the tool call settings to their default values:
264284
@@ -268,12 +288,17 @@ Serial.println("\nResetting tool call settings to defaults...");
268288
aiClient.tcChatReset();
269289
270290
Serial.println("\n--- Tool Call Configuration After Reset ---");
271-
Serial.println("System Role: " + aiClient.getTCSystemRole());
272-
Serial.println("Max Tokens: " + String(aiClient.getTCMaxToken()));
273-
Serial.println("Tool Choice: " + aiClient.getTCToolChoice());
291+
Serial.println("System Role: " + aiClient.getTCChatSystemRole());
292+
Serial.println("Max Tokens: " + String(aiClient.getTCChatMaxTokens()));
293+
Serial.println("Tool Choice: " + aiClient.getTCChatToolChoice());
294+
295+
// Verify raw response was cleared
296+
if (aiClient.getTCRawResponse().isEmpty()) {
297+
Serial.println("Raw response was cleared successfully");
298+
}
274299
```
275300

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.
277302

278303
## Advanced: Using Multiple Tools
279304

@@ -367,12 +392,19 @@ If you encounter issues with tool calls, here are some common problems and solut
367392
368393
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.
369394
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+
370397
## Conclusion
371398
372399
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.
373400
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.
375402
376403
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.
377404
378405
Happy building with ESP32 and AI!
406+
407+
---
408+
>🚀 **Explore our GitHub** for more projects:
409+
>- [ESP32_AI_Connect GitHub Repo](https://github.com/AvantMaker/ESP32_AI_Connect)
410+
>- [AvantMaker GitHub](https://github.com/AvantMaker/)

0 commit comments

Comments
 (0)