Skip to main content
Open In ColabOpen on GitHub

ChatClovaX

本笔记本提供了通过CLOVA Studio开始使用Naver的HyperCLOVA X 聊天模型的快速概述。有关所有ChatClovaX功能和配置的详细文档,请前往API参考

CLOVA Studio 提供了多种聊天模型。您可以在 CLOVA Studio API 指南 文档 中找到有关最新模型及其成本、上下文窗口和支持的输入类型的信息。

概述

集成详情

本地可序列化JS支持包下载包最新
ChatClovaXlangchain-communityPyPI - 下载量PyPI - 版本

模型特性

工具调用结构化输出JSON模式图像输入音频输入视频输入令牌级流式传输原生异步令牌使用Logprobs

设置

在使用聊天模型之前,您必须完成以下三个步骤。

  1. 创建NAVER Cloud Platform账户
  2. 申请使用 CLOVA Studio
  3. 创建CLOVA Studio测试应用或服务应用后查找API密钥(参见这里。)

凭证

CLOVA Studio 需要2个密钥(NCP_CLOVASTUDIO_API_KEYNCP_APIGW_API_KEY)。

  • NCP_CLOVASTUDIO_API_KEY 是为每个测试应用或服务应用颁发的
  • NCP_APIGW_API_KEY 是按账户发放的,根据您使用的地区,可能是可选的

两个API密钥可以通过点击App Request Status > Service App, Test App List > ‘Details’ button for each appCLOVA Studio中找到。

你可以像下面这样将它们添加到你的环境变量中:

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: "
)

如果你想获取模型调用的自动追踪,你也可以通过取消注释以下内容来设置你的LangSmith API密钥:

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安装

LangChain Naver 集成位于 langchain-community 包中:

# install package
!pip install -qU langchain-community

实例化

现在我们可以实例化我们的模型对象并生成聊天完成:

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...
)
API Reference:ChatClovaX

调用

除了invoke,我们还支持批处理和流处理功能。

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를 사용하는 것이 좋아요.

链式调用

我们可以链式我们的模型与一个提示模板,如下所示:

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='저는 네이버 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})

流处理

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 "안녕" (annyeong). The first syllable, "안," sounds like the "ahh" sound in "apple," while the second syllable, "녕," sounds like the "yuh" sound in "you." So when you put them together, it's like saying "ahhyuh-nyuhng." Remember to pronounce each syllable clearly and separately for accurate pronunciation.

附加功能

使用微调模型

您可以通过传入相应的task_id参数来调用微调模型。(调用微调模型时不需要指定model_name参数。)

您可以从相应的测试应用程序或服务应用程序详细信息中检查task_id

fine_tuned_model = ChatClovaX(
task_id="5s8egt3a", # set if you want to use fine-tuned model
# other params...
)

fine_tuned_model.invoke(messages)
AIMessage(content='저는 네이버 AI를 사용하는 것이 너무 좋아요.', additional_kwargs={}, response_metadata={'stop_reason': 'stop_before', 'input_length': 25, 'output_length': 15, 'seed': 52559061, 'ai_filter': None}, id='run-5bea8d4a-48f3-4c34-ae70-66e60dca5344-0', usage_metadata={'input_tokens': 25, 'output_tokens': 15, 'total_tokens': 40})

服务应用

当使用CLOVA Studio上线生产级应用程序时,您应该申请并使用服务应用程序。(参见这里。)

对于一个服务应用,会颁发一个相应的NCP_CLOVASTUDIO_API_KEY,并且只能使用它进行调用。

# Update environment variables

os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass(
"Enter 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-003",
# other params...
)
ai_msg = chat.invoke(messages)

AI 过滤器

AI过滤器检测从Playground创建的测试应用程序(或包含的服务应用程序)中的不当输出,例如脏话,并通知用户。详情请参见这里

chat = ChatClovaX(
model="HCX-003",
include_ai_filters=True, # True if you want to enable ai filter
# other params...
)

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

API参考

有关所有ChatNaver功能和配置的详细文档,请访问API参考:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.naver.ChatClovaX.html


这个页面有帮助吗?