New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ai-fallback

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ai-fallback - npm Package Compare versions

Comparing version

to
0.1.0

16

dist/index.js
export function createFallback(settings) {
return new FallbackModel(settings);
}
// Default error retry logic
const retryableStatusCodes = [
401,
403,
408,
409,
413,
429,
500, // server error (and above)
];
function defaultShouldRetryThisError(error) {
let statusCode = error === null || error === void 0 ? void 0 : error['statusCode'];
if (statusCode &&
(statusCode === 408 || // request timeout
statusCode === 409 || // conflict
statusCode === 429 || // too many requests
statusCode >= 500) // server error)
) {
(retryableStatusCodes.includes(statusCode) || statusCode > 500)) {
return true;

@@ -14,0 +18,0 @@ }

@@ -12,2 +12,3 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {

import { createOpenAI } from '@ai-sdk/openai';
import { createGroq } from '@ai-sdk/groq';
import { createAnthropic } from '@ai-sdk/anthropic';

@@ -53,2 +54,22 @@ import { generateText, streamText, streamObject } from 'ai';

});
test('groq switches model on error, switches to third model', async () => {
const model = createFallback({
models: [
createGroq({ apiKey: 'wrong-key' })('gpt-3.5-turbo'),
createOpenAI({ apiKey: 'wrong-key' })('gpt-3.5-turbo'),
anthropic('claude-3-haiku-20240307'),
],
});
model.currentModelIndex = 0;
const result = await generateText({
model,
system: 'You only respond hello',
messages: [{ role: 'user', content: 'Say hello' }],
});
// After error with OpenAI, should have switched to Anthropic
expect(model.currentModelIndex).toBe(2);
expect(model.modelId).toMatchInlineSnapshot(`"claude-3-haiku-20240307"`);
expect(result.text).toBeTruthy();
model.currentModelIndex = 0;
});
test('streamText works', async () => {

@@ -55,0 +76,0 @@ var _a, e_1, _b, _c;

{
"name": "ai-fallback",
"version": "0.0.6",
"version": "0.1.0",
"description": "Automatically switch AI SDK model provider when one of the providers has downtime",

@@ -24,2 +24,3 @@ "type": "module",

"@ai-sdk/anthropic": "^1.0.5",
"@ai-sdk/groq": "^1.0.9",
"@ai-sdk/openai": "^1.0.8",

@@ -26,0 +27,0 @@ "ai": "^4.0.14",

@@ -5,2 +5,3 @@ import { test, expect } from 'vitest'

import { createOpenAI } from '@ai-sdk/openai'
import { createGroq } from '@ai-sdk/groq'
import { createAnthropic } from '@ai-sdk/anthropic'

@@ -56,2 +57,26 @@ import { generateText, streamText, streamObject } from 'ai'

test('groq switches model on error, switches to third model', async () => {
const model = createFallback({
models: [
createGroq({ apiKey: 'wrong-key' })('gpt-3.5-turbo'),
createOpenAI({ apiKey: 'wrong-key' })('gpt-3.5-turbo'),
anthropic('claude-3-haiku-20240307'),
],
})
model.currentModelIndex = 0
const result = await generateText({
model,
system: 'You only respond hello',
messages: [{ role: 'user', content: 'Say hello' }],
})
// After error with OpenAI, should have switched to Anthropic
expect(model.currentModelIndex).toBe(2)
expect(model.modelId).toMatchInlineSnapshot(`"claude-3-haiku-20240307"`)
expect(result.text).toBeTruthy()
model.currentModelIndex = 0
})
test('streamText works', async () => {

@@ -58,0 +83,0 @@ const model = createFallback({

@@ -28,11 +28,18 @@ import {

// Default error retry logic
const retryableStatusCodes = [
401, // wrong API key
403, // permission error, like cannot access model or from a non accessible region
408, // request timeout
409, // conflict
413, // payload too large
429, // too many requests/rate limits
500, // server error (and above)
]
function defaultShouldRetryThisError(error: Error): boolean {
let statusCode = error?.['statusCode']
if (
statusCode &&
(statusCode === 408 || // request timeout
statusCode === 409 || // conflict
statusCode === 429 || // too many requests
statusCode >= 500) // server error)
(retryableStatusCodes.includes(statusCode) || statusCode > 500)
) {

@@ -144,3 +151,2 @@ return true

lastError = error as Error
// Only retry if it's a server/capacity error

@@ -147,0 +153,0 @@ const shouldRetry =

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet