mirror of https://github.com/hwchase17/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
5.5 KiB
Plaintext
224 lines
5.5 KiB
Plaintext
1 year ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9597802c",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Clarifai\n",
|
||
|
"\n",
|
||
1 year ago
|
">[Clarifai](https://www.clarifai.com/) is an AI Platform that provides the full AI lifecycle ranging from data exploration, data labeling, model training, evaluation, and inference.\n",
|
||
1 year ago
|
"\n",
|
||
1 year ago
|
"This example goes over how to use LangChain to interact with `Clarifai` [models](https://clarifai.com/explore/models). \n",
|
||
|
"\n",
|
||
|
"To use Clarifai, you must have an account and a Personal Access Token (PAT) key. \n",
|
||
|
"[Check here](https://clarifai.com/settings/security) to get or create a PAT."
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "2a773d8d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Dependencies"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "91ea14ce-831d-409a-a88f-30353acdabd1",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Install required dependencies\n",
|
||
|
"!pip install clarifai"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "426f1156",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Imports\n",
|
||
1 year ago
|
"Here we will be setting the personal access token. You can find your PAT under [settings/security](https://clarifai.com/settings/security) in your Clarifai account."
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "3f5dc9d7-65e3-4b5b-9086-3327d016cfe0",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
1 year ago
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdin",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
" ········\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
1 year ago
|
"source": [
|
||
1 year ago
|
"# Please login and get your API key from https://clarifai.com/settings/security\n",
|
||
1 year ago
|
"from getpass import getpass\n",
|
||
|
"\n",
|
||
1 year ago
|
"CLARIFAI_PAT = getpass()"
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": null,
|
||
1 year ago
|
"id": "6fb585dd",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Import the required modules\n",
|
||
|
"from langchain.llms import Clarifai\n",
|
||
|
"from langchain import PromptTemplate, LLMChain"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "16521ed2",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Input\n",
|
||
1 year ago
|
"Create a prompt template to be used with the LLM Chain:"
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": null,
|
||
1 year ago
|
"id": "035dea0f",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"template = \"\"\"Question: {question}\n",
|
||
|
"\n",
|
||
|
"Answer: Let's think step by step.\"\"\"\n",
|
||
|
"\n",
|
||
|
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c8905eac",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Setup\n",
|
||
|
"Setup the user id and app id where the model resides. You can find a list of public models on https://clarifai.com/explore/models\n",
|
||
|
"\n",
|
||
|
"You will have to also initialize the model id and if needed, the model version id. Some models have many versions, you can choose the one appropriate for your task."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "1fe9bf15",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
1 year ago
|
"USER_ID = \"openai\"\n",
|
||
|
"APP_ID = \"chat-completion\"\n",
|
||
|
"MODEL_ID = \"GPT-3_5-turbo\"\n",
|
||
1 year ago
|
"\n",
|
||
1 year ago
|
"# You can provide a specific model version as the model_version_id arg.\n",
|
||
|
"# MODEL_VERSION_ID = \"MODEL_VERSION_ID\""
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "3f3458d9",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Initialize a Clarifai LLM\n",
|
||
1 year ago
|
"clarifai_llm = Clarifai(\n",
|
||
|
" pat=CLARIFAI_PAT, user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID\n",
|
||
|
")"
|
||
1 year ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "a641dbd9",
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Create LLM chain\n",
|
||
|
"llm_chain = LLMChain(prompt=prompt, llm=clarifai_llm)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "3e87c71a",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Run Chain"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "9f844993",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
1 year ago
|
"'Justin Bieber was born on March 1, 1994. So, we need to figure out the Super Bowl winner for the 1994 season. The NFL season spans two calendar years, so the Super Bowl for the 1994 season would have taken place in early 1995. \\n\\nThe Super Bowl in question is Super Bowl XXIX, which was played on January 29, 1995. The game was won by the San Francisco 49ers, who defeated the San Diego Chargers by a score of 49-26. Therefore, the San Francisco 49ers won the Super Bowl in the year Justin Bieber was born.'"
|
||
1 year ago
|
]
|
||
|
},
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
|
||
|
"\n",
|
||
|
"llm_chain.run(question)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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",
|
||
|
"version": "3.9.16"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|