ESP32S3接入文心一言

ESP32S3接入文心一言

Author: kkl


一、概述

使用ESP32S3接入文心一言,其实是为我后面的某个小项目做铺垫啦!这里记录一下实现的过程。

环境

  • 主控:ESP32S3N16R8
  • 平台:Arduino + PlatformIO + VScode

二、步骤

  1. 在百度智能云的云千帆控制台并创建应用获取API KeySecret Key
  2. 根据创建应用生成的API KeySecret Key来获取access_token
  3. 在ESP32S3中发送POST请求API

三、实现

1. 在百度智能云的云千帆控制台并创建应用获取API KeySecret Key

首先要使用云账号登录百度智能云,然后进入千帆大模型平台,创建一个新应用。(注意:分清楚百度账号还是云账号,这两个不一样的)

百度智能云千帆控制台:https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application

创建新应用:

创建好应用以后就可以得到API KeySecret Key

2. 根据创建应用生成的API KeySecret Key来获取access_token

进入API代码调试界面:

应用列表选择自己创建的ESP32S3智能语音助手

获取access_token

指令格式:"role":"user","content":"介绍一下名侦探柯南这部动画"

调试结果如下:

3. 在ESP32S3中发送POST请求API

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
String apiERNIEbotUrl = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=xxxxxxxxx"; // 把申请的access_token填上去
String inputText = "你好!";

/* 文心一言 */
String getGPTResponse(String inputText)
{
HTTPClient http_client;

http_client.begin(apiERNIEbotUrl);
http_client.addHeader("Content-Type", "application/json");

http_client.setTimeout(20000); // 20s的超时时间

String payload = "{\"messages\":[{\"role\": \"user\",\"content\": \"" + inputText + "\"}],\"disable_search\": false,\"enable_citation\": false}";

int httpResponseCode = http_client.POST(payload);

if (httpResponseCode == HTTP_CODE_OK)
{
String response = http_client.getString();

http_client.end();
_LOG("\r\n%s\r\n", response.c_str());

// Parse JSON response
DynamicJsonDocument doc(2048);
deserializeJson(doc, response);

String outPutText = doc["result"];
return outPutText;
}
else
{
_LOG("[HTTP] GET... failed, error: %s\n", http_client.errorToString(httpResponseCode).c_str());
http_client.end();

return "[HTTP] GET... failed, error!";
}
}

四、总结

意外的顺利耶!Yapi😘

  • 要注意的是,文心一言的API新用户第一个月可以获得一张20元的优惠券有效期1个月,1个月以后就要付费使用文心一言啦!
  • access_token默认有效期30天,单位是秒,生产环境注意及时刷新。刷新了旧的也能用(只要不超时)

鸣谢

项目教程:https://blog.csdn.net/vor234/article/details/135372118


ESP32S3接入文心一言
https://zhangkeliang0627.github.io/2024/03/25/ESP32S3接入文心一言/README/
Author
Zhang-keliang
Posted on
March 25, 2024
Licensed under