LangChain實作不用API Token的免費網路搜尋功能,查詢最新網路資料:包括標題、摘要和連結等

Weibert Weiberson 崴寶
21 min readJun 6, 2024

--

本文工具皆不需要用到API Token,非常方便使用。另外也包含維基百科資料搜尋和Youtube影片搜尋等功能教學。

因為現在的search tool幾乎都要先註冊,拿它生成的API進行使用,而且免費版本都會有限制次數,不然就是要付費才能使用,像是google search api, TAVILY API, serpapi等等。

在LangChain>>Components>>Tools之中有非常多的功能可以使用

本文使用LangChain Tools中的DuckDuckGo、Wikipedia、Youtube和Wikidata等功能,代碼可以參考Github

目錄
實作教學
- Langchain網路搜尋功能(web search)
- Langchain 維基百科搜尋功能(Wikipedia search)
- Langchain 維基數據搜尋功能(Wikidata search)
- LangChain查詢YouTube影片(Youtube search)

總結

實作教學

Langchain網路搜尋功能(web search)

用LangChain與DuckDuckGo進行搜索操作。DuckDuckGo是一款網際網路搜尋引擎,我們將使用DuckDuckGoSearchRunDuckDuckGoSearchResults兩個組件,並展示如何自定義搜索結果。

1. 安裝必要的Python包

%pip install --upgrade --quiet  duckduckgo-search langchain-community

2. 使用DuckDuckGoSearchRun進行簡單搜索

允許我們使用DuckDuckGo搜索引擎來查詢信息。

# 只會產生一筆result
# DuckDuckGo Search
from langchain_community.tools import DuckDuckGoSearchRun

search = DuckDuckGoSearchRun()

print(search.run("黃仁勳"))
  • 搜尋結果:

這些結果通常包含相關的信息摘要,但不包括詳細的鏈接和來源。

root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_DuckDuckGo.py
黃仁勳身價也跟著水漲船高,躋身全球第14大富豪。 美聯社報導,輝達股價繼2023年暴漲超過2倍後,
今年又增加1倍多,再下一城,躋身標準普爾500 ... 黃仁勳表示,
他給出的答案可能和人們印象中的完全相反,人類可能記得在過去10至15年內,
幾乎每個在正式場合回答這個問題的人都會明確地告訴你 ... 不只3兆!黃仁勳很快會變「10兆男」
財星:輝達市值將飆270%直逼10兆美元 「AI教父」、輝達(Nvidia)執行長黃仁勳魅力席捲全台,
創造AI浪潮的他,短 ... 黃仁勳演講亮點|數位人類(Digital Humans)
「數位人類是我們的願景。 」黃仁勳表示將來進入數位人類階段,AI能像人類般互動,
將徹底改變各個行業,可能性是無限的,甚至還會出現AI品牌大使、AI室內設計師、AI客服代理等,
數位人類將以類似人類的方式理解 ... 黃仁勳說,AI將成為製造業,掀起新的工業革命,
創造新的大宗商品,相較於過去,電腦不只能產出資訊,還能產出技能,也就是說,
人們不只能 ...

3. 使用DuckDuckGoSearchResults獲取更多信息

這個組件返回更豐富的搜索結果,包括標題、鏈接和摘要。

# 會產生好幾個snippet和title片段
from langchain_community.tools import DuckDuckGoSearchResults

search = DuckDuckGoSearchResults()

print(search.run("黃仁勳"))
  • 搜尋結果:

返回多個搜索結果,每個結果都包括標題、摘要和鏈接,使你能夠更方便地訪問原始信息。

root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_DuckDuckGo2.py
[snippet: 黃仁勳表示,當時學校並沒有輔導員可以協助,「你只能堅強起來,繼續前進。」
他認為在美國這個機會之地,遭遇磨難是正常的,因此他努力工作,在霸凌中仍面帶微笑生存。
由於寄宿學校的室友是 17 歲的文盲,黃仁勳便教他識字,而室友則反教他臥推。,
title: 黃仁勳創立輝達以前,是怎樣的人?曾挺過霸凌,還是成績全 a 的桌球好手|經理人,
link: https://www.managertoday.com.tw/articles/view/68013],

[snippet: 黃仁勳身價也跟著水漲船高,躋身全球第14大富豪。 美聯社報導,
輝達股價繼2023年暴漲超過2倍後,今年又增加1倍多,再下一城,躋身標準普爾500 ...,
title: 輝達股價飆進千美元俱樂部 黃仁勳躋身全球14大富豪 | 國際 | 中央社 Cna,
link: https://www.cna.com.tw/news/aopl/202406040349.aspx],

[snippet: 黃仁勳演講亮點|數位人類(Digital Humans) 「數位人類是我們的願景。 」
黃仁勳表示將來進入數位人類階段,AI能像人類般互動,將徹底改變各個行業,可能性是無限的,
甚至還會出現AI品牌大使、AI室內設計師、AI客服代理等,數位人類將以類似人類的方式理解 ...,
title: 黃仁勳演講結尾影片告白超暖「台灣是無名的英雄,卻是世界的支柱」,
link: https://www.marieclaire.com.tw/lifestyle/news/79670/nvidia-ceo-jensen-huang-keynote-at-computex-2024],

[snippet: 黃仁勳表示,他給出的答案可能和人們印象中的完全相反,人類可能記得在過去10至15年內,
幾乎每個在正式場合回答這個問題的人都會明確地告訴你 ...,
title: 黃仁勳:計算機時代已逝 下一個黃金賽道是生命科學 | 全球財經 | 全球 | 聯合新聞網,
link: https://udn.com/news/story/6811/7779358]

Langchain 維基百科搜尋功能(Wikipedia search)

1. 安裝必要的Python包

%pip install --upgrade --quiet wikipedia

2. 初始化WikipediaAPIWrapper和WikipediaQueryRun

我們需要初始化WikipediaAPIWrapperWikipediaQueryRun。這些組件將幫助我們方便地查詢Wikipedia數據。

from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

# 初始化API Wrapper
wikipedia_api_wrapper = WikipediaAPIWrapper()

# 初始化查詢運行器
wikipedia_query_run = WikipediaQueryRun(api_wrapper=wikipedia_api_wrapper)

3. 查詢Wikipedia數據

# 執行查詢
result = wikipedia_query_run.run("Yeh Shuhua")

# 顯示查詢結果
print(result)
  • 搜尋結果:

有一點需注意,即使你輸入中文字,他也會返回給你英文的查詢結果。

我們可以看到Langchain查找到了3個wiki的page,分別為(G)I-dle, Yeh Shuhua, Chinese people in Korea

root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_Wikipedia.py
Page: (G)I-dle
Summary: (G)I-dle (Korean: (여자)아이들; RR: Yeoja Aideul; lit. Girls Idol; stylized as (G)I-DLE) is a South Korean girl group formed by Cube Entertainment in 2018. The group consists of five members: Miyeon, Minnie, Soyeon, Yuqi, and Shuhua. The group was originally a sextet, until Soojin left the group on August 14, 2021. They are praised for their musicality, versatility, and for breaking stereotypes as a "self-producing" idol group, known for writing and producing much of their material. Since their debut, they have been acknowledged as one of the most successful South Korean girl groups outside of the "big four" record labels.
Described as bold and sensual, they attract a predominantly female fanbase with music that spans multiple genres, ranging from moombahton to hip hop, and mostly explores themes of self-love, female empowerment and self-acceptance. Critics praise their eclectic style, symbolic and conceptual lyrics, and their confidence.
Debuting with "Latata" on May 2, 2018, which peaked at number 12 on the Circle Digital Chart, they saw moderate success with their subsequent releases until they rose to prominence with their critically acclaimed single "Hwaa," which peaked at number five on said Chart. This was followed by their first number-one single, "Tomboy," which gained virality and critical acclaim. Featured on their full-length album, I Never Die (2022); it topped the Circle album charts and was certified platinum by the Korea Music Content Association (KMCA). Their next single, "Nxde", also topped the Circle Chart and made (G)I-dle the only artist to have two songs achieve a perfect all-kill in 2022.
(G)I-dle's next extended play, I Feel (2023), produced the single "Queencard" and marked the group's third number-one single in South Korea. It became their first record to sell over one million copies in the country, and sold two million copies worldwide in 2023 according to the IFPI. During the same year, they became the first act from an independent label to appear on Mediabase Top 40 Radio airplay charts and to debut on the US Billboard Pop Airplay chart with a non-English song. The group's second studio album, 2 (2024), was also met with commercial success and sold over one million copies in South Korea. It produced top-ten lead single "Super Lady" and yielded the number-one song "Fate", which found success despite not being released as a single.

Page: Yeh Shuhua
Summary: Yeh Shuhua (Chinese: 葉舒華; born January 6, 2000), known mononymously as Shuhua (Korean: 슈화), is a Taiwanese singer based in South Korea. She is a member of the South Korean girl group (G)I-dle, which debuted under Cube Entertainment in May 2018.

Page: Chinese people in Korea
Summary: A recognizable community of Chinese people in Korea has existed since the 1880s, and are often known as Hwagyo. Over 90% of early Chinese migrants came from Shandong province on the east coast of China. These ethnic Han Chinese residents in Korea often held Republic of China and Korean citizenship. The Republic of China used to govern the entirety of China, but now only governs Taiwan and a minor part of Fujian province. Due to the conflation of Republic of China citizenship with Taiwanese identity in the modern era, these ethnic Chinese people in Korea or Hwagyo are now usually referred to as "Taiwanese". However, in reality most Hwagyo hold little to no ties with Taiwan.
After China's "reform and opening up" and subsequent normalization of China–South Korea relations, a new wave of Chinese migration to South Korea has occurred. In 2009, more than half of the South Korea's 1.1 million foreign residents were PRC citizens; 71% of those are Joseonjok (Chaoxianzu in Korea), PRC citizens of Joseon ethnicity. There is also a small community of PRC citizens in North Korea.
Between 2018 and 2020, the presence of Chinese (Han Chinese) workers was felt more than ethnic Korean-Chinese workers, as evidenced by the noticeable increase in conversations in Man

Langchain 維基數據搜尋功能(Wikidata search)

1. 安裝必要的Python包

%pip install --upgrade --quiet wikibase-rest-api-client mediawikiapi

2. 初始化WikidataAPIWrapper和WikidataQueryRun

我們需要初始化WikidataAPIWrapperWikidataQueryRun。這些組件將幫助我們方便地查詢Wikidata數據。

from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun

# 初始化API Wrapper
wikidata_api_wrapper = WikidataAPIWrapper()

# 初始化查詢運行器
wikidata_query_run = WikidataQueryRun(api_wrapper=wikidata_api_wrapper)

3. 查詢Wikidata數據

# 執行查詢
result = wikidata_query_run.run("林依晨")

# 顯示查詢結果
print(result)
  • 搜尋結果:

結果通常包括與查詢相關的豐富信息,例如生平、成就和相關資源。有一點需注意,即使你輸入中文字,他也會返回給你英文的查詢結果。

root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_Wikidata.py
Result Q253724:
Label: Ariel Lin
Description: Taiwanese singer-actress
Aliases: Lin Yichen, Ariel Lin Yi-chen
instance of: human
country of citizenship: Taiwan
occupation: actor, singer, television actor, film actor, voice actor
sex or gender: female
date of birth: 1982-10-29
place of birth: Yilan County
educated at: National Chengchi University
genre: mandopop

Result Q4926892:
Label: Blissful Encounter
Description: album by Ariel Lin
instance of: album
publication date: 2009, 2009-07-10
genre: mandopop
performer: Ariel Lin

LangChain查詢YouTube影片(Youtube search)

1. 安裝必要的Python包

%pip install --upgrade --quiet youtube_search

2. 初始化YouTubeSearchTool

我們需要初始化YouTubeSearchTool。這個工具將幫助我們方便地查詢YouTube影片。

from langchain_community.tools import YouTubeSearchTool

# 初始化搜索工具
tool = YouTubeSearchTool()

3. 查詢YouTube影片

# 執行查詢
result = tool.run("五月天")

# 顯示查詢結果
print(result)

這段代碼將查詢YouTube並返回與”五月天”相關的影片。結果通常包含影片的URL。

  • 搜尋結果:
root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_YouTube.py
['https://www.youtube.com/watch?v=89SyGIfrEWs&pp=ygUJ5LqU5pyI5aSp',
'https://www.youtube.com/watch?v=amAVTzMJQic&pp=ygUJ5LqU5pyI5aSp']

4. 指定查詢結果數量

如果你需要指定返回的結果數量,可以在查詢中指定數量參數。

# 執行查詢
result = tool.run("五月天, 5")

# 顯示查詢結果
print(result)
  • 搜尋結果:
root@c8c21d9dfc73:/app/Tools# python3 langchain_tools_YouTube.py
['https://www.youtube.com/watch?v=89SyGIfrEWs&pp=ygUJ5LqU5pyI5aSp',
'https://www.youtube.com/watch?v=amAVTzMJQic&pp=ygUJ5LqU5pyI5aSp',
'https://www.youtube.com/watch?v=H5e2dM_vAzg&pp=ygUJ5LqU5pyI5aSp',
'https://www.youtube.com/watch?v=qX2GsMj7154&pp=ygUJ5LqU5pyI5aSp',
'https://www.youtube.com/watch?v=IPfltLbpX8k&pp=ygUJ5LqU5pyI5aSp']

總結:

可以把這些Tools和LLM串在一起,或甚至和Agent串在一起,可以達到更多元的應用唷😘

嘿!你是否曾經對程式碼感到無比困惑,或者對 debug 的過程想大喊三聲「為什麼」? 不用擔心,崴寶的社群就是你的救星!🌟

📌 Discord 群組:

📌 LINE 社團:

在這裡,你將找到志同道合的小夥伴,無論是新手還是資深高手,大家都來這裡一起探索、分享和大聊特聊!

社群媒體

Instagram:

https://www.instagram.com/weibert_music/ https://www.instagram.com/weibert_coding/

YouTube:

https://www.youtube.com/@weibert

Threads

https://www.threads.net/@weibert_coding

FB粉絲專頁:

https://www.facebook.com/weibert1/

GitHub:

https://github.com/weitsung50110

TikTok:

https://www.tiktok.com/@weibert1

崴寶網站:

https://weitsung50110.github.io/

— 未來會在yotube頻道well崴寶程式開發天堂拍成影片教學

References

https://python.langchain.com/v0.2/docs/integrations/tools/

https://python.langchain.com/v0.2/docs/integrations/tools/ddg/

https://python.langchain.com/v0.2/docs/integrations/tools/wikidata/

https://python.langchain.com/v0.2/docs/integrations/tools/wikipedia/

https://www.toolify.ai/tw/ai-news-tw/%E4%BD%BF%E7%94%A8langchain%E8%88%87duckduckgowikipedia%E5%92%8Cpython-repl%E5%B7%A5%E5%85%B7-1096402

https://python.langchain.com/v0.2/docs/integrations/tools/youtube/

--

--