使用 GPT 搜尋網路資料

OpenAI 的 GPT 提供強大的連網功能,而且現在免費和付費的 GPT 用戶都能享用。

https://openai.com/chatgpt/pricing/

彼得潘實測後,覺得連網功能的確滿方便的,尤其在程式開發上 AI 可以從指定的網址讀取內容,我們不用再辛苦貼上超長的程式和 JSON。不過 AI 也並非完美,為了連網它會花費較長的時間,而且有時會找不到想要的資訊。

以下我們以多個例子實測 GPT 的連網功能。

實測 Browse with Bing 功能

  • 將網頁的公車路線資訊整理成 JSON。
  • 將全聯的牛排資料整理成 JSON。
  • 定義 API 網址回傳的 JSON 對應的 Codable 型別。
  • 用 SwiftUI,JSONDecoder & AsyncImage 寫一個 App 清單畫面,資料來自 API 網址。
  • 說明 GitHub 的程式。
  • 幫 GitHub 的程式加上注解。
  • 將 wiki 內容整理成 JSON。
  • 查詢 WWDC22 的 SwiftUI 影片。
  • 將 WWDC22 的 SwiftUI 影片資訊整理成 JSON 格式。
  • 查詢台灣今年七月推出的 iOS App 開發課程。
  • 查詢今年七月彼得潘推出的 iOS App 開發課程。
  • 將 Taylor Swift YouTube 頻道的影片資訊整理成 JSON 格式。
  • 台北這星期新上映的電影。
  • 用 JSON 定義最近五代 iPhone 的相關資訊。

用 JSON 寫一個台灣公車 307 的路線資訊,路線資訊如下 https://ebus.gov.taipei/EBus/VsSimpleMap?routeid=0100030700&gb=1

點選 Finished browsing 旁的箭頭可查詢 GPT 查資料的過程做了哪些事。

{
"route": "307",
"adminArea": "臺北市",
"startEndStops": "板橋 - 撫遠街",
"serviceTimes": {
"weekday": "0500~2210",
"weekend": "0500~2210"
},
"frequency": {
"weekday": {
"peak": "4分至6分",
"offPeak": "5分至10分",
"after2100": "5-10分一班次"
},
"weekend": {
"peak": "7分至10分",
"offPeak": "7分至10分",
"after2100": "10-15分一班次"
}
},
"bufferZone": "萬大國小-中和國稅局",
"fare": "兩段票",
"servicePhone": {
"首都客運": "0800-000-866",
"臺北客運": "0800-003-307"
},
"stops": [
"莊敬里",
"新益里",
"民生國中",
"新東里",
"發電所(西松高中)",
"南松山(塔悠)",
"南松山(南京)",
"南京公寓(捷運南京三民)",
"南京三民路口",
"南京寧安街口",
"南京敦化路口(小巨蛋)",
"捷運南京復興站",
"南京龍江路口",
"南京建國路口",
"捷運松江南京站",
"南京吉林路口",
"南京林森路口",
"中山市場",
"行政院",
"臺北車站(忠孝)",
"中華路北站",
"捷運西門站",
"小南門",
"聯合醫院和平院區",
"大埔街",
"莒光派出所",
"萬大路",
"西藏路口",
"德昌街口",
"萬大國小",
"果菜市場",
"華中橋",
"捷運中和站",
"連城景平路(暫時裁撤)",
"中和國稅局",
"台貿一村",
"連城錦和路口(暫時裁撤)",
"連城中正路口",
"連城路",
"中和高中(員山路)",
"員山派出所",
"積穗國中",
"員山公園",
""積穗",
"板橋監理站",
"環球購物中心",
"國泰街口",
"電信訓練所",
"民族區運路口",
"深丘里(板橋放送所)",
"新北市政府(新府路)",
"新北板橋公車站",
"萬坪公園",
"北門街",
"板橋戶政事務所(黃石中繼市場)",
"社后",
"大庭新村",
"港尾福德宮",
"港尾",
"板橋國中(435藝文特區)",
"名來新城(中正路)",
"臺北客運板橋前站(藝文)"
]
}

