Intercept Langfuse traces before they are sent to LangFuse #12572
-
Hi, I have a callback handler that uses Langfuse for my RAG application in Streamlit. The following code is an excerpt.
This helps me record traces while I use LlamaIndex's chat engine. However, I want to add information to certain traces before they are sent to Langfuse. How can I intercept when the event recording starts and when it ends so that I can find the Do I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @anirbanbasu! Great to see you diving into another adventure with LlamaIndex. How's the coding journey been treating you lately? Sure, you can use the First, you need to create a custom callback handler by subclassing from llama_index.core.callbacks import CallbackManager
from langfuse.llama_index import LlamaIndexCallbackHandler
class CustomLangfuseCallbackHandler(LlamaIndexCallbackHandler):
def on_event_start(self, event):
# Custom logic before an event starts
# For example, modify event's trace_id or add additional information
event['trace_id'] = 'custom_trace_id' # Modify this according to your needs
super().on_event_start(event)
def on_event_end(self, event):
# Custom logic after an event ends
# For example, log additional information or modify the event
print(f"Event ended with trace_id: {event['trace_id']}")
super().on_event_end(event) After creating your custom callback handler, you can use it in your Streamlit application like this: # Instantiate your custom callback handler
custom_langfuse_callback_handler = CustomLangfuseCallbackHandler()
# Use with CallbackManager
from llama_index.core import Settings
Settings.callback_manager = CallbackManager([custom_langfuse_callback_handler]) This setup will allow you to intercept the event recording process and add additional information to the traces before they are sent to Langfuse. For more information, you can refer to the LangfuseCallbackHandler example in the LlamaIndex repository. Regarding the structure of the Lastly, the I hope this helps! Let me know if you have any other questions. Sources
|
Beta Was this translation helpful? Give feedback.
Hey @anirbanbasu! Great to see you diving into another adventure with LlamaIndex. How's the coding journey been treating you lately?
Sure, you can use the
on_event_start
andon_event_end
methods to intercept the event recording process and add additional information to the traces. Here's how you can achieve this:First, you need to create a custom callback handler by subclassing
LlamaIndexCallbackHandler
. In this custom handler, you can override theon_event_start
andon_event_end
methods to add your custom logic. Here's an example: