File size: 6,034 Bytes
81e0b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c750639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81e0b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c750639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81e0b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from "react";

const BENCHMARK_STEPS = [
  "configuration",
  "provider_check",
  "ingestion",
  "upload_ingest_to_hub",
  "summarization",
  "chunking",
  "single_shot_question_generation",
];

export const useBenchmarkLogs = (sessionId, isDefault, onComplete) => {
  const [generationLogs, setGenerationLogs] = useState([]);
  const [error, setError] = useState(null);
  const [currentPhase, setCurrentPhase] = useState("initializing");
  const [completedSteps, setCompletedSteps] = useState([]);
  const [activeStep, setActiveStep] = useState(1);
  const [generationComplete, setGenerationComplete] = useState(false);

  const checkForErrors = (logs) => {
    // Check for document with insufficient information error
    const hasInsufficientInfoError = logs.some((log) =>
      log.includes(
        "Failed to generate benchmark: The document does not contain enough information"
      )
    );

    if (hasInsufficientInfoError) {
      return {
        hasError: true,
        error:
          "Your document doesn't contain enough information to generate a benchmark. Please try with a more comprehensive document that includes richer content.",
      };
    }

    // Check for rate limiting errors
    const hasRateLimitError = logs.some(
      (log) =>
        log.includes("RATE_LIMIT_EXCEEDED") ||
        log.includes("heavy load") ||
        log.includes("rate limit")
    );

    if (hasRateLimitError) {
      return {
        hasError: true,
        error:
          "The demo is under heavy load at the moment. Please try again later.",
      };
    }

    // Check for model availability errors
    const hasModelError = logs.some(
      (log) =>
        log.includes("Required models not available") ||
        log.includes("Some required models are not available")
    );

    if (hasModelError) {
      return {
        hasError: true,
        error:
          "Some required models are not available at the moment. Please try again later.",
      };
    }

    // Check for configuration errors
    const hasConfigError = logs.some(
      (log) =>
        log.includes("Error generating configuration") ||
        log.includes("Configuration failed")
    );

    if (hasConfigError) {
      return {
        hasError: true,
        error:
          "Failed to generate benchmark configuration. Please try again later.",
      };
    }

    // Check for insufficient content errors
    const hasInsufficientContentError = logs.some(
      (log) =>
        log.includes(
          "No valid questions produced in single_shot_question_generation"
        ) || log.includes("No parseable JSON found")
    );

    if (hasInsufficientContentError) {
      return {
        hasError: true,
        error:
          "The document does not contain enough information to generate a meaningful benchmark. Please try with a more detailed document.",
      };
    }

    return { hasError: false };
  };

  const updateSteps = (logs) => {
    const newCompletedSteps = [];

    logs.forEach((log) => {
      const match = log.match(/\[SUCCESS\] Stage completed: (\w+)/);
      if (match && match[1]) {
        const completedStep = match[1].trim();
        if (
          BENCHMARK_STEPS.includes(completedStep) &&
          !newCompletedSteps.includes(completedStep)
        ) {
          newCompletedSteps.push(completedStep);
        }
      }
    });

    let newActiveStep = activeStep;

    if (newCompletedSteps.length > 0) {
      const maxCompletedStepIndex = Math.max(
        ...newCompletedSteps.map((step) => BENCHMARK_STEPS.indexOf(step))
      );
      const calculatedStep = maxCompletedStepIndex + 1;

      if (calculatedStep > activeStep) {
        newActiveStep = calculatedStep;
      }

      if (newActiveStep >= BENCHMARK_STEPS.length) {
        newActiveStep = BENCHMARK_STEPS.length;
      }
    } else if (activeStep === 0) {
      newActiveStep = 1;
    }

    return { newCompletedSteps, newActiveStep };
  };

  const updatePhase = (logs) => {
    const recentLogs = logs.slice(-10);

    const isComplete = recentLogs.some((log) =>
      log.includes("[SUCCESS] Benchmark process completed successfully")
    );

    if (isComplete) {
      return "complete";
    } else if (
      recentLogs.some((log) => log.includes("Starting ingestion process"))
    ) {
      return "benchmarking";
    } else if (
      recentLogs.some((log) => log.includes("Generating base configuration"))
    ) {
      return "configuring";
    }

    return currentPhase;
  };

  useEffect(() => {
    if (generationLogs.length === 0) return;

    const errorCheck = checkForErrors(generationLogs);
    if (errorCheck.hasError) {
      setError(errorCheck.error);
      setGenerationComplete(true);
      if (onComplete) {
        onComplete({
          success: false,
          error: errorCheck.error,
          sessionId,
        });
      }
      return;
    }

    const { newCompletedSteps, newActiveStep } = updateSteps(generationLogs);
    const newPhase = updatePhase(generationLogs);

    if (JSON.stringify(newCompletedSteps) !== JSON.stringify(completedSteps)) {
      setCompletedSteps(newCompletedSteps);
    }

    if (newActiveStep !== activeStep) {
      setActiveStep(newActiveStep);
    }

    if (newPhase !== currentPhase) {
      setCurrentPhase(newPhase);
    }

    // Vérifier si le benchmark est réellement terminé sans erreur
    const recentLogs = generationLogs.slice(-10);
    const isComplete = recentLogs.some((log) =>
      log.includes("[SUCCESS] Benchmark process completed successfully")
    );

    if (isComplete) {
      setGenerationComplete(true);
      if (onComplete) {
        onComplete({
          success: true,
          sessionId,
          logs: generationLogs,
        });
      }
    }
  }, [generationLogs, sessionId, onComplete]);

  return {
    generationLogs,
    setGenerationLogs,
    error,
    setError,
    currentPhase,
    completedSteps,
    activeStep,
    generationComplete,
    setGenerationComplete,
  };
};