Cookbook LlamaIndex 集成 (Instrumentation 模块)¶
这是一个简单的指南,演示如何使用 LlamaIndex Langfuse 集成,该集成使用了 LlamaIndex 的Instrumentation 模块(在 llama-index v0.10.20 及更高版本中可用)。
设置¶
确保您已安装 `llama-index` 和 `langfuse`。
In [ ]
已复制!
%pip install langfuse llama_index --upgrade
%pip install langfuse llama_index --upgrade
初始化集成。从 Langfuse 项目设置中获取您的 API 密钥。本例使用 OpenAI 进行嵌入和聊天补全。您也可以使用 LlamaIndex 支持的任何其他模型。
In [ ]
已复制!
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 [ ]
已复制!
from langfuse.llama_index import LlamaIndexInstrumentor
instrumentor = LlamaIndexInstrumentor()
instrumentor.start()
from langfuse.llama_index import LlamaIndexInstrumentor instrumentor = LlamaIndexInstrumentor() instrumentor.start()
索引¶
In [ ]
已复制!
# 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.
"""
)
# 示例文本,感谢 ChatGPT from llama_index.core import Document doc1 = Document( text=""" 麦克斯韦·“马克斯”·西尔弗斯坦(Maxwell "Max" Silverstein),一位备受赞誉的电影导演、编剧和制片人,于1978年10月25日出生于马萨诸塞州波士顿。他从小就是个电影爱好者,电影之旅始于用超8毫米摄影机拍摄家庭电影。他的热情驱使他进入南加州大学(USC),主修电影制作。最终,他在派拉蒙影业公司开始了助理导演的职业生涯。西尔弗斯坦的导演处女作《看不见的门》(Doors Unseen),一部心理惊悚片,为他在圣丹斯电影节赢得了认可,并标志着他成功导演生涯的开端。 """ ) doc2 = Document( text=""" 在其整个职业生涯中,西尔弗斯坦因其多样化的作品和独特的叙事手法而备受赞誉。他在故事情节中巧妙地融合了悬念、人类情感和微妙的幽默。他的知名作品包括《转瞬即逝的回响》(Fleeting Echoes)、《平静的黄昏》(Halcyon Dusk)以及奥斯卡获奖科幻史诗片《事件视界的边缘》(Event Horizon's Brink)。他对电影的贡献在于审视人性、关系的复杂性,以及探索现实与感知。在镜头之外,他是一位热心的慈善家,与妻子和两个孩子住在洛杉矶。 """ )
In [ ]
已复制!
# Example index construction + LLM query
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents([doc1, doc2])
# 示例索引构建 + LLM 查询 from llama_index.core import VectorStoreIndex index = VectorStoreIndex.from_documents([doc1, doc2])
查询¶
In [ ]
已复制!
# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
# 查询 response = index.as_query_engine().query("What did he do growing up?") print(response)
He made home movies using a Super 8 camera.
In [ ]
已复制!
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)
# 聊天 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 [ ]
已复制!
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)