Skip to content

test: ✅ retry on failed llm api tests #5495

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

Merged
merged 2 commits into from
May 20, 2025
Merged
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
34 changes: 24 additions & 10 deletions core/llm/llm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ const COMPLETION_OPTIONS: Partial<CompletionOptions> = {
// maxTokens: 5,
};

/**
* Retries a test function if it fails on the first attempt
* @param testFn The test function to run
* @returns A function that will retry once if the test fails
*/
const retryOnce = (testFn: () => Promise<any>) => async () => {
try {
return await testFn();
} catch (error) {
console.log("Test failed on first attempt, retrying once...");
return await testFn();
}
};

function testLLM(
llm: BaseLLM,
{
Expand All @@ -47,7 +61,7 @@ function testLLM(
describe(llm.providerName + "/" + llm.model, () => {
test(
"Stream Chat works",
async () => {
retryOnce(async () => {
let total = "";
for await (const chunk of llm.streamChat(
[{ role: "user", content: "Hi" }],
Expand All @@ -58,13 +72,13 @@ function testLLM(

expect(total.length).toBeGreaterThan(0);
return;
},
}),
timeout,
);

test(
"Stream Complete works",
async () => {
retryOnce(async () => {
let total = "";
for await (const chunk of llm.streamComplete(
"Hi",
Expand All @@ -75,28 +89,28 @@ function testLLM(

expect(total.length).toBeGreaterThan(0);
return;
},
}),
timeout,
);

test(
"Complete works",
async () => {
retryOnce(async () => {
const completion = await llm.complete(
"Hi",
new AbortController().signal,
);

expect(completion.length).toBeGreaterThan(0);
return;
},
}),
timeout,
);

if (testFim) {
test(
"FIM works",
async () => {
retryOnce(async () => {
let total = "";
for await (const chunk of llm.streamFim(
"Hi",
Expand All @@ -108,15 +122,15 @@ function testLLM(

expect(total.length).toBeGreaterThan(0);
return;
},
}),
timeout,
);
}

if (testToolCall) {
test(
"Tool Call works",
async () => {
retryOnce(async () => {
let args = "";
let isFirstChunk = true;
for await (const chunk of llm.streamChat(
Expand Down Expand Up @@ -175,7 +189,7 @@ function testLLM(

const parsedArgs = JSON.parse(args);
expect(parsedArgs.name).toBe("Nate");
},
}),
timeout,
);
}
Expand Down
Loading