用 LLM 來源建立信任
你正在建立一個財務分析助理,需要向使用者提供最新的股市資訊。由於 LLM 有知識截止時間,你必須啟用網頁搜尋以取得即時資料。此外,為了提升透明度與可信度,你也想向使用者顯示搜尋過程中參考了哪些來源。
OpenAI 用戶端已初始化為 client,你將查詢 Netflix 股票的即時價格。
本練習屬於課程
使用 OpenAI Responses API
練習說明
- 建立一個啟用網頁搜尋的請求,並確保在回應中包含網頁搜尋的來源。
- 迴圈走訪回應項目,只擷取型別為
"web_search_call"的項目,然後列印每個網頁搜尋呼叫之.action屬性中的.sources。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a response with web search enabled and sources included
response = client.responses.create(
model="gpt-5.4-mini",
tools=[{"type": "web_search"}],
input="What is the current stock price of Netflix?",
include=["web_search_call.____.____"]
)
# Extract and print sources from web search calls
for item in response.output:
if ____:
print(item.action.sources)
print(response.output_text)