網頁上的路線圖如下,看來 AI 的確成功用 307 的路線生成 JSON。

值得注意的, GPT 讀取某些網頁會出問題,因此不能假設讀取會 100% 成功。

將全聯的牛排資料整理成 JSON http://event.pxmart.com.tw/activity/2020steak/

{
"Steaks": [
{
"Name": "冷藏美國翼板霜降牛排",
"Price": "$76.5元/100g",
"Promotion Period": "6/05~6/11",
"Promotion Price": "$75元/100g"
},
{
"Name": "冷藏澳洲穀飼牛梅花牛排",
"Price": "$56元/100g",
"Promotion Period": "6/05~6/11",
"Promotion Price": "$53元/100g"
},
{
"Name": "澳洲榖飼帶骨牛小排",
"Weight": "約300g/盒",
"Promotion Period": "6/05~7/02",
"Promotion Price": "$245/盒"
},
{
"Name": "空運安格斯菲力牛排",
"Weight": "約200g/盒",
"Promotion Period": "6/05~6/18",
"Promotion Price": "$429/盒"
},
{
"Name": "冷藏美國無骨牛小排",
"Price": "$104元/100g",
"Promotion Period": "6/12~6/18",
"Promotion Price": "$101元/100g"
},
{
"Name": "澳洲牛肩里肌牛排",
"Price": "$63元/100g",
"Promotion Period": "6/12~6/18",
"Promotion Price": "$59元/100g"
},
{
"Name": "空運安格斯紐約客牛排",
"Weight": "約200g/盒",
"Promotion Period": "6/05~6/18",
"Promotion Price": "$329/盒"
},
{
"Name": "空運安格斯無骨牛小排",
"Weight": "約200g/盒",
"Promotion Period": "6/19~6/25",
"Promotion Price": "$339/盒"
},
{
"Name": "空運安格斯沙朗牛排",
"Weight": "約200g/盒",
"Promotion Period": "6/26~7/02",
"Promotion Price": "$369/盒"
}
]
}

定義以下 API 回傳的 JSON 對應的 Codable 型別
https://rss.applemarketingtools.com/api/v2/us/apps/top-free/10/apps.json

import Foundation

struct Feed: Codable {
let feed: FeedDetails
}

struct FeedDetails: Codable {
let title: String
let id: String
let author: Author
let links: [Link]
let copyright: String
let country: String
let icon: String
let updated: String
let results: [App]
}

struct Author: Codable {
let name: String
let url: String
}

struct Link: Codable {
let selfLink: String

enum CodingKeys: String, CodingKey {
case selfLink = "self"
}
}

struct App: Codable {
let artistName: String
let id: String
let name: String
let releaseDate: String
let kind: String
let artworkUrl100: String
let genres: [String]
let url: String
}

用 SwiftUI,JSONDecoder & AsyncImage 寫一個 App 清單畫面,資料來自以下網址,包含 App 名字和縮圖。 https://rss.applemarketingtools.com/api/v2/us/apps/top-free/10/apps.json

GPT 定義的資料型別名稱 App 會跟內建的 App protocol 衝突,因此以下我們將它改成 AppInfo。

  • TopApps.swift
import Foundation

struct Feed: Codable {
let results: [AppInfo]
}

struct AppInfo: Codable, Identifiable {
let id: String
let artistName: String
let name: String
let artworkUrl100: URL
}

struct TopApps: Codable {
let feed: Feed
}
  • ViewModel.swift
import SwiftUI

class ViewModel: ObservableObject {
@Published var apps: [AppInfo] = []

func fetchApps() {
guard let url = URL(string: "https://rss.applemarketingtools.com/api/v2/us/apps/top-free/10/apps.json") else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let topApps = try JSONDecoder().decode(TopApps.self, from: data)
DispatchQueue.main.async {
self.apps = topApps.feed.results
}
} catch {
print("Failed to decode JSON: \(error)")
}
} else if let error = error {
print("Failed to fetch data: \(error)")
}
}.resume()
}
}
  • ContentView.swift
