Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

trace-assertions.js 2.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. module.exports = (output, context) => {
  2. // Check if trace data is available
  3. if (!context.trace) {
  4. // Tracing not enabled, skip trace-based checks
  5. return true;
  6. }
  7. const { spans } = context.trace;
  8. // Check for errors in any span
  9. const errorSpans = spans.filter((s) => s.statusCode >= 400);
  10. if (errorSpans.length > 0) {
  11. return {
  12. pass: false,
  13. score: 0,
  14. reason: `Found ${errorSpans.length} error spans`,
  15. };
  16. }
  17. // Calculate total trace duration
  18. if (spans.length > 0) {
  19. const duration =
  20. Math.max(...spans.map((s) => s.endTime || 0)) - Math.min(...spans.map((s) => s.startTime));
  21. if (duration > 5000) {
  22. // 5 seconds
  23. return {
  24. pass: false,
  25. score: 0,
  26. reason: `Trace took too long: ${duration}ms`,
  27. };
  28. }
  29. }
  30. // Check for specific operations - look for HTTP/API calls
  31. const apiCalls = spans.filter((s) => s.name.toLowerCase().includes('http'));
  32. if (apiCalls.length > 10) {
  33. return {
  34. pass: false,
  35. score: 0,
  36. reason: `Too many API calls: ${apiCalls.length}`,
  37. };
  38. }
  39. // Verify all expected RAG steps are present
  40. const expectedSteps = [
  41. 'query_analysis',
  42. 'document_retrieval',
  43. 'context_augmentation',
  44. 'reasoning_chain',
  45. 'response_generation',
  46. ];
  47. const missingSteps = expectedSteps.filter((step) => !spans.some((s) => s.name === step));
  48. if (missingSteps.length > 0) {
  49. return {
  50. pass: false,
  51. score: 0,
  52. reason: `Missing RAG workflow steps: ${missingSteps.join(', ')}`,
  53. };
  54. }
  55. // Check for the main workflow span (optional - may be missing due to timing)
  56. const workflowSpan = spans.find((s) => s.name === 'rag_agent_workflow');
  57. if (workflowSpan) {
  58. // Validate workflow attributes if present
  59. const attrs = workflowSpan.attributes;
  60. if (!attrs['workflow.success'] || attrs['workflow.success'] !== true) {
  61. return {
  62. pass: false,
  63. score: 0,
  64. reason: 'Workflow did not complete successfully',
  65. };
  66. }
  67. }
  68. // Check document retrieval performance
  69. const retrievalSpans = spans.filter((s) => s.name.startsWith('retrieve_document_'));
  70. if (retrievalSpans.length < 3) {
  71. return {
  72. pass: false,
  73. score: 0,
  74. reason: `Expected 3 document retrievals, found ${retrievalSpans.length}`,
  75. };
  76. }
  77. // Check reasoning chain has expected sub-steps
  78. const reasoningSteps = spans.filter((s) => s.name.startsWith('reasoning_'));
  79. if (reasoningSteps.length < 3) {
  80. return {
  81. pass: false,
  82. score: 0,
  83. reason: `Expected at least 3 reasoning steps, found ${reasoningSteps.length}`,
  84. };
  85. }
  86. // All checks passed
  87. return {
  88. pass: true,
  89. score: 1,
  90. reason: 'Trace validation successful',
  91. };
  92. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...