Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 60 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ESP32_AI_Connect is an Arduino library that enables ESP32 microcontrollers to in
- **Multi-platform support**: Single interface for different AI providers
- **Tool calls support**: Enables tool call capabilities with AI models
- **Streaming support**: Supports streaming communication with AI model, featuring thread safety, user interruption, etc.
- **Secure connections**: Optional SSL/TLS certificate verification for production deployments
- **Expandable framework**: Built to easily accommodate additional model support
- **Configurable features**: Enable/disable tool calls feature to optimize microcontroller resources
- **OpenAI-compatible support**: Use alternative platforms by supplying custom endpoints and model names
Expand Down Expand Up @@ -187,6 +188,39 @@ Streaming chat enables real-time interaction with AI models by delivering respon
- **Thread-safe Design**: Built on FreeRTOS primitives for reliable operation
- **Memory Efficient**: Optimized for ESP32's limited resources

## Secure Connections (SSL/TLS)

By default, the library operates in **insecure mode** (no SSL certificate verification) for ease of use. For production applications, you can enable secure connections by providing a Root CA certificate:

```cpp
// Root CA certificate (PEM format)
const char* root_ca = R"(
-----BEGIN CERTIFICATE-----
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
-----END CERTIFICATE-----
)";

// Enable secure mode
aiClient.setRootCA(root_ca);

// Check current security status
if (aiClient.getRootCA() != nullptr) {
Serial.println("Secure mode enabled");
}

// Disable secure mode (back to insecure)
aiClient.setRootCA(nullptr);
```

| Method | Description |
|--------|-------------|
| `setRootCA(cert)` | Set Root CA certificate to enable SSL verification. Pass `nullptr` to disable. |
| `getRootCA()` | Returns current Root CA certificate, or `nullptr` if in insecure mode. |

See the `secure_connection_demo` example for a complete demonstration.

## User Guide

For detailed instructions on how to use this library, please refer to the comprehensive User Guide documents in the `doc/User Guide` folder. The User Guide includes:
Expand All @@ -200,26 +234,34 @@ For detailed instructions on how to use this library, please refer to the compre
These guides provide step-by-step instructions, code examples, and best practices to help you get the most out of the ESP32_AI_Connect library.

## Configuration
Edit ESP32_AI_Connect_config.h to customize the library to your specific needs:

All platforms and features are **enabled by default**. Override settings without editing library files:

**Arduino IDE** - Define options before including the library:
```cpp
// Platform support - enable only what you need
#define USE_AI_API_OPENAI
#define USE_AI_API_GEMINI
#define USE_AI_API_DEEPSEEK
#define USE_AI_API_CLAUDE

// Feature toggles - disable to save resources
#define ENABLE_TOOL_CALLS // Enable/disable tool calls support
#define ENABLE_DEBUG_OUTPUT // Enable/disable debug messages
#define ENABLE_STREAM_CHAT // // Enable/disable streaming support

// Memory allocation
#define AI_API_REQ_JSON_DOC_SIZE 1024
#define AI_API_RESP_JSON_DOC_SIZE 2048

// Network settings
#define AI_API_HTTP_TIMEOUT_MS 30000
// Disable unused platforms to save code space
#define DISABLE_AI_API_GEMINI
#define DISABLE_AI_API_DEEPSEEK

// Disable unused features to save memory
#define DISABLE_TOOL_CALLS
#define DISABLE_DEBUG_OUTPUT
#define DISABLE_STREAM_CHAT

// Adjust buffer sizes if needed (defaults: 5120, 2048, 30000)
#define AI_API_REQ_JSON_DOC_SIZE 8192
#define AI_API_RESP_JSON_DOC_SIZE 4096
#define AI_API_HTTP_TIMEOUT_MS 60000

#include <ESP32_AI_Connect.h>
```

