@absolutejs/rag 0.0.17 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2897,6 +2897,78 @@ var createRAGClient = (options) => {
2897
2897
  }
2898
2898
  return parseJson(response);
2899
2899
  },
2900
+ async evaluateStream(input, options2) {
2901
+ const response = await fetchImpl(`${basePath}/evaluate/stream`, {
2902
+ body: JSON.stringify(input),
2903
+ headers: jsonHeaders,
2904
+ method: "POST",
2905
+ signal: options2?.signal
2906
+ });
2907
+ if (!response.ok || !response.body) {
2908
+ throw new Error(await toErrorMessage(response));
2909
+ }
2910
+ const reader = response.body.getReader();
2911
+ const decoder = new TextDecoder;
2912
+ let buffer = "";
2913
+ let result = null;
2914
+ let streamError = null;
2915
+ const handleEvent = (rawEvent) => {
2916
+ let eventName = "message";
2917
+ const dataLines = [];
2918
+ for (const line of rawEvent.split(`
2919
+ `)) {
2920
+ if (line.startsWith("event:")) {
2921
+ eventName = line.slice("event:".length).trim();
2922
+ } else if (line.startsWith("data:")) {
2923
+ dataLines.push(line.slice("data:".length).trim());
2924
+ }
2925
+ }
2926
+ if (dataLines.length === 0) {
2927
+ return;
2928
+ }
2929
+ const payload = JSON.parse(dataLines.join(`
2930
+ `));
2931
+ if (eventName === "start") {
2932
+ options2?.onStart?.(payload);
2933
+ } else if (eventName === "case") {
2934
+ options2?.onCase?.(payload);
2935
+ } else if (eventName === "result") {
2936
+ result = payload;
2937
+ } else if (eventName === "error") {
2938
+ streamError = payload.error ?? "RAG evaluation stream failed";
2939
+ }
2940
+ };
2941
+ for (;; ) {
2942
+ const { done, value } = await reader.read();
2943
+ if (done) {
2944
+ break;
2945
+ }
2946
+ buffer += decoder.decode(value, { stream: true });
2947
+ let separator = buffer.indexOf(`
2948
+
2949
+ `);
2950
+ while (separator !== -1) {
2951
+ const rawEvent = buffer.slice(0, separator);
2952
+ buffer = buffer.slice(separator + 2);
2953
+ if (rawEvent.trim().length > 0) {
2954
+ handleEvent(rawEvent);
2955
+ }
2956
+ separator = buffer.indexOf(`
2957
+
2958
+ `);
2959
+ }
2960
+ }
2961
+ if (buffer.trim().length > 0) {
2962
+ handleEvent(buffer);
2963
+ }
2964
+ if (streamError) {
2965
+ throw new Error(streamError);
2966
+ }
2967
+ if (!result) {
2968
+ throw new Error("RAG evaluation stream ended without a result");
2969
+ }
2970
+ return result;
2971
+ },
2900
2972
  async compareRetrievals(input) {
2901
2973
  const response = await fetchImpl(`${basePath}/compare/retrieval`, {
2902
2974
  body: JSON.stringify(input),
@@ -4201,6 +4273,7 @@ var useRAGEvaluate = (path) => {
4201
4273
  const [suiteRuns, setSuiteRuns] = useState3([]);
4202
4274
  const [lastRequest, setLastRequest] = useState3(null);
4203
4275
  const [lastResponse, setLastResponse] = useState3(null);
4276
+ const [streamProgress, setStreamProgress] = useState3(null);
4204
4277
  const evaluate = useCallback3(async (input) => {
4205
4278
  setIsEvaluating(true);
4206
4279
  setError(null);
@@ -4217,6 +4290,33 @@ var useRAGEvaluate = (path) => {
4217
4290
  setIsEvaluating(false);
4218
4291
  }
4219
4292
  }, [client]);
4293
+ const evaluateStream = useCallback3(async (input, options) => {
4294
+ setIsEvaluating(true);
4295
+ setError(null);
4296
+ setLastRequest(input);
4297
+ setStreamProgress({ done: 0, total: input.cases.length });
4298
+ try {
4299
+ const response = await client.evaluateStream(input, {
4300
+ onCase: (event) => {
4301
+ setStreamProgress((current) => ({
4302
+ done: (current?.done ?? 0) + 1,
4303
+ total: event.total
4304
+ }));
4305
+ options?.onCase?.(event);
4306
+ },
4307
+ onStart: ({ total }) => setStreamProgress({ done: 0, total }),
4308
+ signal: options?.signal
4309
+ });
4310
+ setLastResponse(response);
4311
+ return response;
4312
+ } catch (caught) {
4313
+ const message = caught instanceof Error ? caught.message : String(caught);
4314
+ setError(message);
4315
+ throw caught;
4316
+ } finally {
4317
+ setIsEvaluating(false);
4318
+ }
4319
+ }, [client]);
4220
4320
  const saveSuite = useCallback3((suite) => {
4221
4321
  setSuites((current) => {
4222
4322
  const next = current.filter((entry) => entry.id !== suite.id);
@@ -4263,6 +4363,7 @@ var useRAGEvaluate = (path) => {
4263
4363
  clearRuns,
4264
4364
  error,
4265
4365
  evaluate,
4366
+ evaluateStream,
4266
4367
  isEvaluating,
4267
4368
  lastRequest,
4268
4369
  lastResponse,
@@ -4271,6 +4372,7 @@ var useRAGEvaluate = (path) => {
4271
4372
  reset,
4272
4373
  runSuite,
4273
4374
  saveSuite,
4375
+ streamProgress,
4274
4376
  suiteRuns,
4275
4377
  suites
4276
4378
  };
@@ -4941,5 +5043,5 @@ export {
4941
5043
  useRAG
4942
5044
  };
4943
5045
 
4944
- //# debugId=90DDA4852A9BCCA364756E2164756E21
5046
+ //# debugId=2C44ECEE37502BBC64756E2164756E21
4945
5047
  //# sourceMappingURL=index.js.map