Commit 1c94c54a authored by Ali Hassan Shah's avatar Ali Hassan Shah

chatgpt_demo: support modify base URL through UF2

Close https://github.com/espressif/esp-box/issues/110
parent 71e06b38
......@@ -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**
......
......@@ -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
\ No newline at end of file
......@@ -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";
......@@ -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();
......@@ -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);
......@@ -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);
}
......
## 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"
......@@ -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);
......
......@@ -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:
......
......@@ -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 password[PASSWORD_SIZE]; /* Password of target AP. */
char key[KEY_SIZE]; /* OpenAI key. */
char url[URL_SIZE]; /* OpenAI Base url. */
} sys_param_t;
esp_err_t settings_factory_reset(void);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment