Clarifai LLM
Example notebook to call different LLM models using Clarifai
Section titled “Example notebook to call different LLM models using Clarifai”If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install llama-index-llms-clarifai!pip install llama-indexInstall clarifai
!pip install clarifaiSet clarifai PAT as environment variable.
import os
os.environ["CLARIFAI_PAT"] = "<YOUR CLARIFAI PAT>"Import clarifai package
from llama_index.llms.clarifai import ClarifaiExplore various models according to your prefrence from Our Models page
# Example parametersparams = dict( user_id="clarifai", app_id="ml", model_name="llama2-7b-alternative-4k", model_url=( "https://clarifai.com/clarifai/ml/models/llama2-7b-alternative-4k" ),)Initialize the LLM
# Method:1 using model_url parameterllm_model = Clarifai(model_url=params["model_url"])# Method:2 using model_name, app_id & user_id parametersllm_model = Clarifai( model_name=params["model_name"], app_id=params["app_id"], user_id=params["user_id"],)Call complete function
llm_reponse = llm_model.complete( prompt="write a 10 line rhyming poem about science")print(llm_reponse).Science is fun, it's true!From atoms to galaxies, it's all new!With experiments and tests, we learn so fast,And discoveries come from the past.It helps us understand the world around,And makes our lives more profound.So let's embrace this wondrous art,And see where it takes us in the start!Call chat function
from llama_index.core.llms import ChatMessage
messages = [ ChatMessage(role="user", content="write about climate change in 50 lines")]Response = llm_model.chat(messages)print(Response)user: or less.Climate change is a serious threat to our planet and its inhabitants. Rising temperatures are causing extreme weather events, such as hurricanes, droughts, and wildfires. Sea levels are rising, threatening coastal communities and ecosystems. The melting of polar ice caps is disrupting global navigation and commerce. Climate change is also exacerbating air pollution, which can lead to respiratory problems and other health issues. It's essential that we take action now to reduce greenhouse gas emissions and transition to renewable energy sources to mitigate the worst effects of climate change.Using Inference parameters
Section titled “Using Inference parameters”Alternatively you can call models with inference parameters.
# Here is an inference parameter example for GPT model.inference_params = dict(temperature=str(0.3), max_tokens=20)llm_reponse = llm_model.complete( prompt="What is nuclear fission and fusion?", inference_params=params,)messages = [ChatMessage(role="user", content="Explain about the big bang")]Response = llm_model.chat(messages, inference_params=params)Note for AI agents: this documentation is built for programmatic access.
- Overview of all docs: https://developers.llamaindex.ai/llms.txt
- Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md
- Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters.
- A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/