跳到主要內容

ChatClovaX

本筆記本提供快速概觀,以開始使用 Naver 的 HyperCLOVA X 聊天模型,透過 CLOVA Studio。如需所有 ChatClovaX 功能和設定的詳細文件,請前往 API 參考文件

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

概觀

整合詳細資訊

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

模型功能

工具呼叫結構化輸出JSON 模式圖像輸入音訊輸入影片輸入Token 層級串流原生非同步Token 使用量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 金鑰可以透過點擊 CLOVA 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 參考: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 上線生產級應用程式時,您應該申請並使用服務應用程式。(請參閱此處。)

對於服務應用程式,會發出對應的 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://langchain-python.dev.org.tw/api_reference/community/chat_models/langchain_community.chat_models.naver.ChatClovaX.html


此頁面是否對您有幫助?