Skip to main content

ChatClovaX

This notebook provides a quick overview for getting started with Naverโ€™s HyperCLOVA X chat models via CLOVA Studio. For detailed documentation of all ChatClovaX features and configurations head to the API reference.

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.

Overviewโ€‹

Integration detailsโ€‹

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
ChatClovaXlangchain-communityโŒbeta/โŒโŒPyPI - DownloadsPyPI - Version

Model featuresโ€‹

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
โŒโŒโŒโŒโŒโŒโœ…โœ…โœ…โŒ

Setupโ€‹

Before using the chat model, you must go through the three steps below.

  1. Creating NAVER Cloud Platform account
  2. Apply to use CLOVA Studio
  3. Find API Keys after creating CLOVA Studio Test App or Service App (See here.)

Credentialsโ€‹

CLOVA Studio requires 2 keys (NCP_CLOVASTUDIO_API_KEY and NCP_APIGW_API_KEY).

  • NCP_CLOVASTUDIO_API_KEY is issued per Test App or Service App
  • NCP_APIGW_API_KEY is issued per account, could be optional depending on the region you are using

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

import getpass
import os

os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass("NCP CLOVA Studio API Key: ")
os.environ["NCP_APIGW_API_KEY"] = getpass.getpass("NCP API Gateway API Key: ")

Installationโ€‹

The LangChain Naver integration lives in the langchain-community package:

# install package
!pip install -qU langchain-community

Instantiationโ€‹

Now we can instantiate our model object and generate chat completions:

from langchain_community.chat_models import ChatClovaX

chat = ChatClovaX(
model="HCX-DASH-001",
temperature=0.5,
max_tokens=None,
max_retries=2,
# clovastudio_api_key="..." # if you prefer to pass api key in directly instead of using env vars
# task_id="..." # if you want to use fine-tuned model
# service_app=False # True if using Service App. Default value is False (means using Test App)
# include_ai_filters=False # True if you want to detect inappropriate content. Default value is False
# other params...
)
API Reference:ChatClovaX

Usageโ€‹

ChatClovaX supports all invoke, batch, stream methods of ChatModel including async APIs.

messages = [
(
"system",
"You are a helpful assistant that translates English to Korean. Translate the user sentence.",
),
("human", "I love using NAVER AI."),
]

Invocationโ€‹

In addition to invoke, we also support batch and stream functionalities.

#using invoke
chat.invoke(messages)
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')
#using batch
chat.batch([messages, messages])
[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'),
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')]
#using stream
for chunk in chat.stream(messages):
print(chunk.content, end="", flush=True)

Chainingโ€‹

We can chain our model with a prompt template like so:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}. Translate the user sentence.",
),
(
"human",
"{input}"
),
]
)

chain = prompt | chat
chain.invoke(
{
"input_language": "English",
"output_language": "Korean",
"input": "I love using NAVER AI.",
}
)
API Reference:ChatPromptTemplate
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})

Additional functionalitiesโ€‹

Fine-tuningโ€‹

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.)

You can check task_id from corresponding Test App or Service App details.

fine_tuned_model = ChatClovaX(
task_id='abcd123e',
temperature=0.5,
)

fine_tuned_model.invoke(messages)

Service Appโ€‹

When going live with production-level application using CLOVA Studio, you should apply for and use Service App. (See here.)

For a Service App, a corresponding NCP_CLOVASTUDIO_API_KEY is issued and can only be called with the API Key.

#### Update environment variables

os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass("NCP CLOVA Studio API Key for Service App: ")
chat = ChatClovaX(
service_app=True, # True if you want to use your service app, default value is False.
# clovastudio_api_key="..." # if you prefer to pass api key in directly instead of using env vars
model="HCX-DASH-001",
temperature=0.5,
max_tokens=None,
max_retries=2,
# other params...
)
ai_msg = chat.invoke(messages)

AI Filterโ€‹

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 for details.

chat = ChatClovaX(
model="HCX-DASH-001",
temperature=0.5,
max_tokens=None,
max_retries=2,
include_ai_filters=True, # True if you want to enabled ai filter
# other params...
)

ai_msg = chat.invoke(messages)
print(ai_msg.response_metadata['ai_filter'])

API referenceโ€‹

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


Was this page helpful?


You can also leave detailed feedback on GitHub.