Skip to content

Commit

Permalink
fix: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper-clova committed Sep 2, 2024
1 parent f39ba26 commit 9898831
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 118 deletions.
187 changes: 117 additions & 70 deletions docs/docs/integrations/chat/naver.ipynb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"cells": [
{
"metadata": {},
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Naver\n",
"---"
],
"id": "f0e6bc10e127ff6c"
]
},
{
"cell_type": "markdown",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": [
Expand All @@ -97,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
Expand All @@ -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",
")"
]
Expand All @@ -128,10 +128,13 @@
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": 14,
"id": "62e0dbc3",
"metadata": {
"tags": []
},
"outputs": [],
"execution_count": null,
"source": [
"messages = [\n",
" (\n",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -220,8 +266,9 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"id": "d1ee55bc-ffc8-4cfa-801c-993953a08cfd",
"metadata": {},
"source": [
"## Additional functionalities\n",
"\n",
Expand All @@ -230,83 +277,82 @@
"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",
" temperature=0.5,\n",
")\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",
" max_retries=2,\n",
" # 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",
Expand All @@ -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": {
Expand All @@ -356,7 +403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 9898831

Please sign in to comment.