Skip to main content

AI21

AI21 Studio provides API access to Jurassic-2 large language models.

This example goes over how to use LangChain to interact with AI21 models.

# install the package:
%pip install --upgrade --quiet ai21
WARNING: There was an error checking the latest version of pip.
Note: you may need to restart the kernel to use updated packages.
# get AI21_API_KEY. Use https://studio.ai21.com/account/account

from getpass import getpass

AI21_API_KEY = getpass()
from langchain_community.llms import AI21
from langchain_core.prompts import PromptTemplate
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)
llm = AI21(ai21_api_key=AI21_API_KEY)
llm_chain = prompt | llm
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

llm_chain.invoke({"question": question})
'\nThe Super Bowl in the year Justin Beiber was born was in the year 1991.\nThe Super Bowl in 1991 was won by the Washington Redskins.\nFinal answer: Washington Redskins'