This topic describes how to implement chat features using the device-side C software development kit (SDK).
1. Prepare for development
Follow the instructions in the Create Application document to create an application, purchase a license, and obtain an App ID and AppSecret.
Refer to Device-side C SDK and download the SDK package that corresponds to your chip model.
If you use the semi-managed pattern, follow the instructions in Device-side C SDK to complete the integration with server-side APIs.
2. Adapt the SDK
In the examples and logs in this document, the
IDin formats such as<ID>represents actual data. The data shown in this document is anonymized.qwen_sdk_<PLATFORM>_<SDK_VERSION>.tar.gz
2.1. SDK directory structure
The SDK directory structure is as follows:
- The
includedirectory contains the header files required to use the SDK. Add this directory to the header file path of your project. libqwen_sdk.acontains the core code of the SDK. You must load this library.libc_license.acontains code related to the license mode. You must load this library if you use the license mode. Do not load this library if you use the pay-as-you-go mode.libhal_dummy.acontains dummy Hardware Abstraction Layer (HAL) code. Use it to check the compilation environment before you adapt the HAL. We recommend that you delete this library after the HAL porting is complete.libqwen_test.acontains test code. Load this library for automated testing. Remove it for production.libtinycrypt.acontains dependencies for encryption and decryption. You must load this library if your platform has not already integrated this third-party library.libcjson.acontains SDK dependencies. You must load this library if your platform has not already integrated this third-party library.
2.2. HAL layer adaptation
You must implement the functions declared in the five header files from the SDK package on your development platform.
- Memory module (
qwen_sdk/include/c_utils/hal_util_mem.h)
- Random number module (
qwen_sdk/include/c_utils/hal_util_random.h)
- Storage module (
qwen_sdk/include/c_utils/hal_util_storage.h)
- Time module (
qwen_sdk/include/c_utils/hal_util_time.h)
- Mutex module (
qwen_sdk/include/c_utils/hal_util_mutex.h)
2.3. HAL layer porting acceptance criteria
After you implement the preceding modules, load libqwen_test.a and call the qwen_sdk_test() function in your main program to test the modules. You can view the test results in the output logs.
Provide the output logs to Alibaba Cloud for confirmation.
The following log output shows a successful test:
3. Device initialization
Before using the SDK, go to the Alibaba Cloud Model Studio multimodal console to create an application and obtain an App ID and API key.
- For the license mode, you must also purchase a license to obtain the corresponding AppSecret.
4. Build device-side network communication
The SDK uses the HTTP and WebSocket protocols. You must implement the corresponding processes for sending and receiving data. The SDK only encapsulates data to be sent and parses received data.
4.1. HTTP communication
This document simplifies HTTP communication into two functions: one for sending and one for receiving. You must adapt the implementation to your platform. The following sample code only illustrates the interaction flow.
4.1.1. Device registration
The following code shows an example of device registration:
-
If you use the semi-managed mode, configure the HTTP communication parameters based on the registration API of the
server-side service that you developed. - If you use the fully managed mode, Alibaba Cloud provides the HTTP communication parameters. The access information is as follows: Host: bailian.multimodalagent.aliyuncs.com Register API: /api/device/v1/register
Sample data The following code shows a sample request packet generated by the device-side SDK. This packet is the body field of the HTTP request message.
4.1.2. Device logon
Each time the device connects to the Alibaba Cloud multimodal interactive AI application, it must first obtain a token. The following code shows an example of how to obtain a token:
-
If you use the semi-managed mode, configure the HTTP communication parameters based on the device logon (getToken) API of the
server-side service that you developed. - If you use the fully managed mode, Alibaba Cloud provides the HTTP communication parameters. The access information is as follows: Host: bailian.multimodalagent.aliyuncs.com Register API: /api/token/v1/getToken
4.1.3. Sample data
The following code shows a sample request packet generated by the device-side SDK. This packet is the body field of the HTTP request message.
4.2. WebSocket communication
After you complete the device logon integration, start debugging the WebSocket communication.
The Alibaba Cloud Model Studio multimodal interactive SDK only processes WebSocket data. It does not handle the sending or receiving of data. We recommend that you create separate threads for sending and receiving WebSocket data. The sample code in this document is based on this interaction method.
WebSocket sample code
4.2.1. Establish a WebSocket connection
The following sample code describes how to establish a WebSocket connection. The SDK provides the host, port, api, and header fields. You must fill in the remaining fields, package the data, and send the request.
- TLS version: TLS 1.2 or later
- Enable Server Name Indication (SNI).
- Configure the CA certificate (GlobalSign Root CA - R3). You can also download it from the official GlobalSign website.
The following code shows a sample upgrade request message for establishing a WebSocket connection:
4.2.2. WebSocket data interaction
When using the Alibaba Cloud Model Studio multimodal interactive SDK, all data exchanged over WebSocket must be processed by the SDK. Otherwise, unexpected issues may occur.
The following sample code describes how to implement WebSocket data interaction by creating threads.
-
The recommended buffer size for the data received over WebSocket varies depending on the downstream data format:
- For downstream PCM data, we recommend a buffer size of 8 KB or larger.
- For downstream MP3 data, we recommend a buffer size of 8 KB or larger.
- For downstream Opus data, we recommend a buffer size of 4 KB or larger.
- The buffer size for the data sent over WebSocket must not be less than 1.5 KB.
-
When you call the
c_mmi_analyze_recv_dataorc_mmi_get_send_datafunction, theopcodeisWS_DATA_TYPE_TEXTorWS_DATA_TYPE_BINARY. This value is defined in the SDK header file. To understand this value, refer to the specific platform definitions and the WebSocket protocol.