forked from ggrieco25/RAG-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_test_langfuse.py
More file actions
646 lines (518 loc) Β· 23.9 KB
/
Copy pathquick_test_langfuse.py
File metadata and controls
646 lines (518 loc) Β· 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
"""
Quick test per Langfuse con Gemini e funzionalitΓ avanzate di tracing.
Questo script testa:
1. Inizializzazione Langfuse con Gemini
2. Traces personalizzate
3. Spans annidati
4. Scores custom
5. Generazioni multiple
6. Metriche e valutazioni
Usage:
python quick_test_langfuse.py
"""
import os
import time
import uuid
import random
from typing import Dict, List, Any
from datetime import datetime
from dotenv import load_dotenv
# Langfuse imports (v2/v3 compatible)
from langfuse import observe
# Try to import langfuse_context for v2 compatibility
try:
from langfuse.decorators import langfuse_context
LANGFUSE_V2 = True
except ImportError:
# v3 doesn't have langfuse_context in decorators
LANGFUSE_V2 = False
langfuse_context = None
# LangChain imports
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.schema import HumanMessage, AIMessage
from langchain.prompts import ChatPromptTemplate
# Import nostro manager
from src.telemetry.langfuse_setup import LangfuseManager
from config.config import LangfuseConfig
# Load environment variables
load_dotenv()
class LangfuseQuickTest:
"""
Classe per testare tutte le funzionalitΓ di Langfuse con Gemini.
"""
def __init__(self):
"""Inizializza il test con Langfuse e Gemini."""
print("π Inizializzazione Quick Test Langfuse...")
# Setup Langfuse
self.langfuse_config = LangfuseConfig.from_environment()
self.langfuse_manager = LangfuseManager(self.langfuse_config)
# Setup Gemini
self.google_api_key = os.getenv("GOOGLE_API_KEY")
if not self.google_api_key:
raise ValueError("β GOOGLE_API_KEY non trovata nelle variabili d'ambiente")
self.model = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-lite",
google_api_key=self.google_api_key,
temperature=0.3,
max_tokens=1000
)
# Direct Langfuse client per operazioni avanzate
self.langfuse = self.langfuse_manager.get_client()
print("β
Inizializzazione completata!")
@observe(name="simple_generation_test", as_type="generation")
def test_simple_generation(self) -> str:
"""Test base: generazione semplice con trace automatica."""
print("\nπ Test 1: Generazione Semplice")
prompt = "Dimmi una curiositΓ interessante sulla fisica quantistica in 2 frasi."
# Usa il nostro manager per invocare il modello
response = self.langfuse_manager.invoke_model_with_langchain(
self.model,
[HumanMessage(content=prompt)],
extra_config={"temperature": 0.3}
)
result = response.content if hasattr(response, 'content') else str(response)
print(f"π¬ Risposta: {result[:100]}...")
return result
def generate_and_trace(self) -> Any:
"""
Funzione semplice che:
1. Chiama il LLM con model.invoke()
2. Crea una trace su Langfuse con i dettagli della chiamata
Args:
langfuse_manager: Istanza del LangfuseManager
model: Modello LangChain
prompt: Prompt da inviare
config: Configurazione per model.invoke()
Returns:
Response dal modello
"""
prompt = "Dimmi una curiositΓ interessante sulla fisica quantistica in 2 frasi."
config = {}
# 1. Chiama il LLM
response = self.model.invoke(prompt, config=config)
# Estrai numero di tokens
input_tokens = response.usage_metadata.get('input_tokens', 0)
output_tokens = response.usage_metadata.get('output_tokens', 0)
total_tokens = input_tokens + output_tokens
# Estrai contenuto della risposta
response_content = response.content if hasattr(response, 'content') else str(response)
# 2. Crea trace su Langfuse
# langfuse_client = self.langfuse
if self.langfuse:
try:
# Crea la trace con tutti i dettagli della chiamata LLM
with self.langfuse.start_as_current_span(
name = "test_trace",
input = {
"prompt": prompt,
"config": config
},
output = response_content,
version = "test_version",
level="test_level",
status_message="test_status_message",
) as root_span:
# Aggiunge gli attributi del primo span
root_span.update_trace(
user_id="test_user",
session_id="test_session",
version = "test_version",
tags=["test_tag"],
public = None
)
# Crea una generazione innestata (gen1)
with self.langfuse.start_as_current_observation(
name="test-gen1",
as_type='generation',
input={
"prompt": prompt,
"config": config
}
) as gen1:
gen1.update(
output = response,
metadata = {
"confidence" : 0.9
},
version = None,
level = "DEFAULT",
status_message = "test_status_message",
completion_start_time = datetime.now().isoformat(),
model = self.model.model,
model_parameters = {"temperature": self.model.temperature},
usage_details = {"input_tokens": input_tokens, "output_tokens": output_tokens, "total_tokens": total_tokens},
cost_details = {"input_tokens": 0.3, "output_tokens": 0.5, "total_tokens": 0.8},
)
# Crea uno span intermedio (mid_span)
with root_span.start_as_current_observation(name="second_span", as_type='span') as second_span:
# Aggiunge gli attributi del primo span
second_span.update(
input = "second span input",
output = "second span output"
)
# Crea una generazione innestata (gen2)
with second_span.start_as_current_observation(name="gen2", as_type='generation') as gen2:
gen2.update(output = response)
except Exception as e:
print(f"Errore creazione trace: {e}")
return response
@observe(name="complex_rag_simulation", as_type="generation")
def test_complex_rag_simulation(self) -> Dict[str, Any]:
"""Test complesso: simulazione RAG con spans multipli."""
print("\nπ Test 2: Simulazione RAG Complessa")
user_query = "Come funziona il machine learning nel riconoscimento delle immagini?"
# Simula il retrieval di documenti
retrieved_docs = self._simulate_document_retrieval(user_query)
# Simula il reranking
reranked_docs = self._simulate_reranking(retrieved_docs, user_query)
# Genera la risposta finale
final_response = self._generate_final_response(user_query, reranked_docs)
# Aggiungi scores personalizzati
self._add_custom_scores(user_query, final_response)
return {
"query": user_query,
"retrieved_docs": len(retrieved_docs),
"final_response": final_response,
"processing_time": time.time()
}
@observe(name="document_retrieval", as_type="span")
def _simulate_document_retrieval(self, query: str) -> List[Dict[str, Any]]:
"""Simula il retrieval di documenti."""
print(" π Simulando retrieval documenti...")
# Simula latenza
time.sleep(0.1)
# Documenti fittizi
docs = [
{
"id": f"doc_{i}",
"content": f"Documento {i} sul machine learning e computer vision...",
"score": random.uniform(0.6, 0.95),
"source": f"source_{i}.pdf"
}
for i in range(5)
]
# Log come evento Langfuse (v2/v3 compatible)
if LANGFUSE_V2 and langfuse_context:
langfuse_context.update_current_observation(
input={"query": query},
output={"documents_found": len(docs)},
metadata={
"retrieval_method": "semantic_search",
"embedding_model": "text-embedding-ada-002"
}
)
# Note: v3 uses different context management through OTEL
return docs
@observe(name="document_reranking", as_type="span")
def _simulate_reranking(self, docs: List[Dict], query: str) -> List[Dict]:
"""Simula il reranking dei documenti."""
print(" π Simulando reranking...")
time.sleep(0.05)
# Simula reranking (ordina per score decrescente)
reranked = sorted(docs, key=lambda x: x["score"], reverse=True)[:3]
if LANGFUSE_V2 and langfuse_context:
langfuse_context.update_current_observation(
input={"documents": len(docs), "query": query},
output={"reranked_documents": len(reranked)},
metadata={
"reranking_model": "cross-encoder",
"top_k": 3
}
)
return reranked
@observe(name="response_generation", as_type="generation")
def _generate_final_response(self, query: str, context_docs: List[Dict]) -> str:
"""Genera la risposta finale usando il contesto."""
print(" π€ Generando risposta finale...")
# Costruisci il prompt con contesto
context = "\n".join([doc["content"] for doc in context_docs])
prompt = ChatPromptTemplate.from_messages([
("system", "Sei un assistente AI esperto. Usa il contesto fornito per rispondere alla domanda."),
("human", f"Contesto:\n{context}\n\nDomanda: {query}")
])
# Genera risposta
response = self.langfuse_manager.invoke_model_with_langchain(
self.model,
prompt.format_messages(),
extra_config={
"temperature": 0.3,
"max_tokens": 500
}
)
result = response.content if hasattr(response, 'content') else str(response)
print(f" π¬ Risposta generata: {result[:80]}...")
return result
def _add_custom_scores(self, query: str, response: str) -> None:
"""Aggiunge scores personalizzati alla trace corrente."""
print(" π Aggiungendo scores personalizzati...")
# Simula diverse metriche
scores = {
"relevance": random.uniform(0.7, 1.0),
"accuracy": random.uniform(0.6, 0.95),
"completeness": random.uniform(0.65, 0.9),
"clarity": random.uniform(0.75, 1.0),
"response_length": len(response.split()),
}
# Aggiungi scores tramite il client Langfuse diretto
if LANGFUSE_V2 and langfuse_context:
trace_id = langfuse_context.get_current_trace_id()
else:
# v3 usa OTEL context, piΓΉ complesso - per ora usiamo il client diretto
trace_id = None
for score_name, score_value in scores.items():
try:
if trace_id:
self.langfuse.score(
trace_id=trace_id,
name=score_name,
value=score_value,
comment=f"Automated evaluation for {score_name}"
)
print(f" β
Score '{score_name}': {score_value:.3f}")
else:
print(f" β οΈ Score '{score_name}': {score_value:.3f} (trace_id not available in v3)")
except Exception as e:
print(f" β Errore aggiungendo score {score_name}: {e}")
def test_batch_processing(self) -> List[Dict[str, Any]]:
"""Test batch: processa multiple queries con tracing."""
print("\nπ¦ Test 3: Batch Processing")
queries = [
"Cos'Γ¨ l'intelligenza artificiale?",
"Come funziona una rete neurale?",
"Quali sono le applicazioni del deep learning?",
"Che differenza c'Γ¨ tra ML e AI?"
]
results = []
with self.langfuse_manager:
for i, query in enumerate(queries, 1):
print(f" π Processing query {i}/{len(queries)}")
# Crea una trace personalizzata per ogni query
trace = self.langfuse.trace(
name=f"batch_query_{i}",
input={"query": query, "batch_position": i},
metadata={
"batch_id": str(uuid.uuid4()),
"timestamp": datetime.now().isoformat()
}
)
try:
# Genera risposta
response = self.langfuse_manager.invoke_model_with_langchain(
self.model,
[HumanMessage(content=query)],
extra_config={"temperature": 0.6}
)
result_text = response.content if hasattr(response, 'content') else str(response)
# Aggiorna la trace con il risultato
trace.update(
output={"response": result_text},
metadata={
"response_length": len(result_text),
"processing_successful": True
}
)
# Aggiungi score di qualitΓ
quality_score = random.uniform(0.7, 0.95)
trace.score(
name="batch_quality",
value=quality_score,
comment="Automated batch processing quality score"
)
results.append({
"query": query,
"response": result_text[:100] + "...",
"quality_score": quality_score,
"trace_id": trace.id
})
except Exception as e:
print(f" β Errore processing query {i}: {e}")
trace.update(
output={"error": str(e)},
metadata={"processing_successful": False}
)
results.append({
"query": query,
"response": f"Error: {str(e)}",
"quality_score": 0.0,
"trace_id": trace.id
})
return results
@observe(name="conversation_simulation", as_type="generation")
def test_conversation_flow(self) -> List[Dict[str, Any]]:
"""Test conversazione: simula un dialogo multi-turn."""
print("\n㪠Test 4: Conversazione Multi-turn")
conversation_history = []
topics = [
"Spiegami cos'Γ¨ il quantum computing",
"Come si collega alla crittografia?",
"Quali sono i vantaggi rispetto ai computer classici?",
"Quando sarΓ disponibile commercialmente?"
]
for turn, user_message in enumerate(topics, 1):
print(f" π£οΈ Turn {turn}: {user_message}")
# Costruisci il contesto della conversazione
messages = []
# Aggiungi la storia della conversazione
for hist in conversation_history[-3:]: # Ultimi 3 scambi
messages.append(HumanMessage(content=hist["user"]))
messages.append(AIMessage(content=hist["assistant"]))
# Aggiungi il messaggio corrente
messages.append(HumanMessage(content=user_message))
# Genera risposta con contesto
response = self.langfuse_manager.invoke_model_with_langchain(
self.model,
messages,
extra_config={
"temperature": 0.4,
"max_tokens": 300
}
)
response_text = response.content if hasattr(response, 'content') else str(response)
# Salva nella storia
conversation_history.append({
"turn": turn,
"user": user_message,
"assistant": response_text,
"timestamp": datetime.now().isoformat()
})
# Aggiungi scores di conversazione
self._add_conversation_scores(turn, user_message, response_text)
print(f" π€ Risposta: {response_text[:60]}...")
return conversation_history
def _add_conversation_scores(self, turn: int, user_msg: str, assistant_msg: str) -> None:
"""Aggiunge scores specifici per la conversazione."""
if LANGFUSE_V2 and langfuse_context:
trace_id = langfuse_context.get_current_trace_id()
else:
trace_id = None
scores = {
"conversation_coherence": min(1.0, 0.6 + (turn * 0.1)), # Migliora nel tempo
"context_retention": random.uniform(0.7, 0.9),
"response_relevance": random.uniform(0.8, 1.0),
}
for score_name, score_value in scores.items():
if trace_id:
self.langfuse.score(
trace_id=trace_id,
name=score_name,
value=score_value,
comment=f"Turn {turn} - {score_name} evaluation"
)
def test_error_handling(self) -> Dict[str, Any]:
"""Test gestione errori con tracing."""
print("\nβ οΈ Test 5: Gestione Errori")
error_scenarios = [
{"prompt": "A" * 50000, "expected_error": "Token limit"}, # Prompt troppo lungo
{"prompt": "", "expected_error": "Empty prompt"}, # Prompt vuoto
{"prompt": "Normal prompt", "config": {"temperature": 5.0}, "expected_error": "Invalid temperature"} # Config invalida
]
results = []
for i, scenario in enumerate(error_scenarios, 1):
print(f" π§ͺ Scenario errore {i}: {scenario['expected_error']}")
# Crea trace per l'errore
trace = self.langfuse.trace(
name=f"error_scenario_{i}",
input={"scenario": scenario["expected_error"]},
metadata={"test_type": "error_handling"}
)
try:
config = scenario.get("config", {"temperature": 0.7})
response = self.langfuse_manager.invoke_model(
self.model,
[HumanMessage(content=scenario["prompt"])],
extra_config=config
)
# Se non c'Γ¨ errore, segna come inaspettato
trace.update(
output={"unexpected_success": True},
level="WARNING"
)
results.append({
"scenario": scenario["expected_error"],
"result": "Unexpected success",
"trace_id": trace.id
})
except Exception as e:
# Errore atteso
trace.update(
output={"error": str(e)},
level="ERROR",
status_message=f"Expected error: {str(e)[:100]}"
)
# Aggiungi score di error handling
trace.score(
name="error_handling_success",
value=1.0,
comment="Successfully caught and handled expected error"
)
results.append({
"scenario": scenario["expected_error"],
"result": f"Caught: {str(e)[:50]}...",
"trace_id": trace.id
})
print(f" β
Errore gestito correttamente: {str(e)[:50]}...")
return {"error_scenarios": results}
def run_all_tests(self) -> Dict[str, Any]:
"""Esegue tutti i test e fornisce un summary."""
print("=" * 60)
print("π― QUICK TEST LANGFUSE - INIZIO")
print("=" * 60)
start_time = time.time()
all_results = {}
try:
# Test 1: Generazione semplice
# all_results["simple_generation"] = self.test_simple_generation()
all_results["generate_and_trace"] = self.generate_and_trace()
# Test 2: RAG simulation
# all_results["rag_simulation"] = self.test_complex_rag_simulation()
# Test 3: Batch processing
# all_results["batch_processing"] = self.test_batch_processing()
# Test 4: Conversazione
# all_results["conversation"] = self.test_conversation_flow()
# Test 5: Error handling
# all_results["error_handling"] = self.test_error_handling()
execution_time = time.time() - start_time
# Summary finale
print("\n" + "=" * 60)
print("π SUMMARY FINALE")
print("=" * 60)
print(f"β±οΈ Tempo totale esecuzione: {execution_time:.2f}s")
print(f"π Traces create: ~{len(all_results) * 3}") # Stima approssimativa
print(f"π Scores aggiunti: ~{len(all_results) * 5}") # Stima approssimativa
print("β
Tutti i test completati con successo!")
# Flush finale per assicurarsi che tutto sia inviato
if self.langfuse:
self.langfuse.flush()
print("π Dati inviati a Langfuse!")
return {
"results": all_results,
"execution_time": execution_time,
"status": "success"
}
except Exception as e:
print(f"\nβ ERRORE DURANTE I TEST: {e}")
return {
"results": all_results,
"execution_time": time.time() - start_time,
"status": "error",
"error": str(e)
}
finally:
# Cleanup
self.langfuse_manager.cleanup()
print("π§Ή Cleanup completato")
def main():
"""Funzione principale per eseguire i test."""
try:
tester = LangfuseQuickTest()
results = tester.run_all_tests()
print("\nπ Quick Test completato!")
print(f"Status: {results['status']}")
if results['status'] == 'error':
print(f"Errore: {results.get('error', 'Unknown')}")
except Exception as e:
print(f"β Errore fatale: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()