指南CookbookIntegration Llama Index Callback

Cookbook:LlamaIndex 集成

这是一个简单的 cookbook,演示如何使用 LlamaIndex 与 Litefuse 的集成。例子里只用了非常简单的 Index 和 Query。

有反馈? 欢迎在 Discord 或 GitHub 告诉我们。这是个新集成,我们很想听到你的想法。

准备工作

确保你同时安装了 llama-indexlangfuse

%pip install llama-index "langfuse<3.0.0" --upgrade

初始化集成。在 Litefuse 项目设置 中获取你的 API key。

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://litefuse.cloud"
)
Settings.callback_manager = CallbackManager([langfuse_callback_handler])

本示例使用 OpenAI 做 embedding 和 chat completion。

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)

在 Litefuse 中查看 trace

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

搞定!✨ 你可以在自己的 Litefuse 项目里看到 index 和 query 的 trace。

示例 trace(公开链接):

  1. Query
  2. Query (chat)
  3. Session

Litefuse 中的 trace:

Litefuse Traces

想了解更多高级功能?

完整的集成文档 介绍了更多高级特性以及使用方法:

  • 与 Langfuse Python SDK 及其他集成的互操作性
  • 为 trace 添加自定义元数据和属性
这个页面对你有帮助吗?