可以召喚 ChatGPT 使用 WikiData 資料嗎?
-- ChatGPT 整合公開資料的可能
[User:Planetoid]
---
# 大綱
* ChatGPT 案例研究
* 應用在公開資料集
* 公開資料集 x ChatGPT 外掛
* 公開資料集 x ChatGPT x Embedding
* 應用在開放街圖的查詢
* ChatGPT x Wikidata 的應用
* 應用在短詞彙的翻譯
* 詢問 SPARQL 語法
* Embedding 搜尋,以電影為例
---
## ChatGPT 案例研究:<br />應用在公開資料集
* 公開資料集 x ChatGPT 外掛
* 公開資料集 x ChatGPT x Embedding
---
### 應用在公開資料集的 ChatGPT 外掛
ntuaha/OpenTaiwan: A plugin to provide Taiwan government information on chatGPT https://github.com/ntuaha/OpenTaiwan
---
使用的政府公開資料集
* [水庫水情資料 \| 政府資料開放平臺](https://data.gov.tw/dataset/45501) (每日更新1次)
* [水庫代碼 \| 政府資料開放平臺](https://data.gov.tw/dataset/139336)
* [信用卡各項費用及客服電話等資訊 \| 政府資料開放平臺](https://data.gov.tw/dataset/11711)
---
文件上的範例
* 全台水庫狀況如何?
* Shimen Reservoir Water Level Change Trend Chart (石門水庫水位變化趨勢圖)
---
### 問機器人「全台水庫狀況如何?」
OpenTaiwan:
![](https://hackmd.io/_uploads/Syh7ysGs3.png =75%x)
---
問機器人「石門水庫水位變化趨勢圖?」
OpenTaiwan:
![](https://hackmd.io/_uploads/B1uW7wDrh.png)
---
問機器人「石門水庫水位變化趨勢圖?」
啟用外掛 OpenTaiwan + Wolfram:
![](https://hackmd.io/_uploads/rye4Vofin.png =80%x)
---
問機器人「曾文水庫水位變化趨勢圖?」
啟用外掛 Code Interpreter:(可上傳資料集檔案)
![](https://hackmd.io/_uploads/S1n1NoGo2.png =75%x)
---
問機器人「全台水庫中哪三座水庫狀況最低?」
OpenTaiwan:
![](https://hackmd.io/_uploads/HJafrsGsh.png)
---
其他問法
* 哪些水庫的水位穩定上升或下降、激烈變化
* 異常預期 (採用的資料集只有一天範圍)
* 季節性變化 (加上氣象資料、颱風或乾旱事件)
* 政策影響 (加上水文政策的新聞資料)
* 建議行動的點子
---
### 應用在公開資料集的 ChatGPT Embedding
台北市停車場資訊查詢 https://github.com/TheExplainthis/ChatGPT-Master-Guide-Book/tree/main/Chapter7/04-ChatGPT-Parking-Lot-Searching
---
使用的政府公開資料集
* [臺北市停車場資訊](https://data.gov.tw/dataset/128435) 停車場資訊、剩餘停車位數
---
功能流程圖
![](https://hackmd.io/_uploads/BkcmKOSOn.png =75%x)
資料來源:[博客來\-ChatGPT指令大全與創新應用](https://www.books.com.tw/products/0010957366)、icon 來源:[Simple Icons](http://simpleicon.com/)
---
什麼是 Embedding (嵌入)?
Embedding 測量文本字串的關聯性。Embedding 是一個浮點數字的向量(列表)。兩個向量之間的距離衡量它們的關聯性。小距離表示高關聯,大距離表示低關聯。來源:[OpenAI](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings)
---
Embedding 應用:
* 搜尋(根據查詢字串的相關性對結果進行排名)
* 集群(文本字串按相似性進行分組)
* 推薦(具有相關文本字串的項目被推薦)。
* 異常偵測(識別關聯度小的異常值)
* 多樣性測量(對相似性分布進行分析)
* 分類(文本字串按其最相似的標籤進行分類)
---
![](https://hackmd.io/_uploads/SyMeA_BOn.png =500x)
Source: T Mikolov, W Yih, G Zweig (2013) [Linguistic Regularities in Continuous Space Word Representation](https://aclanthology.org/N13-1090.pdf)
---
## ChatGPT 案例研究:應用在開放街圖的查詢
steveattewell/osm-ai-map: Using chatGPT to assist retrieval of map features from OpenStreetMap https://github.com/steveattewell/osm-ai-map
---
<video src="https://i.imgur.com/erxihM0.mp4" style="width: 60%; height: 60%;" muted=true autoplay=true></video>
<!-- https://video.twimg.com/ext_tw_video/1641060448660344836/pu/vid/752x720/SWPwGFlTR2_6Bb1p.mp4?tag=12 -->
source: https://twitter.com/steveattewell/status/1641060760875933696
---
機器人 🤖 扮演「翻譯」的角色
* 輸入:將人類的問題 「吃飯的地方?」
* 翻譯成:[Overpass API](https://wiki.openstreetmap.org/wiki/Overpass_API) queries 語法
---
問機器人:
Write me a data query for Open Streetmap that returns common features known to exist in the OpenStreetMap database that are related to the term "I want to watch a movie" within this bounding-box 25.00846, 121.52591, 25.02221, 121.54269 .
Respond with just the "data" part of the querystring on a single line and no explanation. Format your response like this:
```
data=[out:json][timeout:25];
{INSERT_QUERY_HERE};
out body;;>
;out skel qt;
```
---
![](https://hackmd.io/_uploads/rydFLOwrh.png)
[overpass turbo](https://overpass-turbo.eu/)
---
### 小結:整理現有用法
| 原始資料 | ChatGPT 功用 | 中間處理 | 輸出 |
| -------- | -------- | -------- | -------- |
| 開放資料集 | 輸入問題 「石門水庫水位變化?」 | 取得與分析資料集 | 文字或圖表 |
| 開放街圖 | 輸入問題 「吃飯的地方?」 | 轉換 Overpass API 語法 | 查詢語法 |
---
## ChatGPT 案例研究:<br />應用在短詞彙的翻譯
---
### 機器翻譯的短詞彙問題
😕 中國與台灣用語混淆
例:全息圖、全像圖
![](https://hackmd.io/_uploads/Sy4FL4Vuh.png)
---
😕😕 短詞彙字面直譯
例:荷語 Mexicaanse griep、[2009年H1N1流感疫情](https://zh.wikipedia.org/zh-tw/2009%E5%B9%B4H1N1%E6%B5%81%E6%84%9F%E5%A4%A7%E6%B5%81%E8%A1%8C)
![](https://hackmd.io/_uploads/B1fnI44_n.png)
---
墨西哥人表示 ...
![](https://hackmd.io/_uploads/BJOtP4Vuh.jpg)
---
🔨詢問機器人,可能會犯錯
Bing Chat:
![](https://hackmd.io/_uploads/Bk62TE4u2.png)
---
🔨ChatGPT
![](https://hackmd.io/_uploads/HJeqYNE_2.png)
GPT-4 犯一樣錯誤
---
⚒️ ChatGPT + Web Browsing
![](https://hackmd.io/_uploads/BJX2KVNuh.png)
但是 GPT-4 + Web Browsing 則正確 (註:7/5 瀏覽網頁功能被停用)
---
🔨工人智慧:維基百科上的操作
1. 荷蘭語的維基百科,搜尋「[Mexicaanse griep](https://nl.wikipedia.org/wiki/Mexicaanse_griep)」關鍵字
2. 取得英文維基百科條目名稱「[2009 swine flu pandemic](https://en.wikipedia.org/wiki/2009_swine_flu_pandemic)」(2009年豬流感大流行)、中文維基百科條目名稱「[2009年H1N1流感疫情](https://zh.wikipedia.org/wiki/2009%E5%B9%B4H1N1%E6%B5%81%E6%84%9F%E5%A4%A7%E6%B5%81%E8%A1%8C)」
3. 選擇性:從簡體切換到繁體
---
🔨工人智慧:WikiData 上的操作
1. WikiData 上,直接搜尋「[Mexicaanse griep](https://www.wikidata.org/w/index.php?go=%E5%9F%B7%E8%A1%8C&search=Mexicaanse+griep&search=Mexicaanse+griep&title=Special:Search&ns0=1&ns120=1)」關鍵字
2. 從五筆一模一樣的搜尋結果中篩選
3. 找到繁體中文「[2009年H1N1新型流感疫情](https://www.wikidata.org/wiki/Q101452)」翻譯
---
可能問題:
1. WikiData 上有多筆一模一樣的條目名稱
2. WikiData 條目內可能只有名稱、其他大部分是空的
3. WikiData 條目可能還沒有建立多語言連結
---
🔨 人工流程
![](https://hackmd.io/_uploads/HyS7c4Nun.png)
---
採取的步驟
1. 搜尋維基百科,取得對應的 <英文條目名稱>
2. 將 <英文條目名稱> 當作關鍵字,搜尋中文維基百科對應的 <繁體中文條目名稱>、<簡體中文條目名稱>
3. 將 <英文條目名稱> 當作關鍵字,搜尋 WikiData 對應的 <繁體中文條目名稱>、<簡體中文條目名稱>
4. ~~站內搜尋 https://terms.naer.edu.tw/ 取得的搜尋結果~~ (robots.txt 未開放)
---
🔨給機器人的提示
```
我會提供你幾項工作,我的關鍵字是中文的「3D全息投影」
1. 搜尋維基百科,取得對應的 <英文條目名稱>
2. 將 <英文條目名稱> 當作關鍵字,搜尋中文維基百科對應的 <繁體中文條目名稱>
、<簡體中文條目名稱>
3. 將 <英文條目名稱> 當作關鍵字,搜尋 WikiData對應的 <繁體中文條目名稱>
、<簡體中文條目名稱>
將結果以表格呈現,欄位分別是 (1) 網站來源、(2) 英文條目名稱、
(3) 繁體中文條目名稱、(4) 簡體中文條目名稱
```
---
![](https://hackmd.io/_uploads/rJdJZBVOh.png)
GPT-4 + Web Browsing ([Shared Chat](https://chat.openai.com/share/8ed077c0-5e88-4269-b955-06f2964a26cc))
---
![](https://hackmd.io/_uploads/ByDoer4dn.png)
GPT-4 + Web Browsing ([Shared Chat](https://chat.openai.com/share/9fca0519-4ba9-401b-953f-994cc403e93d))
---
![](https://hackmd.io/_uploads/Syv9mHNO3.png)
Bing Chat
---
敏感詞彙議題?
⭕ 流行性感冒、❌ COVID-19、❌ 武漢肺炎
![](https://hackmd.io/_uploads/SJXMx8BOn.png =619x)
![](https://hackmd.io/_uploads/SkOYxIruh.png =584x279)
---
🔨給機器人的另一版本提示
```
我會提供你幾項工作,我的 <關鍵字> 是中文的「3D全息投影」
1. 搜尋 <關鍵字> site:en.wikipedia.org,取得 <英文條目名稱>
2. 搜尋 <英文條目名稱> site:zh.wikipedia.org
3. 搜尋 <英文條目名稱> site:wikidata.org
將結果以表格呈現,欄位分別是 (1) 網站來源、(2) 英文條目名稱、
(3) 繁體中文條目名稱、(4) 簡體中文條目名稱
```
💡 使用 site 參數,要求搜尋引擎進行站內搜尋
---
### 小結
* 提示: (1) 使用 < ... > 符號,將結果變數化 (2) 使用站內搜尋 site 參數
* 優點:將多次的步驟,整合在一次的機器人提示
* 缺點:機器人讀取網頁內容,有時不太穩定,導致遺缺值
---
## 詢問 ChatGPT SPARQL 語法
👩 「請幫我寫 SPARQL 查詢最近一年上映的電影」
![](https://hackmd.io/_uploads/SJOfGjo7n.png =50%x)
---
```
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?movie ?movieLabel ?publicationDate WHERE {
?movie wdt:P31 wd:Q11424. # 找到實例是電影的實體
?movie p:P577 ?pubdate_statement. # 找到電影的上映日期聲明
?pubdate_statement ps:P577 ?publicationDate. # 從聲明中獲取日期
# 過濾出最近一年的電影
FILTER(
?publicationDate >= xsd:dateTime("2022-04-30T00:00:00Z") &&
?publicationDate <= xsd:dateTime("2023-04-30T00:00:00Z")
)
# 獲取電影標籤
OPTIONAL {
?movie rdfs:label ?movieLabel.
FILTER(LANG(?movieLabel) = "zh")
}
}
ORDER BY DESC(?publicationDate) # 按上映日期降序排列
LIMIT 100 # 限制結果數量為100部電影
```
---
![](https://hackmd.io/_uploads/r1TczisX2.png)
https://w.wiki/6eD6
---
### 詢問 ChatGPT
👩 「請幫我寫 SPARQL 查詢最近一年上映的電影
電影名稱不能是空白」
---
🤖
```
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?movie ?movieLabel WHERE {
?movie wdt:P31 wd:Q11424;
wdt:P577 ?release_date.
FILTER (STR(?movieLabel) != "")
FILTER (?release_date >= "2022-04-30"^^xsd:dateTime && ?release_date <= "2023-04-30"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "zh" }
}
ORDER BY DESC(?release_date)
```
https://w.wiki/6eDo
---
⚠️ No matching records found
![](https://hackmd.io/_uploads/SyGVp3sQh.png)
---
![](https://hackmd.io/_uploads/SJAVAnjm2.png)
---
一年找不到,就找兩年
兩年找不到,就找三年
![](https://hackmd.io/_uploads/Syl9CnsQn.jpg)
image source: [HTTP Cats](https://http.cat/)
---
👩 修改問題
* 「請幫我寫 SPARQL 查詢最近一年上映的電影 英文或中文的電影名稱不能是空白」 --> ok
https://w.wiki/6eDn
* 「請幫我寫 SPARQL 查詢最近一年上映的電影 英文或中文的電影名稱不能是空白 優先顯示中文電影名稱」 --> 無結果
https://w.wiki/6eDs
---
### 詢問 ChatGPT 可能狀況
建議的應用
| 狀況 | 處理方式 |
| -------- | -------- |
| 不懂語法 | 解釋或比較語法 |
| 伺服器錯誤: Unexpected end of JSON input | 提供查詢於法格式可以追問機器人解法 |
---
不建議的應用
* 追加更複雜的條件,可是查詢沒有結果
* 查詢逾時,沒有錯誤訊息可以追問機器人
---
👩 <strike>沒有解法,要多讀書 </strike> 需要 SPARQL 的領域知識
🤖 Fine-tuning 訓練機器人,學習 SPARQL
* [Code completion \- OpenAI API](https://platform.openai.com/docs/guides/code)
* [How to get Codex to produce the code you want\! \| Prompt Engineering](https://microsoft.github.io/prompt-engineering/)
---
## Embedding 搜尋,以電影為例
---
以前的搜尋
![](https://hackmd.io/_uploads/Hy8SKjMs2.gif)
圖源:[Google](https://blog.google/products/chrome/chrome-dino/)
---
1. 關鍵字搜尋
2. 多面向搜尋、篩選
![](https://hackmd.io/_uploads/rkF9hofjn.png =30%x) ![](https://hackmd.io/_uploads/SyM0sizon.png =47%x)
圖源:[WikiData](https://www.wikidata.org/wiki/Wikidata:Tools/Enhance_user_interface/zh#/media/File:Wikidata_-_CiteTool_gadget.png)、[Amzon](https://www.amazon.com)
---
關鍵字比對問題:snake、serpent
---
Embedding 搜尋:輸入想要看的電影劇情
![](https://hackmd.io/_uploads/SJmgCofo2.png)
---
![](https://hackmd.io/_uploads/S1zHl3fjh.png =40%x) ![](https://hackmd.io/_uploads/B1lIg3fsh.jpg =28%x)
圖源:Bing + playgroundai、[A\.I\.創世者海報](https://zh.wikipedia.org/zh-tw/A.I.%E5%89%B5%E4%B8%96%E8%80%85)
---
功能流程圖
![](https://hackmd.io/_uploads/r1gVrhfj3.png)
[source code](https://github.com/planetoid/chatgpt_x_wikidata)
---
遭遇的問題
* WikiData 沒有 Wikipedia 的條目摘要 (extract)
* 有些電影只有英文描述、沒有中文描述
* OpenAI API model `text-embedding-ada-002` 的 token 長度限制:取前 5,500 字
---
不建議的問法
❌ 太短:沙丘
❌ 太具體:2021 年的科幻電影
⭕ 比較長的劇情描述
---
## 結論
| 原始資料 | ChatGPT 功用 | 中間處理 | 輸出 |
| -------- | -------- | -------- | -------- |
| 開放資料集、開放街圖 | 輸入問題 「石門水庫水位變化?」 | 取得與分析資料集 | 文字、圖表或Overpass查詢語法|
| 網路資料 | 完成多次任務 | 站內搜尋 | 翻譯用語 |
| Wikipedia 電影劇情 | Embedding | 相似度比較 | 推薦其他電影 |
---
參考資料
* [维基数据:SPARQL查询服务/查询/示例 \- Wikidata](https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples/zh)
* [Embeddings \- OpenAI API](https://platform.openai.com/docs/guides/embeddings)
* [\[OpenAI\] 使用 PHP 搭配 Embeddings 開發個人化 AI 問答機器人 – YourGPT – 一介資男](https://www.mxp.tw/9785/)
{"title":"ChatGPT x WikiData","breaks":"true","slideOptions":"{\"theme\":\"moon\",\"transition\":\"fade\"}","contributors":"[{\"id\":\"2d64bd97-01e5-4872-b941-bf0089ad4bda\",\"add\":18049,\"del\":7021}]"}