在 RAG 中使用 Langfuse 追蹤查詢
這是一個簡單的烹飪手冊,示範如何在 RAG 中使用 Langfuse 來追蹤您的查詢。RAG 管道是使用 LlamaIndex 和 Milvus Lite 來儲存和擷取文件。
在本快速入門中,我們將教您如何使用 Milvus Lite 作為向量儲存,建立 LlamaIndex 應用程式。我們也會告訴您如何使用 Langfuse LlamaIndex 整合來追蹤您的應用程式。
Langfuse是一個開放原始碼的 LLM 工程平台,可協助團隊協同除錯、分析和迭代他們的 LLM 應用程式。所有平台功能都是原生整合,以加速開發工作流程。
Milvus Lite是 Milvus 的輕量級版本,Milvus是一個開放原始碼向量資料庫,以向量嵌入和相似性搜尋為 AI 應用程式提供動力。
設定
確定您已安裝llama-index
和langfuse
。
$ pip install llama-index langfuse llama-index-vector-stores-milvus --upgrade
初始化整合。從Langfuse 專案設定取得您的 API 金鑰,並將 public_key secret_key 替換為您的金鑰值。本範例使用 OpenAI 進行嵌入和聊天完成,因此您也需要在環境變數中指定 OpenAI 金鑰。
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"] = ""
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from langfuse.llama_index import LlamaIndexCallbackHandler
langfuse_callback_handler = LlamaIndexCallbackHandler()
Settings.callback_manager = CallbackManager([langfuse_callback_handler])
使用 Milvus Lite 建立索引
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
from llama_index.core import StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore
vector_store = MilvusVectorStore(
uri="tmp/milvus_demo.db", dim=1536, overwrite=False
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
[doc1,doc2], storage_context=storage_context
)
查詢
# 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)
在 Langfuse 中探索痕跡
# As we want to immediately see result in Langfuse, we need to flush the callback handler
langfuse_callback_handler.flush()
完成!您可以在 Langfuse 專案中看到索引和查詢的軌跡。
示例軌跡(公共鏈接):
Langfuse 中的軌跡:
Langfuse 軌跡
對更多進階功能感興趣?
請參閱完整的整合說明文件,瞭解更多進階功能及使用方式:
- 與 Langfuse Python SDK 及其他整合的互操作性
- 新增自訂元資料與屬性至追蹤資料