Skip to main content

ChatAI21

This notebook covers how to get started with AI21 chat models. Note that different chat models support different parameters. See the AI21 documentation to learn more about the parameters in your chosen model. See all AI21's LangChain components.

Installation

!pip install -qU langchain-ai21

Environment Setup

We'll need to get an AI21 API key and set the AI21_API_KEY environment variable:

import os
from getpass import getpass

os.environ["AI21_API_KEY"] = getpass()

Usage

from langchain_ai21 import ChatAI21
from langchain_core.prompts import ChatPromptTemplate

chat = ChatAI21(model="jamba-instruct")

prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant that translates English to French."),
("human", "Translate this sentence from English to French. {english_text}."),
]
)

chain = prompt | chat
chain.invoke({"english_text": "Hello, how are you?"})
AIMessage(content='Bonjour, comment vas-tu?')

Was this page helpful?


You can also leave detailed feedback on GitHub.