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
Class | Package | Local | Serializable | JS support | Package downloads | Package latest |
---|---|---|---|---|---|---|
ChatClovaX | langchain-community | ❌ | ❌ | ❌ |
Model features
Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
---|---|---|---|---|---|---|---|---|---|
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
Setup
Before using the chat model, you must go through the three steps below.
- Creating NAVER Cloud Platform account
- Apply to use CLOVA Studio
- 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 AppNCP_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
You can add them to your environment variables as below:
export NCP_CLOVASTUDIO_API_KEY="your-api-key-here"
export NCP_APIGW_API_KEY="your-api-key-here"
import getpass
import os
if not os.getenv("NCP_CLOVASTUDIO_API_KEY"):
os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass(
"Enter your NCP CLOVA Studio API Key: "
)
if not os.getenv("NCP_APIGW_API_KEY"):
os.environ["NCP_APIGW_API_KEY"] = getpass.getpass(
"Enter your NCP API Gateway API key: "
)
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("Enter your LangSmith 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-003",
max_tokens=100,
temperature=0.5,
# clovastudio_api_key="..." # set if you prefer to pass api key directly instead of using environment variables
# task_id="..." # set if you want to use fine-tuned model
# service_app=False # set True if using Service App. Default value is False (means using Test App)
# include_ai_filters=False # set True if you want to detect inappropriate content. Default value is False
# other params...
)
Invocation
In addition to invoke, we also support batch and stream functionalities.
messages = [
(
"system",
"You are a helpful assistant that translates English to Korean. Translate the user sentence.",
),
("human", "I love using NAVER AI."),
]
ai_msg = chat.invoke(messages)
ai_msg
AIMessage(content='저는 네이버 AI를 사용하는 것이 좋아요.', additional_kwargs={}, response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 14, 'seed': 1112164354, 'ai_filter': None}, id='run-b57bc356-1148-4007-837d-cc409dbd57cc-0', usage_metadata={'input_tokens': 25, 'output_tokens': 14, 'total_tokens': 39})
print(ai_msg.content)
저는 네이버 AI를 사용하는 것이 좋아요.
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.",
}
)
AIMessage(content='저는 네이버 AI를 사용하는 것이 좋아요.', additional_kwargs={}, response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 14, 'seed': 2575184681, 'ai_filter': None}, id='run-7014b330-eba3-4701-bb62-df73ce39b854-0', usage_metadata={'input_tokens': 25, 'output_tokens': 14, 'total_tokens': 39})
Streaming
system = "You are a helpful assistant that can teach Korean pronunciation."
human = "Could you let me know how to say '{phrase}' in Korean?"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
for chunk in chain.stream({"phrase": "Hi"}):
print(chunk.content, end="", flush=True)
Certainly! In Korean, "Hi" is pronounced as "