Clarifai
Clarifai is an AI Platform that provides the full AI lifecycle ranging from data exploration, data labeling, model training, evaluation, and inference.
This example goes over how to use LangChain to interact with Clarifai
models.
To use Clarifai, you must have an account and a Personal Access Token (PAT) key. Check here to get or create a PAT.
Dependencies
# Install required dependencies
%pip install --upgrade --quiet clarifai
# Declare clarifai pat token as environment variable or you can pass it as argument in clarifai class.
import os
os.environ["CLARIFAI_PAT"] = "CLARIFAI_PAT_TOKEN"
Imports
Here we will be setting the personal access token. You can find your PAT under settings/security in your Clarifai account.
# Please login and get your API key from https://clarifai.com/settings/security
from getpass import getpass
CLARIFAI_PAT = getpass()
# Import the required modules
from langchain.chains import LLMChain
from langchain_community.llms import Clarifai
from langchain_core.prompts import PromptTemplate
Input
Create a prompt template to be used with the LLM Chain:
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
Setup
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
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.
Alternatively, You can use the model_url (for ex: "https://clarifai.com/anthropic/completion/models/claude-v2") for intialization.
USER_ID = "openai"
APP_ID = "chat-completion"
MODEL_ID = "GPT-3_5-turbo"
# You can provide a specific model version as the model_version_id arg.
# MODEL_VERSION_ID = "MODEL_VERSION_ID"
# or
MODEL_URL = "https://clarifai.com/openai/chat-completion/models/GPT-4"
# Initialize a Clarifai LLM
clarifai_llm = Clarifai(user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
# or
# Initialize through Model URL
clarifai_llm = Clarifai(model_url=MODEL_URL)
# Create LLM chain
llm_chain = LLMChain(prompt=prompt, llm=clarifai_llm)
Run Chain
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"
llm_chain.run(question)
' Okay, here are the steps to figure this out:\n\n1. Justin Bieber was born on March 1, 1994.\n\n2. The Super Bowl that took place in the year of his birth was Super Bowl XXVIII. \n\n3. Super Bowl XXVIII was played on January 30, 1994.\n\n4. The two teams that played in Super Bowl XXVIII were the Dallas Cowboys and the Buffalo Bills. \n\n5. The Dallas Cowboys defeated the Buffalo Bills 30-13 to win Super Bowl XXVIII.\n\nTherefore, the NFL team that won the Super Bowl in the year Justin Bieber was born was the Dallas Cowboys.'