55from constants import SECRET_NAME
66from utils .llm_utils import process_input_with_retrieval
77from zenml .client import Client
8+ from datetime import datetime , timedelta , UTC
9+ from langfuse import Langfuse
10+ import time
11+ from rich import print
12+
13+ langfuse = Langfuse ()
814
9- # Set up logging
1015logging .basicConfig (level = logging .INFO )
1116logger = logging .getLogger (__name__ )
1217
2429 raise RuntimeError (f"Application startup failed: { e } " )
2530
2631
27- # def predict(message, history):
28- # try:
29- # return process_input_with_retrieval(
30- # input=message,
31- # n_items_retrieved=20,
32- # use_reranking=True,
33- # tracing_tags=["gradio", "web-interface", APP_ENVIRONMENT],
34- # )
35- # except Exception as e:
36- # logger.error(f"Error processing message: {e}")
37- # return f"Sorry, I encountered an error: {str(e)}"
32+ def get_langfuse_trace_id () -> str | None :
33+ """Get the trace from Langfuse.
3834
35+ This is a very naive implementation. It simply returns the id of the first trace
36+ in the last 60 seconds. Will retry up to 3 times if no traces are found or if
37+ there's an error.
3938
40- # # Launch the Gradio interface
41- # interface = gr.ChatInterface(
42- # predict,
43- # title="ZenML Documentation Assistant",
44- # description="Ask me anything about ZenML!",
45- # )
39+ Returns:
40+ str | None: The trace ID if found, None otherwise
41+ """
42+ logger .info ("Getting trace from Langfuse" )
43+ retries = 0
44+ max_retries = 3
45+ while retries < max_retries :
46+ try :
47+ # Wait 5 seconds before making the API call
48+ time .sleep (5 )
49+ traces = langfuse .fetch_traces (
50+ limit = 1 , order_by = "timestamp.desc"
51+ ).data
52+ if not traces :
53+ retries += 1
54+ if retries == max_retries :
55+ logger .error (
56+ f"No traces found after { max_retries } attempts"
57+ )
58+ return None
59+ logger .warning (
60+ f"No traces found (attempt { retries } /{ max_retries } )"
61+ )
62+ time .sleep (10 )
63+ continue
64+ return traces [0 ].id
65+ except Exception as e :
66+ retries += 1
67+ if retries == max_retries :
68+ logger .error (
69+ f"Error fetching traces after { max_retries } attempts: { e } "
70+ )
71+ return None
72+ logger .warning (
73+ f"Error fetching traces (attempt { retries } /{ max_retries } ): { e } "
74+ )
75+ time .sleep (10 )
76+ return None
4677
4778
4879def vote (data : gr .LikeData ):
@@ -51,13 +82,25 @@ def vote(data: gr.LikeData):
5182 Args:
5283 data (gr.LikeData): The vote data.
5384 """
54- trace = logger .info ("Getting trace from Langfuse" )
5585
86+ trace_id = get_langfuse_trace_id ()
5687 logger .info (f"Vote data: { data } " )
5788 if data .liked :
5889 logger .info ("Vote up" )
90+ langfuse .score (
91+ trace_id = trace_id ,
92+ name = "user-explicit-feedback" ,
93+ value = "like" ,
94+ comment = "I like this response" ,
95+ )
5996 else :
6097 logger .info ("Vote down" )
98+ langfuse .score (
99+ trace_id = trace_id ,
100+ name = "user-explicit-feedback" ,
101+ value = "dislike" ,
102+ comment = "I don't like the response" ,
103+ )
61104
62105
63106def predict (message , history ):
0 commit comments