Skip to main content

LLM Models

Use LlamaIndex to interact with Clarifai LLMs


Let’s illustrate how you can use LlamaIndex 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 URL of the model you want to use. Large language models can be found here
  • Alternatively, get the ID of the user owning the model you want to use, the ID of the app where the model is found, and the name of the model
  • Install LlamaIndex and Clarifai Python SDK by running pip install llama-index-llms-clarifai
info

You can learn how to authenticate with the Clarifai platform here.

note

Clarifai models can be referenced either through their full URL or by using the combination of user ID, app ID, and model name. If the model version is not specified, the latest version will be used by default.

Text Completion

Here is an example of how to use a Clarifai LLM and LlamaIndex for a text completion task.

##########################################################################################################################
# In this section, we set the user authentication, model URL, and prompt text. Alternatively, set the user and app ID,
# and model name. Change these strings to run your own example.
#########################################################################################################################

PAT = "YOUR_PAT_HERE"

MODEL_URL = "https://clarifai.com/openai/chat-completion/models/GPT-4"
PROMPT = "Write a 5 line rhyming poem about science"

# Alternatively, you can specify user ID, app ID, and model name
#USER_ID = 'openai'
#APP_ID = 'chat-completion'
#MODEL_NAME = 'GPT-4'

###############################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##############################################################################

# Import the required packages
import os
from llama_index.llms.clarifai import Clarifai

# Set Clarifai PAT as environment variable
os.environ["CLARIFAI_PAT"] = PAT

# Initialize the LLM class
llm_model = Clarifai(model_url=MODEL_URL)

# Alternatively
# llm_model = Clarifai(
# user_id=USER_ID,
# app_id=APP_ID,
# model_name=MODEL_NAME
# )

# Call complete function
llm_response = llm_model.complete(prompt=PROMPT)

print(llm_response)
Output Example
In the lab, where ideas take flight,
Underneath the microscope's bright light.
Atoms dance and cells divide,
In science's mystery, we confide.
Unveiling the universe, day and night.

Chat

Here is an example of how to use a Clarifai LLM and LlamaIndex for a chatting task.

##########################################################################################################################
# In this section, we set the user authentication, model URL, and prompt text. Alternatively, set the user and app ID,
# and model name. Change these strings to run your own example.
##########################################################################################################################

PAT = "YOUR_PAT_HERE"

MODEL_URL = "https://clarifai.com/meta/Llama-2/models/llama2-7b-chat"
PROMPT = "Write about climate change in 5 lines"

# Alternatively, you can specify user ID, app ID, and model name
#USER_ID = 'meta'
#APP_ID = 'Llama-2'
#MODEL_NAME = 'llama2-70b-chat'

###########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

# Import the required packages
import os
from llama_index.llms.clarifai import Clarifai
from llama_index.llms import ChatMessage

# Set Clarifai PAT as environment variable
os.environ["CLARIFAI_PAT"] = PAT

# Initialize the LLM class
llm_model = Clarifai(model_url=MODEL_URL)

# Alternatively
# llm_model = Clarifai(
# user_id=USER_ID,
# app_id=APP_ID,
# model_name=MODEL_NAME
# )

messages = [ChatMessage(role="user", content=PROMPT)]

# Call chat function
Response = llm_model.chat(messages)

print(Response)
Output Example
user: 

model: Climate change is a pressing global issue
It's caused by human activities like burning fossil fuels
This releases greenhouse gases into the atmosphere
Which trap heat and lead to rising temperatures
With devastating consequences for our planet and its inhabitants
info

You can explore the LlamaIndex documentation to learn more on how to use the framework to interact with Clarifai’s LLMs.