Cookbook:LlamaIndex 集成(instrumentation 模块)
这是一个简单的 cookbook,演示如何通过 LlamaIndex 提供的 instrumentation 模块(自 llama-index v0.10.20 起可用)使用 LlamaIndex 与 Litefuse 的集成。
注意:此集成处于 beta 阶段。 如遇到问题或有反馈,请在 GitHub 上提交。
准备工作
确保你同时安装了 llama-index 和 langfuse。
%pip install "langfuse<3.0.0" llama_index --upgrade初始化集成。在 Litefuse 项目设置中获取你的 API key。本示例使用 OpenAI 做 embedding 和 chat completion,你也可以换成任何 LlamaIndex 支持的模型。
import os
# Get keys for your project from the project settings page
# https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your openai key
os.environ["OPENAI_API_KEY"] = ""注册 Litefuse 的 LlamaIndexInstrumentor:
from langfuse.llama_index import LlamaIndexInstrumentor
instrumentor = LlamaIndexInstrumentor()
instrumentor.start()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)He made home movies using a Super 8 camera.示例 trace:https://litefuse.cloud/project/cloramnkj0002jz088vzn1ja4/traces/d933c7cc-20bf-4db3-810d-bab1c8d9a2a1
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)He made home movies using a Super 8 camera growing up.示例 trace:https://litefuse.cloud/project/cloramnkj0002jz088vzn1ja4/traces/4e285b8f-9789-4cf0-a8b4-45473ac420f1

自定义 trace 属性
你可以使用 instrumentor.observe 这个上下文管理器来管理 trace ID、设置自定义 trace 属性,以及拿到 trace 客户端供后续打分使用。
with instrumentor.observe(user_id='my-user', session_id='my-session') as trace:
response = index.as_query_engine().query("What did he do growing up?")
# Use the trace client yielded by the context manager for e.g. scoring:
trace.score(name="my-score", value=0.5)示例 trace:https://litefuse.cloud/project/cloramnkj0002jz088vzn1ja4/traces/6f554d6b-a2bc-4fba-904f-aa54de2897ca
这个页面对你有帮助吗?