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

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 (default) # True if using service app
# include_ai_filters=False (default) ## True if you want to detect inappropriate content
# 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."),
]
#using invoke
chat.invoke(messages)
#using batch
chat.batch([messages, messages])
#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

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
# apigw_api_key="..." # if you prefer to pass gateway 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 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


Was this page helpful?


You can also leave detailed feedback on GitHub.