import SwiftUI

struct ContentView: View {
@StateObject var viewModel = ViewModel()

var body: some View {
NavigationView {
List(viewModel.apps) { app in
HStack {
AsyncImage(url: app.artworkUrl100) { image in
image.resizable().frame(width: 50, height: 50)
} placeholder: {
ProgressView()
}
VStack(alignment: .leading) {
Text(app.name)
.font(.headline)
Text(app.artistName)
.font(.subheadline)
}
}
}
.navigationTitle("Top Free Apps")
}
.onAppear {
viewModel.fetchApps()
}
}
}

GPT 變得更厲害了。現在只要給它 API 網址,它就能解析裡面的 JSON,生出有著連網功能的 iOS App。

請說明以下程式
https://github.com/apple/sample-food-truck/blob/main/App/Donut/TopFiveDonutsView.swift

請幫以下程式加上注解 https://raw.githubusercontent.com/apple/sample-food-truck/main/App/Donut/TopFiveDonutsView.swift

// 引入所需的模組
import Charts // 用於繪製圖表
import SwiftUI // 用於建立使用者介面
import FoodTruckKit // 這應該是一個自定義的模組,可能包含了和食物相關的數據模型和邏輯
import AppIntents // 用於與 Siri 進行互動

// 定義一個 SwiftUI View,名為 TopFiveDonutsView
struct TopFiveDonutsView: View {
// 這個 View 觀察一個 FoodTruckModel 物件
@ObservedObject var model: FoodTruckModel

// timeframe 用來表示時間範圍,初始設為一週
@State private var timeframe: Timeframe = .week

// today 是當前日期的開始時間
let today = Calendar.current.startOfDay(for: .now)

// body 是 View 的主體,定義了界面的布局和行為
var body: some View {
// 使用 ScrollView 包裹所有元素,當內容超出視圖範圍時,可以滾動查看
ScrollView {
// VStack 是垂直堆疊的 View
VStack(alignment: .leading) {
// Picker 允許使用者選擇時間範圍
Picker("Timeframe", selection: $timeframe) {
Text("Day").tag(Timeframe.today)
Text("Week").tag(Timeframe.week)
Text("Month").tag(Timeframe.month)
Text("Year").tag(Timeframe.year)
}
.pickerStyle(.segmented)
.padding(.bottom, 10)

// 顯示 TopFiveDonutsChart,這個 View 顯示售出最多的五種甜甜圈的圖表
TopFiveDonutsChart(model: model, timeframe: timeframe)

// 在 iOS 上,SiriTipView 會顯示一些與 Siri 相關的提示
#if os(iOS)
SiriTipView(intent: ShowTopDonutsIntent(timeframe: timeframe))
.padding(.top, 20)
#endif
}
.padding()
}
.navigationTitle("Top 5 Donuts")
.background()
}
}

// 這部分的程式碼是為了預覽 TopFiveDonutsView
struct TopFiveDonutsView_Previews: PreviewProvider {
struct Preview: View {
@StateObject private var model = FoodTruckModel()

var body: some View {
TopFiveDonutsView(model: model)
}
}

static var previews: some View {
NavigationStack {
Preview()
}
}
}

參考以下網頁,用 JSON 寫出劉德華的相關資訊,key 用英文,JSON 字數 1000 字以內。只需要參考以下網頁的內容,不需要再連到其它網頁。 https://zh.wikipedia.org/zh-tw/劉德華

