2023-02-18 22:25:37 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "9597802c",
"metadata": {},
"source": [
2023-02-20 05:14:59 +00:00
"# AI21\n",
2023-04-18 03:25:32 +00:00
"\n",
"[AI21 Studio](https://docs.ai21.com/) provides API access to `Jurassic-2` large language models.\n",
"\n",
"This example goes over how to use LangChain to interact with [AI21 models](https://docs.ai21.com/docs/jurassic-2-models)."
2023-02-18 22:25:37 +00:00
]
},
{
"cell_type": "code",
2023-04-18 03:25:32 +00:00
"execution_count": null,
"id": "02be122d-04e8-4ec6-84d1-f1d8961d6828",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# install the package:\n",
"!pip install ai21"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4229227e-6ca2-41ad-a3c3-5f29e3559091",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" ········\n"
]
}
],
"source": [
"# get AI21_API_KEY. Use https://studio.ai21.com/account/account\n",
"\n",
"from getpass import getpass\n",
2023-06-16 18:52:56 +00:00
"\n",
"AI21_API_KEY = getpass()"
2023-04-18 03:25:32 +00:00
]
},
{
"cell_type": "code",
"execution_count": 8,
2023-02-18 22:25:37 +00:00
"id": "6fb585dd",
2023-04-18 03:25:32 +00:00
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"from langchain.llms import AI21\n",
2023-09-17 00:22:48 +00:00
"from langchain.prompts import PromptTemplate\nfrom langchain.chains import LLMChain"
2023-02-18 22:25:37 +00:00
]
},
{
"cell_type": "code",
2023-04-18 03:25:32 +00:00
"execution_count": 9,
2023-02-18 22:25:37 +00:00
"id": "035dea0f",
2023-04-18 03:25:32 +00:00
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])"
]
},
{
"cell_type": "code",
2023-04-18 03:25:32 +00:00
"execution_count": 10,
2023-02-18 22:25:37 +00:00
"id": "3f3458d9",
2023-04-18 03:25:32 +00:00
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
2023-04-18 03:25:32 +00:00
"llm = AI21(ai21_api_key=AI21_API_KEY)"
2023-02-18 22:25:37 +00:00
]
},
{
"cell_type": "code",
2023-04-18 03:25:32 +00:00
"execution_count": 11,
2023-02-18 22:25:37 +00:00
"id": "a641dbd9",
2023-04-18 03:25:32 +00:00
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
]
},
{
"cell_type": "code",
2023-04-18 03:25:32 +00:00
"execution_count": 12,
2023-02-18 22:25:37 +00:00
"id": "9f0b1960",
2023-04-18 03:25:32 +00:00
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'\\n1. What year was Justin Bieber born?\\nJustin Bieber was born in 1994.\\n2. What team won the Super Bowl in 1994?\\nThe Dallas Cowboys won the Super Bowl in 1994.'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
2023-02-18 22:25:37 +00:00
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"\n",
"llm_chain.run(question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22bce013",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-06-16 18:52:56 +00:00
"version": "3.11.3"
2023-02-18 22:25:37 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}