printf("Chat result parse failed\n");
return CHAT_EVENT_ERROR;
}
+ // printf("Chat result parse %s\n",buf);
char *type_str = NULL;
char *content_str = NULL;
return false;
}
}
+// ws_server_send_message(g_asr_server,( uint8_t *)data,size,WS_TEXT);
+
+// {
+// "type": "chat",
+// "content": "你好,你是谁",
+// "system_message":"你是小瑞,是一个人工智障"
+// }
+
+bool send_ai_system_message( WebSocketClient *chat_client)
+{
+ cJSON* json = cJSON_CreateObject();
+ if (json == NULL) {
+ printf("Failed to create JSON object\n");
+ return false;
+ }
+
+
+ // 添加header字段
+ if (cJSON_AddStringToObject(json, "type", "chat") == NULL) {
+ printf("Failed to add event field\n");
+ return false;
+ }
+
+ if (cJSON_AddStringToObject(json, "content", "你好,你是谁") == NULL) {
+ printf("Failed to add trace_id field\n");
+ cJSON_Delete(json);
+ return false;
+ }
+
+ // 将header添加到根对象
+ if (cJSON_AddStringToObject(json, "system_message", "你是中国移动的智能助理灵犀。如果别人询问你的身份,你会回答:“您好,我是中国移动智能助理灵犀”。我能够回答各种类型的问题。如果遇到我无法解答的问题,我会告知您:“您的问题超出了我的能力范围,请尝试问其他问题。")== NULL)
+ {
+ printf("Failed to add header to root object\n");
+ cJSON_Delete(json);
+ return false;
+ }
+ char *jsonString = cJSON_PrintUnformatted(json);
+
+ if (jsonString == NULL) {
+ printf("Error printing JSON to string\n");
+ cJSON_Delete(json);
+ return false;
+ }
+
+ uint32_t data_len = strlen(jsonString);
+ cJSON_Delete(json);
+
+ if (send_websocket_message(chat_client, (uint8_t *)jsonString, strlen(jsonString), WS_TEXT) == 0)
+ {
+ printf("send_websocket_message succ\n");
+ }
+ cJSON_free(json);
+ return true;
+
+}
+
+
/**
* @brief 初始化Chat缓存内存
switch (event_data->event_type) {
case WS_EVENT_CONNECTED:
printf("WebSocket connected: type=%d\n", event_data->server_type);
+ switch (event_data->server_type)
+ {
+ case WS_SERVER_TYPE_CHAT:
+
+ if(g_chat_ai_type==1)
+ {
+ send_ai_system_message(g_chat_server->client);
+ }
+
+ }
break;
case WS_EVENT_DATA_RECEIVED: