iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
生成式 AI

阿,又是一個RAG系列 第 4

Day3 Observability: 把 LlamaIndex 的呼叫接到 Langfuse 上

  • 分享至 

  • xImage
  •  

Situation:

今天是一個額外的任務,
主要是有時候想直接套一些 LlamaIndex 包好的範例,但是沒看 source code 的情況下實在不知道它裡面到底做了什麼,
有時候甚至就連他裡面到底呼叫了幾次 llm 我都不知道
這時候我們用 Langfuse 直上幾行 code 就可以看到精美的過程,連每步的 input/output 都有,不香嗎!

Task:

這個部分會分成兩篇,
一篇就是這篇:Day3 Observability: 把 LlamaIndex 的呼叫接到 Langfuse 上
我們先跑通讓每次 LlamaIndex 的呼叫都會幫我們上到 Langfuse 上
明天預計是:Day4 Instrumentation: 更好的看懂 Langfuse 上的 Tracing
理解一下LlamaIndex event 還有 span 的概念

Action:

env

pip install llama-index 
pip install langfuse 
pip install openinference-instrumentation-llama-index
  • 首先當然是裝 llama-index 還有 langfuse
  • openinference-instrumentation-llama-index 這個主要是可以自動攔截 LlamaIndex 的 event / span,並以 OpenTelemetry 格式送出去

setup

  • 先直接到 Langfuse 註冊並且拿到LANGFUSE_PUBLIC_KEY、LANGFUSE_SECRET_KEY,然後把他們放到專案下的.env檔案裡
  • 專案路徑下的.env檔案預計會有:
OPENAI_API_KEY=xxxxx
LANGFUSE_PUBLIC_KEY=xxxxx
LANGFUSE_SECRET_KEY=xxxxx
LANGFUSE_HOST=https://cloud.langfuse.com
  • Langfuse其實可以本地佈署,這個就跟 ollama 一樣,我們後面有空再說

code

  1. 先把.env load 進來,然後import langfuse
from dotenv import find_dotenv, load_dotenv
from langfuse import get_client

_ = load_dotenv(find_dotenv())
langfuse = get_client()

# Verify connection
if langfuse.auth_check():
    print("Langfuse client is authenticated and ready!")
else:
    print("Authentication failed. Please check your credentials and host.")
  1. 這個官方文檔是這麼說的:

Now, we initialize the OpenInference LlamaIndex instrumentation. This third-party instrumentation automatically captures LlamaIndex operations and exports OpenTelemetry (OTel) spans to Langfuse.

總之加了這行才會把 LlamaIndex 的 span 送到Langfuse

from openinference.instrumentation.llama_index import LlamaIndexInstrumentor

# Initialize LlamaIndex instrumentation
LlamaIndexInstrumentor().instrument()
  1. 來秒做一個最小包含 embedding 跟 llm 呼叫的範例: query_engine
from llama_index.core import Document
from llama_index.core import VectorStoreIndex

index = VectorStoreIndex.from_documents([Document.example()])

query_engine = index.as_query_engine()

# call
# query_engine.query("Hello LangFuse!")
  • 3.1: 從Document.example()拿到一個範例的document
  • 3.2: 用VectorStoreIndex.from_documents做成index
    • 關於 Llama-index 的 index 說明
  1. call it!
query_engine.query("Hello LangFuse!")

回傳什麼的我們就先不管,到 Langfuse上再看

  1. 點開LangFuse -> 左側選 Tracing -> 找到最近的一筆 -> 點擊放大
    https://ithelp.ithome.com.tw/upload/images/20250918/20177855cZDxTNDQ05.jpg

  2. 先看左下角的Graph,可以看到都呼叫了什麼,然後就可以把它關掉了

https://ithelp.ithome.com.tw/upload/images/20250918/20177855NIf8VP7mKc.jpg

  1. 再來是左上角的TraceTree,這邊可以看到每個步驟花的時間,主要就是Retrieve跟synthesize

https://ithelp.ithome.com.tw/upload/images/20250918/201778556oIYW5ygJ9.jpg

  1. 點擊最後一個 OpenAI.chat -> 然後檢查右側的面板

https://ithelp.ithome.com.tw/upload/images/20250918/20177855uair3WPy4x.jpg

這邊就可以看到:

  • 首先他有input/output只是太長了output沒有截出來
  • messages裡面有2個items
    • 一個是system prompt
    • 第二個的role是USER,text是已經把context 放進去的prompt
  1. user_id, session_id, span
user_id = "woodchuck_chuck"
sess_id = "hello_langfuse_v2"

with langfuse.start_as_current_span(name="hello_langfuse_v2") as span:
    span.update_trace(user_id=user_id, session_id=sess_id, tags=["tag_v3"])
    query_engine.query("Hello LangFuse!")

可以在call的時候:
- 用with包起來,這樣with下的就會被當成是同筆呼叫
- 可以給user_id,後面就可以在其他面板根據user查找
- 可以給session_id,同上

  1. 可以把資料取回來
  • 官方有 Python SDK 不過UI就可以直接下載了,所以這邊就沒試 等到需要再說
  • 他整個紀錄分成Traces跟Observations,Traces裡面沒有每一步的input跟output,要到Observations才有

Summary:

  • 我們今天主要是把 Llama-index的呼叫接到 Langfuse上,然後探索了一下 Langfuse
  • 明天來詳細看看他的 TraceTree 都是些什麼內容

其他:

  • ==舊的== Llama-index 文檔會說: These partner integrations use our legacy CallbackManager or third-party calls.
  • 不過現在的已經是說:Observability is now being handled via the instrumentation module (available in v0.10.20 and later.)
  • 今天壓線發文好緊張喔喔喔

Reference:


上一篇
Day2-RagDatasetGenerator
下一篇
Day4: Langfuse 上的 Tracing 是怎麼來的,以及怎麼從 Langfuse 拿資料
系列文
阿,又是一個RAG8
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言