LlamaIndex 集成指南(Instrumentation 模块)¶
本指南简明演示如何通过 LlamaIndex 的 instrumentation 模块(适用于 llama-index v0.10.20 及更高版本)使用 LlamaIndex Langfuse 集成功能。
安装配置¶
请确保已安装 llama-index
和 langfuse
这两个依赖包。
In [ ]:
Copied!
%pip install langfuse llama_index --upgrade
%pip install langfuse llama_index --upgrade
初始化集成。从 Langfuse 项目设置中获取您的 API 密钥。此示例使用 OpenAI 进行嵌入和聊天补全操作,您也可以使用 LlamaIndex 支持的任何其他模型。
In [ ]:
Copied!
import os
# Get keys for your project from the project settings page: https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region
# Your openai key
os.environ["OPENAI_API_KEY"] = ""
import os
# Get keys for your project from the project settings page: https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region
# Your openai key
os.environ["OPENAI_API_KEY"] = ""
注册 Langfuse 的 LlamaIndexInstrumentor
:
In [ ]:
Copied!
from langfuse.llama_index import LlamaIndexInstrumentor
instrumentor = LlamaIndexInstrumentor()
instrumentor.start()
from langfuse.llama_index import LlamaIndexInstrumentor
instrumentor = LlamaIndexInstrumentor()
instrumentor.start()
目录¶
In [ ]:
Copied!
# 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 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.
"""
)
In [ ]:
Copied!
# Example index construction + LLM query
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents([doc1, doc2])
# Example index construction + LLM query
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents([doc1, doc2])
查询¶
In [ ]:
Copied!
# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
He made home movies using a Super 8 camera.
In [ ]:
Copied!
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)
# 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.
自定义追踪属性¶
你可以使用 instrumentor.observe
上下文管理器来管理追踪ID、设置自定义追踪属性,并访问追踪客户端以便后续评分。
In [ ]:
Copied!
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)
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)