LangChain integrations

Prev Next

Available in Classic and VPC

You can use LangChain to easily integrate and use CLOVA Studio's HyperCLOVA X model and embedding tools.

LangChain is an open source framework that supports language model-based applications. You can link various tools, including language models like HyperCLOVA X, vector databases, and search engines, in a chain structure, simplifying the process of connecting features and integrating the development process. LangGraph, an AI agent development framework, can also be utilized through LangChain integrations.

This LangChain integrations guide describes how to install LangChain and configure settings for integrating CLOVA Studio. Additionally, it provides example codes for utilizing CLOVA Studio's HyperCLOVA X model and embedding tools through LangChain for practical references for real world development.

Note
  • LangChain is implemented in Python, JavaScript, and TypeScript. CLOVA Studio supports the Python-based version of LangChain, and this guide is also written with a focus on Python.
  • LangChain is a trademark of LangChain Inc. All trademark rights are held by LangChain Inc. NAVER Cloud is used in this guide for reference purposes only. This does not imply any sponsorship, endorsement, or partnership between LangChain Inc. and NAVER Cloud.
  • LangChain is open source software, and NAVER Cloud does not guarantee or take responsibility for the quality or performance of LangChain. For more information about LangChain, see the LangChain official documentation.

Install and verify LangChain

To use LangChain integrated with CLOVA Studio, Python version 3.9 or higher is required. After you install Python, use the following commands to install LangChain, then install the langchain-naver package necessary for integrations.

pip install -qU langchain # install LangChain
pip install -qU langchain-naver # install LangChain-NAVER integration package
Note
  • To properly use all features provided by LangChain, keep the package up to date. Check your installed version regularly and perform updates.
  • You may continue to use the langchain-community (≥ v.0.3.4) package used for existing LangChain integrations, but as of April 17, 2025, technical support is discontinued, and integrations are unavailable except for the following models and APIs.
  • langchain-community supported models and APIs: HCX-003 and HCX-DASH-001 (including tuned models), and embedding (including v2)

Verify scope of integrations

CLOVA Studio offers these key features you can use through LangChain:

Integration settings

To securely use CLOVA Studio features through LangChain, register the API key as an environment variable, which acts as the authentication information required for API calls. You can issue and check your API key from the [API key] menu in CLOVA Studio. Create the service app when you apply it to an actual service.

Caution

When you use a service app, you must register the service API key as an environment variable.

To register the authentication information as environment variables:

  • Registration through the terminal
    export CLOVASTUDIO_API_KEY="<CLOVA-STUDIO-API-KEY>"
    
  • Registration through Python
    import getpass
    import os
    
    os.environ["CLOVASTUDIO_API_KEY"] = getpass.getpass(
            "Enter CLOVA Studio API key: "
        )
    
Note
  • When you use langchain-community (≤ v.0.3.14), 2 pieces of authentication information are required: API key and API Gateway key. When you use the embedding (including v2) tool, you must additionally register the app ID of the service app as an environment variable.
    • To view the relevant information, navigate to [Management] > [Apply for service app] on the left of the CLOVA Studio interface, select the service app from the list, click [View code], and check the [Current] tab.
    • Registration through the terminal
      export NCP_CLOVASTUDIO_API_KEY="<NCP-CLOVASTUDIO-API-KEY>"
      export NCP_APIGW_API_KEY="<NCP-APIGW-API-KEY>"
      export NCP_CLOVASTUDIO_APP_ID="<embedding service app ID>"
      
    • Registration through Python
      import getpass
      import os
      
      os.environ["NCP_CLOVASTUDIO_API_KEY"] = getpass.getpass(
          "Enter NCP CLOVA STUDIO API key: "
      )
      os.environ["NCP_APIGW_API_KEY"] = getpass.getpass(
          "Enter NCP API Gateway API key: "
      )
      os.environ["NCP_CLOVASTUDIO_APP_ID"] = input("Enter embedding service ID: ")
      

Integration examples

This section introduces example codes for integrating LangChain with CLOVA Studio.

Use HyperCLOVA X model

The following is an example code for using CLOVA Studio's HyperCLOVA X model through LangChain:

from langchain_naver import ChatClovaX
  
chat = ChatClovaX(
    model="HCX-005" # Enter model name (default value: HCX-005) 
)

You can use the following parameters with your request when defining the ChatClovaX class instance:

Field Type Required Description
model String Optional Model name.
  • The Playground menu's chat mode basic model | tuned model.
  • Basic model: HCX-005 (default value).
  • Tuned model: ft:{튜닝 모델의 Task ID} (Example: ft:abcd1234).
temperature Float Optional Degree of diversity in created tokens.
  • See temperature of chat completions or chat completions v3.
max_tokens Integer Optional Maximum number of tokens created.
  • See maxTokens{target="_blank"} of chat completions or chat completions v3.
  • Unavailable simultaneously with max_completion_tokens.
