diff --git a/docs/docs/integrations/chat/naver.ipynb b/docs/docs/integrations/chat/naver.ipynb index f3366d7239477..834f34cfddb8b 100644 --- a/docs/docs/integrations/chat/naver.ipynb +++ b/docs/docs/integrations/chat/naver.ipynb @@ -1,14 +1,14 @@ { "cells": [ { - "metadata": {}, "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, "source": [ "---\n", "sidebar_label: Naver\n", "---" - ], - "id": "f0e6bc10e127ff6c" + ] }, { "cell_type": "markdown", @@ -17,7 +17,7 @@ "source": [ "# ChatClovaX\n", "\n", - "This notebook provides a quick overview for getting started with Naver’s HyperCLOVA X [chat models](https://python.langchain.com/v0.2/docs/concepts/#chat-models) via CLOVA Studio. For detailed documentation of all ChatClovaX features and configurations head to the [API reference](https://api.python.langchain.com/en/latest/chat_models/langchain_community.chat_models.naver.ChatClovaX.html).\n", + "This notebook provides a quick overview for getting started with Naver’s HyperCLOVA X [chat models](https://python.langchain.com/v0.2/docs/concepts/#chat-models) via CLOVA Studio. For detailed documentation of all ChatClovaX features and configurations head to the [API reference](https://api.python.langchain.com/en/latest/community/chat_models/langchain_community.chat_models.naver.ChatClovaX.html).\n", "\n", "CLOVA Studio has several chat models. You can find information about latest models and their costs, context windows, and supported input types in the CLOVA Studio API Guide [documentation](https://api.ncloud-docs.com/docs/clovastudio-chatcompletions).\n", "\n", @@ -45,28 +45,28 @@ "\n", "CLOVA Studio requires 2 keys (`NCP_CLOVASTUDIO_API_KEY` and `NCP_APIGW_API_KEY`).\n", " - `NCP_CLOVASTUDIO_API_KEY` is issued per Test App or Service App\n", - " - `NCP_APIGW_API_KEY` is issued per account\n", + " - `NCP_APIGW_API_KEY` is issued per account, could be optional depending on the region you are using\n", "\n", "The two API Keys could be found by clicking `App Request Status` > `Service App, Test App List` > `‘Details’ button for each app` in [CLOVA Studio](https://clovastudio.ncloud.com/studio-application/service-app)" ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "2def81b5-b023-4f40-a97b-b2c5ca59d6a9", + "metadata": {}, + "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "os.environ[\"NCP_CLOVASTUDIO_API_KEY\"] = getpass.getpass(\"NCP CLOVA Studio API Key: \")\n", "os.environ[\"NCP_APIGW_API_KEY\"] = getpass.getpass(\"NCP API Gateway API Key: \")" - ], - "id": "bcc89bab8951dbe1" + ] }, { "cell_type": "markdown", - "id": "0730d6a1-c893-4840-9817-5e5251676d5d", + "id": "17bf9053-90c5-4955-b239-55a35cb07566", "metadata": {}, "source": [ "### Installation\n", @@ -77,7 +77,7 @@ { "cell_type": "code", "execution_count": null, - "id": "652d6238-1f87-422a-b135-f5abbb8652fc", + "id": "a15d341e-3e26-4ca3-830b-5aab30ed66de", "metadata": {}, "outputs": [], "source": [ @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", "metadata": {}, "outputs": [], @@ -109,10 +109,10 @@ " temperature=0.5,\n", " max_tokens=None,\n", " max_retries=2,\n", - " # clovastudio_api_key=\"...\" # if you prefer to pass api key in directly instead of using env vars\n", - " # task_id=\"...\" # if you want to use fine-tuned model\n", - " # service_app=False (default) # True if using service app\n", - " # include_ai_filters=False (default) ## True if you want to detect inappropriate content\n", + " # clovastudio_api_key=\"...\" # if you prefer to pass api key in directly instead of using env vars\n", + " # task_id=\"...\" # if you want to use fine-tuned model\n", + " # service_app=False # True if using Service App. Default value is False (means using Test App)\n", + " # include_ai_filters=False # True if you want to detect inappropriate content. Default value is False\n", " # other params...\n", ")" ] @@ -128,10 +128,13 @@ ] }, { - "metadata": {}, "cell_type": "code", + "execution_count": 14, + "id": "62e0dbc3", + "metadata": { + "tags": [] + }, "outputs": [], - "execution_count": null, "source": [ "messages = [\n", " (\n", @@ -140,42 +143,74 @@ " ),\n", " (\"human\", \"I love using NAVER AI.\"),\n", "]" - ], - "id": "b1e9edff909a9705" + ] }, { + "cell_type": "markdown", + "id": "47752b59", "metadata": {}, + "source": [ + "## Invocation\n", + "\n", + "In addition to invoke, we also support batch and stream functionalities." + ] + }, + { "cell_type": "code", - "outputs": [], - "execution_count": null, + "execution_count": 15, + "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='저는 NAVER AI를 사용하는 것이 너무 좋아요.', response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 15, 'seed': 1655372866, 'ai_filter': None}, id='run-412be878-f8a2-4917-ad71-f23ee250771a-0')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#using invoke\n", "chat.invoke(messages)" - ], - "id": "c7769b0eaf9ffcd6" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], - "execution_count": null, + "execution_count": 13, + "id": "876d2c2a-1b29-4b9e-92ed-381223c9439f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[AIMessage(content='나는 NAVER AI를 사용하는 것이 좋아요.', response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 13, 'seed': 1938804380, 'ai_filter': None}, id='run-b5349497-a959-4ab9-a3b0-28483b0f223d-0'),\n", + " AIMessage(content='나는 NAVER AI를 사용하는 것이 좋아요.', response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 13, 'seed': 2603129649, 'ai_filter': None}, id='run-ab428833-d6b4-43d8-8342-d034ed6c6447-0')]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#using batch\n", "chat.batch([messages, messages])" - ], - "id": "1d116306fa105a00" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "2c07af21-dda5-4514-b4de-1f214c2cebcd", + "metadata": {}, + "outputs": [], "source": [ "#using stream\n", "for chunk in chat.stream(messages):\n", " print(chunk.content, end=\"\", flush=True)" - ], - "id": "3f09503abaf56a8f" + ] }, { "cell_type": "markdown", @@ -189,10 +224,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='나는 NAVER AI를 사용하는 것이 좋아요.', response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 13, 'seed': 4065547179, 'ai_filter': None}, id='run-2662b461-382e-4aac-a59f-8cb824b8cc51-0', usage_metadata={'input_tokens': 25, 'output_tokens': 13, 'total_tokens': 38})" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from langchain_core.prompts import ChatPromptTemplate\n", "\n", @@ -220,8 +266,9 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "d1ee55bc-ffc8-4cfa-801c-993953a08cfd", + "metadata": {}, "source": [ "## Additional functionalities\n", "\n", @@ -230,14 +277,14 @@ "You can call fine-tuned CLOVA X models by passing in your corresponding `task_id` parameter. (You don’t need to specify the model_name parameter when calling fine-tuned model.)\n", "\n", "You can check `task_id` from corresponding Test App or Service App details." - ], - "id": "b17f1745ecca4559" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "cb436788", + "metadata": {}, + "outputs": [], "source": [ "fine_tuned_model = ChatClovaX(\n", " task_id='abcd123e',\n", @@ -245,43 +292,42 @@ ")\n", "\n", "fine_tuned_model.invoke(messages)" - ], - "id": "955392044d4c967" + ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "f428deaf", + "metadata": {}, "source": [ "### Service App\n", "\n", "When going live with production-level application using CLOVA Studio, you should apply for and use Service App. (See [here](https://guide.ncloud-docs.com/docs/en/clovastudio-playground01#서비스앱신청).)\n", "\n", "For a Service App, a corresponding `NCP_CLOVASTUDIO_API_KEY` is issued and can only be called with the API Key." - ], - "id": "60f993c25f29f615" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "dcf566df", + "metadata": {}, + "outputs": [], "source": [ "#### Update environment variables\n", "\n", "os.environ[\"NCP_CLOVASTUDIO_API_KEY\"] = getpass.getpass(\"NCP CLOVA Studio API Key for Service App: \")" - ], - "id": "10afeeff7b96a826" + ] }, { - "metadata": {}, "cell_type": "code", + "execution_count": 8, + "id": "cebe27ae", + "metadata": {}, "outputs": [], - "execution_count": null, "source": [ "chat = ChatClovaX(\n", " service_app=True, # True if you want to use your service app, default value is False.\n", " # clovastudio_api_key=\"...\" # if you prefer to pass api key in directly instead of using env vars\n", - " # apigw_api_key=\"...\" # if you prefer to pass gateway key in directly instead of using env vars\n", " model=\"HCX-DASH-001\",\n", " temperature=0.5,\n", " max_tokens=None,\n", @@ -289,24 +335,24 @@ " # other params...\n", ")\n", "ai_msg = chat.invoke(messages)" - ], - "id": "79a8b75e0f3c62cf" + ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "d73e7140", + "metadata": {}, "source": [ "### AI Filter\n", "\n", "AI Filter detects inappropriate output such as profanity from the test app (or service app included) created in Playground and informs the user. See [here](https://guide.ncloud-docs.com/docs/en/clovastudio-playground01#AIFilter) for details. " - ], - "id": "a44107dc538a570c" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "32bfbc93", + "metadata": {}, + "outputs": [], "source": [ "chat = ChatClovaX(\n", " model=\"HCX-DASH-001\",\n", @@ -318,26 +364,27 @@ ")\n", "\n", "ai_msg = chat.invoke(messages)" - ], - "id": "23d40ddcc3ad428" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, - "source": "print(ai_msg.response_metadata['ai_filter'])", - "id": "2f0ac38b0d6f4473" + "id": "7bd9e179", + "metadata": {}, + "outputs": [], + "source": [ + "print(ai_msg.response_metadata['ai_filter'])" + ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3", + "metadata": {}, "source": [ "## API reference\n", "\n", - "For detailed documentation of all ChatClovaX features and configurations head to the API reference: https://api.python.langchain.com/en/latest/chat_models/langchain_community.chat_models.naver.ChatClovaX.html" - ], - "id": "fb21a159a74f9dad" + "For detailed documentation of all ChatNaver features and configurations head to the API reference: https://api.python.langchain.com/en/latest/community/chat_models/langchain_community.chat_models.naver.ChatClovaX.html" + ] } ], "metadata": { @@ -356,7 +403,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/docs/docs/integrations/text_embedding/naver.ipynb b/docs/docs/integrations/text_embedding/naver.ipynb index 16a8e1b130e83..f0cd61edd448c 100644 --- a/docs/docs/integrations/text_embedding/naver.ipynb +++ b/docs/docs/integrations/text_embedding/naver.ipynb @@ -1,78 +1,105 @@ { "cells": [ { - "metadata": {}, "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, "source": [ "---\n", "sidebar_label: Naver\n", "---" - ], - "id": "e12128533632650f" + ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "e49f1e0d", + "metadata": {}, "source": [ "# ClovaXEmbeddings\n", "\n", - "This notebook covers how to get started with embedding models provided by CLOVA Studio.\n", + "This notebook covers how to get started with embedding models provided by CLOVA Studio. For detailed documentation on `ClovaXEmbeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/latest/api_reference/community/embeddings/langchain_community.embeddings.naver.ClovaXEmbeddings.html).\n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "| Provider | Package |\n", + "|:--------:|:-------:|\n", + "| [Naver](/docs/integrations/providers/naver.mdx) | [ClovaXEmbeddings](https://python.langchain.com/latest/api_reference/community/embeddings/langchain_community.embeddings.naver.ClovaXEmbeddings.html) |\n", + "\n", + "## Setup\n", "\n", - "## Installation" - ], - "id": "f90236718398be30" + "Before using embedding models provided by CLOVA Studio, you must go through the three steps below.\n", + "\n", + "1. Creating [NAVER Cloud Platform](https://www.ncloud.com/) account \n", + "2. Apply to use [CLOVA Studio](https://www.ncloud.com/product/aiService/clovaStudio)\n", + "3. Find API Keys after creating CLOVA Studio Test App or Service App (See [here](https://guide.ncloud-docs.com/docs/en/clovastudio-playground01#테스트앱생성).)\n", + "\n", + "### Credentials\n", + "\n", + "CLOVA Studio requires 3 keys (`NCP_CLOVASTUDIO_API_KEY`, `NCP_APIGW_API_KEY` and `NCP_CLOVASTUDIO_APP_ID`) for embeddings.\n", + "- `NCP_CLOVASTUDIO_API_KEY` and `NCP_CLOVASTUDIO_APP_ID` is issued per serviceApp or testApp\n", + "- `NCP_APIGW_API_KEY` is issued per account\n", + "\n", + "The two API Keys could be found by clicking `App Request Status` > `Service App, Test App List` > `‘Details’ button for each app` in [CLOVA Studio](https://clovastudio.ncloud.com/studio-application/service-app)." + ] }, { "cell_type": "code", "execution_count": null, - "id": "4c3bef91", + "id": "c52e8a50-3e67-4272-bc80-3954d98f8dea", "metadata": {}, "outputs": [], "source": [ - "# install package\n", - "!pip install -U langchain-community" + "import getpass\n", + "import os\n", + "\n", + "os.environ[\"NCP_CLOVASTUDIO_API_KEY\"] = getpass.getpass(\"NCP CLOVA Studio API Key: \")\n", + "os.environ[\"NCP_APIGW_API_KEY\"] = getpass.getpass(\"NCP API Gateway API Key: \")" ] }, { + "cell_type": "code", + "execution_count": null, + "id": "83520d8e-ecf8-4e47-b3bc-1ac205b3a2ab", "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"NCP_CLOVASTUDIO_APP_ID\"] = input(\"NCP CLOVA Studio App ID: \")" + ] + }, + { "cell_type": "markdown", + "id": "ff00653e", + "metadata": {}, "source": [ - "## Environment Setup\n", - "\n", - "CLOVA Studio requires 3 keys (`NCP_CLOVASTUDIO_API_KEY`, `NCP_APIGW_API_KEY` and `NCP_CLOVASTUDIO_APP_ID`) for embeddings.\n", - "- `NCP_CLOVASTUDIO_API_KEY` and `NCP_CLOVASTUDIO_APP_ID` is issued per serviceApp or testApp\n", - "- `NCP_APIGW_API_KEY` is issued per account\n", + "### Installation\n", "\n", - "The two API Keys could be found by clicking `App Request Status` > `Service App, Test App List` > `‘Details’ button for each app` in [CLOVA Studio](https://clovastudio.ncloud.com/studio-application/service-app)." - ], - "id": "5fbc16fa0bc03aa4" + "ClovaXEmbeddings integration lives in the `langchain_community` package:" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "99400c9b", + "metadata": {}, + "outputs": [], "source": [ - "import getpass\n", - "import os\n", - "\n", - "os.environ[\"NCP_CLOVASTUDIO_API_KEY\"] = getpass.getpass(\"NCP CLOVA Studio API Key: \")\n", - "os.environ[\"NCP_APIGW_API_KEY\"] = getpass.getpass(\"NCP API Gateway API Key: \")" - ], - "id": "2924b3b804d36837" + "# install package\n", + "!pip install -U langchain-community" + ] }, { + "cell_type": "markdown", + "id": "2651e611-9d5b-4315-9bbd-f99f56be4e19", "metadata": {}, - "cell_type": "code", - "outputs": [], - "execution_count": null, "source": [ - "os.environ[\"NCP_CLOVASTUDIO_APP_ID\"] = input(\"NCP CLOVA Studio App ID: \")\n", - "## Usage\n", + "## Instantiation\n", "\n", - "- There are several embeddings models available in CLOVA Studio. Please refer [here](https://guide.ncloud-docs.com/docs/en/clovastudio-explorer03#임베딩API) for further details." - ], - "id": "fb35b89f27117b14" + "Now we can instantiate our embeddings object and embed query or document:\n", + "\n", + "- There are several embedding models available in CLOVA Studio. Please refer [here](https://guide.ncloud-docs.com/docs/en/clovastudio-explorer03#임베딩API) for further details.\n", + "- Note that you might need to normalize the embeddings depending on your specific use case." + ] }, { "cell_type": "code", @@ -90,6 +117,61 @@ ")" ] }, + { + "cell_type": "markdown", + "id": "0493b4a8", + "metadata": {}, + "source": [ + "## Indexing and Retrieval\n", + "\n", + "Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the [working with external knowledge tutorials](/docs/tutorials/#working-with-external-knowledge).\n", + "\n", + "Below, see how to index and retrieve data using the `embeddings` object we initialized above. In this example, we will index and retrieve a sample document in the `InMemoryVectorStore`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4d59653", + "metadata": {}, + "outputs": [], + "source": [ + "# Create a vector store with a sample text\n", + "from langchain_core.vectorstores import InMemoryVectorStore\n", + "\n", + "text = \"CLOVA Studio is an AI development tool that allows you to customize your own HyperCLOVA X models.\"\n", + "\n", + "vectorstore = InMemoryVectorStore.from_texts(\n", + " [text],\n", + " embedding=embeddings,\n", + ")\n", + "\n", + "# Use the vectorstore as a retriever\n", + "retriever = vectorstore.as_retriever()\n", + "\n", + "# Retrieve the most similar text\n", + "retrieved_documents = retriever.invoke(\"What is CLOVA Studio?\")\n", + "\n", + "# show the retrieved document's content\n", + "retrieved_documents[0].page_content" + ] + }, + { + "cell_type": "markdown", + "id": "b1a249e1", + "metadata": {}, + "source": [ + "## Direct Usage\n", + "\n", + "Under the hood, the vectorstore and retriever implementations are calling `embeddings.embed_documents(...)` and `embeddings.embed_query(...)` to create embeddings for the text(s) used in `from_texts` and retrieval `invoke` operations, respectively.\n", + "\n", + "You can directly call these methods to get embeddings for your own use cases.\n", + "\n", + "### Embed single texts\n", + "\n", + "You can embed single texts or documents with `embed_query`:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -100,6 +182,16 @@ "embeddings.embed_query(\"My query to look up\")" ] }, + { + "cell_type": "markdown", + "id": "8b383b53", + "metadata": {}, + "source": [ + "### Embed multiple texts\n", + "\n", + "You can embed multiple texts with `embed_documents`:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -112,6 +204,16 @@ ")" ] }, + { + "cell_type": "markdown", + "id": "464a4aae", + "metadata": {}, + "source": [ + "### Embed with async\n", + "\n", + "There are also async functionalities:\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -137,8 +239,9 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "eee40d32367cc5c4", + "metadata": {}, "source": [ "## Additional functionalities\n", "\n", @@ -147,29 +250,40 @@ "When going live with production-level application using CLOVA Studio, you should apply for and use Service App. (See [here](https://guide.ncloud-docs.com/docs/en/clovastudio-playground01#서비스앱신청).)\n", "\n", "For a Service App, corresponding `NCP_CLOVASTUDIO_API_KEY` and `NCP_CLOVASTUDIO_APP_ID` are issued and can only be called with the API Keys." - ], - "id": "eee40d32367cc5c4" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "id": "08f9f44e-c6a4-4163-8caf-27a0cda345b7", + "metadata": {}, + "outputs": [], "source": [ "#### Update environment variables\n", "\n", "os.environ[\"NCP_CLOVASTUDIO_API_KEY\"] = getpass.getpass(\"NCP CLOVA Studio API Key for Service App: \")\n", "os.environ[\"NCP_CLOVASTUDIO_APP_ID\"] = input(\"NCP CLOVA Studio Service App ID: \")" - ], - "id": "c5964e008e2562e9" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, - "source": "embeddings = ClovaXEmbeddings(service_app=True)", - "id": "b81408aac235e83b" + "id": "86f59698-b3f4-4b19-a9d4-4facfcea304b", + "metadata": {}, + "outputs": [], + "source": [ + "embeddings = ClovaXEmbeddings(service_app=True)" + ] + }, + { + "cell_type": "markdown", + "id": "1ddeaee9", + "metadata": {}, + "source": [ + "## API Reference\n", + "\n", + "For detailed documentation on `ClovaXEmbeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/latest/api_reference/community/embeddings/langchain_community.embeddings.naver.ClovaXEmbeddings.html)." + ] } ], "metadata": { @@ -188,7 +302,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.3" } }, "nbformat": 4,