增加ai模型暴露接口
authorhcj <1224667075@qq.com>
Mon, 17 Nov 2025 04:07:25 +0000 (12:07 +0800)
committerhcj <1224667075@qq.com>
Mon, 17 Nov 2025 04:07:25 +0000 (12:07 +0800)
code/sdk/zhihui/ai_old/chat_websocket_server.cpp
code/sdk/zhihui/ai_old/chat_websocket_server.h
code/sdk/zhihui/ai_old/websocket_server_example.cpp

index c7fc98b..9e179e1 100644 (file)
@@ -169,6 +169,7 @@ chat_event_type_t chat_result_analysis(char *buf, char *out_put, size_t out_put_
         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;
@@ -398,6 +399,63 @@ bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, boo
         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缓存内存
index 1b50825..a79514a 100644 (file)
@@ -127,6 +127,9 @@ errcode_t chat_server_init(void);
  */
 void chat_server_deinit(void);
 
+bool send_ai_system_message( WebSocketClient *chat_client);
+
+
 // bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, bool cache)
 
 /* 全局变量声明 */
index fe325e6..06c0a0c 100644 (file)
@@ -96,6 +96,16 @@ void websocket_event_callback(const ws_event_data_t *event_data)
     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: