Naver 搜尋
概觀
整合詳細資訊
類別 | 套件 | 可序列化 | JS 支援 | 最新套件 |
---|---|---|---|---|
NaverSearchResults | langchain-naver-community | ❌ | ❌ |
工具功能
搜尋:Naver 搜尋工具提供一個簡單的介面來搜尋 Naver 並取得結果。
設定
設定 API 憑證
若要使用 Naver 搜尋,您需要取得 API 憑證。請依照下列步驟操作
登入 Naver 開發人員入口網站。建立新的應用程式並啟用 Search API。從「應用程式列表」區段取得您的 NAVER_CLIENT_ID 和 NAVER_CLIENT_SECRET。
設定環境變數
取得憑證後,將它們設定為您腳本中的環境變數
%pip install --upgrade --quiet langchain-naver-community
import getpass
import os
if not os.environ.get("NAVER_CLIENT_ID"):
os.environ["NAVER_CLIENT_ID"] = getpass.getpass("Enter your Naver Client ID:\n")
if not os.environ.get("NAVER_CLIENT_SECRET"):
os.environ["NAVER_CLIENT_SECRET"] = getpass.getpass(
"Enter your Naver Client Secret:\n"
)
實例化
from langchain_naver_community.utils import NaverSearchAPIWrapper
search = NaverSearchAPIWrapper()
調用
search.results("Seoul")[:3]
[{'title': 'Seoul shares rise for 4th day on tech gains; won at 2-week low',
'link': 'https://n.news.naver.com/mnews/article/001/0015277717?sid=104',
'description': 'stocks-summary Seoul shares rise for 4th day on tech gains; won at 2-week low SEOUL, March 20 (Yonhap) -- Seoul shares extended their winning streak to a fourth day Thursday on the back of gains... ',
'pubDate': 'Thu, 20 Mar 2025 16:09:00 +0900'},
{'title': "Seoul Mayor Oh's residence, office raided over alleged ties to shadowy po...",
'link': 'https://n.news.naver.com/mnews/article/640/0000067073?sid=100',
'description': 'Prosecutors on Thursday raided Seoul Mayor Oh Se-hoon’s official residence and the City Hall... The raid came as part of the Seoul Central District Prosecutors’ Office’s probe into... ',
'pubDate': 'Thu, 20 Mar 2025 19:12:00 +0900'},
{'title': 'Education can heal divides: Seoul schools chief',
'link': 'https://n.news.naver.com/mnews/article/044/0000267866?sid=104',
'description': 'Jung Keun-sik, Superintendent of Seoul Metropolitan Office of Education speaks during an interview with The Korea Herald at his office on March 13. (Lim Se-jun/ The Korea Herald) Seoul education... ',
'pubDate': 'Thu, 20 Mar 2025 14:35:00 +0900'}]
工具使用方式
from langchain_naver_community.tool import NaverSearchResults
from langchain_naver_community.utils import NaverSearchAPIWrapper
search = NaverSearchAPIWrapper()
tool = NaverSearchResults(api_wrapper=search)
tool.invoke("what is the weather in seoul?")[3:5]
[{'title': "2025 is here. Here's what to watch out for",
'link': 'https://n.news.naver.com/mnews/article/044/0000265707?sid=104',
'description': 'The trend was predicted in "Trend Korea 2025," written by Kim Ran-do, a professor of consumer science at Seoul National University, and his team. The annually published book also predicts that... ',
'pubDate': 'Sat, 18 Jan 2025 16:01:00 +0900'},
{'title': '[INTERVIEW] Korea to overhaul weather prediction model against climate ch...',
'link': 'https://www.koreatimes.co.kr/www/nation/2023/06/371_353628.html?utm_source=na',
'description': 'western Seoul to protest its confusing weather predictions, false forecasting is hardly accepted compared to what Yoo saw in Oklahoma. The administrator hopes the Korean public would understand... ',
'pubDate': 'Sun, 25 Jun 2023 17:22:00 +0900'}]
在代理程式中使用
Naver 搜尋工具可以整合到 LangChain 代理程式中,以執行更複雜的任務。以下我們示範如何設定一個可以搜尋 Naver 以取得目前資訊的代理程式。
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
system_prompt = """
You are a helpful assistant that can search the web for information.
"""
API 參考:ChatOpenAI
from langchain_naver_community.tool import NaverNewsSearch
from langgraph.prebuilt import create_react_agent
tools = [NaverNewsSearch()]
agent_executor = create_react_agent(
llm,
tools,
prompt=system_prompt,
)
API 參考:create_react_agent
現在我們可以執行帶有查詢的代理程式。
query = "What is the weather in Seoul?"
result = agent_executor.invoke({"messages": [("human", query)]})
result["messages"][-1].content