{
"name": "劉德華",
"romanized_name": "Lau Tak Wah",
"english_name": "Andy Lau",
"nicknames": ["華仔", "劉天王"],
"nationality": "中華人民共和國(香港)",
"origin": "廣東新會縣荷塘鎮(今江門市)",
"birth_date": "1961-09-27",
"birth_place": "英屬香港新界大埔泰亨鄉(菜坑村)",
"profession": ["演員", "歌手", "監製", "填詞人", "出品人"],
"languages": ["粵語", "英語", "國語"],
"education": [
"大磡村街坊福利會小學",
"黃大仙天主教小學畢業",
"可立中學中五畢業",
"可立中學中六預科肄業",
"無線電視藝員訓練班第10期畢業",
"四川省川劇學校註冊學習"
],
"honorary_degrees": [
"藍仕橋大學榮譽博士",
"香港樹仁大學榮譽文學博士",
"香港演藝學院榮譽院士"
],
"religion": "佛教",
"spouse": "朱麗倩",
"marriage_year": "2008",
"children": 1,
"music_genres": ["華語流行", "粵語流行"],
"debut_year": "1981",
"debut_works": {
"tv_show": "香港香港:江湖再見",
"movie": "彩雲曲",
"album": "只知道此刻愛你"
},
"representative_works": {
"music_albums": [
"真我的風采",
"答案就是你",
"忘情水",
"五時三十分",
"真永遠",
"愛在刻骨銘心時",
"男人的愛",
"心藍",
"美麗的一天",
"如果有一天"
],
"songs": [
"可不可以",
"我恨我痴心",
"如果你是我的傳說",
"愛不完",
"再吻我吧",
"一起走過的日子",
"來生緣",
"謝謝你的愛",
"真情難收",
"獨自去偷歡",
"開心的馬騮",
"暗裡著迷",
"忘情水",
"天意",
"真永遠",
"今天",
"中國人",
"情深的一句",
"你是我的女人",
"笨小孩",
"孤星淚",
"冰雨",
"世界第一等",
"愛你一萬年",
"男人哭吧不是罪",
"我不夠愛你",
"心藍",
"當我遇上你",
"夏日Fiesta",
"練習",
"無間道",
"如果有一天",
"17歲"
],
"movies": [
"投奔怒海",
"法外情",
"最佳損友",
"旺角卡門",
"至尊無上",
"天若有情",
"異域",
"賭俠",
"與龍共舞",
"九一神鵰俠侶",
"雷洛傳",
"天長地久",
"天與地",
"大冒險家",
"烈火戰車",
"新上海灘",
"龍在江湖",
"賭俠1999",
"賭俠大戰拉斯維加斯"
]
}
}

網頁裡有哪些 SwiftUI 相關的影片,網址如下 https://developer.apple.com/videos/wwdc2022

用 JSON 格式列出網頁裡五部 SwiftUI 相關影片的資料,包含影片名稱,影片網址,縮圖網址,簡介,影片時間。 網址如下 https://developer.apple.com/videos/wwdc2022

AI 的回答還不錯,不過也有以下缺點。

  • 找不到影片的縮圖網址和時間長度。
  • 有些影片跟 SwiftUI 無關。
  • 只找到 4 部影片,不是我們要求的 5 部。
  • 格式不是我們要求的 JSON。

彼得潘接著請 AI 將剛剛找到的 4 部影片資訊整理成 JSON 格式,包含影片名稱、影片網址、簡介,這次 AI 順利產生了 JSON 格式的結果。

[
{
"title": "Display EDR content with Core Image, Metal, and SwiftUI",
"url": "https://developer.apple.com/videos/play/wwdc2022/10114/",
"description": "Discover how you can add support for rendering in Extended Dynamic Range (EDR) from a Core Image based multi-platform SwiftUI application. We'll outline best practices for displaying CIImages to a MTKView using ViewRepresentable. We'll also share the simple steps to enable EDR rendering and explore some of the over 150 built-in CIFilters that support EDR."
},
{
"title": "Deliver reliable streams with HLS Content Steering",
"url": "https://developer.apple.com/videos/play/wwdc2022/10144/",
"description": "HLS Content Steering dynamically steers clients to different servers based on load and redundancy. We'll take you through the latest updates to this framework and explore how you can introduce dynamically spawned CDNs to existing HLS clients with Pathway Cloning. We'll also share how you can achieve global traffic steering with bucket-based Steering Server rules and more."
},
{
"title": "Display HDR video in EDR with AVFoundation and Metal",
"url": "https://developer.apple.com/videos/play/wwdc2022/110565/",
"description": "Learn how you can take advantage of AVFoundation and Metal to build an efficient EDR pipeline. Follow along as we demonstrate how you can use AVPlayer to display HDR video as EDR, add playback into an app view, render it with Metal, and use Core Image or custom Metal shaders to add video effects such as keying or color management. Whether you develop games or pro apps, we'll help you decide which frameworks to use and share best practices for selecting transports, colorspaces, and pixelbuffer formats."
},
{
"title": "What’s new in Wallet and Apple Pay",
"url": "https://developer.apple.com/videos/play/wwdc2022/10041/",
"description": "Discover the latest updates to Wallet & Apple Pay. We'll show you how to support Orders in Wallet for your apps and websites and securely validate someone's age and identity with the Identity Verification API. We'll also explore PassKit support for SwiftUI, and discuss how you how you can improve your Apple Pay experience with Automatic Payments."
}
]