**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags =
-DDISABLE_AI_API_GEMINI
-DDISABLE_AI_API_DEEPSEEK
-DAI_API_REQ_JSON_DOC_SIZE=8192
```

Disabling unused platforms or features can significantly reduce memory usage and binary size, making the library more efficient for resource-constrained ESP32 projects.
Expand Down
2 changes: 2 additions & 0 deletions doc/User Guides/1 Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ESP32_AI_Connect is a powerful, flexible library designed to connect ESP32 micro
- **Multi-Platform Support**: Connect to multiple AI platforms including OpenAI, Anthropic Claude, Google Gemini, and DeepSeek
- **Simple API**: Easy-to-use interface for sending prompts and receiving responses
- **Tool Calls Support**: Enable your ESP32 to use LLM function calling capabilities
- **Streaming support**: Supports streaming communication with AI model, featuring thread safety, user interruption, etc.
- **Secure Connections**: Optional SSL/TLS certificate verification for production deployments
- **Memory Efficient**: Optimized for the limited resources of ESP32 devices
- **Customizable**: Configure parameters like temperature, max tokens, and system prompts
- **Extensible**: Modular design makes it easy to add support for new AI platforms
Expand Down
67 changes: 57 additions & 10 deletions doc/User Guides/2 Basic LLM Chat Implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,18 @@ ESP32_AI_Connect aiClient("claude", apiKey, "claude-3.7-sonnet");
ESP32_AI_Connect aiClient("deepseek", apiKey, "deepseek-chat");
```

Make sure the corresponding platform is enabled in the `ESP32_AI_Connect_config.h` file:
All platforms are **enabled by default**. To disable unused platforms (saves code space), add before including the library:

**Arduino IDE** - Add before `#include <ESP32_AI_Connect.h>`:
```cpp
// --- Platform Selection ---
#define USE_AI_API_OPENAI // Enable OpenAI and OpenAI-compatible APIs
#define USE_AI_API_GEMINI // Enable Google Gemini API
#define USE_AI_API_DEEPSEEK // Enable DeepSeek API
#define USE_AI_API_CLAUDE // Enable Anthropic Claude API
#define DISABLE_AI_API_GEMINI // Disable if not using Gemini
#define DISABLE_AI_API_DEEPSEEK // Disable if not using DeepSeek
#include <ESP32_AI_Connect.h>
```

**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags = -DDISABLE_AI_API_GEMINI -DDISABLE_AI_API_DEEPSEEK
```

## Using a Custom Endpoint
Expand All @@ -293,6 +297,41 @@ This is useful for self-hosted models or alternative API providers that are comp

For more detailed information on how to use OpenAI compatible API, please refer to the custom_llm_chat.ino example code in the examples folder of the ESP32_AI_Connect Library.

## Secure Connections (SSL/TLS)

By default, the library operates in **insecure mode** (no SSL certificate verification) for ease of use and compatibility. For production applications requiring enhanced security, you can enable SSL verification by providing a Root CA certificate:

```cpp
// Root CA certificate in PEM format
const char* root_ca = R"(
-----BEGIN CERTIFICATE-----
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
-----END CERTIFICATE-----
)";

// Enable secure mode with SSL verification
aiClient.setRootCA(root_ca);

// Check if secure mode is enabled
if (aiClient.getRootCA() != nullptr) {
Serial.println("Secure mode: SSL verification enabled");
}

