LangChain is a software framework for developing applications powered by (large language models) LLMs. This document describes how to use LangChain to interact with SambaNova models.

Follow the example below to see how you can directly instantiate a ChatSambaNovaCloud model from LangChain and prompt it.

Setup

Do the following to access ChatSambaNovaCloud models:

  1. Create a SambaNovaCloud account and get an API key.

  2. Run the command below to install the langchain-sambanova integration package.

pip install langchain-sambanova

Credentials

Run the commands below to register the API key you received from cloud.sambanova.ai as an environment variable.

import os

sambanova_api_key = "<your-api-key>"
os.environ["SAMBANOVA_API_KEY"] = sambanova_api_key

Instantiation

Now you can instantiate a ChatSambaNovaCloud model object and generate chat completions, as shown in the example below. The example below uses Meta’s Llama 3.1 70B.

from langchain_sambanova import ChatSambaNovaCloud

llm = ChatSambaNovaCloud(
    model="Meta-Llama-3.3-70B-Instruct",
    max_tokens=1024,
    temperature=0.7,
    top_p=0.01,
)

Invocation

You can invoke the model using the command below.

messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. "
        "Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
print(ai_msg.content)

You should see an output similar to the one below.

J'adore la programmation.

Chaining

You can chain the model with a prompt template, as shown in the example below.

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate(
    [
        (
            "system",
            "You are a helpful assistant that translates {input_language} "
            "to {output_language}.",
        ),
        ("human", "{input}"),
    ]
)

chain = prompt | llm
response = chain.invoke(
    {
        "input_language": "English",
        "output_language": "German",
        "input": "I love programming.",
    }
)
print(response.content)

You should see an output similar to the one below.

Ich liebe das Programmieren.