From e2d9fe766f5eba2e9fa048a498eecaed3e1b1516 Mon Sep 17 00:00:00 2001 From: Changyong Um Date: Mon, 24 Mar 2025 03:05:24 +0900 Subject: [PATCH] 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 --- docs/docs/integrations/providers/naver.mdx | 49 +++- .../integrations/tools/naver_search.ipynb | 262 ++++++++++++++++++ libs/packages.yml | 5 + 3 files changed, 313 insertions(+), 3 deletions(-) create mode 100644 docs/docs/integrations/tools/naver_search.ipynb diff --git a/docs/docs/integrations/providers/naver.mdx b/docs/docs/integrations/providers/naver.mdx index b9f6a1ca7fe..a64b41734af 100644 --- a/docs/docs/integrations/providers/naver.mdx +++ b/docs/docs/integrations/providers/naver.mdx @@ -12,10 +12,15 @@ Please refer to [NCP User Guide](https://guide.ncloud-docs.com/docs/clovastudio- - Get a CLOVA Studio API Key by [issuing it](https://api.ncloud-docs.com/docs/ai-naver-clovastudio-summary#API%ED%82%A4) and set it as an environment variable (`NCP_CLOVASTUDIO_API_KEY`). - If you are using a legacy API Key (that doesn't start with `nv-*` prefix), you might need to get an additional API Key by [creating your app](https://guide.ncloud-docs.com/docs/en/clovastudio-playground01#create-test-app) and set it as `NCP_APIGW_API_KEY`. -- Install the integration Python package with: + +Naver integrations live in two packages: + +- `langchain-naver-community`: a dedicated integration package for Naver. It is a community-maintained package and is not officially maintained by Naver or LangChain. +- `langchain-community`: a collection of [third-party integrations](https://python.langchain.com/docs/concepts/architecture/#langchain-community), +including Naver. **New features should be implemented in the dedicated `langchain-naver-community` package**. ```bash -pip install -U langchain-community +pip install -U langchain-community langchain-naver-community ``` ## Chat models @@ -36,4 +41,42 @@ See a [usage example](/docs/integrations/text_embedding/naver). ```python from langchain_community.embeddings import ClovaXEmbeddings -``` \ No newline at end of file +``` + +## Tools + +### Naver Search + +The Naver Search integration allows your LangChain applications to retrieve information from Naver's search engine. This is particularly useful for Korean language queries and getting up-to-date information about Korean topics. + +To use the Naver Search tools, you need to: + +1. Sign in to the [Naver Developers portal](https://developers.naver.com/main/) +2. Create a new application and enable the Search API +3. Obtain your **NAVER_CLIENT_ID** and **NAVER_CLIENT_SECRET** from the "Application List" section +4. Set these as environment variables in your application + +```python +from langchain_naver_community.tool import NaverSearchResults +from langchain_naver_community.utils import NaverSearchAPIWrapper + +# Set up the search wrapper +search = NaverSearchAPIWrapper() + +# Create a tool +tool = NaverSearchResults(api_wrapper=search) +``` + +See a [usage example](/docs/integrations/tools/naver_search) for more details. + +### Specialized Search Tools + +The package also provides specialized search tools for different types of content: + +```python +from langchain_naver_community.tool import NaverNewsSearch # For news articles +from langchain_naver_community.tool import NaverBlogSearch # For blog posts +from langchain_naver_community.tool import NaverImageSearch # For images +``` + +Each of these can be used within LangChain agents to provide more targeted search capabilities. diff --git a/docs/docs/integrations/tools/naver_search.ipynb b/docs/docs/integrations/tools/naver_search.ipynb new file mode 100644 index 00000000000..969a6c2854a --- /dev/null +++ b/docs/docs/integrations/tools/naver_search.ipynb @@ -0,0 +1,262 @@ +{ + "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-hoon’s official residence and the City Hall... The raid came as part of the Seoul Central District Prosecutors’ Office’s 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 "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... ',\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 +} diff --git a/libs/packages.yml b/libs/packages.yml index 1e597e938bd..b3d79811c2c 100644 --- a/libs/packages.yml +++ b/libs/packages.yml @@ -546,3 +546,8 @@ packages: provider_page: zotero downloads: 153 downloads_updated_at: '2025-03-22T21:59:15.663971+00:00' +- name: langchain-naver-community + name_title: Naver + provider_page: naver + path: . + repo: e7217/langchain-naver-community