// To disable secure mode and return to insecure mode
aiClient.setRootCA(nullptr);
```

### SSL Methods Reference

| Method | Description |
|--------|-------------|
| `setRootCA(const char* cert)` | Set the Root CA certificate to enable SSL verification. Pass `nullptr` to disable and return to insecure mode. |
| `getRootCA()` | Returns a pointer to the current Root CA certificate, or `nullptr` if in insecure mode. |

**Note:** You'll need to obtain the appropriate Root CA certificate for your AI provider. For a complete example, see the `secure_connection_demo` in the examples folder.

## Accessing Raw API Responses

For advanced usage, you might want to access the complete raw JSON response from the API. The library provides methods to retrieve these:
Expand All @@ -319,13 +358,21 @@ If you encounter issues with your AI chat application, here are some common prob

3. **API Key Problems**: Verify that your API key is valid and has the necessary permissions.

4. **Memory Limitations**: The ESP32 has limited memory. If you're receiving large responses, you might need to adjust the JSON buffer sizes in `ESP32_AI_Connect_config.h`:
4. **Memory Limitations**: The ESP32 has limited memory. If you're receiving large responses, you can adjust the JSON buffer sizes by defining them before including the library:

**Arduino IDE**:
```cpp
#define AI_API_REQ_JSON_DOC_SIZE 1024
#define AI_API_RESP_JSON_DOC_SIZE 2048
#define AI_API_REQ_JSON_DOC_SIZE 8192
#define AI_API_RESP_JSON_DOC_SIZE 4096
#include <ESP32_AI_Connect.h>
```

**PlatformIO** (`platformio.ini`):
```ini
build_flags = -DAI_API_REQ_JSON_DOC_SIZE=8192 -DAI_API_RESP_JSON_DOC_SIZE=4096
```

5. **Platform Not Enabled**: Ensure that the platform you're trying to use is enabled in `ESP32_AI_Connect_config.h`.
5. **Platform Disabled**: All platforms are enabled by default. If you previously disabled a platform with `DISABLE_AI_API_<PLATFORM>`, remove that define.

## Conclusion

Expand Down
43 changes: 30 additions & 13 deletions doc/User Guides/3 Tool Calls Implementation Basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ Before you begin, make sure you have:
- An API key for your chosen AI platform (Ensure the AI Model you choose supports Tool Calls functionality)
- Basic understanding of JSON and Arduino programming

## Step 1: Enable Tool Calls in Configuration
## Step 1: Tool Calls Configuration

First, ensure that tool calls support is enabled in the library configuration file. Open `ESP32_AI_Connect_config.h` and make sure the following line is uncommented:
Tool calls support is **enabled by default** in the library. No configuration changes are required to use it.

To **disable** tool calls (saves memory if not needed), add one of the following before including the library:

**Arduino IDE** - Add before `#include <ESP32_AI_Connect.h>`:
```cpp
// --- Tool Calls Support ---
// Uncomment the following line to enable tool calls (tool calling) support
// This will add tool calling methods to the library
// If you don't need tool calls, keep this commented out to save memory
#define ENABLE_TOOL_CALLS
#define DISABLE_TOOL_CALLS
#include <ESP32_AI_Connect.h>
```

You may also want to enable debug output to see detailed request/response information:
**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags = -DDISABLE_TOOL_CALLS
```

Debug output is also enabled by default to help you see detailed request/response information. To disable it:

**Arduino IDE**:
```cpp
// --- Debug Output ---
// Uncomment the following line to enable debug output to Serial
#define ENABLE_DEBUG_OUTPUT
#define DISABLE_DEBUG_OUTPUT
#include <ESP32_AI_Connect.h>
```

**PlatformIO**:
```ini
build_flags = -DDISABLE_DEBUG_OUTPUT
```

## Step 2: Include Required Libraries
Expand Down Expand Up @@ -369,11 +379,18 @@ With multiple tools defined, the AI can choose which function to call based on t

Tool calls can require significant memory, especially when defining complex tools. If you encounter memory issues, you may need to increase the JSON document size in the configuration file:

**Arduino IDE** - Add before `#include <ESP32_AI_Connect.h>`:
```cpp
// --- Advanced Configuration (Optional) ---
// Adjust JSON buffer sizes if needed (consider ESP32 memory)
#define AI_API_REQ_JSON_DOC_SIZE 5120 // Increased from default 1024
#define AI_API_RESP_JSON_DOC_SIZE 4096 // Increased from default 2048
#define AI_API_REQ_JSON_DOC_SIZE 8192 // Default is 5120
#define AI_API_RESP_JSON_DOC_SIZE 4096 // Default is 2048
#include <ESP32_AI_Connect.h>
```

**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags = -DAI_API_REQ_JSON_DOC_SIZE=8192 -DAI_API_RESP_JSON_DOC_SIZE=4096
```

The maximum tool call size is automatically set to half of `AI_API_REQ_JSON_DOC_SIZE`, so increasing this value will allow for larger tool definitions.
Expand Down
14 changes: 10 additions & 4 deletions doc/User Guides/4 Tool Calls Follow-Up Techniques.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,20 @@ if (finishReason == "tool_calls" || finishReason == "tool_use") {

### 4. Buffer Size Configuration

For complex tools with large JSON schemas, you may need to increase the buffer size in `ESP32_AI_Connect_config.h`:
The default `AI_API_REQ_JSON_DOC_SIZE` is 5120 bytes, which allows for tool calls up to 2560 bytes (half of the document size). This is sufficient for most use cases.

For complex tools with larger JSON schemas, you can increase the buffer size:

**Arduino IDE** - Add before `#include <ESP32_AI_Connect.h>`:
```cpp
// In ESP32_AI_Connect_config.h
#define AI_API_REQ_JSON_DOC_SIZE 5120
#define AI_API_REQ_JSON_DOC_SIZE 8192
#include <ESP32_AI_Connect.h>
```

