### Install RAG Example Dependencies Source: https://llamahub.ai/l/vector_stores/llama-index-vector-stores-objectbox?from= Install additional packages required for the complete RAG example. ```bash pip install llama-index --quiet pip install llama-index-embeddings-huggingface --quiet pip install llama-index-llms-gemini --quiet ``` -------------------------------- ### End-to-End Example: AzureIndexStore Setup and Index Creation Source: https://llamahub.ai/l/storage/llama-index-storage-index-store-azure?from= Demonstrates loading documents, parsing them into nodes, and initializing a `StorageContext` with `AzureIndexStore`. It then adds documents to the docstore and creates a `SimpleKeywordTableIndex` and `VectorStoreIndex`. ```python from llama_index.core import SimpleDirectoryReader, StorageContext from llama_index.core.node_parser import SentenceSplitter from llama_index.core import VectorStoreIndex reader = SimpleDirectoryReader("./data/paul_graham/") documents = reader.load_data() nodes = SentenceSplitter().get_nodes_from_documents(documents) storage_context = StorageContext.from_defaults( index_store=AzureIndexStore.from_account_and_key( "your_account_name", "your_account_key", service_mode=ServiceMode.STORAGE, ), ) storage_context.docstore.add_documents(nodes) keyword_table_index = SimpleKeywordTableIndex( nodes, storage_context=storage_context ) vector_index = VectorStoreIndex(nodes, storage_context=storage_context) ``` -------------------------------- ### Load Data from Mangopps Guides Source: https://llamahub.ai/l/readers/llama-index-readers-mangoapps-guides?from= Instantiate the MangoppsGuidesReader and load data by providing the base URL of the MangoppsGuides installation and an optional limit for the number of links to crawl. ```python from llama_index.readers.mangoapps_guides import MangoppsGuidesReader loader = MangoppsGuidesReader() documents = loader.load_data( domain_url="https://guides.mangoapps.com", limit=1 ) ``` -------------------------------- ### Run Example Script Source: https://llamahub.ai/l/embeddings/llama-index-embeddings-heroku?from= Navigate to the examples directory and run a Python example script using `uvicorn`. ```bash cd examples uv run python basic_usage.py ``` -------------------------------- ### Minimal Gemini Live Voice Agent Example Source: https://llamahub.ai/l/voice_agents/llama-index-voice-agents-gemini-live?from= A basic example demonstrating how to set up and run a Gemini Live Voice Agent with a custom tool. Ensure GOOGLE_API_KEY is set as an environment variable or passed during initialization. The conversation starts automatically. ```python from llama_index.voice_agents.gemini_live import GeminiLiveVoiceAgent from llama_index.core.tools import FunctionTool from llama_index.core.voice_agents import BaseVoiceAgentEvent from llama_index.core.llms import ChatMessage, TextBlock from typing import List import random import json # use filter functions to export messages and events without your terminal being swamped by base64-encoded audio bytes :) def filter_events( events: List[BaseVoiceAgentEvent], ) -> List[BaseVoiceAgentEvent]: evs = [] for event in events: if not "audio" in event.type_t: evs.append(event) return evs def filter_messages(messages: List[ChatMessage]) -> List[ChatMessage]: msgs = [] for message in messages: msg = ChatMessage(role=message.role, blocks=[]) for b in message.blocks: if isinstance(b, TextBlock): msg.blocks.append(b) if len(msg.blocks) > 0: msgs.append(msg) return msgs def get_weather(location: str) -> dict: """Fetch weather data for a given location.""" return json.dumps( { "location": location, "temperature_c": round(random.uniform(15, 30), 1), "humidity_percent": random.randint(40, 90), "wind_speed_kmh": round(random.uniform(5, 25), 1), "precipitation_probability_percent": random.randint(0, 100), }, indent=4, ) weather_tool = FunctionTool.from_defaults( fn=get_weather, name="get_weather", description="Get the weather at a given location", ) async def main(): conversation = GeminiLiveVoiceAgent(tools=[weather_tool]) await conversation.start() if conversation._quitflag: print("Events") print(conversation.export_events(filter=filter_events)) print() print("Messages") print(conversation.export_messages(filter=filter_messages)) if __name__ == "__main__": import asyncio asyncio.run(main()) ``` -------------------------------- ### Install Dependencies Source: https://llamahub.ai/l/readers/llama-index-readers-earnings-call-transcript?from= Install all necessary dependencies from the requirements.txt file. ```bash pip install -r requirements.txt ``` -------------------------------- ### LMstudio LLM Chat Example Source: https://llamahub.ai/l/llms/llama-index-llms-lmstudio?from= Instantiate the LMStudio LLM and perform a chat completion. Ensure LM Studio is running with a model loaded and the local server started. ```python from llama_index.llms.lmstudio import LMStudio from llama_index.schema import ChatMessage, MessageRole llm = LMStudio( model_name="Hermes-2-Pro-Llama-3-8B", base_url="http://localhost:1234/v1", temperature=0.7, ) messages = [ ChatMessage( role=MessageRole.SYSTEM, content="You an expert AI assistant. Help User with their queries.", ), ChatMessage( role=MessageRole.USER, content="What is the significance of the number 42?", ), ] response = llm.chat(messages=messages) print(str(response)) ``` -------------------------------- ### Install llama-index-postprocessor-alibabacloud-aisearch-rerank Source: https://llamahub.ai/l/postprocessor/llama-index-postprocessor-alibabacloud-aisearch-rerank?from= Install the necessary package using pip. ```bash pip install llama-index-postprocessor-alibabacloud-aisearch-rerank ``` -------------------------------- ### Install llama-index-tools-parallel-web-systems Source: https://llamahub.ai/l/tools/llama-index-tools-parallel-web-systems?from= Install the necessary package using pip. ```bash pip install llama-index-tools-parallel-web-systems ``` -------------------------------- ### Example Usage: ZyteSerpReader and ZyteWebReader Source: https://llamahub.ai/l/readers/llama-index-readers-zyte-serp?from= Demonstrates initializing ZyteSerpReader to get search URLs, then using ZyteWebReader in 'article' mode to extract content from those URLs. Replace 'your_api_key_here' with your actual Zyte API key. ```python from llama_index.readers.zyte_serp import ZyteSerpReader from llama_index.readers.web.zyte.base import ZyteWebReader # Initialize the ZyteSerpReader with your API key zyte_serp = ZyteSerpReader( api_key="your_api_key_here", # Replace with your actual API key ) # Get the search results (URLs from google search results) search_urls = zyte_serp.load_data(query="llama index docs") # Display the results print(search_urls) urls = [result.text for result in search_urls] # Initialize the ZyteWebReader to load the content from search results zyte_web = ZyteWebReader( api_key="your_api_key_here", # Replace with your actual API key mode="article", ) documents = zyte_web.load_data(urls) print(documents) ``` -------------------------------- ### Install llama-index-llms-alibabacloud-aisearch Source: https://llamahub.ai/l/llms/llama-index-llms-alibabacloud-aisearch?from= Install the necessary package using pip. ```bash pip install llama-index-llms-alibabacloud-aisearch ``` -------------------------------- ### Install MangoppsGuides Reader Source: https://llamahub.ai/l/readers/llama-index-readers-mangoapps-guides?from= Install the MangoppsGuides reader using pip. ```bash pip install llama-index-readers-mangoapps-guides ``` -------------------------------- ### Install llama-index-multi-modal-llms-nvidia Source: https://llamahub.ai/l/multi_modal_llms/llama-index-multi-modal-llms-nvidia?from= Install the necessary package using pip. ```bash pip install llama-index-multi-modal-llms-nvidia ``` -------------------------------- ### Install llama-index-tools-finance Source: https://llamahub.ai/l/tools/llama-index-tools-finance?from= Install the finance tools package using pip. ```bash pip install llama-index-tools-finance ``` -------------------------------- ### Install Dependencies Source: https://llamahub.ai/l/readers/llama-index-readers-sec-filings?from= Install project dependencies from the requirements.txt file. ```bash python install -r requirements.txt ``` -------------------------------- ### Install llama-index-protocols-ag-ui Source: https://llamahub.ai/l/protocols/llama-index-protocols-ag-ui?from= Install the necessary package using pip. ```bash pip install llama-index-protocols-ag-ui ``` -------------------------------- ### Install llama-index-readers-google Source: https://llamahub.ai/l/readers/llama-index-readers-google?from= Install the necessary package for Google readers. ```bash pip install llama-index-readers-google ``` -------------------------------- ### Install llama-index-readers-preprocess Source: https://llamahub.ai/l/readers/llama-index-readers-preprocess?from= Install the llama-index-readers-preprocess package using pip. ```bash pip install llama-index-readers-preprocess ``` -------------------------------- ### Install llama-index-retrievers-videodb Source: https://llamahub.ai/l/retrievers/llama-index-retrievers-videodb?from= Install the necessary packages for VideoDB integration. Ensure you have llama-index and videodb installed. ```bash pip install llama-index llama-index-retrievers-videodb videodb ``` -------------------------------- ### Install llama-index-callbacks-wandb Source: https://llamahub.ai/l/callbacks/llama-index-callbacks-wandb?from= Install the necessary package using pip. ```bash pip install llama-index-callbacks-wandb ``` -------------------------------- ### Install llama-index-agent-llm-compiler Source: https://llamahub.ai/l/agent/llama-index-agent-llm-compiler?from= Install the package using pip. ```bash pip install llama-index-agent-llm-compiler ``` -------------------------------- ### Install llama-index-vector-stores-analyticdb Source: https://llamahub.ai/l/vector_stores/llama-index-vector-stores-analyticdb?from= Install the necessary package for the AnalyticDB vector store. ```bash pip install llama-index-vector-stores-analyticdb ``` -------------------------------- ### Install LlamaIndex Packages Source: https://llamahub.ai/l/llms/llama-index-llms-llama-cpp?from= Install the necessary LlamaIndex packages for Hugging Face embeddings and Llama Cpp LLM integration. Ensure you have followed the performance optimization guide for Llama Cpp installation. ```python pip install llama-index-embeddings-huggingface pip install llama-index-llms-llama-cpp ``` -------------------------------- ### Setup Source: https://llamahub.ai/l/indices/llama-index-indices-managed-bge-m3?from= Install the necessary package for Bge-M3 managed index integration. ```APIDOC pip install -U llama-index-indices-managed-bge-m3 ``` -------------------------------- ### Install llama-index-output-parsers-langchain Source: https://llamahub.ai/l/output_parsers/llama-index-output-parsers-langchain?from= Install the necessary package using pip. ```bash pip install llama-index-output-parsers-langchain ``` -------------------------------- ### Get GitHub Installation ID via API Source: https://llamahub.ai/l/readers/llama-index-readers-github?from= Use this curl command to retrieve your GitHub App installation IDs. Replace YOUR_JWT_TOKEN with your generated JWT. ```bash curl -H "Authorization: Bearer YOUR_JWT_TOKEN" \ https://api.github.com/app/installations ``` -------------------------------- ### RAG-Style Direct Answers Example Source: https://llamahub.ai/l/tools/llama-index-tools-airweave?from= Demonstrates using `search_and_generate_answer` to get a direct natural language answer from a collection. ```APIDOC ## RAG-Style Direct Answers ```python # Get a direct answer instead of raw documents answer = airweave_tool.search_and_generate_answer( collection_id="finance-data", query="What was our Q4 revenue growth?", limit=10, use_reranking=True, ) print(answer) # "Q4 revenue grew by 23% to $45M compared to Q3..." ``` ``` -------------------------------- ### End-to-End ZepReader Example Source: https://llamahub.ai/l/readers/llama-index-readers-zep?from= Demonstrates creating a Zep collection, adding documents, waiting for embedding, and querying with ZepReader. Requires a running Zep instance. ```python import time from uuid import uuid4 from llama_index.core.node_parser import SimpleNodeParser from llama_index.core import Document from zep_python import ZepClient from zep_python.document import Document as ZepDocument from llama_index.readers.zep import ZepReader # Create a Zep collection zep_api_url = "http://localhost:8000" # replace with your Zep API URL collection_name = f"babbage{uuid4().hex}" file = "babbages_calculating_engine.txt" print(f"Creating collection {collection_name}") client = ZepClient(base_url=zep_api_url, api_key="optional_api_key") collection = client.document.add_collection( name=collection_name, # required description="Babbage's Calculating Engine", # optional metadata={"foo": "bar"}, # optional metadata embedding_dimensions=1536, # this must match the model you've configured in Zep is_auto_embedded=True, # use Zep's built-in embedder. Defaults to True ) node_parser = SimpleNodeParser.from_defaults(chunk_size=250, chunk_overlap=20) with open(file) as f: raw_text = f.read() print("Splitting text into chunks and adding them to the Zep vector store.") docs = node_parser.get_nodes_from_documents( [Document(text=raw_text)], show_progress=True ) # Convert nodes to ZepDocument zep_docs = [ZepDocument(content=d.get_content()) for d in docs] uuids = collection.add_documents(zep_docs) print(f"Added {len(uuids)} documents to collection {collection_name}") print("Waiting for documents to be embedded") while True: c = client.document.get_collection(collection_name) print( "Embedding status: " f"{c.document_embedded_count}/{c.document_count} documents embedded" ) time.sleep(1) if c.status == "ready": break query = "Was Babbage awarded a medal?" # Using the ZepReader to load data from Zep reader = ZepReader(api_url=zep_api_url, api_key="optional_api_key") results = reader.load_data( collection_name=collection_name, query=query, top_k=3 ) print("\n\n".join([r.text for r in results])) ``` -------------------------------- ### Filter SharePoint Pages with Callback Source: https://llamahub.ai/l/readers/llama-index-readers-microsoft-sharepoint?from= Use the process_document_callback to filter which pages are processed. This example only processes pages that do not start with 'Draft'. ```python def page_filter(page_name: str) -> bool: # Only process pages that don't start with "Draft" return not page_name.startswith("Draft") loader = SharePointReader( client_id="", client_secret="", tenant_id="", sharepoint_site_name="", sharepoint_type=SharePointType.PAGE, process_document_callback=page_filter, ) ``` -------------------------------- ### Zendesk Support Configuration Schema Source: https://llamahub.ai/l/readers/llama-index-readers-airbyte-zendesk-support?from= Example structure for configuring the Airbyte Zendesk Support reader, including subdomain, start date, and credentials. ```json { "subdomain": "", "start_date": "", "credentials": { "credentials": "api_token", "email": "", "api_token": "", }, } ``` -------------------------------- ### Install from Source Source: https://llamahub.ai/l/agent/llama-index-agent-azure-foundry?from= Install the Azure Foundry Agent integration from local source code. ```bash cd llama_index/llama-index-integrations/agent/llama-index-agent-azure pip install -e . ``` -------------------------------- ### Hubspot Reader Configuration Schema Source: https://llamahub.ai/l/readers/llama-index-readers-airbyte-hubspot?from= Example JSON structure for configuring the Airbyte Hubspot reader, including start date and private app credentials. ```json { "start_date": "", "credentials": { "credentials_title": "Private App Credentials", "access_token": "", }, } ``` -------------------------------- ### Create FunctionAgent with Yahoo Finance Tool Source: https://llamahub.ai/l/memory/llama-index-memory-bedrock-agentcore?from= Create a FunctionAgent with a specific LLM and tools. This example uses YahooFinanceToolSpec and BedrockConverse. Install the finance tool package if needed. ```python %pip install llama-index-tools-yahoo-finance ``` ```python from llama_index.llms.bedrock_converse import BedrockConverse from llama_index.core.agent.workflow import FunctionAgent from llama_index.tools.yahoo_finance import YahooFinanceToolSpec llm = BedrockConverse(model="us.anthropic.claude-sonnet-4-20250514-v1:0") finance_tool_spec = YahooFinanceToolSpec() agent = FunctionAgent( tools=finance_tool_spec.to_tool_list(), llm=llm, ) ``` -------------------------------- ### Install llama-index-agent-openai Source: https://llamahub.ai/l/agent/llama-index-agent-openai?from= Install the necessary package using pip. ```bash pip install llama-index-agent-openai ``` -------------------------------- ### Initialize and Use TldwRetriever Source: https://llamahub.ai/l/retrievers/llama-index-retrievers-tldw?from= Initialize the TldwRetriever with your API key and collection ID, then create a query engine to search video content. ```python from llama_index.retrievers.tldw import TldwRetriever from llama_index.core.query_engine import RetrieverQueryEngine # Initialize the retriever retriever = TldwRetriever( api_key="YOUT_TLDW_API_KEY", collection_id="YOUR_COLLECTION_ID", # Replace with your actual collection ID ) # Create a query engine query_engine = RetrieverQueryEngine( retriever=retriever, ) # Query and summarize response response = query_engine.query("What are the brands of smart watches reviewed?") print( response ) # "The brands of smartwatches reviewed in the videos are Apple and Garmin." ``` -------------------------------- ### Install llama-index-llms-novita Source: https://llamahub.ai/l/llms/llama-index-llms-novita?from= Install the necessary package for NovitaAI integration. ```bash %pip install llama-index-llms-novita ``` -------------------------------- ### Basic GaudiLLM Usage Example Source: https://llamahub.ai/l/llms/llama-index-llms-gaudi?from= Demonstrates how to initialize and use the GaudiLLM for text completion. Ensure necessary imports and argument parsing are handled. ```python import argparse import os, logging from llama_index.llms.gaudi import GaudiLLM def setup_parser(parser): parser.add_argument(...) args = parser.parse_args() return args if __name__ == "__main__": parser = argparse.ArgumentParser( description="GaudiLLM Basic Usage Example" ) args = setup_parser(parser) args.model_name_or_path = "HuggingFaceH4/zephyr-7b-alpha" llm = GaudiLLM( args=args, logger=logger, model_name="HuggingFaceH4/zephyr-7b-alpha", tokenizer_name="HuggingFaceH4/zephyr-7b-alpha", query_wrapper_prompt=PromptTemplate( "<|system|> <|user|> {query_str} <|assistant|> " ), context_window=3900, max_new_tokens=256, generate_kwargs={"temperature": 0.7, "top_k": 50, "top_p": 0.95}, messages_to_prompt=messages_to_prompt, device_map="auto", ) query = "Is the ocean blue?" print("\n----------------- Complete ------------------") completion_response = llm.complete(query) print(completion_response.text) ``` -------------------------------- ### Install llama-index-node_parser-chonkie Source: https://llamahub.ai/l/node_parser/llama-index-node-parser-chonkie?from= Install the necessary package using pip. ```python pip install llama_index_node_parser-chonkie ``` -------------------------------- ### Paginate Confluence Data from a Space Source: https://llamahub.ai/l/readers/llama-index-readers-confluence?from= Fetch Confluence pages from a space with pagination. This example shows how to retrieve the first 5 pages and then the next 5 pages by adjusting the `start` and `max_num_results` parameters. ```python from llama_index.readers.confluence import ConfluenceReader token = {"access_token": "", "token_type": ""} oauth2_dict = {"client_id": "", "token": token} base_url = "https://yoursite.atlassian.com/wiki" space_key = "" reader = ConfluenceReader(base_url=base_url, oauth2=oauth2_dict) documents = reader.load_data( space_key=space_key, include_attachments=True, page_status="current", start=0, max_num_results=5, ) documents.extend( reader.load_data( space_key=space_key, include_children=True, include_attachments=True, start=5, max_num_results=5, ) ) ``` -------------------------------- ### Usage Example Source: https://llamahub.ai/l/readers/llama-index-readers-upstage?from= Demonstrates how to initialize the UpstageDocumentParseReader and load data from a file. ```APIDOC ```python import os os.environ["UPSTAGE_API_KEY"] = "YOUR_API_KEY" from llama_index.readers.upstage import UpstageDocumentParseReader file_path = "/PATH/TO/YOUR/FILE.pdf" reader = UpstageDocumentParseReader() docs = reader.load_data(file_path=file_path) for doc in docs[:3]: print(doc) ``` ``` -------------------------------- ### Quick Start: SignNow Agent with LlamaIndex Source: https://llamahub.ai/l/tools/llama-index-tools-signnow?from= Initialize the SignNowMCPToolSpec, discover tools from the MCP server, and wire them into a LlamaIndex agent. This example shows how to pass credentials via environment overrides and run a query. ```python import asyncio from llama_index.tools.signnow import SignNowMCPToolSpec from llama_index.core.agent.workflow import FunctionAgent from llama_index.llms.openai import ( OpenAI, ) # or any LLM that supports tool/function calling async def main(): # Option A: pass credentials directly (no .env needed) spec = SignNowMCPToolSpec.from_env( env_overrides={ # Option 1: token-based auth # "SIGNNOW_TOKEN": "your_signnow_token_here", # Option 2: credential-based auth "SIGNNOW_USER_EMAIL": "login@example.com", "SIGNNOW_PASSWORD": "password", "SIGNNOW_API_BASIC_TOKEN": "basic_token_base64", } ) # Fetch tools from the MCP server tools = await spec.to_tool_list_async() print({"count": len(tools), "names": [t.metadata.name for t in tools]}) # Wire them into a LlamaIndex agent agent = FunctionAgent( name="SignNow Agent", description="Query SignNow via MCP tools", tools=tools, llm=OpenAI(model="gpt-4o"), # make sure your LLM supports tools system_prompt="Be helpful.", ) # Ask for something useful resp = await agent.run("Show me the list of templates and their names.") print(resp) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Install OCI SDK Source: https://llamahub.ai/l/embeddings/llama-index-embeddings-oci-genai?from= Install the Oracle Cloud Infrastructure SDK for Python. ```bash pip install -U oci ``` -------------------------------- ### PandasAI Reader Usage Example Source: https://llamahub.ai/l/readers/llama-index-readers-pandas-ai?from= Demonstrates how to use the PandasAIReader with a sample DataFrame. You can either run a PandasAI query directly and get the parsed output by setting `is_conversational_answer=False`, or load the data into Document objects. ```python from pandasai.llm.openai import OpenAI import pandas as pd # Sample DataFrame df = pd.DataFrame( { "country": [ "United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China", ], "gdp": [ 21400000, 2940000, 2830000, 3870000, 2160000, 1350000, 1780000, 1320000, 516000, 14000000, ], "happiness_index": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0], } ) llm = OpenAI() from llama_index.readers.pandas_ai import PandasAIReader # use run_pandas_ai directly # set is_conversational_answer=False to get parsed output loader = PandasAIReader(llm=llm) response = reader.run_pandas_ai( df, "Which are the 5 happiest countries?", is_conversational_answer=False ) print(response) # load data with is_conversational_answer=False # will use our PandasCSVReader under the hood docs = reader.load_data( df, "Which are the 5 happiest countries?", is_conversational_answer=False ) # load data with is_conversational_answer=True # will use our PandasCSVReader under the hood docs = reader.load_data( df, "Which are the 5 happiest countries?", is_conversational_answer=True ) ``` -------------------------------- ### Initialize MyMagicAI Source: https://llamahub.ai/l/llms/llama-index-llms-mymagic?from= Create an instance of MyMagicAI, configuring API key, storage provider, bucket details, and session parameters. Optional parameters include role ARN, system prompt, region, and output handling. ```python from llama_index.llms.mymagic import MyMagicAI llm = MyMagicAI( api_key="your-api-key", storage_provider="s3", # Options: 's3' or 'gcs' bucket_name="your-bucket-name", session="your-session-name", # Directory for batch inference role_arn="your-role-arn", system_prompt="your-system-prompt", region="your-bucket-region", return_output=False, # Set to True to return output JSON input_json_file=None, # Input file stored on the bucket list_inputs=None, # List of inputs for small batch structured_output=None, # JSON schema of the output ) ``` -------------------------------- ### Example Usage of AgentCoreBrowserToolSpec Source: https://llamahub.ai/l/tools/llama-index-tools-aws-bedrock-agentcore?from= Demonstrates how to use the AgentCoreBrowserToolSpec to create a LlamaIndex agent that can browse websites, extract information, and perform tasks. Includes setup for LLM, agent, and task execution, followed by cleanup. ```python import asyncio from llama_index.llms.bedrock_converse import BedrockConverse from llama_index.tools.aws_bedrock_agentcore import AgentCoreBrowserToolSpec from llama_index.core.agent.workflow import FunctionAgent import nest_asyncio nest_asyncio.apply() # In case of existing loop (ex. in JupyterLab) async def main(): tool_spec = AgentCoreBrowserToolSpec(region="us-west-2") tools = tool_spec.to_tool_list() llm = BedrockConverse( model="us.anthropic.claude-sonnet-4-6-v1", region_name="us-west-2", ) agent = FunctionAgent( tools=tools, llm=llm, ) task = "Go to https://news.ycombinator.com/ and tell me the titles of the top 5 posts." response = await agent.run(task) print(str(response)) await tool_spec.cleanup() if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### End-to-end Example with AzureDocumentStore Source: https://llamahub.ai/l/storage/llama-index-storage-docstore-azure?from= Demonstrates loading documents, parsing them into nodes, and initializing a LlamaIndex StorageContext with AzureDocumentStore. The documents are then added to the store. ```python from llama_index.core import SimpleDirectoryReader, StorageContext from llama_index.core.node_parser import SentenceSplitter from llama_index.core import VectorStoreIndex, SimpleKeywordTableIndex reader = SimpleDirectoryReader("./data/paul_graham/") documents = reader.load_data() nodes = SentenceSplitter().get_nodes_from_documents(documents) storage_context = StorageContext.from_defaults( docstore=AzureDocumentStore.from_account_and_key( "your_account_name", "your_account_key", service_mode=ServiceMode.STORAGE, ), ) vector_index = VectorStoreIndex(nodes, storage_context=storage_context) storage_context.docstore.add_documents(nodes) ``` -------------------------------- ### ElevenLabs Voice Agent Basic Usage Source: https://llamahub.ai/l/voice_agents/llama-index-voice-agents-elevenlabs?from= A simple example demonstrating how to initialize and start an ElevenLabs voice agent for realtime conversation. Ensure your ElevenLabs API key and agent ID are set as environment variables. ```python import os from llama_index.voice_agents.elevenlabs import ElevenLabsVoiceAgent from dotenv import load_dotenv from elevenlabs.client import ElevenLabs load_dotenv() AGENT_ID = os.environ.get("AGENT_ID") API_KEY = os.environ.get("ELEVENLABS_API_KEY") def main(): client = ElevenLabs(api_key=API_KEY) conversation = ElevenLabsVoiceAgent( client, AGENT_ID, requires_auth=bool(API_KEY), ) conversation.start() while True: try: # GET MESSAGES IN llama-index ChatMessage FORMAT messages = conversation.export_messages() events = conversation.export_events() # GET AVERAGE LATENCY latency = conversation.average_latency except KeyboardInterrupt: conversation.interrupt() conversation.stop() print(f"Messages: {messages}") print(f"Events: {events}") print(f"Latency: {latency}") if __name__ == "__main__": main() ``` -------------------------------- ### Quick Start with Cloudflare AI Gateway Source: https://llamahub.ai/l/llms/llama-index-llms-cloudflare-ai-gateway?from= Initialize and use the CloudflareAIGateway LLM. Configure it with a list of fallback LLMs, your Cloudflare account details, and gateway name. This example demonstrates trying OpenAI first, then Anthropic. ```python from llama_index.llms.cloudflare_ai_gateway import CloudflareAIGateway from llama_index.llms.openai import OpenAI from llama_index.llms.anthropic import Anthropic from llama_index.core.llms import ChatMessage # Create LLM instances openai_llm = OpenAI(model="gpt-4o-mini", api_key="your-openai-key") anthropic_llm = Anthropic( model="claude-3-5-sonnet-20241022", api_key="your-anthropic-key" ) # Create Cloudflare AI Gateway LLM llm = CloudflareAIGateway( llms=[openai_llm, anthropic_llm], # Try OpenAI first, then Anthropic account_id="your-cloudflare-account-id", gateway="your-ai-gateway-name", api_key="your-cloudflare-api-key", ) # Use the LLM messages = [ChatMessage(role="user", content="What is 2+2?")] response = llm.chat(messages) print(response.message.content) ``` -------------------------------- ### Install Airbyte ZendeskSupport Reader Source: https://llamahub.ai/l/readers/llama-index-readers-airbyte-zendesk-support?from= Install the necessary package for the Airbyte ZendeskSupport Reader. ```bash pip install llama-index-readers-airbyte-zendesk-support ``` -------------------------------- ### Example Usage of AgentCoreCodeInterpreterToolSpec Source: https://llamahub.ai/l/tools/llama-index-tools-aws-bedrock-agentcore?from= Demonstrates using the AgentCoreCodeInterpreterToolSpec to create a LlamaIndex agent capable of executing code and shell commands. Includes setup for LLM, agent, and running both code and command-based tasks, followed by cleanup. ```python import asyncio from llama_index.llms.bedrock_converse import BedrockConverse from llama_index.tools.aws_bedrock_agentcore import ( AgentCoreCodeInterpreterToolSpec, ) from llama_index.core.agent.workflow import FunctionAgent import nest_asyncio nest_asyncio.apply() # In case of existing loop (ex. in JupyterLab) async def main(): tool_spec = AgentCoreCodeInterpreterToolSpec(region="us-west-2") tools = tool_spec.to_tool_list() llm = BedrockConverse( model="us.anthropic.claude-sonnet-4-6-v1", region_name="us-west-2", ) agent = FunctionAgent( tools=tools, llm=llm, ) code_task = "Write a Python function that calculates the factorial of a number and test it." code_response = await agent.run(code_task) print(str(code_response)) command_task = "Use terminal CLI commands to: 1) Show the environment's Python version. 2) Show me the list of Python package currently installed in the environment." command_response = await agent.run(command_task) print(str(command_response)) await tool_spec.cleanup() if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Start TextEmbed Server Source: https://llamahub.ai/l/embeddings/llama-index-embeddings-textembed?from= Launch the TextEmbed server, specifying the models to use, the number of worker processes, and an API key for authentication. ```bash python -m textembed.server --models sentence-transformers/all-MiniLM-L12-v2 --workers 4 --api-key TextEmbed ``` -------------------------------- ### Install llama-index-readers-make-com Source: https://llamahub.ai/l/readers/llama-index-readers-make-com?from= Install the Make-Com reader package using pip. ```python pip install llama-index-readers-make-com ``` -------------------------------- ### Custom Parsers and Callbacks Example Source: https://llamahub.ai/l/readers/llama-index-readers-service-now?from= This snippet shows the setup for using custom parsers with the SnowKBReader, including the necessary imports and the structure for defining parsers for different file types. It serves as a template for advanced configurations. ```python # Example with custom parsers and callbacks from llama_index.readers.service_now import SnowKBReader, FileType from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document from markitdown import MarkItDown from typing import List, Union import pathlib ``` -------------------------------- ### Basic Usage Source: https://llamahub.ai/l/llms/llama-index-llms-clarifai?from= Demonstrates how to set up Clarifai credentials and initialize the Clarifai LLM for text completion. ```APIDOC ## Basic Usage ### Description This section shows how to set your Clarifai Personal Access Token (PAT) as an environment variable and initialize the `Clarifai` LLM class for text completion. ### Installation ```bash %pip install llama-index-llms-clarifai !pip install llama-index !pip install clarifai ``` ### Initialization ```python import os os.environ["CLARIFAI_PAT"] = "" from llama_index.llms.clarifai import Clarifai # Method 1: using model_url parameter llm_model_url = Clarifai(model_url="https://clarifai.com/clarifai/ml/models/llama2-7b-alternative-4k") # Method 2: using model_name, app_id & user_id parameters llm_model_params = Clarifai( model_name="llama2-7b-alternative-4k", app_id="ml", user_id="clarifai", ) ``` ### Text Completion ```python # Call complete function llm_response = llm_model_url.complete( prompt="write a 10 line rhyming poem about science" ) print(llm_response) ``` ### Output Example ``` 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! ``` ``` -------------------------------- ### Ray Ingestion Pipeline Setup and Execution Source: https://llamahub.ai/l/ingestion/llama-index-ingestion-ray?from= Set up and run a LlamaIndex ingestion pipeline distributed across a Ray cluster. This example demonstrates wrapping transformations like SentenceSplitter, TitleExtractor, and OpenAIEmbedding with RayTransformComponent and configuring batch processing. ```python import ray from llama_index.core import Document from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.core.node_parser import SentenceSplitter from llama_index.core.extractors import TitleExtractor from llama_index.ingestion.ray import ( RayIngestionPipeline, RayTransformComponent, ) # Start a new cluster (or connect to an existing one, see https://docs.ray.io/en/latest/ray-core/configure.html) ray.init() # Create transformations transformations = [ RayTransformComponent(SentenceSplitter, chunk_size=25, chunk_overlap=0), RayTransformComponent( transform_class=TitleExtractor, map_batches_kwargs={ "batch_size": 10, # Define the batch size # "num_cpus": 4 # The number of CPUs to reserve for each parallel map worker. # "num_gpus": 1 # The number of GPUs to reserve for each parallel map worker. # See https://docs.ray.io/en/latest/data/api/doc/ray.data.Dataset.map_batches.html for all the available parameters }, ), RayTransformComponent( transform_class=OpenAIEmbedding, map_batches_kwargs={ "batch_size": 10, }, ), ] # Create the Ray ingestion pipeline pipeline = RayIngestionPipeline(transformations=transformations) # Run the pipeline with many documents nodes = pipeline.run(documents=[Document.example()] * 10) ``` -------------------------------- ### OpenAI Realtime Voice Agent Example Source: https://llamahub.ai/l/voice_agents/llama-index-voice-agents-openai?from= A full example demonstrating how to set up and run an OpenAI voice agent with custom tools and instructions. It includes signal handling for graceful shutdown and logging of messages and events. ```python import os import signal import time import logging from typing import List from dotenv import load_dotenv from llama_index.voice_agents.openai import OpenAIVoiceAgent from llama_index.core.voice_agents import BaseVoiceAgentEvent from llama_index.core.llms import ChatMessage, TextBlock from llama_index.core.tools import FunctionTool logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s" ) # Load environment variables from a .env file load_dotenv() quitFlag = False # use filter functions to export messages and events without your terminal being swamped by base64-encoded audio bytes :) def filter_events( events: List[BaseVoiceAgentEvent], ) -> List[BaseVoiceAgentEvent]: evs = [] for event in events: if "audio" in event.type_t and "transcript" not in event.type_t: if "delta" in event.type_t: event.delta = "" evs.append(event) else: evs.append(event) return evs def filter_messages(messages: List[ChatMessage]) -> List[ChatMessage]: msgs = [] for message in messages: msg = ChatMessage(role=message.role, blocks=[]) for b in message.blocks: if isinstance(b, TextBlock): msg.blocks.append(b) if len(msg.blocks) > 0: msgs.append(msg) return msgs def signal_handler(sig, frame): """Handle Ctrl+C and initiate graceful shutdown.""" global quitFlag quitFlag = True async def main(): # this is just a mock tool def get_weather(location: str): """ Get the weather for a location """ return f"The weather in {location} is sunny, there are 32°C, wind is 3 km/h toward west, and humidity is at 34%." api_key = os.getenv("OPENAI_API_KEY") if not api_key: return tools = [ FunctionTool.from_defaults( fn=get_weather, name="get_weather", description="Get the current weather...", ) ] # use custom models and tools! conversation = OpenAIVoiceAgent( api_key=api_key, tools=tools, model="gpt-4o-mini-realtime-preview-2024-12-17", ) signal.signal( signal.SIGINT, lambda sig, frame: signal_handler(sig, frame), ) try: # customize the model with instructions and other arguments you can find here: https://platform.openai.com/docs/api-reference/realtime-client-events/session/update await conversation.start( instructions="You are a very helpful assistant. You can use the 'get_weather' tool to get weather information about a location: you just have to input the location to the tool. Please execute the tool whenever you are asked about the weather of a location.", ) while not quitFlag: time.sleep(0.1) if quitFlag: await conversation.interrupt() await conversation.stop() except Exception as e: logging.error(f"Error in main loop: {e}") await conversation.interrupt() await conversation.stop() finally: logging.info("Exiting main.") print("Messages:") print(conversation.export_messages(filter=filter_messages)) print("Events:") print(conversation.export_events(filter=filter_events)) await conversation.stop() # Ensures cleanup if any error occurs if __name__ == "__main__": import asyncio asyncio.run(main()) # remember that YOU have to start the conversation! ``` -------------------------------- ### Install GraphQL Reader Source: https://llamahub.ai/l/readers/llama-index-readers-graphql?from= Install the necessary package for the GraphQL reader. ```bash pip install llama-index-readers-graphql ``` -------------------------------- ### Install pypreprocess library Source: https://llamahub.ai/l/readers/llama-index-readers-preprocess?from= Install the pypreprocess library, which is a requirement for the PreprocessReader. ```bash pip install pypreprocess ``` -------------------------------- ### Quick Start: NotDiamond Selector with RouterQueryEngine Source: https://llamahub.ai/l/selectors/llama-index-selectors-notdiamond?from= Set up API keys, create LlamaIndex indexes, define query engine tools, and initialize the NotDiamondSelector with a RouterQueryEngine. This example demonstrates how to use NotDiamond to route queries across different LLMs based on their configurations. ```python import os from typing import List from llama_index.core import ( SimpleDirectoryReader, VectorStoreIndex, SummaryIndex, Settings, ) from llama_index.core.query_engine import RouterQueryEngine from llama_index.core.tools import QueryEngineTool from llama_index.selectors.notdiamond.base import NotDiamondSelector from notdiamond import NotDiamond # Set up your API keys os.environ["OPENAI_API_KEY"] = "sk-..." os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..." os.environ["NOTDIAMOND_API_KEY"] = "sk-..." # Create indexes documents = SimpleDirectoryReader("data/paul_graham").load_data() nodes = Settings.node_parser.get_nodes_from_documents(documents) vector_index = VectorStoreIndex.from_documents(documents) summary_index = SummaryIndex.from_documents(documents) query_text = "What was Paul Graham's role at Yahoo?" # Set up Tools for the QueryEngine list_query_engine = summary_index.as_query_engine( response_mode="tree_summarize", use_async=True, ) vector_query_engine = vector_index.as_query_engine() list_tool = QueryEngineTool.from_defaults( query_engine=list_query_engine, description=( "Useful for summarization questions related to Paul Graham eassy on" " What I Worked On." ), ) vector_tool = QueryEngineTool.from_defaults( query_engine=vector_query_engine, description=( "Useful for retrieving specific context from Paul Graham essay on What" " I Worked On." ), ) # Create a NotDiamondSelector and RouterQueryEngine client = NotDiamond( api_key=os.environ["NOTDIAMOND_API_KEY"], llm_configs=["openai/gpt-4o", "anthropic/claude-3-5-sonnet-20240620"], ) preference_id = client.create_preference_id() client.preference_id = preference_id nd_selector = NotDiamondSelector(client=client) query_engine = RouterQueryEngine( selector=nd_selector, query_engine_tools=[ list_tool, vector_tool, ], ) # Use Not Diamond to Query Indexes response = query_engine.query( "Please summarize Paul Graham's working experience." ) print(str(response)) ``` -------------------------------- ### Initialize Alibaba Cloud MySQL Vector Store using from_params Source: https://llamahub.ai/l/vector_stores/llama-index-vector-stores-alibabacloud-mysql?from= Alternatively, initialize the AlibabaCloudMySQLVectorStore using the from_params class method with your database credentials. ```python from llama_index.vector_stores.alibabacloud_mysql import \ AlibabaCloudMySQLVectorStore vector_store = AlibabaCloudMySQLVectorStore.from_params( host="your-instance-endpoint.mysql.rds.aliyuncs.com", port=3306, user="llamaindex", password="password", database="vectordb", ) ``` -------------------------------- ### Initialize DeepInfraLLM Source: https://llamahub.ai/l/llms/llama-index-llms-deepinfra?from= Set up the DeepInfraLLM class with your API key and desired parameters. Replace 'your-deepinfra-api-key' with your actual key. ```python from llama_index.llms.deepinfra import DeepInfraLLM import asyncio llm = DeepInfraLLM( model="mistralai/Mixtral-8x22B-Instruct-v0.1", # Default model name api_key="your-deepinfra-api-key", # Replace with your DeepInfra API key temperature=0.5, max_tokens=50, additional_kwargs={"top_p": 0.9}, ) ``` -------------------------------- ### Install Rust Source: https://llamahub.ai/l/llms/llama-index-llms-mistral-rs?from= Install Rust using the official script. This is a prerequisite for installing mistralrs. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source $HOME/.cargo/env ``` -------------------------------- ### Install llama-index-llms-friendli Source: https://llamahub.ai/l/llms/llama-index-llms-friendli?from= Install the necessary Python packages for the Friendli integration. Ensure llama-index is also installed. ```python %pip install llama-index-llms-friendli !pip install llama-index ``` -------------------------------- ### Example using a Private Key Source: https://llamahub.ai/l/llms/llama-index-llms-cortex?from= Authenticate with Cortex using a private key file. Ensure environment variables for user, account, and the private key file path are set. ```python import os from llama_index.llms.cortex import Cortex llm = Cortex( model="llama3.2-1b", user=os.environ["YOUR_SF_USER"], account=os.environ["YOUR_SF_ACCOUNT"], private_key_file=os.environ["PATH_TO_SF_PRIVATE_KEY"], ) completion_response = llm.complete( "write me a haiku about a snowflake", temperature=0.0 ) print(completion_response) ``` -------------------------------- ### Initialize and Use VectorDB Tool with Agent Source: https://llamahub.ai/l/tools/llama-index-tools-vector-db?from= Demonstrates how to initialize a VectorDB tool with a VectorStoreIndex and integrate it into a FunctionAgent. The tool is configured with metadata for structured queries and then used by the agent to retrieve information. ```python from llama_index.tools.vector_db import VectorDB from llama_index.core.agent.workflow import FunctionAgent from llama_index.llms.openai import OpenAI from llama_index.core.vector_stores import VectorStoreInfo from llama_index.core import VectorStoreIndex index = VectorStoreIndex(nodes=nodes) tool_spec = VectorDB(index=index) vector_store_info = VectorStoreInfo( content_info="brief biography of celebrities", metadata_info=[ MetadataInfo( name="category", type="str", description="Category of the celebrity, one of [Sports, Entertainment, Business, Music]", ), MetadataInfo( name="country", type="str", description="Country of the celebrity, one of [United States, Barbados, Portugal]", ), ], ) agent = FunctionAgent( tools=tool_spec.to_tool_list( func_to_metadata_mapping={ "auto_retrieve_fn": ToolMetadata( name="celebrity_bios", description=f""" Use this tool to look up biographical information about celebrities. The vector database schema is given below: {vector_store_info.json()} {tool_spec.auto_retrieve_fn.__doc__} """, fn_schema=create_schema_from_function( "celebrity_bios", tool_spec.auto_retrieve_fn ), ) } ), llm=OpenAI(model="gpt-4.1"), ) print( await agent.run("Tell me about two celebrities from the United States. ") ) ``` -------------------------------- ### Install llama-index-embeddings-ollama Source: https://llamahub.ai/l/embeddings/llama-index-embeddings-ollama?from= Install the necessary package using pip. Ensure Ollama is also installed and running. ```bash pip install llama-index-embeddings-ollama ``` -------------------------------- ### Basic Usage Example Source: https://llamahub.ai/l/readers/llama-index-readers-genius?from= An example demonstrating how to initialize the GeniusReader and search for songs by lyrics. ```python from llama_index.readers.genius import GeniusReader access_token = "your_generated_access_token" loader = GeniusReader(access_token) documents = loader.search_songs_by_lyrics("Imagine") ``` -------------------------------- ### Install llama-index-tools-jina Source: https://llamahub.ai/l/tools/llama-index-tools-jina?from= Install the Jina search tool for LlamaIndex using pip. Ensure Python is installed on your system. ```bash pip install llama-index-tools-jina ```