Skip to content

Commit 5bc3de5

Browse files
author
Eric
committed
Version 0.2.0
1 parent e8667ff commit 5bc3de5

File tree

11 files changed

+656
-190
lines changed

11 files changed

+656
-190
lines changed

README.md

Lines changed: 120 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
[![arduino-library-badge](https://www.ardu-badge.com/badge/ChatGPT_Client.svg)](https://www.ardu-badge.com/ChatGPT_Client)
2-
# ChatGPT Client For Arduino
32

4-
This library helps to use ChatGPT in Arduino environment. There are many features, but currently, only the Create chat completion part is implemented.
5-
Since this uses an official API, you must obtain an API Key from openAI to use it.
3+
## Overview
4+
The ChatGPT Arduino Library provides a convenient way to interact with the OpenAI GPT models from Arduino environments, such as ESP32 devices. With this library, you can easily send text and vision queries to the ChatGPT API and receive responses directly in your Arduino projects.
65

7-
## Authentication
6+
## Features
7+
- **Text Generation**: Generate text-based responses to user queries using the ChatGPT models.
8+
- **Vision Question Answering**: Utilize the vision capabilities of ChatGPT to answer questions about images.
9+
- **Simple API Integration**: Simple and intuitive API for seamless integration into your Arduino projects.
810

11+
## Authentication
912
The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use in your requests.
1013

1114
First, get your SECRET KEY issued here.
12-
https://platform.openai.com/account/api-keys
15+
https://platform.openai.com/api-keys
1316
![API Keys](misc/openai_API-Keys.png)
1417

1518
## Installation
@@ -38,41 +41,130 @@ Then you should see the examples and be able to include the library in your proj
3841
```
3942

4043
## Usage
44+
1. Include the `ChatGPT.hpp` header file in your Arduino sketch.
45+
2. Create a `ChatGPT` object and provide your API key and other required parameters.
46+
3. Use the provided methods to send queries to the ChatGPT API and receive responses.
47+
48+
### Example:
49+
```cpp
50+
#include <ChatGPT.hpp>
51+
52+
// Initialize ChatGPT client
53+
WiFiSSLClient wifiClient;
54+
ChatGPT<WiFiSSLClient> chatGPT(&wifiClient, "v1", "YOUR_API_KEY", 10000);
55+
56+
void exampleTextQuestion() {
57+
/*
58+
model: Model to use for generating the response (e.g., "gpt-4o", "gpt-3.5-turbo").
59+
role: Role of the message sender (e.g., "user" or "assistant").
60+
content: Content of the message to send.
61+
max_tokens: Maximum number of tokens to generate in the response.
62+
content_only: Flag indicating whether to extract only the content of the response. (e.g., true - answer only, false - full response)
63+
result: Reference to a String variable to store the result of the API call.
64+
*/
65+
66+
String result;
67+
Serial.println("\n\n[ChatGPT] - Asking a Text Question");
68+
if (chatGPT_Client.chat_message("gpt-3.5-turbo", "user", "What is the best feature of GPT-4o?", 100, false, result)) {
69+
Serial.print("[ChatGPT] Response: ");
70+
Serial.println(result);
71+
} else {
72+
Serial.print("[ChatGPT] Error: ");
73+
Serial.println(result);
74+
}
75+
}
4176

42-
Here's a minimal example:
43-
```
44-
if (chat_gpt.simple_message("gpt-3.5-turbo-0301", "user", "Planning a 3-day trip to San Diego", result)) {
45-
Serial.println("===OK===");
77+
void exampleVisionQuestionWithURL() {
78+
/*
79+
model: Model to use for generating the response (e.g., "gpt-4o").
80+
role: Role of the message sender (e.g., "user" or "assistant").
81+
type: Type of content (e.g., "text").
82+
text: Text content of the message.
83+
image_type: Type of the image (e.g., "image_url").
84+
image_url: URL of the image.
85+
detail: Detail level of the image (e.g., "high", "low", "auto").
86+
max_tokens: Maximum number of tokens to generate in the response.
87+
content_only: Flag indicating whether to extract only the content of the response. (e.g., true - answer only, false - full response)
88+
result: Reference to a String variable to store the result of the API call.
89+
*/
90+
String result;
91+
Serial.println("\n\n[ChatGPT] - Asking a Vision Question");
92+
if (chatGPT_Client.vision_question("gpt-4o", "user", "text", "What’s in this image?", "image_url", "https://samplelib.com/lib/preview/jpeg/sample-city-park-400x300.jpg", "auto", 400, false, result)) {
93+
Serial.print("[ChatGPT] Response: ");
4694
Serial.println(result);
47-
} else {
48-
Serial.println("===ERROR===");
95+
} else {
96+
Serial.print("[ChatGPT] Error: ");
4997
Serial.println(result);
98+
}
5099
}
51100
```
52101
53-
Result
102+
### Result:
103+
104+
```json
105+
[ChatGPT] - Asking a Text Question
106+
[ChatGPT] Response: {
107+
"id": "chatcmpl-9QjdlCkqkB4dsVXBzUTRTBYafI7f3",
108+
"object": "chat.completion",
109+
"created": 1716158997,
110+
"model": "gpt-3.5-turbo-0125",
111+
"choices": [
112+
{
113+
"index": 0,
114+
"message": {
115+
"role": "assistant",
116+
"content": "One of the best features of GPT-4o is its advanced natural language processing capabilities. GPT-4o has a deeper understanding of context, nuance, and complex language structures, allowing it to generate more accurate and coherent responses to a wide range of prompts. Its improved language modeling capabilities make it more versatile and effective in various tasks such as text generation, language translation, and question answering. Overall, the enhanced natural language processing capabilities of GPT-4o make it a powerful tool"
117+
},
118+
"logprobs": null,
119+
"finish_reason": "length"
120+
}
121+
],
122+
"usage": {
123+
"prompt_tokens": 19,
124+
"completion_tokens": 100,
125+
"total_tokens": 119
126+
},
127+
"system_fingerprint": null
128+
}
129+
```
130+
```json
131+
[ChatGPT] - Asking a Vision Question
132+
[ChatGPT] Response: {
133+
"id": "chatcmpl-9Qjdp5B4wyE5cVbcLOucvm9uoT6rH",
134+
"object": "chat.completion",
135+
"created": 1716159001,
136+
"model": "gpt-4o-2024-05-13",
137+
"choices": [
138+
{
139+
"index": 0,
140+
"message": {
141+
"role": "assistant",
142+
"content": "The image shows a peaceful lakeside pathway. The path is made of bricks and runs alongside a body of water. There is greenery on both sides of the path with trees providing shade on the right side. Off in the distance, you can see some buildings, possibly residential apartments, and more greenery. The atmosphere appears to be calm and serene."
143+
},
144+
"logprobs": null,
145+
"finish_reason": "stop"
146+
}
147+
],
148+
"usage": {
149+
"prompt_tokens": 268,
150+
"completion_tokens": 69,
151+
"total_tokens": 337
152+
},
153+
"system_fingerprint": "fp_927397958d"
154+
}
54155
```
55-
As an AI language model, I don't have personal preferences or can make travel arrangements but here's an outline of what one can experience in San Diego in a 3-day trip:
56156

57-
Day 1:
58-
- Visit Balboa Park, a 1,200-acre cultural oasis with gardens, museums, theaters, and the famous San Diego Zoo.
59-
- Check out the Gaslamp Quarter, a historic district filled with Victorian-era buildings, trendy restaurants, bars, and shops.
60-
- Take in the stunning panoramic views of the city from Cabrillo National Monument, located on the southern tip of the Point Loma Peninsula.
157+
### Limitation
61158

62-
Day 2:
63-
- Spend the day at the beach. San Diego has many beautiful beaches, including La Jolla Cove, Coronado Beach, and Mission Beach.
64-
- Visit the USS Midway Museum, where visitors can board the decommissioned aircraft carrier and explore its 60 exhibits and 29 restored aircraft.
65-
- Check out the Maritime Museum of San Diego, where you can tour historic ships and learn about maritime history.
159+
AVR and ESP8266 may not operate smoothly due to insufficient memory allocation. It looks like this part will need to be updated again soon.
66160

67-
Day 3:
68-
- Explore Old Town San Diego, known as the birthplace of California, with its preserved adobe buildings, museums, and shops.
69-
- Go on a whale watching excursion from the San Diego Harbor.
70-
- Visit the San Diego Botanic Garden, which boasts 37 acres of gardens and includes a tropical rainforest, desert, and bamboo groves.
161+
# Updates
71162

72-
Overall, San Diego offers a wide range of attractions and activities for all tastes and ages.
73-
```
163+
- v0.2.0
164+
- Support Vision Question (GPT-4o)
165+
- Support larger payloads
166+
- ArduinoJson 7
74167

75-
# Updates
76168
- v0.1.2
77169
- Demo Bugs Fixed
78170

examples/Arduino_BearSSLExample/Arduino_BearSSLExample.ino

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,88 @@
22
* Project ChatGPT Client For Arduino
33
* Description: For HTTPS connection using ArduinoBearSSL
44
* Author: Eric Nam
5-
* Date: 03-08-2023
5+
* Date: 05-19-2024
66
*/
77

88
//#define ARDUINO_DISABLE_ECCX08
99
#include <ArduinoBearSSL.h>
1010
#include <WiFi.h>
1111
#include <ArduinoJson.h>
1212
#include <ChatGPT.hpp>
13+
#include "Base64ImageData.h"
1314

1415
static const char *ssid = "<WIFI_SSID>";
1516
static const char *password = "<WIFI_PW>";
17+
int status = WL_IDLE_STATUS;
18+
19+
/*
20+
client: Pointer to the HTTP client object used for making API requests.
21+
api_version: API version to use for communication (e.g., "v1").
22+
api_key: API key for authentication with the ChatGPT API.
23+
timeout: Timeout duration for API requests, in milliseconds.
24+
*/
1625

1726
WiFiClient client;
1827
BearSSLClient sslClient(client);
19-
ChatGPT<BearSSLClient> chat_gpt(&sslClient, "v1", "<OpenAI_API_KEY>");
28+
ChatGPT<BearSSLClient> chatGPT_Client(&sslClient, "v1", "<OpenAI_API_KEY>", 60000);
2029

21-
int status = WL_IDLE_STATUS;
30+
void exampleTextQuestion() {
31+
/*
32+
model: Model to use for generating the response (e.g., "gpt-4o", "gpt-3.5-turbo").
33+
role: Role of the message sender (e.g., "user" or "assistant").
34+
content: Content of the message to send.
35+
max_tokens: Maximum number of tokens to generate in the response.
36+
content_only: Flag indicating whether to extract only the content of the response. (e.g., true - answer only, false - full response)
37+
result: Reference to a String variable to store the result of the API call.
38+
*/
39+
40+
String result;
41+
Serial.println("\n\n[ChatGPT] - Asking a Text Question");
42+
if (chatGPT_Client.chat_message("gpt-3.5-turbo", "user", "What is the best feature of GPT-4o?", 100, false, result)) {
43+
Serial.print("[ChatGPT] Response: ");
44+
Serial.println(result);
45+
} else {
46+
Serial.print("[ChatGPT] Error: ");
47+
Serial.println(result);
48+
}
49+
}
50+
51+
void exampleVisionQuestionBaee64() {
52+
/*
53+
model: Model to use for generating the response (e.g., "gpt-4o").
54+
role: Role of the message sender (e.g., "user" or "assistant").
55+
type: Type of content (e.g., "text").
56+
text: Text content of the message.
57+
image_type: Type of the image (e.g., "image_url").
58+
image_url: URL of the image or Base64 Image Data
59+
detail: Detail level of the image (e.g., "high", "low", "auto").
60+
max_tokens: Maximum number of tokens to generate in the response.
61+
content_only: Flag indicating whether to extract only the content of the response. (e.g., true - answer only, false - full response)
62+
result: Reference to a String variable to store the result of the API call.
63+
*/
64+
65+
String result;
66+
Serial.println("\n\n[ChatGPT] - Asking a Vision Question");
67+
if (chatGPT_Client.vision_question("gpt-4o", "user", "text", "What’s in this image?", "image_url", base64ImageData, "auto", 500, true, result)) {
68+
Serial.print("[ChatGPT] Response: ");
69+
Serial.println(result);
70+
} else {
71+
Serial.print("[ChatGPT] Error: ");
72+
Serial.println(result);
73+
}
74+
}
75+
76+
void exampleVisionQuestionWithURL() {
77+
String result;
78+
Serial.println("\n\n[ChatGPT] - Asking a Vision Question");
79+
if (chatGPT_Client.vision_question("gpt-4o", "user", "text", "What’s in this image?", "image_url", "https://samplelib.com/lib/preview/jpeg/sample-city-park-400x300.jpg", "auto", 400, false, result)) {
80+
Serial.print("[ChatGPT] Response: ");
81+
Serial.println(result);
82+
} else {
83+
Serial.print("[ChatGPT] Error: ");
84+
Serial.println(result);
85+
}
86+
}
2287

2388
unsigned long getTime() {
2489
return WiFi.getTime();
@@ -43,25 +108,11 @@ void setup() {
43108
// DO NOT USE IN PRODUCTION
44109
sslClient.setInsecure(BearSSLClient::SNI::Insecure);
45110

46-
String result;
47-
48-
Serial.println("[ChatGPT] Only print a content message");
49-
if (chat_gpt.simple_message("gpt-3.5-turbo-0301", "user", "Planning a 3-day trip to San Diego", result)) {
50-
Serial.println("===OK===");
51-
Serial.println(result);
52-
} else {
53-
Serial.println("===ERROR===");
54-
Serial.println(result);
55-
}
56-
57-
Serial.println("\n\n[ChatGPT] Print full message(JSON Type)");
58-
if (chat_gpt.full_message("gpt-3.5-turbo", "user", "What is the OpenAI mission?", result)) {
59-
Serial.println("===OK===");
60-
Serial.println(result);
61-
} else {
62-
Serial.println("===ERROR===");
63-
Serial.println(result);
64-
}
111+
Serial.println("[ChatGPT] - Examples");
112+
delay(1000);
113+
exampleTextQuestion();
114+
delay(1000);
115+
exampleVisionQuestionWithURL();
65116
}
66117

67118
void loop() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef BASE64_IMAGE_DATA_H
2+
#define BASE64_IMAGE_DATA_H
3+
4+
// The image is a solid red color.
5+
const char* base64ImageData = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gATQ3JlYXRlZCB3aXRoIEdJTVD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wgARCABLAGQDAREAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAWAQEBAQAAAAAAAAAAAAAAAAAABgj/2gAMAwEAAhADEAAAAZzC6pAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAEFAgL/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAEDAQE/AQL/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAECAQE/AQL/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAY/AgL/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAE/IQL/2gAMAwEAAgADAAAAEP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/8QAFBEBAAAAAAAAAAAAAAAAAAAAcP/aAAgBAwEBPxAC/8QAFBEBAAAAAAAAAAAAAAAAAAAAcP/aAAgBAgEBPxAC/8QAFBABAAAAAAAAAAAAAAAAAAAAcP/aAAgBAQABPxAC/9k=";
6+
7+
#endif

0 commit comments

Comments
 (0)