This sets the maximum request JSON document size to 5120 bytes, allowing for tool calls up to 2560 bytes (half of the document size).
**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags = -DAI_API_REQ_JSON_DOC_SIZE=8192
```

## Advanced: Creating a Reusable Tool Calls Handler

Expand Down
45 changes: 29 additions & 16 deletions doc/User Guides/5 Stream Chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,34 @@ Before implementing streaming chat, ensure you have:
- **API Access**: Valid API key for your chosen platform
- **Knowledge**: Basic understanding of callback functions in C++

## Step 1: Enable Streaming Chat in Configuration
## Step 1: Streaming Chat Configuration

First, enable streaming chat support in the library configuration. Open `ESP32_AI_Connect_config.h` and ensure these lines are uncommented:
Streaming chat is **enabled by default** in the library. No configuration changes are required to use it.

To **disable** streaming chat (saves memory if not needed), add one of the following before including the library:

**Arduino IDE** - Add before `#include <ESP32_AI_Connect.h>`:
```cpp
// --- Streaming Chat Support ---
// Uncomment the following line to enable streaming chat functionality
// This will add streamChat methods to the library
// If you don't need streaming chat, keep this commented out to save memory
#define ENABLE_STREAM_CHAT

// --- Debug Options ---
// Uncomment the following line to enable detailed debug output (Request/Response)
// via the Serial monitor.
#define ENABLE_DEBUG_OUTPUT
#define DISABLE_STREAM_CHAT
#include <ESP32_AI_Connect.h>
```

**PlatformIO** - Add to `platformio.ini`:
```ini
build_flags = -DDISABLE_STREAM_CHAT
```

Similarly, debug output is enabled by default. To disable it for cleaner output:

**Arduino IDE**:
```cpp
#define DISABLE_DEBUG_OUTPUT
#include <ESP32_AI_Connect.h>
```

**PlatformIO**:
```ini
build_flags = -DDISABLE_DEBUG_OUTPUT
```

**Memory Considerations:**
Expand Down Expand Up @@ -564,7 +577,7 @@ The newline formatting in the callback ensures clean separation between content

### Setup Steps:

1. **Configure the library**: Ensure `ENABLE_STREAM_CHAT` is uncommented in `ESP32_AI_Connect_config.h`
1. **Library ready**: Streaming chat is enabled by default - no configuration needed
2. **Update credentials**: Replace WiFi credentials and API key in the example
3. **Select platform**: Uncomment your preferred platform configuration
4. **Upload and run**: Upload to ESP32 and open Serial Monitor at 115200 baud
Expand Down Expand Up @@ -619,9 +632,9 @@ Enter your next message (or 'status' for metrics):

### 1. Streaming Doesn't Start
```cpp
// Check configuration
#ifndef ENABLE_STREAM_CHAT
#error "ENABLE_STREAM_CHAT must be defined in ESP32_AI_Connect_config.h"
// Check that streaming wasn't disabled
#ifdef DISABLE_STREAM_CHAT
#error "Remove DISABLE_STREAM_CHAT to use streaming"
#endif

// Verify WiFi connection
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_llm_chat/basic_llm_chat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setup() {
// Optional: Check if begin was successful if using the begin() method approach
if (!aiClient.begin(platform, apiKey, model)) {
Serial.println("Failed to initialize AI Client for platform: " + String(platform));
Serial.println("Check API Key, Model, and ensure platform is enabled in config.");
Serial.println("Check API Key, Model, and ensure platform was not disabled.");
while(1) delay(1000); // Halt on failure
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_llm_chat/custom_llm_chat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void setup() {
// Optional: Check if begin was successful if using the begin() method approach
if (!aiClient.begin(platform, apiKey, model, customEndpoint)) {
Serial.println("Failed to initialize AI Client for platform: " + String(platform));
Serial.println("Check API Key, Model, and ensure platform is enabled in config.");
Serial.println("Check API Key, Model, and ensure platform was not disabled.");
while(1) delay(1000); // Halt on failure
}

Expand Down
Loading