台灣今年七月有推出哪些 iOS App 開發課程?

AI 沒有找到彼得潘的課程,令人有點難過。沒關係,我們再給 AI 一次機會,這次問得具體一點。

今年七月彼得潘有推出 iOS App 開發課程嗎

GPT 只找到一個,彼得潘其實有開兩門課程,因此我們再問他,今年七月彼得潘還有推出別的課程嗎。

AI 的回答還不錯,找到的第二個跟第三個都是彼得潘的課程。雖然他可能突然想秀英文,變成用英文回答。

將 Taylor Swift YouTube 頻道的影片資訊整理成 JSON 格式

可惜失敗了,GPT 無法抓取某些網站的資料。

台北這星期新上映的電影有哪些

用 JSON 定義最近五代 iPhone 的相關資訊,第一層是 [ ],資料以台灣中文呈現,key 用英文

AI 只找到最近兩代的 iPhone,不過其中一代竟然是還沒發表的 iPhone 15。

[
{
"model": "iPhone 15",
"announcement": "2023年9月預期公告",
"description": "iPhone 15 的設計將橋接標準和高階模型之間的差距。預計將取代 'Pro Max' 品牌的全新 'Ultra' iPhone 15 模型將可能於明年推出。預計所有或部分 iPhone 15 系列模型將放棄專有的 Lightning 接口,轉向 USB-C 接口。所有四款預定於明年推出的 iPhone 15 模型都將採用丸孔顯示設計,取代了凹口。",
"key_features": [
{
"feature": "Display",
"description": "Pill-and-hole display design"
},
{
"feature": "Port",
"description": "USB-C"
},
{
"feature": "Chip",
"description": "Next-generation technology from TSMC"
}
]
},
{
"model": "iPhone 14",
"description": "iPhone 14 和 iPhone 14 Pro 的螢幕大小為 6.1 吋,而 iPhone 14 Plus 和 iPhone 14 Pro Max 的螢幕大小為 6.7 吋。iPhone 14 和 iPhone 14 Plus 使用的是升級的 A15 晶片,iPhone 14 Pro 和 iPhone 14 Pro Max 則是全新的 A16 晶片。所有的模型都有 6 核心 CPU、5 核心 GPU 以及 16 核心神經網絡,但 A16 晶片在資料的處理頻率上更快。",
"key_features": [
{
"feature": "Display",
"description": "6.1 inch for iPhone 14 and iPhone 14 Pro, 6.7 inch for iPhone 14 Plus and iPhone 14 Pro Max"
},
{
"feature": "Chip",
"description": "A15 chip for iPhone 14 and iPhone 14 Plus, A16 chip for iPhone 14 Pro and iPhone 14 Pro Max"
},
{
"feature": "Camera",
"description": "Upgraded main camera for iPhone 14 Pro and iPhone 14 Pro Max with 48 million pixels, second-generation optical stabilization system, supports night portrait, macro photography, and Apple ProRAW format shooting"
}
]
}
]

使用 AI 寫 python 網頁爬蟲程式

當網頁的內容較複雜時,AI 可能會無法完整解析內容,此時我們可採用另一種方法,請 AI 用 python 寫網頁爬蟲程式。

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com