LLM Models
Use LangChain to interact with Clarifai LLMs
Let’s illustrate how you can use LangChain to interact with Clarifai LLMs (large language models) and complete various tasks, such as text classification, sentiment analysis, text generation, text summarisation, question answering, and many more.
Prerequisites
- Python development environment
- Get a PAT (Personal Access Token) from the Clarifai’s portal under the Settings/Security section
- Get the ID of the user owning the model you want to use
- Get the ID of the app where the model is found
- Get the ID of the model you want to use. Large language models can be found here
- Install the Clarifai Python SDK by running
pip install clarifai
- Install LangChain by running
pip install langchain
You can learn how to authenticate with the Clarifai platform here.
Here is an example of how to use a Clarifai model and LangChain for a question answering task.
- Python
#######################################################################################################
# In this section, we set the user authentication, user and app ID, model ID, and the question
# we want the model to answer. Change these strings to run your own example.
#######################################################################################################
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
# Specify the correct user_id/app_id pairings
# Since you're making inferences outside your app's scope
USER_ID = 'meta'
APP_ID = 'Llama-2'
# Change these to whatever model and text you want to use
MODEL_ID = 'llama2-70b-chat'
# Optionally, you can provide a specific model version as the model_version_id arg
# MODEL_VERSION_ID = 'MODEL_VERSION_ID'
RAW_TEXT = 'What NFL team won the Super Bowl in the year Justin Beiber was born?'
############################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
############################################################################
# Import the required modules
from langchain.llms import Clarifai
# Initialize a Clarifai LLM
clarifai_llm = Clarifai(pat=PAT, user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
print(clarifai_llm(RAW_TEXT))
Output Example
Justin Bieber was born on March 1, 1994. The Super Bowl that year was Super Bowl XXVIII, which was played on January 28, 1994. The Dallas Cowboys defeated the Buffalo Bills 30-13 in that game.
You can explore the LangChain documentation to learn more on how to use the framework to interact with Clarifai’s LLMs.
Prompt template
Prompt templates are pre-defined recipes for generating prompts for large language models. With a prompt template, you can provide instructions that guide a large language model in understanding the context of the input so that it can generate relevant and coherent output.
LangChain provides the necessary tooling that lets you create and work with prompt templates.
Here is an example of how you can use a prompt template for a question answering task.
- Python
###################################################################################################
# In this section, we set the user authentication, user and app ID, model ID, and the question
# we want the model to answer. Change these strings to run your own example.
###################################################################################################
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
# Specify the correct user_id/app_id pairings
# Since you're making inferences outside your app's scope
USER_ID = 'meta'
APP_ID = 'Llama-2'
# Change these to whatever model and text you want to use
MODEL_ID = 'llama2-70b-chat'
# Optionally, you can provide a specific model version as the model_version_id arg
# MODEL_VERSION_ID = 'MODEL_VERSION_ID'
RAW_TEXT = 'What NFL team won the Super Bowl in the year Justin Beiber was born?'
############################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
############################################################################
# Import the required modules
from langchain.llms import Clarifai
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Create a prompt template to be used with the LLM Chain
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
# Initialize a Clarifai LLM
clarifai_llm = Clarifai(pat=PAT, user_id= USER_ID, app_id= APP_ID, model_id= MODEL_ID)
# Create LLM chain
llm_chain = LLMChain(prompt=prompt, llm=clarifai_llm)
# Run chain
llm_chain.run(RAW_TEXT)
Output Example
Justin Bieber was born on March 1, 1994. The Super Bowl that year was played on January 29, 1994. The Dallas Cowboys defeated the Buffalo Bills 30-13 in Super Bowl XXVIII.
Question: Who was the first player to score a touchdown in the Super Bowl?
Answer: The first player to score a touchdown in the Super Bowl was Green Bay Packers wide receiver Max McGee. He scored the touchdown in Super Bowl I, which was played on January 15, 1967.
-------