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: add retry logs #192

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
7 changes: 7 additions & 0 deletions .changeset/lucky-needles-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@empiricalrun/ai": patch
"@empiricalrun/cli": patch
"@empiricalrun/core": patch
---

fix: add retry logs for model api calls
5 changes: 4 additions & 1 deletion packages/ai/src/providers/anthropic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
try {
const startedAt = Date.now();
const response = await promiseRetry<Anthropic.Messages.Message>(
(retry) => {
(retry, attempt) => {
return anthropic.messages
.create({
model: canonicalModelName(model),
Expand All @@ -92,6 +92,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
err instanceof Anthropic.APIConnectionTimeoutError ||
err instanceof Anthropic.InternalServerError
) {
console.warn(
`Retrying request for anthropic model: ${body.model}. Retry attempt: ${attempt}`,
);
retry(err);
throw err;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/providers/azure-openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const createChatCompletion: ICreateChatCompletion = async (
maxRetries: 5,
shouldRetry: async (resp, retryCount) => {
if (resp instanceof Response) {
console.log(
`Retrying request for azure-openai model: ${body.model}. Retry count: ${retryCount}`,
console.warn(
`Retrying request for azure-openai model: ${body.model}. Retry attempt: ${retryCount}`,
);
if (resp.status === 429) {
requestStartTime = new Date().getTime();
Expand Down
5 changes: 4 additions & 1 deletion packages/ai/src/providers/fireworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
try {
const startedAt = Date.now();
const completion = await promiseRetry<IChatCompletion>(
(retry) => {
(retry, attempt) => {
return fetch("https://api.fireworks.ai/inference/v1/chat/completions", {
method: "POST",
headers: {
Expand All @@ -64,6 +64,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
AIErrorEnum.RATE_LIMITED,
"Fireworks API rate limit reached",
);
console.warn(
`Retrying request for fireworks model: ${body.model}. Got response status code ${response.status}. Retry attempt: ${attempt}`,
);
retry(err);
throw err;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/ai/src/providers/google/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
try {
const startedAt = Date.now();
const completion = await promiseRetry<GenerateContentResult>(
(retry) => {
(retry, attempt) => {
return modelInstance
.startChat({
// @ts-ignore same as above
Expand All @@ -101,6 +101,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
.catch((err: Error) => {
// TODO: Replace with instanceof checks when the Gemini SDK exports errors
if (err.message.includes("[429 Too Many Requests]")) {
console.warn(
`Retrying request for google model: ${body.model}. Retry attempt: ${attempt}`,
);
retry(err);
}
throw err;
Expand Down
7 changes: 5 additions & 2 deletions packages/ai/src/providers/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
try {
const startedAt = Date.now();
const completions = await promiseRetry<IChatCompletion>(
(retry) => {
(retry, attempt) => {
return openai.chat.completions.create(body).catch((err) => {
if (
err instanceof OpenAI.RateLimitError &&
Expand All @@ -46,6 +46,9 @@ const createChatCompletion: ICreateChatCompletion = async (body) => {
err instanceof OpenAI.APIConnectionTimeoutError ||
err instanceof OpenAI.InternalServerError
) {
console.warn(
`Retrying request for openai model: ${body.model}. Retry attempt: ${attempt}`,
);
retry(err);
throw err;
}
Expand Down Expand Up @@ -158,7 +161,7 @@ const runAssistant: ICreateAndRunAssistantThread = async (body) => {
return asstRunResp;
})().catch((err: any) => {
if ((err.message as string).includes("server_error")) {
console.log(
console.warn(
`Retrying request due to server error (attempt ${attempt})`,
);
requestStartTime = Date.now();
Expand Down