Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fireworks errors are absorbed #195

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-clouds-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@empiricalrun/ai": patch
---

fix: fireworks error should not get absorbed
8 changes: 3 additions & 5 deletions packages/ai/src/providers/azure-openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ const createChatCompletion: ICreateChatCompletion = async (
maxRetries: 5,
shouldRetry: async (resp, retryCount) => {
if (resp instanceof Response) {
console.warn(
`Retrying request for azure-openai model: ${body.model}. Retry attempt: ${retryCount}`,
);
if (resp.status === 429) {
requestStartTime = new Date().getTime();
console.warn(
`Retrying request for azure-openai model: ${body.model}. Retry attempt: ${retryCount}`,
);
return true;
}
}
Expand All @@ -67,8 +67,6 @@ const createChatCompletion: ICreateChatCompletion = async (
let errMsg = `Failed to fetch output from model ${body.model}: ${e}`;
if (e instanceof Response) {
errMsg = `Failed to fetch output from model ${body.model}: api response status: ${e.status}`;
} else if (e instanceof Error) {
errMsg = `Failed to fetch output from model ${body.model}: error: ${e.message}`;
}
console.error(errMsg);
throw new AIError(AIErrorEnum.FAILED_CHAT_COMPLETION, errMsg);
Expand Down
86 changes: 45 additions & 41 deletions packages/ai/src/providers/fireworks/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
ICreateChatCompletion,
IAIProvider,
IChatCompletion,
} from "@empiricalrun/types";
import { ICreateChatCompletion, IAIProvider } from "@empiricalrun/types";
import { BatchTaskManager, getPassthroughParams } from "../../utils";
import { AIError, AIErrorEnum } from "../../error";
import promiseRetry from "promise-retry";
import { fetchWithRetry } from "@empiricalrun/fetch";
import { DEFAULT_TIMEOUT } from "../../constants";

const batchTaskManager = new BatchTaskManager(10);

Expand Down Expand Up @@ -38,49 +35,56 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
const { executionDone } = await batchTaskManager.waitForTurn();

try {
const startedAt = Date.now();
const completion = await promiseRetry<IChatCompletion>(
(retry, attempt) => {
return fetch("https://api.fireworks.ai/inference/v1/chat/completions", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: payload,
}).then(async (response) => {
const parsed = await response.json();

if (response.status === 200) {
return parsed;
} else if (response.status === 400) {
throw new AIError(
AIErrorEnum.INCORRECT_PARAMETERS,
`Incorrect request payload: ${parsed.error?.message || "Unknown error"}`,
);
} else if (response.status === 429 || response.status >= 500) {
const err = new AIError(
AIErrorEnum.RATE_LIMITED,
"Fireworks API rate limit reached",
);
let startedAt = Date.now();
const response = await fetchWithRetry(
"https://api.fireworks.ai/inference/v1/chat/completions",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: payload,
maxRetries: 5,
timeout: body.timeout || DEFAULT_TIMEOUT,
backoffMultiple: 1.8,
shouldRetry: async (err, attempt) => {
if (err instanceof Response && err.status === 429) {
console.warn(
`Retrying request for fireworks model: ${body.model}. Got response status code ${response.status}. Retry attempt: ${attempt}`,
`Retrying request for fireworks model: ${body.model}. Retry attempt: ${attempt}`,
);
retry(err);
throw err;
startedAt = Date.now();
return true;
}
});
},
{
randomize: true,
return false;
},
},
);
const completion = await response.json();
const latency = Date.now() - startedAt;
executionDone();
return { ...completion, latency };
} catch (err) {
throw new AIError(AIErrorEnum.FAILED_CHAT_COMPLETION, "Unknown error");
} catch (e) {
executionDone();
let error = new AIError(
AIErrorEnum.FAILED_CHAT_COMPLETION,
`Failed to fetch output from fireworks model ${body.model}: ${e}`,
);
if (e instanceof Response) {
let parsed: any = {};
try {
parsed = await e.json();
} catch (e) {
// ignore error
}
error = new AIError(
AIErrorEnum.FAILED_CHAT_COMPLETION,
`Failed to fetch output from fireworks model ${body.model}: HTTP status ${e.status}: ${parsed.error?.message || "Unknown error"}`,
);
}
console.error(error.message);
throw error;
}
};

Expand Down