max_completion_tokens Integer Optional Maximum number of tokens created.
  • See maxCompletionTokens{target="_blank"} of chat completions v3.
  • Unavailable simultaneously with max_tokens.
thinking Dict Optional Inference model settings information.
repeat_penalty Float Optional Penalty degree for creating the same token.
repetition_penalty Float Optional Penalty degree for creating the same token.
stop List [String] Optional Stop character to terminate token creation.
seed Integer Optional Adjust the consistency level of results when running the model repeatedly.
  • See seed of chat completions or chat completions v3.
top_k Integer Optional Sampling from the designated K tokens with the highest probabilities in created token candidates.
  • See topK of chat completions or chat completions v3.
top_p Float Optional Sampling based on cumulative probability in the created token candidates.
  • See topP of chat completions or chat completions v3.
timeout Integer Optional Timeout (seconds).
  • 1-N (default value: 90).
max_retries Integer Optional Retry count.
  • 2-N (default: 2).
  • Automatic retry on errors.
api_key String Optional API Key.
  • Use when entering a separate input, such as if a value has not been set in advance or has been changed.
base_url String Optional CLOVA Studio common request API URL.
  • Default value: https://clovastudio.stream.ntruss.com/v1/openai.

You can run chat models using invoke or stream, LangChain's Chat model class methods. The following are example codes:

  • invoke
    messages = [
        (
            "system",
            "CLOVA Studio is a development tool that allows you to easily create AI services using the HyperCLOVA X model.",
        ),
        ("human", "What is CLOVA Studio?"),
    ]
    ai_msg = chat.invoke(messages)
    ai_msg
    
  • stream
    messages = [
        (
            "system",
            "CLOVA Studio is a development tool that allows you to easily create AI services using the HyperCLOVA X model.",
        ),
        ("human", "What is CLOVA Studio?"),
    ]
    
    for chunk in chat.stream(messages):
        print(chunk.content, end="", flush=True)
    

Additionally, you can use the bind_tools method to implement LangChain's tool calling feature based on CLOVA Studio's feature calling feature. The following are example codes:

from langchain_naver import ChatClovaX
from pydantic import BaseModel, Field

chat = ChatClovaX(
    model="HCX-005" # Enter the model name (default value: HCX-005)
    max_tokens=1024 # Set the value to more than 1024 when using Function calling
)

class GetWeather(BaseModel):
    '''Check the current weather for the given location.'''

    location: str = Field(
        ..., description="The name of the city where you want to check the weather. Example: Gyeonggi-do, Seongnam-si, Bundang-gu"
    )

chat_with_tool = chat.bind_tools(
    [GetWeather]
)
ai_msg = chat_with_tools.invoke(
    "Is it hotter in Bundang or Pangyo?"
)
ai_msg.tool_calls

If you use the thinking model, you can set related features through the thinking parameter when you define the ChatClovaX class instance. You can also check the reasoning process using the following separate method.

from langchain_naver import ChatClovaX

chat = ChatClovaX(
    model="HCX-007",
    thinking={
        "effort": "low"  # 'none' (do not use inference), 'low' (default value), 'medium', or 'high'
    },
)
ai_msg = chat.invoke("What is 3 cubed?")

print(ai_msg.content) # Final response
print(ai_msg.additional_kwargs["thinking_content"]) # Reasoning process
Note

For more information about how to use CLOVA Studio's chat models through LangChain, see the official documentation.

Use embedding tools

The following is an example code for using CLOVA Studio's embedding (embedding v2) tool through LangChain:

from langchain_naver import ClovaXEmbeddings
 
embeddings = ClovaXEmbeddings(
    model="clir-emb-dolphin",  # Model name (default value: clir-emb-dolphin)
)

You can use the following parameters with your request when defining the ClovaXEmbeddings class instance:

Field Type Required Description
model String Optional Model name.
  • Explorer menu's embedding (embedding v2) model.
    • clir-emb-dolphin (default value) | clir-sts-dolphin | bge-m3.
api_key String Optional API Key.
  • Use when entering a separate input, such as if a value has not been set in advance or has been changed.
timeout Integer Optional Timeout (seconds).
  • 1-N (default value: 90).
base_url String Optional CLOVA Studio common request API URL.
  • Default value: https://clovastudio.stream.ntruss.com/v1/openai.

You can run chat models using embed_query orembed_documents, LangChain's Embedding model class methods. The following are example codes:

  • embed_query
    query = "CLOVA Studio is a development tool that allows you to easily create AI services using the HyperCLOVA X model."
    single_vector = embeddings.embed_query(query)
    
  • embed_documents
    text1="CLOVA Studio is a development tool that allows you to easily create AI services using the HyperCLOVA X model."
    text2 = "LangChain is an open source framework designed to support the development of language model-based applications."
    document = [text1, text2]
    multiple_vector = embeddings.embed_documents(document)
    
Note

For more information about how to use CLOVA Studio's embedding (embedding v2) tool through LangChain, see the official documentation.