Skip to content

Commit

Permalink
Merge branch 'feature/chatgpt-link-update' into 'master'
Browse files Browse the repository at this point in the history
ChatGPT example Update based on OpenAI component

See merge request ae_group/esp-box!92
  • Loading branch information
leeebo committed Nov 28, 2023
2 parents 71e06b3 + 1c94c54 commit 10d166a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
16 changes: 8 additions & 8 deletions examples/chatgpt_demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
| ----------------- | -------------- |
| ESP32-S3-BOX | YES |
| ESP32-S3-BOX-Lite | YES |
| ESP32-S3-BOX-3 | YES |
| ESP32-S3-BOX-3 | YES |


In this example, we are utilizing the OpenAI API in conjunction with an ESP-BOX to create a voice-based chatbot. The ESP-BOX is a device or system that incorporates an ESP32-S3 microcontroller. The purpose of this implementation is to enable users to communicate with the chatbot using spoken language. The process involves capturing audio input from the user, sending it to the OpenAI API for processing, and receiving a response that is then converted into speech and played back to the user.
Expand Down Expand Up @@ -32,7 +32,7 @@ git checkout 53ff7d43dbff642d831a937b066ea0735a6aca24 && git pull && git submodu
Due to the lack of native text-to-speech support in the [OpenAI](https://platform.openai.com/docs/api-reference) API, an external API is used to meet this requirement. This example utilizes the text-to-speech functionality offered by [TalkingGenie](https://www.talkinggenie.com/tts). Additional information can be found in this [blog post](https://czyt.tech/post/a-free-tts-api/?from_wecom=1).

### **Build and Flash**
There is another project called **factory_nvs** within the **ChatGPT_demo** project. It includes the code to store credentials in the NVS (Non-Volatile Storage) of the ESP-Box. On the other hand, **Chat_GPT Demo** consists of the demo code. Therefore, it is essential to build both projects.
There is another project named **factory_nvs** within the **ChatGPT_demo** project. It includes the code to store credentials in the NVS (Non-Volatile Storage) of the ESP-Box. On the other hand, **Chat_GPT Demo** consists of the demo code. Therefore, it is essential to build both projects.


**1. Clone the Github repository**
Expand All @@ -49,13 +49,13 @@ cd examples/chatgpt_demo/factory_nvs

```

**3. Hardware Selection**
**3. Hardware Selection**

To select the appropriate hardware ([ESP32-S3-BOX](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box/hardware_overview_for_box.md), [ESP32-S3-BOX-Lite](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box_lite/hardware_overview_for_lite.md) or [ESP32-S3-BOX-3](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box_3/hardware_overview_for_box_3.md)) and access the **HMI Board Cofig**, use the following command.


```bash
idf.py menuconfig
idf.py menuconfig

```

Expand All @@ -72,12 +72,12 @@ idf.py build
cd examples/chatgpt_demo/

```
**6. Hardware Selection**
**6. Hardware Selection**

To select the appropriate hardware ([ESP32-S3-BOX](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box/hardware_overview_for_box.md), [ESP32-S3-BOX-Lite](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box_lite/hardware_overview_for_lite.md) or [ESP32-S3-BOX-3](https://github.com/espressif/esp-box/blob/master/docs/hardware_overview/esp32_s3_box_3/hardware_overview_for_box_3.md)) and access the **HMI Board Cofig**, use the following command.

```bash
idf.py menuconfig
idf.py menuconfig

```

Expand Down Expand Up @@ -108,8 +108,8 @@ Follow the [blog posts](https://blog.espressif.com/), [demos and tutorials](http
2. BLOG: [OpenAI Component | Accelerating the integration of OpenAI APIs in projects](https://blog.espressif.com/openai-component-accelerating-the-integration-of-openai-apis-in-projects-e5fa87998126)
3. Tutorial: [ESP Tutorial: Unleashing the Power of ESP32 S3 BOX 3 with OpenAI](https://www.youtube.com/watch?v=Y97vdw7y3S4&t=2s)

## **Note**
Please note that,
## **Note**
Please note that,
1. To proceed with the demo, you need an **OpenAI API key**, and you must possess valid tokens to access the OpenAI server.
2. To provide the WIFI credentials and the OpenAI secret key, please follow the on display prompts to proceed.
3. Additionally, as a result of **OpenAI's restrictions**, this particular example cannot be supported within Mainland China.
Expand Down
5 changes: 5 additions & 0 deletions examples/chatgpt_demo/factory_nvs/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ menu "Example Configuration"
default "sk-xxxxxxxx"
help
OpenAI api key for the example to use.
config OPENAI_URL
string "Base URL"
default "https://api.openai.com/v1/"
help
Base URL for OpenAI API

endmenu
21 changes: 20 additions & 1 deletion examples/chatgpt_demo/factory_nvs/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define SSID_SIZE 32
#define PASSWORD_SIZE 64
#define KEY_SIZE 64
#define URL_SIZE 64

static const char *TAG = "ChatGPT_NVS";

Expand All @@ -46,6 +47,7 @@ void app_main(void)
char ssid[SSID_SIZE] = {0};
char password[PASSWORD_SIZE] = {0};
char key[KEY_SIZE] = {0};
char url[URL_SIZE] = {0};

s_event_group = xEventGroupCreate();

Expand Down Expand Up @@ -90,6 +92,16 @@ void app_main(void)
} else {
ESP_LOGI(TAG, "stored ChatGPT key:%s", key);
}

buf_len_long = sizeof(url);
err = nvs_get_str(my_handle, "Base_url", url, &buf_len_long);
if (err != ESP_OK || buf_len_long == 0) {
ESP_ERROR_CHECK(nvs_set_str(my_handle, "Base_url", CONFIG_OPENAI_URL));
ESP_ERROR_CHECK(nvs_commit(my_handle));
ESP_LOGI(TAG, "no base url, give a init value to key");
} else {
ESP_LOGI(TAG, "stored base url:%s", url);
}
}
nvs_close(my_handle);

Expand Down Expand Up @@ -146,7 +158,14 @@ void app_main(void)
}
ESP_LOGD(TAG, "OpenAI Key", key);


buf_len_long = sizeof(url);
err = nvs_get_str(my_handle, "Base_url", url, &buf_len_long);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to read 'BASE_url' from NVS: %s", esp_err_to_name(err));
nvs_close(my_handle);
return;
}
ESP_LOGD(TAG, "BASE url", url);
nvs_close(my_handle);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/chatgpt_demo/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/openai: "^0.1.1"
espressif/openai: "^0.2.0"
chmorgan/esp-file-iterator: "1.0.0"
espressif/esp-sr: "1.3.3"
3 changes: 2 additions & 1 deletion examples/chatgpt_demo/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ esp_err_t start_openai(uint8_t *audio, int audio_len)

if (openai == NULL) {
openai = OpenAICreate(sys_param->key);
OpenAIChangeBaseURL(openai, sys_param->url);
audioTranscription = openai->audioTranscriptionCreate(openai);
chatCompletion = openai->chatCreate(openai);

Expand Down Expand Up @@ -103,7 +104,7 @@ esp_err_t start_openai(uint8_t *audio, int audio_len)
vTaskDelay(pdMS_TO_TICKS(SCROLL_START_DELAY_S * 1000));
ui_ctrl_reply_set_audio_start_flag(true);
}
// Clearing resources
// Clearing resources
result->delete(result);
free(text);
return ESP_OK;
Expand Down
10 changes: 10 additions & 0 deletions examples/chatgpt_demo/main/settings/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ esp_err_t settings_read_parameter_from_nvs(void)
ESP_LOGI(TAG, "No OpenAI key found");
goto err;
}

// Read url
len = sizeof(g_sys_param.url);
ret = nvs_get_str(my_handle, "Base_url", g_sys_param.url, &len);
if (ret != ESP_OK || len == 0) {
ESP_LOGI(TAG, "No OpenAI Base url found");
goto err;
}

nvs_close(my_handle);

ESP_LOGI(TAG, "stored ssid:%s", g_sys_param.ssid);
ESP_LOGI(TAG, "stored password:%s", g_sys_param.password);
ESP_LOGI(TAG, "stored OpenAI:%s", g_sys_param.key);
ESP_LOGI(TAG, "stored Base URL:%s", g_sys_param.url);
return ESP_OK;

err:
Expand Down
6 changes: 4 additions & 2 deletions examples/chatgpt_demo/main/settings/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#define SSID_SIZE 32
#define PASSWORD_SIZE 64
#define KEY_SIZE 64
#define URL_SIZE 64

typedef struct {
char ssid[SSID_SIZE]; /* SSID of target AP. */
char ssid[SSID_SIZE]; /* SSID of target AP. */
char password[PASSWORD_SIZE]; /* Password of target AP. */
char key[KEY_SIZE]; /* OpenAI key. */
char key[KEY_SIZE]; /* OpenAI key. */
char url[URL_SIZE]; /* OpenAI Base url. */
} sys_param_t;

esp_err_t settings_factory_reset(void);
Expand Down

0 comments on commit 10d166a

Please sign in to comment.