forked from rayhunter/omd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_langfuse_comprehensive.py
More file actions
179 lines (150 loc) · 5.66 KB
/
Copy pathtest_langfuse_comprehensive.py
File metadata and controls
179 lines (150 loc) · 5.66 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
"""
Comprehensive test for Langfuse integration.
This will create actual traces you can see in your Langfuse dashboard.
"""
import time
from langfuse_integration import langfuse_manager, shutdown_langfuse
def test_basic_span():
"""Test basic span tracing."""
print("\n1️⃣ Testing basic span...")
with langfuse_manager.trace_span("test_basic_span",
metadata={"test": "basic", "version": "1.0"},
tags=["test", "basic"]):
time.sleep(0.1) # Simulate work
print(" ✅ Basic span created")
def test_llm_call():
"""Test LLM call tracing."""
print("\n2️⃣ Testing LLM call tracing...")
langfuse_manager.trace_llm_call(
model="gpt-3.5-turbo",
input_text="What is the capital of France?",
output_text="The capital of France is Paris.",
metadata={"test": "llm_call", "provider": "openai"},
usage={"prompt_tokens": 10, "completion_tokens": 8, "total_tokens": 18}
)
print(" ✅ LLM call traced")
def test_agent_steps():
"""Test agent step tracing."""
print("\n3️⃣ Testing agent step tracing...")
# Think step
langfuse_manager.trace_agent_step(
step_type="think",
input_data="User asked about Python",
output_data="I need to explain Python basics",
metadata={"step": 1, "agent": "research_agent"}
)
# Act step
langfuse_manager.trace_agent_step(
step_type="act",
input_data="Explain Python basics",
output_data="Python is a high-level programming language...",
metadata={"step": 2, "agent": "research_agent"}
)
print(" ✅ Agent steps traced")
def test_mcp_call():
"""Test MCP server call tracing."""
print("\n4️⃣ Testing MCP call tracing...")
langfuse_manager.trace_mcp_call(
server_name="llama-mcp",
query="search for AI research",
response="Found 10 papers on AI research...",
latency_ms=235.5,
metadata={"test": "mcp_call", "server_type": "search"}
)
print(" ✅ MCP call traced")
def test_nested_traces():
"""Test nested trace spans."""
print("\n5️⃣ Testing nested traces...")
with langfuse_manager.trace_span("parent_operation",
tags=["test", "nested", "parent"]):
print(" 📦 Parent span created")
with langfuse_manager.trace_span("child_operation_1",
tags=["test", "nested", "child"]):
print(" 📦 Child span 1 created")
time.sleep(0.05)
with langfuse_manager.trace_span("child_operation_2",
tags=["test", "nested", "child"]):
print(" 📦 Child span 2 created")
time.sleep(0.05)
print(" ✅ Nested traces created")
def test_trace_with_metadata():
"""Test trace with rich metadata."""
print("\n6️⃣ Testing trace with rich metadata...")
langfuse_manager.update_current_trace(
name="comprehensive_test",
user_id="test_user_123",
session_id="test_session_456",
tags=["test", "comprehensive", "metadata"],
metadata={
"environment": "test",
"version": "1.0.0",
"timestamp": time.time(),
"test_type": "comprehensive"
}
)
with langfuse_manager.trace_span("metadata_test"):
print(" ✅ Trace with metadata created")
def test_scoring():
"""Test trace scoring."""
print("\n7️⃣ Testing trace scoring...")
with langfuse_manager.trace_span("scored_operation"):
time.sleep(0.05)
# Score the trace
langfuse_manager.score_current_trace(
name="quality",
value=0.95,
comment="Test score for comprehensive test"
)
langfuse_manager.score_current_trace(
name="user_feedback",
value=1.0,
comment="Positive test feedback"
)
print(" ✅ Trace scored")
def main():
print("="*60)
print("🧪 Comprehensive Langfuse Integration Test")
print("="*60)
if not langfuse_manager.enabled:
print("\n❌ Langfuse is not enabled!")
print("Make sure your .env file has:")
print(" LANGFUSE_PUBLIC_KEY=pk-lf-...")
print(" LANGFUSE_SECRET_KEY=sk-lf-...")
print(" LANGFUSE_HOST=https://us.cloud.langfuse.com")
return
print(f"\n✅ Langfuse enabled and connected")
print(f" Host: https://us.cloud.langfuse.com")
try:
# Run all tests
test_basic_span()
test_llm_call()
test_agent_steps()
test_mcp_call()
test_nested_traces()
test_trace_with_metadata()
test_scoring()
# Flush all events
print("\n📤 Flushing events to Langfuse...")
shutdown_langfuse()
print(" ✅ Events flushed")
print("\n" + "="*60)
print("✅ All tests passed!")
print("="*60)
print("\n🎉 View your traces at:")
print(" https://us.cloud.langfuse.com")
print("\nYou should see:")
print(" • Basic spans")
print(" • LLM call with token usage")
print(" • Agent reasoning steps")
print(" • MCP server calls with latency")
print(" • Nested trace hierarchy")
print(" • Rich metadata")
print(" • Scores on traces")
print("\n" + "="*60)
except Exception as e:
print(f"\n❌ Test failed: {e}")
import traceback
traceback.print_exc()
shutdown_langfuse()
if __name__ == "__main__":
main()