跳到主要內容
Open In ColabOpen on GitHub

ChatClovaX

本筆記本快速概述如何開始使用 Naver 的 HyperCLOVA X 聊天模型(透過 CLOVA Studio)。如需所有 ChatClovaX 功能和組態的詳細文件,請前往 API 參考文件

CLOVA Studio 有多個聊天模型。您可以在 CLOVA Studio API 指南 文件中找到有關最新模型及其成本、上下文視窗和支援的輸入類型等資訊。

概述

整合詳細資訊

類別套件本機可序列化JS 支援套件下載套件最新版
ChatClovaXlangchain-communityPyPI - DownloadsPyPI - Version

模型功能

工具呼叫結構化輸出JSON 模式影像輸入音訊輸入影片輸入權杖級別串流原生非同步權杖用量Logprobs

設定

使用聊天模型之前,您必須完成以下四個步驟。

  1. 建立 NAVER Cloud Platform 帳戶
  2. 申請使用 CLOVA Studio
  3. 建立要使用的模型的 CLOVA Studio 測試應用程式或服務應用程式(請參閱此處。)
  4. 發行測試或服務 API 金鑰(請參閱此處。)

憑證

使用您的 API 金鑰設定 NCP_CLOVASTUDIO_API_KEY 環境變數。

  • 請注意,如果您使用的是舊版 API 金鑰(不以 nv-* 字首開頭),您可能需要按一下 CLOVA Studio 中的 App Request Status > Service App, Test App List > 每個應用程式的「詳細資訊」按鈕 來取得額外的 API 金鑰,並將其設定為 NCP_APIGW_API_KEY

您可以如下所示將它們新增至您的環境變數

export NCP_CLOVASTUDIO_API_KEY="your-api-key-here"
# Uncomment below to use a legacy API key
# 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: "
)
# Uncomment below to use a legacy 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["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_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 參考:ChatClovaX

調用

除了調用之外,我們還支援批次和串流功能。

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 參考: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 的生產級應用程式上線時,您應該申請並使用服務應用程式。(請參閱此處。)

對於服務應用程式,您應該使用對應的服務 API 金鑰,並且只能使用該金鑰呼叫。

# Update environment variables

os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass(
"Enter NCP CLOVA Studio Service API Key: "
)
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://langchain-python.dev.org.tw/api_reference/community/chat_models/langchain_community.chat_models.naver.ChatClovaX.html


此頁面是否對您有幫助?