一、前言
當前DataEase BI更多聚焦于BI展示層,然而,在與組件Copilot 以及后續計劃替換的 Sqlbot的融合方面,目前仍存在一些亟待解決的問題,當它們嘗試與 DataEase 進行結合應用時,出現了兩種較為突出的狀況。
一方面,Copilot與 DataEase 的集成割裂感過于強烈。這種割裂不僅體現在操作流程上,用戶需要在不同組件之間頻繁切換,增加了操作的復雜性和繁瑣程度;還表現在數據交互層面,組件之間無法實現無縫的數據傳遞與共享,導致數據的一致性和連貫性受到影響,極大地降低了用戶的使用體驗。
另一方面,Sqlbot組件在進行數據查詢時,往往針對的是全局數據集。這意味著無論用戶當前正在關注哪個具體的大屏展示內容,組件所返回的查詢結果都是基于整個數據集的宏觀信息。但用戶在實際使用過程中,通常更希望針對當前所展示大屏中的特定數據進行問答交互和深入解析,以獲取與當前展示內容緊密相關的詳細信息和分析結果。現有的這種查詢方式與用戶實際需求之間存在明顯偏差,無法精準滿足用戶對于當前展示大屏數據問答和解析的期望。
基于上述痛點,我們將MaxKB對應應用嵌入DataEase公共鏈接大屏,可以實現針對當前大屏的智能問答。
二、實現
1、環境準備
·部署DataEase:詳見 https://dataease.io/docs/v2/installation/online_installation/
·部署Maxkb:詳見 https://maxkb.cn/docs/v2/installation/online_installtion/
2、MaxKB應用制作
2.1 函數庫編寫
·生成DataEase token
import?jwt
import?requestsfrom Crypto.Cipher?import?AES from Crypto.Util.Padding?import?pad from base64?import?b64encode from uuid?import?uuid4 import?time ? def generate_token(accessKey,secretKey): ????"""生成包含AES加密簽名的JWT令牌""" ????# 生成唯一ID和時間戳 ????unique_id = str(uuid4()) ????timestamp = str(time.time_ns()?// 1000000) ????? ????# AES加密邏輯(原aes_encrypt方法的功能) ????src = accessKey +?"|"?+ unique_id +?"|"?+ timestamp ????secret_key = secretKey.encode('utf-8') ????iv = accessKey.encode('utf-8') ????cipher = AES.new(secret_key, AES.MODE_CBC, iv) ????encrypted = cipher.encrypt(pad(src.encode('utf-8'), AES.block_size)) ????signature = b64encode(encrypted).decode('utf-8') ????? ????# 生成并返回JWT令牌 ????token = jwt.encode( ????????{'accessKey': accessKey,?'signature': signature}, ????????secretKey, ????????algorithm='HS256' ????) ????return?token
·調用DataEase查詢看板詳情接口
import?requests
import?json? def findById(token,id,busiFlag,domain): ? ????# 生成簽名和令牌 ????# 設置請求頭 ????headers = { ????????'Accept':?'*/*', ????????'Content-Type':?'application/json', ????????'x-de-ask-token': token ????} ????? ????requestData = { ????????'id': id, ????????'busiFlag': busiFlag, ????????'source':'main', ????????'resourceTable':'core' ????} ? ????# 發送請求 ????response = requests.post(domain+'/de2api/dataVisualization/findById', headers=headers, json=requestData) ????responseJson = json.loads(response.text) ????title_map = {} ????for?key, value in responseJson['data']['canvasViewInfo'].items(): ????????# 獲取每個對象的title作為新的鍵 ????????title = value.get('title') ????????if?title:?# 確保title存在(避免空值) ????????????title_map[title] = value ????????????? ????return?title_map
·獲取圖表數據
import?requests
import?json? def getData(token,key,canvasViewMap,domain): ? ????# 生成簽名和令牌 ????# 設置請求頭 ????headers = { ????????'Accept':?'*/*', ????????'Content-Type':?'application/json', ????????'x-de-ask-token': token ????} ????? ????# 發送請求 ????response = requests.post(domain+'/de2api/chartData/getData', headers=headers, json=canvasViewMap[key]) ????responseJson = json.loads(response.text) ? ? ????data = responseJson['data']['data'] ????? ????shortname_to_name = { ????????field["fieldShortName"]: field["name"] ????????for?field in data["fields"] ????} ? ????mapped_table_rows = [] ????for?row in data["tableRow"]: ????????mapped_row = { ????????????shortname_to_name[shortname]: value ????????????for?shortname, value in row.items() ????????} ????????mapped_table_rows.append(mapped_row) ????????? ????return?mapped_table_rows
2.2高級編排
注:
1、其中涉及的id等信息與de嵌入式那里獲取類似,應用見附件
2、原理是通過接口查詢視圖標題(所以視圖必須得有標題且不一樣,故富文本不支持)
3、中間會用到大模型提取視圖標題,目前有些客戶本地部署的大模型不能正確提取標題
3、部署Nginx
yum install gcc pcre-devel openssl-devel -y
wget http://nginx.org/download/nginx-1.20.2.tar.gz && tar -zxvf nginx-1.20.2.tar.gz && cd nginx-1.20.2 &&?./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-http_sub_module &&?make && make install
cat?/usr/local/nginx/conf/nginx.conf
#user?nobody;
worker_processes??1; ? #error_log?logs/error.log; #error_log?logs/error.log?notice; #error_log?logs/error.log?info; ? #pid???????logs/nginx.pid; ? ? events { ????worker_connections??1024; } ? ? http { ????include??????mime.types; ????default_type?application/octet-stream; ? ????#log_format?main??'$remote_addr - $remote_user [$time_local] "$request" ' ????#??????????????????'$status $body_bytes_sent "$http_referer" ' ????#??????????????????'"$http_user_agent" "$http_x_forwarded_for"'; ? ????#access_log?logs/access.log?main; ? ????sendfile???????on; ????#tcp_nopush????on; ? ????#keepalive_timeout??0; ????keepalive_timeout??65; ? ????#gzip?on; ? ????server_tokens off; ? ? ?????server { ????????????????listen?8088?;? ????????? ????????????????location / { ????????????????????????proxy_pass??http://localhost:9080/;#DataEase ????????????????????????server_name_in_redirect off; ????????????????????????proxy_http_version??????1.1; ????????????????????????proxy_set_header???????Upgrade????????$http_upgrade; ????????????????????????proxy_set_header???????Connection?"upgrade"; ? ????????????????????????proxy_set_header??????????Host $host:$server_port; ????????????????????????proxy_set_header??????????X-Real-IP $remote_addr; ????????????????????????proxy_set_header??????????X-Forwarded-For $proxy_add_x_forwarded_for; ????????????????????????proxy_set_header??????????X-Forwarded-Proto $scheme; ? ????????????????????????sub_filter?'</head>'?'<script async defer src="http://XXX:8080/chat/api/embed?protocol=http&host=XXX:8080&token=XXXX"></script>'; #MaxKB應用嵌入 ????????????????????????sub_filter_once on; ????????????????} ????????} ? ????server { ????????listen???????80; ????????server_name?localhost; ? ????????#charset koi8-r; ? ????????#access_log?logs/host.access.log?main; ? ????????location / { ????????????root??html; ????????????index?index.html index.htm; ????????} ????} }
4、效果展示
三、優化點
- 應用中的提示詞可以根據實際需求優化,也可以與智能問數場景結合;
- 當前nginx那邊多個大屏+對應mk應用需要多個url代理
通過網盤分享的文件:DataEase.mk
鏈接: https://pan.baidu.com/s/1bRJLBHasveXR4l4wPPaE8g?pwd=7htt 提取碼: 7htt