Guides
Cookbooks
Integration Llama Index
This is a Jupyter notebook

Cookbook LlamaIndex Integration

This is a simple cookbook that demonstrates how to use the LlamaIndex Langfuse integration (opens in a new tab). It uses a very simple Index and Query. We've recorded a short video (opens in a new tab) to show you how to use this cookbook.

Any feedback? Let us know on Discord or GitHub. This is a new integration, and we'd love to hear your thoughts.

Setup

Make sure you have both llama-index and langfuse installed.

%pip install llama-index langfuse --upgrade

Initialize the integration. Get your API keys from the Langfuse project settings (opens in a new tab).

from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from langfuse.llama_index import LlamaIndexCallbackHandler
 
langfuse_callback_handler = LlamaIndexCallbackHandler(
    public_key="pk-lf-...",
    secret_key="sk-lf-...",
    host="https://cloud.langfuse.com"
)
Settings.callback_manager = CallbackManager([langfuse_callback_handler])

This example uses OpenAI for embeddings and chat completions.

import os
 
os.environ["OPENAI_API_KEY"] = ""

Index

# Example context, thx ChatGPT
from llama_index.core import Document
 
doc1 = Document(text="""
Maxwell "Max" Silverstein, a lauded movie director, screenwriter, and producer, was born on October 25, 1978, in Boston, Massachusetts. A film enthusiast from a young age, his journey began with home movies shot on a Super 8 camera. His passion led him to the University of Southern California (USC), majoring in Film Production. Eventually, he started his career as an assistant director at Paramount Pictures. Silverstein's directorial debut, “Doors Unseen,” a psychological thriller, earned him recognition at the Sundance Film Festival and marked the beginning of a successful directing career.
""")
doc2 = Document(text="""
Throughout his career, Silverstein has been celebrated for his diverse range of filmography and unique narrative technique. He masterfully blends suspense, human emotion, and subtle humor in his storylines. Among his notable works are "Fleeting Echoes," "Halcyon Dusk," and the Academy Award-winning sci-fi epic, "Event Horizon's Brink." His contribution to cinema revolves around examining human nature, the complexity of relationships, and probing reality and perception. Off-camera, he is a dedicated philanthropist living in Los Angeles with his wife and two children.
""")
# Example index construction + LLM query
from llama_index.core import VectorStoreIndex
 
index = VectorStoreIndex.from_documents([doc1,doc2])

Query

# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)

Explore traces in Langfuse

# As we want to immediately see result in Langfuse, we need to flush the callback handler
langfuse_callback_handler.flush()

Done! ✨ You see traces of your index and query in your Langfuse project.

Example traces (public links):

  1. Query (opens in a new tab)
  2. Query (chat) (opens in a new tab)
  3. Session (opens in a new tab)

Trace in Langfuse:

Langfuse Traces

Interested in more advanced features?

See the full integration docs (opens in a new tab) to learn more about advanced features and how to use them:

  • Interoperability with Langfuse Python SDK and other integrations
  • Add custom metadata and attributes to the traces

Was this page useful?

Questions? We're here to help

Subscribe to updates