@gammatech/aijsx 0.11.0-dev.2024-06-17 → 0.11.1-dev.2024-06-23
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.
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +98 -80
- package/dist/index.mjs +98 -80
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -13,8 +13,9 @@ declare class ChatCompletionError extends Error {
|
|
|
13
13
|
readonly chatCompletionRequest: LogChatCompletionRequest;
|
|
14
14
|
readonly status: number | undefined;
|
|
15
15
|
readonly shouldRetry: boolean;
|
|
16
|
+
readonly originalError?: Error | undefined;
|
|
16
17
|
readonly name = "ChatCompletionError";
|
|
17
|
-
constructor(message: string, chatCompletionRequest: LogChatCompletionRequest, status: number | undefined, shouldRetry?: boolean);
|
|
18
|
+
constructor(message: string, chatCompletionRequest: LogChatCompletionRequest, status: number | undefined, shouldRetry?: boolean, originalError?: Error | undefined);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
declare const SystemMessage: (props: {
|
|
@@ -279,7 +280,7 @@ declare module '@gammatech/aijsx' {
|
|
|
279
280
|
* The set of valid Claude models.
|
|
280
281
|
* @see https://docs.anthropic.com/claude/reference/selecting-a-model
|
|
281
282
|
*/
|
|
282
|
-
type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307';
|
|
283
|
+
type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307' | 'claude-3-5-sonnet-20240620';
|
|
283
284
|
declare const AnthropicClientContext: Context<GetChatCompletionClientAndProvider<ValidAnthropicChatModel, AnthropicClient>>;
|
|
284
285
|
type AnthropicChatCompletionProps = {
|
|
285
286
|
model: ValidAnthropicChatModel;
|
package/dist/index.d.ts
CHANGED
|
@@ -13,8 +13,9 @@ declare class ChatCompletionError extends Error {
|
|
|
13
13
|
readonly chatCompletionRequest: LogChatCompletionRequest;
|
|
14
14
|
readonly status: number | undefined;
|
|
15
15
|
readonly shouldRetry: boolean;
|
|
16
|
+
readonly originalError?: Error | undefined;
|
|
16
17
|
readonly name = "ChatCompletionError";
|
|
17
|
-
constructor(message: string, chatCompletionRequest: LogChatCompletionRequest, status: number | undefined, shouldRetry?: boolean);
|
|
18
|
+
constructor(message: string, chatCompletionRequest: LogChatCompletionRequest, status: number | undefined, shouldRetry?: boolean, originalError?: Error | undefined);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
declare const SystemMessage: (props: {
|
|
@@ -279,7 +280,7 @@ declare module '@gammatech/aijsx' {
|
|
|
279
280
|
* The set of valid Claude models.
|
|
280
281
|
* @see https://docs.anthropic.com/claude/reference/selecting-a-model
|
|
281
282
|
*/
|
|
282
|
-
type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307';
|
|
283
|
+
type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307' | 'claude-3-5-sonnet-20240620';
|
|
283
284
|
declare const AnthropicClientContext: Context<GetChatCompletionClientAndProvider<ValidAnthropicChatModel, AnthropicClient>>;
|
|
284
285
|
type AnthropicChatCompletionProps = {
|
|
285
286
|
model: ValidAnthropicChatModel;
|
package/dist/index.js
CHANGED
|
@@ -78,11 +78,12 @@ module.exports = __toCommonJS(src_exports);
|
|
|
78
78
|
|
|
79
79
|
// src/chat/errors.ts
|
|
80
80
|
var ChatCompletionError = class extends Error {
|
|
81
|
-
constructor(message, chatCompletionRequest, status, shouldRetry4 = false) {
|
|
81
|
+
constructor(message, chatCompletionRequest, status, shouldRetry4 = false, originalError) {
|
|
82
82
|
super(message);
|
|
83
83
|
this.chatCompletionRequest = chatCompletionRequest;
|
|
84
84
|
this.status = status;
|
|
85
85
|
this.shouldRetry = shouldRetry4;
|
|
86
|
+
this.originalError = originalError;
|
|
86
87
|
}
|
|
87
88
|
name = "ChatCompletionError";
|
|
88
89
|
};
|
|
@@ -1538,6 +1539,31 @@ function renderCloseTag(element) {
|
|
|
1538
1539
|
return `</${element.tag.name}>`;
|
|
1539
1540
|
}
|
|
1540
1541
|
|
|
1542
|
+
// src/utils.ts
|
|
1543
|
+
function getEnvVar(name, shouldThrow = true) {
|
|
1544
|
+
let env = globalThis.process?.env ?? void 0;
|
|
1545
|
+
if (env === void 0) {
|
|
1546
|
+
try {
|
|
1547
|
+
env = process.env;
|
|
1548
|
+
} catch {
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
const result = env?.[name];
|
|
1552
|
+
if (result === void 0 && shouldThrow) {
|
|
1553
|
+
throw new Error(`Please specify env var '${name}'`);
|
|
1554
|
+
}
|
|
1555
|
+
return result;
|
|
1556
|
+
}
|
|
1557
|
+
var castToError = (e) => {
|
|
1558
|
+
if (e instanceof Error) {
|
|
1559
|
+
return e;
|
|
1560
|
+
}
|
|
1561
|
+
if (typeof e === "string") {
|
|
1562
|
+
return new Error(e);
|
|
1563
|
+
}
|
|
1564
|
+
return new Error("Unknown error");
|
|
1565
|
+
};
|
|
1566
|
+
|
|
1541
1567
|
// src/retry.tsx
|
|
1542
1568
|
var RetryCountContext = createContext({
|
|
1543
1569
|
retryCount: 0,
|
|
@@ -1583,15 +1609,6 @@ var backoff = (retries) => {
|
|
|
1583
1609
|
const waitTime = BASE_BACKOFF * Math.pow(4, retries);
|
|
1584
1610
|
return new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
1585
1611
|
};
|
|
1586
|
-
var castToError = (e) => {
|
|
1587
|
-
if (e instanceof Error) {
|
|
1588
|
-
return e;
|
|
1589
|
-
}
|
|
1590
|
-
if (typeof e === "string") {
|
|
1591
|
-
return new Error(e);
|
|
1592
|
-
}
|
|
1593
|
-
return new Error("Unknown error");
|
|
1594
|
-
};
|
|
1595
1612
|
|
|
1596
1613
|
// src/fallback.tsx
|
|
1597
1614
|
async function* Fallback({ shouldFallback = () => true, fallback, children }, ctx) {
|
|
@@ -1928,25 +1945,6 @@ function isPromptParsed2(prompt) {
|
|
|
1928
1945
|
// src/lib/openai/OpenAI.tsx
|
|
1929
1946
|
var import_openai2 = require("openai");
|
|
1930
1947
|
|
|
1931
|
-
// src/lib/openai/shouldRetryOpenAI.ts
|
|
1932
|
-
var import_openai = require("openai");
|
|
1933
|
-
var shouldRetryOpenAI = (error) => {
|
|
1934
|
-
if (error instanceof import_openai.OpenAI.APIConnectionError) {
|
|
1935
|
-
return true;
|
|
1936
|
-
}
|
|
1937
|
-
if (error instanceof import_openai.OpenAI.APIError) {
|
|
1938
|
-
if ("status" in error && typeof error.status === "number") {
|
|
1939
|
-
if (error.status === 409)
|
|
1940
|
-
return true;
|
|
1941
|
-
if (error.status === 429)
|
|
1942
|
-
return true;
|
|
1943
|
-
if (error.status >= 500)
|
|
1944
|
-
return true;
|
|
1945
|
-
}
|
|
1946
|
-
}
|
|
1947
|
-
return false;
|
|
1948
|
-
};
|
|
1949
|
-
|
|
1950
1948
|
// src/lib/openai/tokenizer.ts
|
|
1951
1949
|
var import_js_tiktoken = require("js-tiktoken");
|
|
1952
1950
|
var cl100kTokenizer = (0, import_js_tiktoken.getEncoding)("cl100k_base");
|
|
@@ -2058,21 +2056,35 @@ async function buildChatMessages(ctx, children, opts) {
|
|
|
2058
2056
|
});
|
|
2059
2057
|
}
|
|
2060
2058
|
|
|
2061
|
-
// src/
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
if (
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2059
|
+
// src/lib/openai/errors.ts
|
|
2060
|
+
var import_openai = require("openai");
|
|
2061
|
+
var extractStatusFromError = (error) => {
|
|
2062
|
+
if (error instanceof import_openai.OpenAI.APIError) {
|
|
2063
|
+
return error.status;
|
|
2064
|
+
} else if (error instanceof import_openai.OpenAI.APIConnectionError) {
|
|
2065
|
+
return void 0;
|
|
2066
|
+
} else {
|
|
2067
|
+
return void 0;
|
|
2069
2068
|
}
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2069
|
+
};
|
|
2070
|
+
var errorToChatCompletionError = (error, requestData) => {
|
|
2071
|
+
const castError = castToError(error);
|
|
2072
|
+
const status = extractStatusFromError(castError);
|
|
2073
|
+
let messagePrefix = "";
|
|
2074
|
+
if (error instanceof import_openai.OpenAI.APIError) {
|
|
2075
|
+
messagePrefix = "OpenAIClient.APIError: ";
|
|
2076
|
+
} else if (error instanceof import_openai.OpenAI.APIConnectionError) {
|
|
2077
|
+
messagePrefix = "OpenAIClient.APIConnectionError: ";
|
|
2073
2078
|
}
|
|
2074
|
-
|
|
2075
|
-
|
|
2079
|
+
const shouldRetry4 = status !== 400;
|
|
2080
|
+
return new ChatCompletionError(
|
|
2081
|
+
`${messagePrefix}${castError.message}`,
|
|
2082
|
+
requestData,
|
|
2083
|
+
status,
|
|
2084
|
+
shouldRetry4,
|
|
2085
|
+
error instanceof Error ? error : void 0
|
|
2086
|
+
);
|
|
2087
|
+
};
|
|
2076
2088
|
|
|
2077
2089
|
// src/lib/openai/OpenAI.tsx
|
|
2078
2090
|
var defaultClient = null;
|
|
@@ -2202,38 +2214,32 @@ async function* OpenAIChatCompletionInner(props, ctx) {
|
|
|
2202
2214
|
try {
|
|
2203
2215
|
chatResponse = await client.chat.completions.create(chatCompletionRequest);
|
|
2204
2216
|
} catch (ex) {
|
|
2205
|
-
|
|
2206
|
-
if (ex instanceof import_openai2.OpenAI.APIError) {
|
|
2207
|
-
throw new ChatCompletionError(
|
|
2208
|
-
`OpenAIClient.APIError: ${ex.message}`,
|
|
2209
|
-
logRequestData,
|
|
2210
|
-
ex.status,
|
|
2211
|
-
retry
|
|
2212
|
-
);
|
|
2213
|
-
} else if (ex instanceof Error) {
|
|
2214
|
-
throw new ChatCompletionError(
|
|
2215
|
-
ex.message,
|
|
2216
|
-
logRequestData,
|
|
2217
|
-
void 0,
|
|
2218
|
-
retry
|
|
2219
|
-
);
|
|
2220
|
-
}
|
|
2221
|
-
throw ex;
|
|
2217
|
+
throw errorToChatCompletionError(ex, logRequestData);
|
|
2222
2218
|
}
|
|
2223
2219
|
let finishReason = void 0;
|
|
2224
2220
|
let content = "";
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2221
|
+
try {
|
|
2222
|
+
for await (const message of chatResponse) {
|
|
2223
|
+
if (!message.choices || !message.choices[0]) {
|
|
2224
|
+
continue;
|
|
2225
|
+
}
|
|
2226
|
+
const delta = message.choices[0].delta;
|
|
2227
|
+
if (message.choices[0].finish_reason) {
|
|
2228
|
+
finishReason = message.choices[0].finish_reason;
|
|
2229
|
+
span.setAttributes({
|
|
2230
|
+
finishReason
|
|
2231
|
+
});
|
|
2232
|
+
}
|
|
2233
|
+
if (delta.content) {
|
|
2234
|
+
content += delta.content;
|
|
2235
|
+
yield delta.content;
|
|
2236
|
+
}
|
|
2236
2237
|
}
|
|
2238
|
+
} catch (e) {
|
|
2239
|
+
span.setAttributes({
|
|
2240
|
+
output: content
|
|
2241
|
+
});
|
|
2242
|
+
throw errorToChatCompletionError(e, logRequestData);
|
|
2237
2243
|
}
|
|
2238
2244
|
const outputMessage = {
|
|
2239
2245
|
role: "assistant",
|
|
@@ -2406,7 +2412,7 @@ var shouldRetry2 = (error) => {
|
|
|
2406
2412
|
var shouldRetryFromStatus = (status) => Boolean(status && [424, 429, 500].includes(status));
|
|
2407
2413
|
var RE_INTERNAL_SERVER_MESSASGE = /The system encountered an unexpected error during processing/i;
|
|
2408
2414
|
var RE_RATE_LIMIT_MESSAGE = /Too many requests, please wait before trying again/;
|
|
2409
|
-
var
|
|
2415
|
+
var extractStatusFromError2 = (error) => {
|
|
2410
2416
|
if (typeof error !== "object" || !(error instanceof Error)) {
|
|
2411
2417
|
return;
|
|
2412
2418
|
}
|
|
@@ -2505,7 +2511,7 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2505
2511
|
response = client.messages.stream(anthropicCompletionRequest);
|
|
2506
2512
|
} catch (err) {
|
|
2507
2513
|
if (err instanceof import_sdk.default.APIError) {
|
|
2508
|
-
const status =
|
|
2514
|
+
const status = extractStatusFromError2(err);
|
|
2509
2515
|
const retry = shouldRetryFromStatus(status);
|
|
2510
2516
|
throw new ChatCompletionError(
|
|
2511
2517
|
`AnthropicClient.APIError: ${err.message}`,
|
|
@@ -2514,7 +2520,7 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2514
2520
|
retry
|
|
2515
2521
|
);
|
|
2516
2522
|
} else if (err instanceof Error) {
|
|
2517
|
-
const status =
|
|
2523
|
+
const status = extractStatusFromError2(err);
|
|
2518
2524
|
const retry = shouldRetryFromStatus(status);
|
|
2519
2525
|
throw new ChatCompletionError(err.message, logRequestData, status, retry);
|
|
2520
2526
|
}
|
|
@@ -2537,10 +2543,16 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2537
2543
|
if (event.type === "message_delta") {
|
|
2538
2544
|
finishReason = event.delta.stop_reason;
|
|
2539
2545
|
outputUsage = event.usage?.output_tokens;
|
|
2546
|
+
span.setAttributes({
|
|
2547
|
+
finishReason
|
|
2548
|
+
});
|
|
2540
2549
|
}
|
|
2541
2550
|
}
|
|
2542
2551
|
} catch (e) {
|
|
2543
|
-
|
|
2552
|
+
span.setAttributes({
|
|
2553
|
+
output: content
|
|
2554
|
+
});
|
|
2555
|
+
const status = extractStatusFromError2(e);
|
|
2544
2556
|
const retry = shouldRetryFromStatus(status);
|
|
2545
2557
|
throw new ChatCompletionError(e.message, logRequestData, status, retry);
|
|
2546
2558
|
}
|
|
@@ -2627,14 +2639,15 @@ var extractStatusFromMessage = (message) => {
|
|
|
2627
2639
|
}
|
|
2628
2640
|
return 500;
|
|
2629
2641
|
};
|
|
2630
|
-
var
|
|
2642
|
+
var errorToChatCompletionError2 = (error, requestData) => {
|
|
2631
2643
|
const status = extractStatusFromMessage(error.message);
|
|
2632
2644
|
const shouldRetry4 = status !== 400;
|
|
2633
2645
|
return new ChatCompletionError(
|
|
2634
2646
|
error.message,
|
|
2635
2647
|
requestData,
|
|
2636
2648
|
status,
|
|
2637
|
-
shouldRetry4
|
|
2649
|
+
shouldRetry4,
|
|
2650
|
+
error
|
|
2638
2651
|
);
|
|
2639
2652
|
};
|
|
2640
2653
|
|
|
@@ -2806,7 +2819,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2806
2819
|
try {
|
|
2807
2820
|
response = await model.generateContentStream(googleCompletionRequest);
|
|
2808
2821
|
} catch (err) {
|
|
2809
|
-
throw
|
|
2822
|
+
throw errorToChatCompletionError2(err, logRequestData);
|
|
2810
2823
|
}
|
|
2811
2824
|
let content = "";
|
|
2812
2825
|
let outputUsage = 0;
|
|
@@ -2817,6 +2830,9 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2817
2830
|
if (event.candidates) {
|
|
2818
2831
|
if (event.candidates[0]?.finishReason) {
|
|
2819
2832
|
finishReason = event.candidates[0].finishReason;
|
|
2833
|
+
span.setAttributes({
|
|
2834
|
+
finishReason
|
|
2835
|
+
});
|
|
2820
2836
|
}
|
|
2821
2837
|
if (event.usageMetadata) {
|
|
2822
2838
|
if (event.usageMetadata.promptTokenCount) {
|
|
@@ -2852,7 +2868,10 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2852
2868
|
}
|
|
2853
2869
|
}
|
|
2854
2870
|
} catch (err) {
|
|
2855
|
-
|
|
2871
|
+
span.setAttributes({
|
|
2872
|
+
output: content
|
|
2873
|
+
});
|
|
2874
|
+
throw errorToChatCompletionError2(err, logRequestData);
|
|
2856
2875
|
}
|
|
2857
2876
|
const outputMessage = {
|
|
2858
2877
|
role: "assistant",
|
|
@@ -2876,8 +2895,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2876
2895
|
span.setAttributes({
|
|
2877
2896
|
tokensUsed,
|
|
2878
2897
|
output: content,
|
|
2879
|
-
cost
|
|
2880
|
-
finishReason
|
|
2898
|
+
cost
|
|
2881
2899
|
});
|
|
2882
2900
|
}
|
|
2883
2901
|
function cleanChatCompletionRequest3(chatCompletionRequest) {
|
package/dist/index.mjs
CHANGED
|
@@ -8,11 +8,12 @@ import {
|
|
|
8
8
|
|
|
9
9
|
// src/chat/errors.ts
|
|
10
10
|
var ChatCompletionError = class extends Error {
|
|
11
|
-
constructor(message, chatCompletionRequest, status, shouldRetry4 = false) {
|
|
11
|
+
constructor(message, chatCompletionRequest, status, shouldRetry4 = false, originalError) {
|
|
12
12
|
super(message);
|
|
13
13
|
this.chatCompletionRequest = chatCompletionRequest;
|
|
14
14
|
this.status = status;
|
|
15
15
|
this.shouldRetry = shouldRetry4;
|
|
16
|
+
this.originalError = originalError;
|
|
16
17
|
}
|
|
17
18
|
name = "ChatCompletionError";
|
|
18
19
|
};
|
|
@@ -1435,6 +1436,31 @@ function renderCloseTag(element) {
|
|
|
1435
1436
|
return `</${element.tag.name}>`;
|
|
1436
1437
|
}
|
|
1437
1438
|
|
|
1439
|
+
// src/utils.ts
|
|
1440
|
+
function getEnvVar(name, shouldThrow = true) {
|
|
1441
|
+
let env = globalThis.process?.env ?? void 0;
|
|
1442
|
+
if (env === void 0) {
|
|
1443
|
+
try {
|
|
1444
|
+
env = process.env;
|
|
1445
|
+
} catch {
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
const result = env?.[name];
|
|
1449
|
+
if (result === void 0 && shouldThrow) {
|
|
1450
|
+
throw new Error(`Please specify env var '${name}'`);
|
|
1451
|
+
}
|
|
1452
|
+
return result;
|
|
1453
|
+
}
|
|
1454
|
+
var castToError = (e) => {
|
|
1455
|
+
if (e instanceof Error) {
|
|
1456
|
+
return e;
|
|
1457
|
+
}
|
|
1458
|
+
if (typeof e === "string") {
|
|
1459
|
+
return new Error(e);
|
|
1460
|
+
}
|
|
1461
|
+
return new Error("Unknown error");
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1438
1464
|
// src/retry.tsx
|
|
1439
1465
|
var RetryCountContext = createContext({
|
|
1440
1466
|
retryCount: 0,
|
|
@@ -1480,15 +1506,6 @@ var backoff = (retries) => {
|
|
|
1480
1506
|
const waitTime = BASE_BACKOFF * Math.pow(4, retries);
|
|
1481
1507
|
return new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
1482
1508
|
};
|
|
1483
|
-
var castToError = (e) => {
|
|
1484
|
-
if (e instanceof Error) {
|
|
1485
|
-
return e;
|
|
1486
|
-
}
|
|
1487
|
-
if (typeof e === "string") {
|
|
1488
|
-
return new Error(e);
|
|
1489
|
-
}
|
|
1490
|
-
return new Error("Unknown error");
|
|
1491
|
-
};
|
|
1492
1509
|
|
|
1493
1510
|
// src/fallback.tsx
|
|
1494
1511
|
async function* Fallback({ shouldFallback = () => true, fallback, children }, ctx) {
|
|
@@ -1825,25 +1842,6 @@ function isPromptParsed2(prompt) {
|
|
|
1825
1842
|
// src/lib/openai/OpenAI.tsx
|
|
1826
1843
|
import { OpenAI as OpenAIClient2 } from "openai";
|
|
1827
1844
|
|
|
1828
|
-
// src/lib/openai/shouldRetryOpenAI.ts
|
|
1829
|
-
import { OpenAI as OpenAIClient } from "openai";
|
|
1830
|
-
var shouldRetryOpenAI = (error) => {
|
|
1831
|
-
if (error instanceof OpenAIClient.APIConnectionError) {
|
|
1832
|
-
return true;
|
|
1833
|
-
}
|
|
1834
|
-
if (error instanceof OpenAIClient.APIError) {
|
|
1835
|
-
if ("status" in error && typeof error.status === "number") {
|
|
1836
|
-
if (error.status === 409)
|
|
1837
|
-
return true;
|
|
1838
|
-
if (error.status === 429)
|
|
1839
|
-
return true;
|
|
1840
|
-
if (error.status >= 500)
|
|
1841
|
-
return true;
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
return false;
|
|
1845
|
-
};
|
|
1846
|
-
|
|
1847
1845
|
// src/lib/openai/tokenizer.ts
|
|
1848
1846
|
import { getEncoding } from "js-tiktoken";
|
|
1849
1847
|
var cl100kTokenizer = getEncoding("cl100k_base");
|
|
@@ -1955,21 +1953,35 @@ async function buildChatMessages(ctx, children, opts) {
|
|
|
1955
1953
|
});
|
|
1956
1954
|
}
|
|
1957
1955
|
|
|
1958
|
-
// src/
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
if (
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1956
|
+
// src/lib/openai/errors.ts
|
|
1957
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
1958
|
+
var extractStatusFromError = (error) => {
|
|
1959
|
+
if (error instanceof OpenAIClient.APIError) {
|
|
1960
|
+
return error.status;
|
|
1961
|
+
} else if (error instanceof OpenAIClient.APIConnectionError) {
|
|
1962
|
+
return void 0;
|
|
1963
|
+
} else {
|
|
1964
|
+
return void 0;
|
|
1966
1965
|
}
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1966
|
+
};
|
|
1967
|
+
var errorToChatCompletionError = (error, requestData) => {
|
|
1968
|
+
const castError = castToError(error);
|
|
1969
|
+
const status = extractStatusFromError(castError);
|
|
1970
|
+
let messagePrefix = "";
|
|
1971
|
+
if (error instanceof OpenAIClient.APIError) {
|
|
1972
|
+
messagePrefix = "OpenAIClient.APIError: ";
|
|
1973
|
+
} else if (error instanceof OpenAIClient.APIConnectionError) {
|
|
1974
|
+
messagePrefix = "OpenAIClient.APIConnectionError: ";
|
|
1970
1975
|
}
|
|
1971
|
-
|
|
1972
|
-
|
|
1976
|
+
const shouldRetry4 = status !== 400;
|
|
1977
|
+
return new ChatCompletionError(
|
|
1978
|
+
`${messagePrefix}${castError.message}`,
|
|
1979
|
+
requestData,
|
|
1980
|
+
status,
|
|
1981
|
+
shouldRetry4,
|
|
1982
|
+
error instanceof Error ? error : void 0
|
|
1983
|
+
);
|
|
1984
|
+
};
|
|
1973
1985
|
|
|
1974
1986
|
// src/lib/openai/OpenAI.tsx
|
|
1975
1987
|
var defaultClient = null;
|
|
@@ -2099,38 +2111,32 @@ async function* OpenAIChatCompletionInner(props, ctx) {
|
|
|
2099
2111
|
try {
|
|
2100
2112
|
chatResponse = await client.chat.completions.create(chatCompletionRequest);
|
|
2101
2113
|
} catch (ex) {
|
|
2102
|
-
|
|
2103
|
-
if (ex instanceof OpenAIClient2.APIError) {
|
|
2104
|
-
throw new ChatCompletionError(
|
|
2105
|
-
`OpenAIClient.APIError: ${ex.message}`,
|
|
2106
|
-
logRequestData,
|
|
2107
|
-
ex.status,
|
|
2108
|
-
retry
|
|
2109
|
-
);
|
|
2110
|
-
} else if (ex instanceof Error) {
|
|
2111
|
-
throw new ChatCompletionError(
|
|
2112
|
-
ex.message,
|
|
2113
|
-
logRequestData,
|
|
2114
|
-
void 0,
|
|
2115
|
-
retry
|
|
2116
|
-
);
|
|
2117
|
-
}
|
|
2118
|
-
throw ex;
|
|
2114
|
+
throw errorToChatCompletionError(ex, logRequestData);
|
|
2119
2115
|
}
|
|
2120
2116
|
let finishReason = void 0;
|
|
2121
2117
|
let content = "";
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2118
|
+
try {
|
|
2119
|
+
for await (const message of chatResponse) {
|
|
2120
|
+
if (!message.choices || !message.choices[0]) {
|
|
2121
|
+
continue;
|
|
2122
|
+
}
|
|
2123
|
+
const delta = message.choices[0].delta;
|
|
2124
|
+
if (message.choices[0].finish_reason) {
|
|
2125
|
+
finishReason = message.choices[0].finish_reason;
|
|
2126
|
+
span.setAttributes({
|
|
2127
|
+
finishReason
|
|
2128
|
+
});
|
|
2129
|
+
}
|
|
2130
|
+
if (delta.content) {
|
|
2131
|
+
content += delta.content;
|
|
2132
|
+
yield delta.content;
|
|
2133
|
+
}
|
|
2133
2134
|
}
|
|
2135
|
+
} catch (e) {
|
|
2136
|
+
span.setAttributes({
|
|
2137
|
+
output: content
|
|
2138
|
+
});
|
|
2139
|
+
throw errorToChatCompletionError(e, logRequestData);
|
|
2134
2140
|
}
|
|
2135
2141
|
const outputMessage = {
|
|
2136
2142
|
role: "assistant",
|
|
@@ -2303,7 +2309,7 @@ var shouldRetry2 = (error) => {
|
|
|
2303
2309
|
var shouldRetryFromStatus = (status) => Boolean(status && [424, 429, 500].includes(status));
|
|
2304
2310
|
var RE_INTERNAL_SERVER_MESSASGE = /The system encountered an unexpected error during processing/i;
|
|
2305
2311
|
var RE_RATE_LIMIT_MESSAGE = /Too many requests, please wait before trying again/;
|
|
2306
|
-
var
|
|
2312
|
+
var extractStatusFromError2 = (error) => {
|
|
2307
2313
|
if (typeof error !== "object" || !(error instanceof Error)) {
|
|
2308
2314
|
return;
|
|
2309
2315
|
}
|
|
@@ -2402,7 +2408,7 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2402
2408
|
response = client.messages.stream(anthropicCompletionRequest);
|
|
2403
2409
|
} catch (err) {
|
|
2404
2410
|
if (err instanceof AnthropicClient.APIError) {
|
|
2405
|
-
const status =
|
|
2411
|
+
const status = extractStatusFromError2(err);
|
|
2406
2412
|
const retry = shouldRetryFromStatus(status);
|
|
2407
2413
|
throw new ChatCompletionError(
|
|
2408
2414
|
`AnthropicClient.APIError: ${err.message}`,
|
|
@@ -2411,7 +2417,7 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2411
2417
|
retry
|
|
2412
2418
|
);
|
|
2413
2419
|
} else if (err instanceof Error) {
|
|
2414
|
-
const status =
|
|
2420
|
+
const status = extractStatusFromError2(err);
|
|
2415
2421
|
const retry = shouldRetryFromStatus(status);
|
|
2416
2422
|
throw new ChatCompletionError(err.message, logRequestData, status, retry);
|
|
2417
2423
|
}
|
|
@@ -2434,10 +2440,16 @@ async function* AnthropicChatCompletionInner(props, ctx) {
|
|
|
2434
2440
|
if (event.type === "message_delta") {
|
|
2435
2441
|
finishReason = event.delta.stop_reason;
|
|
2436
2442
|
outputUsage = event.usage?.output_tokens;
|
|
2443
|
+
span.setAttributes({
|
|
2444
|
+
finishReason
|
|
2445
|
+
});
|
|
2437
2446
|
}
|
|
2438
2447
|
}
|
|
2439
2448
|
} catch (e) {
|
|
2440
|
-
|
|
2449
|
+
span.setAttributes({
|
|
2450
|
+
output: content
|
|
2451
|
+
});
|
|
2452
|
+
const status = extractStatusFromError2(e);
|
|
2441
2453
|
const retry = shouldRetryFromStatus(status);
|
|
2442
2454
|
throw new ChatCompletionError(e.message, logRequestData, status, retry);
|
|
2443
2455
|
}
|
|
@@ -2528,14 +2540,15 @@ var extractStatusFromMessage = (message) => {
|
|
|
2528
2540
|
}
|
|
2529
2541
|
return 500;
|
|
2530
2542
|
};
|
|
2531
|
-
var
|
|
2543
|
+
var errorToChatCompletionError2 = (error, requestData) => {
|
|
2532
2544
|
const status = extractStatusFromMessage(error.message);
|
|
2533
2545
|
const shouldRetry4 = status !== 400;
|
|
2534
2546
|
return new ChatCompletionError(
|
|
2535
2547
|
error.message,
|
|
2536
2548
|
requestData,
|
|
2537
2549
|
status,
|
|
2538
|
-
shouldRetry4
|
|
2550
|
+
shouldRetry4,
|
|
2551
|
+
error
|
|
2539
2552
|
);
|
|
2540
2553
|
};
|
|
2541
2554
|
|
|
@@ -2707,7 +2720,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2707
2720
|
try {
|
|
2708
2721
|
response = await model.generateContentStream(googleCompletionRequest);
|
|
2709
2722
|
} catch (err) {
|
|
2710
|
-
throw
|
|
2723
|
+
throw errorToChatCompletionError2(err, logRequestData);
|
|
2711
2724
|
}
|
|
2712
2725
|
let content = "";
|
|
2713
2726
|
let outputUsage = 0;
|
|
@@ -2718,6 +2731,9 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2718
2731
|
if (event.candidates) {
|
|
2719
2732
|
if (event.candidates[0]?.finishReason) {
|
|
2720
2733
|
finishReason = event.candidates[0].finishReason;
|
|
2734
|
+
span.setAttributes({
|
|
2735
|
+
finishReason
|
|
2736
|
+
});
|
|
2721
2737
|
}
|
|
2722
2738
|
if (event.usageMetadata) {
|
|
2723
2739
|
if (event.usageMetadata.promptTokenCount) {
|
|
@@ -2753,7 +2769,10 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2753
2769
|
}
|
|
2754
2770
|
}
|
|
2755
2771
|
} catch (err) {
|
|
2756
|
-
|
|
2772
|
+
span.setAttributes({
|
|
2773
|
+
output: content
|
|
2774
|
+
});
|
|
2775
|
+
throw errorToChatCompletionError2(err, logRequestData);
|
|
2757
2776
|
}
|
|
2758
2777
|
const outputMessage = {
|
|
2759
2778
|
role: "assistant",
|
|
@@ -2777,8 +2796,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
|
|
|
2777
2796
|
span.setAttributes({
|
|
2778
2797
|
tokensUsed,
|
|
2779
2798
|
output: content,
|
|
2780
|
-
cost
|
|
2781
|
-
finishReason
|
|
2799
|
+
cost
|
|
2782
2800
|
});
|
|
2783
2801
|
}
|
|
2784
2802
|
function cleanChatCompletionRequest3(chatCompletionRequest) {
|