agentops/examples/llamaindex/llamaindex_example.ipynb

205 lines
6.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "a886f0ad",
"metadata": {},
"source": [
"# LlamaIndex AgentOps Integration Example\n",
"\n",
"This notebook demonstrates how to use AgentOps with LlamaIndex for observability and monitoring of your context-augmented generative AI applications.\n",
"\n",
"## Setup\n",
"\n",
"First, install the required packages:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "002718a0",
"metadata": {},
"outputs": [],
"source": [
"# Install required packages\n",
"!pip install agentops llama-index-instrumentation-agentops llama-index-embeddings-huggingface llama-index-llms-huggingface python-dotenv"
]
},
{
"cell_type": "markdown",
"id": "bc009657",
"metadata": {},
"source": [
"## Initialize AgentOps Handler\n",
"\n",
"Set up the AgentOps handler for LlamaIndex instrumentation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1d8a0c2",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from llama_index.core import VectorStoreIndex, Document, Settings\n",
"from llama_index.instrumentation.agentops import AgentOpsHandler\n",
"\n",
"# Initialize AgentOps handler\n",
"handler = AgentOpsHandler()\n",
"handler.init()\n",
"\n",
"# Load environment variables\n",
"load_dotenv()\n",
"\n",
"# Set API keys (replace with your actual keys)\n",
"os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_agentops_api_key_here\")\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")"
]
},
{
"cell_type": "markdown",
"id": "8794d1f2",
"metadata": {},
"source": [
"## Configure Local Models (Optional)\n",
"\n",
"For this example, we'll use local HuggingFace models to avoid requiring external API keys:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35804326",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
"from llama_index.llms.huggingface import HuggingFaceLLM\n",
"\n",
"# Configure local embeddings and LLM\n",
"Settings.embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")\n",
"Settings.llm = HuggingFaceLLM(model_name=\"microsoft/DialoGPT-medium\")\n",
"print(\"Using local HuggingFace embeddings and LLM\")"
]
},
{
"cell_type": "markdown",
"id": "b23271d3",
"metadata": {},
"source": [
"## Create Sample Documents and Index\n",
"\n",
"Create some sample documents and build a vector index:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80b1c26a",
"metadata": {},
"outputs": [],
"source": [
"print(\"🚀 Starting LlamaIndex AgentOps Integration Example\")\n",
"print(\"=\" * 50)\n",
"\n",
"# Create sample documents\n",
"documents = [\n",
" Document(text=\"LlamaIndex is a framework for building context-augmented generative AI applications with LLMs.\"),\n",
" Document(\n",
" text=\"AgentOps provides observability into your AI applications, tracking LLM calls, performance metrics, and more.\"\n",
" ),\n",
" Document(\n",
" text=\"The integration between LlamaIndex and AgentOps allows you to monitor your RAG applications seamlessly.\"\n",
" ),\n",
" Document(\n",
" text=\"Vector databases are used to store and retrieve embeddings for similarity search in RAG applications.\"\n",
" ),\n",
" Document(\n",
" text=\"Context-augmented generation combines retrieval and generation to provide more accurate and relevant responses.\"\n",
" ),\n",
"]\n",
"\n",
"print(\"📚 Creating vector index from sample documents...\")\n",
"index = VectorStoreIndex.from_documents(documents)\n",
"print(\"✅ Vector index created successfully\")"
]
},
{
"cell_type": "markdown",
"id": "e07f1d6a",
"metadata": {},
"source": [
"## Perform Queries\n",
"\n",
"Now let's perform some queries to demonstrate the AgentOps integration:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5882dd2a",
"metadata": {},
"outputs": [],
"source": [
"# Create query engine\n",
"query_engine = index.as_query_engine()\n",
"\n",
"print(\"🔍 Performing queries...\")\n",
"\n",
"# Sample queries\n",
"queries = [\n",
" \"What is LlamaIndex?\",\n",
" \"How does AgentOps help with AI applications?\",\n",
" \"What are the benefits of using vector databases in RAG?\",\n",
"]\n",
"\n",
"for i, query in enumerate(queries, 1):\n",
" print(f\"\\n📝 Query {i}: {query}\")\n",
" response = query_engine.query(query)\n",
" print(f\"💬 Response: {response}\")"
]
},
{
"cell_type": "markdown",
"id": "b46b944c",
"metadata": {},
"source": [
"## Results\n",
"\n",
"After running this notebook, you should see:\n",
"\n",
"1. **AgentOps Session Link**: A URL to view the session in your AgentOps dashboard\n",
"2. **Cost Tracking**: Information about the cost of LLM calls (if using paid APIs)\n",
"3. **Operation Tracking**: All LlamaIndex operations are automatically tracked\n",
"\n",
"Check your AgentOps dashboard to see detailed information about:\n",
"- LLM calls and responses\n",
"- Performance metrics\n",
"- Cost analysis\n",
"- Session replay\n",
"\n",
"The session link will be printed in the output above by AgentOps."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c141973e",
"metadata": {},
"outputs": [],
"source": [
"print(\"\\n\" + \"=\" * 50)\n",
"print(\"🎉 Example completed successfully!\")\n",
"print(\"📊 Check your AgentOps dashboard to see the recorded session with LLM calls and operations.\")\n",
"print(\"🔗 The session link should be printed above by AgentOps.\")"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}