@xapp/stentor-service-generative-ai 2.2.1 → 2.2.2
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.
|
@@ -6,7 +6,7 @@ export interface FallbackLLMServiceProps {
|
|
|
6
6
|
*/
|
|
7
7
|
primary: LLMService;
|
|
8
8
|
/**
|
|
9
|
-
* The fallback LLM service to use when the primary is rate limited
|
|
9
|
+
* The fallback LLM service to use when the primary is rate limited or unavailable
|
|
10
10
|
*/
|
|
11
11
|
fallback: LLMService;
|
|
12
12
|
/**
|
|
@@ -16,7 +16,8 @@ export interface FallbackLLMServiceProps {
|
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* An LLM service that wraps a primary service with a fallback service.
|
|
19
|
-
* When the primary service
|
|
19
|
+
* When the primary service is rate limited or unavailable (a server fault, a network error, a
|
|
20
|
+
* timeout), the fallback service is used.
|
|
20
21
|
*
|
|
21
22
|
* @example
|
|
22
23
|
* ```typescript
|
|
@@ -34,7 +35,7 @@ export declare class FallbackLLMService implements LLMService {
|
|
|
34
35
|
constructor(props: FallbackLLMServiceProps);
|
|
35
36
|
/**
|
|
36
37
|
* Generate a response from the primary LLM service, falling back to the
|
|
37
|
-
* secondary service if the primary
|
|
38
|
+
* secondary service if the primary is rate limited or unavailable.
|
|
38
39
|
*
|
|
39
40
|
* @param prompt - The prompt to send to the LLM
|
|
40
41
|
* @param options - Optional configuration including timeout
|
|
@@ -12,32 +12,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.FallbackLLMService = void 0;
|
|
14
14
|
/**
|
|
15
|
-
* Error patterns that indicate rate limiting and should trigger fallback
|
|
15
|
+
* Error patterns that indicate rate limiting or an unavailable service and should trigger fallback
|
|
16
16
|
*/
|
|
17
|
-
const
|
|
17
|
+
const FALLBACK_PATTERNS = [
|
|
18
18
|
/rate limit/i,
|
|
19
19
|
/throttl/i,
|
|
20
20
|
/too many requests/i,
|
|
21
21
|
/429/,
|
|
22
22
|
/ThrottlingException/i,
|
|
23
23
|
/ServiceQuotaExceededException/i,
|
|
24
|
-
/ProvisionedThroughputExceededException/i
|
|
24
|
+
/ProvisionedThroughputExceededException/i,
|
|
25
|
+
/ServiceUnavailable/i,
|
|
26
|
+
/service unavailable/i,
|
|
27
|
+
/unable to process your request/i,
|
|
28
|
+
/InternalServer/i,
|
|
29
|
+
/ModelNotReady/i,
|
|
30
|
+
/ModelTimeout/i,
|
|
31
|
+
/timed out/i
|
|
25
32
|
];
|
|
26
33
|
/**
|
|
27
|
-
*
|
|
34
|
+
* Network error codes: the provider could not be reached, so another one might be.
|
|
28
35
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
const NETWORK_ERROR_CODES = ["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND", "EAI_AGAIN", "EPIPE"];
|
|
37
|
+
/**
|
|
38
|
+
* Check if an error means the primary is rate limited or unavailable, so the fallback may succeed.
|
|
39
|
+
*
|
|
40
|
+
* Errors that would fail the same way anywhere -- a malformed request, a prompt the service does
|
|
41
|
+
* not support, denied access -- are not matched and are rethrown.
|
|
42
|
+
*/
|
|
43
|
+
function shouldFallback(error) {
|
|
33
44
|
if (typeof error === "string") {
|
|
34
|
-
return
|
|
45
|
+
return FALLBACK_PATTERNS.some((pattern) => pattern.test(error));
|
|
46
|
+
}
|
|
47
|
+
if (!(error instanceof Error)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
// AWS SDK v3 marks errors the service is responsible for as server faults.
|
|
51
|
+
const { $fault, code } = error;
|
|
52
|
+
if ($fault === "server") {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (code && NETWORK_ERROR_CODES.includes(code)) {
|
|
56
|
+
return true;
|
|
35
57
|
}
|
|
36
|
-
return
|
|
58
|
+
return FALLBACK_PATTERNS.some((pattern) => pattern.test(error.name) || pattern.test(error.message));
|
|
37
59
|
}
|
|
38
60
|
/**
|
|
39
61
|
* An LLM service that wraps a primary service with a fallback service.
|
|
40
|
-
* When the primary service
|
|
62
|
+
* When the primary service is rate limited or unavailable (a server fault, a network error, a
|
|
63
|
+
* timeout), the fallback service is used.
|
|
41
64
|
*
|
|
42
65
|
* @example
|
|
43
66
|
* ```typescript
|
|
@@ -56,7 +79,7 @@ class FallbackLLMService {
|
|
|
56
79
|
}
|
|
57
80
|
/**
|
|
58
81
|
* Generate a response from the primary LLM service, falling back to the
|
|
59
|
-
* secondary service if the primary
|
|
82
|
+
* secondary service if the primary is rate limited or unavailable.
|
|
60
83
|
*
|
|
61
84
|
* @param prompt - The prompt to send to the LLM
|
|
62
85
|
* @param options - Optional configuration including timeout
|
|
@@ -68,7 +91,7 @@ class FallbackLLMService {
|
|
|
68
91
|
return yield this.primary.generate(prompt, options);
|
|
69
92
|
}
|
|
70
93
|
catch (error) {
|
|
71
|
-
if (
|
|
94
|
+
if (shouldFallback(error)) {
|
|
72
95
|
if (this.onFallback && error instanceof Error) {
|
|
73
96
|
this.onFallback(error);
|
|
74
97
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FallbackLLMService.js","sourceRoot":"","sources":["../src/FallbackLLMService.ts"],"names":[],"mappings":";AAAA,kCAAkC;;;;;;;;;;;;AAIlC;;GAEG;AACH,MAAM,
|
|
1
|
+
{"version":3,"file":"FallbackLLMService.js","sourceRoot":"","sources":["../src/FallbackLLMService.ts"],"names":[],"mappings":";AAAA,kCAAkC;;;;;;;;;;;;AAIlC;;GAEG;AACH,MAAM,iBAAiB,GAAG;IACtB,aAAa;IACb,UAAU;IACV,oBAAoB;IACpB,KAAK;IACL,sBAAsB;IACtB,gCAAgC;IAChC,yCAAyC;IACzC,qBAAqB;IACrB,sBAAsB;IACtB,iCAAiC;IACjC,iBAAiB;IACjB,gBAAgB;IAChB,eAAe;IACf,Y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