55"""
66
77import logging
8- from datetime import datetime
8+ from datetime import datetime , timedelta
99from typing import Any , Dict , Iterator , List , Optional , cast
1010
1111from eval_protocol .models import EvaluationRow , InputMetadata , Message
1212
1313logger = logging .getLogger (__name__ )
1414
1515try :
16- from langfuse import Langfuse # pyright: ignore[reportPrivateImportUsage]
16+ from langfuse import get_client # pyright: ignore[reportPrivateImportUsage]
17+ from langfuse .api .resources .trace .types .traces import Traces
18+ from langfuse .api .resources .commons .types .trace_with_full_details import TraceWithFullDetails
1719
1820 LANGFUSE_AVAILABLE = True
1921except ImportError :
@@ -45,35 +47,20 @@ class LangfuseAdapter:
4547 ... ))
4648 """
4749
48- def __init__ (
49- self ,
50- public_key : str ,
51- secret_key : str ,
52- host : str = "https://cloud.langfuse.com" ,
53- project_id : Optional [str ] = None ,
54- ):
55- """Initialize the Langfuse adapter.
56-
57- Args:
58- public_key: Langfuse public key
59- secret_key: Langfuse secret key
60- host: Langfuse host URL (default: https://cloud.langfuse.com)
61- project_id: Optional project ID to filter traces
62- """
50+ def __init__ (self ):
51+ """Initialize the Langfuse adapter."""
6352 if not LANGFUSE_AVAILABLE :
6453 raise ImportError ("Langfuse not installed. Install with: pip install 'eval-protocol[langfuse]'" )
6554
66- self .client = cast (Any , Langfuse )(public_key = public_key , secret_key = secret_key , host = host )
67- self .project_id = project_id
55+ self .client = get_client ()
6856
6957 def get_evaluation_rows (
7058 self ,
7159 limit : int = 100 ,
7260 tags : Optional [List [str ]] = None ,
7361 user_id : Optional [str ] = None ,
7462 session_id : Optional [str ] = None ,
75- from_timestamp : Optional [datetime ] = None ,
76- to_timestamp : Optional [datetime ] = None ,
63+ hours_back : Optional [int ] = None ,
7764 include_tool_calls : bool = True ,
7865 ) -> List [EvaluationRow ]:
7966 """Pull traces from Langfuse and convert to EvaluationRow format.
@@ -83,16 +70,23 @@ def get_evaluation_rows(
8370 tags: Filter by specific tags
8471 user_id: Filter by user ID
8572 session_id: Filter by session ID
86- from_timestamp: Filter traces after this timestamp
87- to_timestamp: Filter traces before this timestamp
73+ hours_back: Filter traces from this many hours ago
8874 include_tool_calls: Whether to include tool calling traces
8975
9076 Yields:
9177 EvaluationRow: Converted evaluation rows
9278 """
9379 # Get traces from Langfuse using new API
80+
81+ if hours_back :
82+ to_timestamp = datetime .now ()
83+ from_timestamp = to_timestamp - timedelta (hours = hours_back )
84+ else :
85+ to_timestamp = None
86+ from_timestamp = None
87+
9488 eval_rows = []
95- traces = self .client .api .trace .list (
89+ traces : Traces = self .client .api .trace .list (
9690 limit = limit ,
9791 tags = tags ,
9892 user_id = user_id ,
@@ -128,7 +122,7 @@ def get_evaluation_rows_by_ids(
128122 eval_rows = []
129123 for trace_id in trace_ids :
130124 try :
131- trace = self .client .api .trace .get (trace_id )
125+ trace : TraceWithFullDetails = self .client .api .trace .get (trace_id )
132126 eval_row = self ._convert_trace_to_evaluation_row (trace , include_tool_calls )
133127 if eval_row :
134128 eval_rows .append (eval_row )
@@ -147,10 +141,10 @@ def _convert_trace_to_evaluation_row(self, trace: Any, include_tool_calls: bool
147141 Returns:
148142 EvaluationRow or None if conversion fails
149143 """
150- # TODO: move this logic into an adapter in llm_judge.py. langfuse.py should just return traces
151144 try :
152145 # Get observations (generations, spans) from the trace
153146 observations_response = self .client .api .observations .get_many (trace_id = trace .id , limit = 100 )
147+ # print(observations_response)
154148 observations = (
155149 observations_response .data if hasattr (observations_response , "data" ) else list (observations_response )
156150 )
@@ -406,7 +400,6 @@ def _create_input_metadata(self, trace: Any, observations: List[Any]) -> InputMe
406400 "trace_id" : trace .id ,
407401 "trace_name" : getattr (trace , "name" , None ),
408402 "trace_tags" : getattr (trace , "tags" , []),
409- "langfuse_project_id" : self .project_id ,
410403 }
411404
412405 # Add trace metadata if available
@@ -418,9 +411,6 @@ def _create_input_metadata(self, trace: Any, observations: List[Any]) -> InputMe
418411 "session_id" : getattr (trace , "session_id" , None ),
419412 "user_id" : getattr (trace , "user_id" , None ),
420413 "timestamp" : getattr (trace , "timestamp" , None ),
421- "langfuse_trace_url" : (
422- f"{ self .client .host } /project/{ self .project_id } /traces/{ trace .id } " if self .project_id else None
423- ),
424414 }
425415
426416 return InputMetadata (
@@ -497,26 +487,7 @@ def _extract_tools(self, observations: List[Any], trace: Any = None) -> Optional
497487 return tools if tools else None
498488
499489
500- def create_langfuse_adapter (
501- public_key : str ,
502- secret_key : str ,
503- host : str = "https://cloud.langfuse.com" ,
504- project_id : Optional [str ] = None ,
505- ) -> LangfuseAdapter :
506- """Factory function to create a Langfuse adapter.
490+ def create_langfuse_adapter () -> LangfuseAdapter :
491+ """Factory function to create a Langfuse adapter."""
507492
508- Args:
509- public_key: Langfuse public key
510- secret_key: Langfuse secret key
511- host: Langfuse host URL
512- project_id: Optional project ID
513-
514- Returns:
515- LangfuseAdapter instance
516- """
517- return LangfuseAdapter (
518- public_key = public_key ,
519- secret_key = secret_key ,
520- host = host ,
521- project_id = project_id ,
522- )
493+ return LangfuseAdapter ()
0 commit comments