Files
langchain/docs/docs/integrations/tools/naver_search.ipynb
Changyong Um e2d9fe766f community[tool]: Integrate a tool for the naver_search (#30392)
Hello!
I have reopened a pull request for tool integration.
Please refer to the previous
[PR](https://github.com/langchain-ai/langchain/pull/30248).

I understand that for the tool integration, a separate package should be
created, and only the documentation should be added under docs/docs/. If
there are any other procedures, please let me know.


[langchain-naver-community](https://github.com/e7217/langchain-naver-community)

cc: @ccurme

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-03-23 14:05:24 -04:00

263 lines
7.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "245a954a",
"metadata": {},
"source": [
"# Naver Search\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"\n",
"| Class | Package | Serializable | JS support | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: |\n",
"| NaverSearchResults | [langchain-naver-community](https://pypi.org/project/langchain-naver-community/) | ❌ | ❌ | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-naver-community?style=flat-square&label=%20) |\n",
"\n",
"### Tool features\n",
"\n",
"**Search** : The Naver Search Tool provides a simple interface to search Naver and get results.\n",
"\n",
"## Setup\n",
"### Setting Up API Credentials\n",
"To use Naver Search, you need to obtain API credentials. Follow these steps:\n",
"\n",
"Sign in to the [Naver Developers portal](https://developers.naver.com/main/).\n",
"Create a new application and enable the Search API.\n",
"Obtain your **NAVER_CLIENT_ID** and **NAVER_CLIENT_SECRET** from the \"Application List\" section.\n",
"\n",
"### Setting Up Environment Variables\n",
"After obtaining the credentials, set them as environment variables in your script:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2998f9c",
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain-naver-community"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "30692b20",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if not os.environ.get(\"NAVER_CLIENT_ID\"):\n",
" os.environ[\"NAVER_CLIENT_ID\"] = getpass.getpass(\"Enter your Naver Client ID:\\n\")\n",
"\n",
"if not os.environ.get(\"NAVER_CLIENT_SECRET\"):\n",
" os.environ[\"NAVER_CLIENT_SECRET\"] = getpass.getpass(\n",
" \"Enter your Naver Client Secret:\\n\"\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "c43917d5",
"metadata": {},
"source": [
"## Instantiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d2be578",
"metadata": {},
"outputs": [],
"source": [
"from langchain_naver_community.utils import NaverSearchAPIWrapper\n",
"\n",
"search = NaverSearchAPIWrapper()"
]
},
{
"cell_type": "markdown",
"id": "c97b3f7a",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ac4910f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'title': 'Seoul shares rise for 4th day on tech gains; won at 2-week low',\n",
" 'link': 'https://n.news.naver.com/mnews/article/001/0015277717?sid=104',\n",
" '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... ',\n",
" 'pubDate': 'Thu, 20 Mar 2025 16:09:00 +0900'},\n",
" {'title': \"Seoul Mayor Oh's residence, office raided over alleged ties to shadowy po...\",\n",
" 'link': 'https://n.news.naver.com/mnews/article/640/0000067073?sid=100',\n",
" 'description': 'Prosecutors on Thursday raided Seoul Mayor Oh Se-hoons official residence and the City Hall... The raid came as part of the Seoul Central District Prosecutors Offices probe into... ',\n",
" 'pubDate': 'Thu, 20 Mar 2025 19:12:00 +0900'},\n",
" {'title': 'Education can heal divides: Seoul schools chief',\n",
" 'link': 'https://n.news.naver.com/mnews/article/044/0000267866?sid=104',\n",
" '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... ',\n",
" 'pubDate': 'Thu, 20 Mar 2025 14:35:00 +0900'}]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"search.results(\"Seoul\")[:3]"
]
},
{
"cell_type": "markdown",
"id": "2511982a",
"metadata": {},
"source": [
"## Tool Usage"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e17126e7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'title': \"2025 is here. Here's what to watch out for\",\n",
" 'link': 'https://n.news.naver.com/mnews/article/044/0000265707?sid=104',\n",
" 'description': 'The trend was predicted in &quot;Trend Korea 2025,&quot; written by Kim Ran-do, a professor of consumer science at Seoul National University, and his team. The annually published book also predicts that... ',\n",
" 'pubDate': 'Sat, 18 Jan 2025 16:01:00 +0900'},\n",
" {'title': '[INTERVIEW] Korea to overhaul weather prediction model against climate ch...',\n",
" 'link': 'https://www.koreatimes.co.kr/www/nation/2023/06/371_353628.html?utm_source=na',\n",
" '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... ',\n",
" 'pubDate': 'Sun, 25 Jun 2023 17:22:00 +0900'}]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_naver_community.tool import NaverSearchResults\n",
"from langchain_naver_community.utils import NaverSearchAPIWrapper\n",
"\n",
"search = NaverSearchAPIWrapper()\n",
"\n",
"tool = NaverSearchResults(api_wrapper=search)\n",
"\n",
"tool.invoke(\"what is the weather in seoul?\")[3:5]"
]
},
{
"cell_type": "markdown",
"id": "74295cb5",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"The Naver Search tool can be integrated into LangChain agents for more complex tasks. Below we demonstrate how to set up an agent that can search Naver for current information.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "386e19b0",
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"system_prompt = \"\"\"\n",
"You are a helpful assistant that can search the web for information.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70da8682",
"metadata": {},
"outputs": [],
"source": [
"from langchain_naver_community.tool import NaverNewsSearch\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"tools = [NaverNewsSearch()]\n",
"\n",
"agent_executor = create_react_agent(\n",
" llm,\n",
" tools,\n",
" prompt=system_prompt,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "4b73dbaf",
"metadata": {},
"source": [
"Now we can run the agent with a query."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "726e575a",
"metadata": {},
"outputs": [],
"source": [
"query = \"What is the weather in Seoul?\"\n",
"result = agent_executor.invoke({\"messages\": [(\"human\", query)]})\n",
"result[\"messages\"][-1].content"
]
},
{
"cell_type": "markdown",
"id": "d566a4c0",
"metadata": {},
"source": [
"## API reference\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}