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

js[patch]: Fix listRuns limit arg #668

Merged
merged 4 commits into from
May 13, 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
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
},
"devDependencies": {
"@babel/preset-env": "^7.22.4",
"@faker-js/faker": "^8.4.1",
"@jest/globals": "^29.5.0",
"@langchain/core": "^0.1.32",
"@langchain/langgraph": "^0.0.8",
Expand Down
16 changes: 15 additions & 1 deletion js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,25 @@
is_root: isRoot,
};

let runsYielded = 0;
for await (const runs of this._getCursorPaginatedList<Run>(
"/runs/query",
body
)) {
yield* runs;
if (limit) {
if (runsYielded >= limit) {
break;
}
if (runs.length + runsYielded > limit) {
const newRuns = runs.slice(0, limit - runsYielded);
yield* newRuns;
break;
}
runsYielded += runs.length;
yield* runs;
} else {
yield* runs;
}
}
}

Expand Down Expand Up @@ -2482,7 +2496,7 @@
public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 2499 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<EvaluationResult[]> {
const results: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
Expand Down
51 changes: 50 additions & 1 deletion js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { FunctionMessage, HumanMessage } from "@langchain/core/messages";

import { Client } from "../client.js";
import { v4 as uuidv4 } from "uuid";
import { deleteDataset, deleteProject, toArray, waitUntil } from "./utils.js";
import {
createRunsFactory,
deleteDataset,
deleteProject,
toArray,
waitUntil,
} from "./utils.js";

type CheckOutputsType = boolean | ((run: Run) => boolean);
async function waitUntilRunFound(
Expand Down Expand Up @@ -558,3 +564,46 @@ test.concurrent(
},
180_000
);

test.concurrent("list runs limit arg works", async () => {
const client = new Client();

const projectName = "test-limit-runs-listRuns-endpoint";
const runsArr: Array<Run> = [];
const limit = 6;

try {
// create a fresh project with 10 runs --default amount created by createRunsFactory
await client.createProject({
projectName,
});
await Promise.all(createRunsFactory(projectName).map(client.createRun));

let iters = 0;
for await (const run of client.listRuns({ limit, projectName })) {
expect(run).toBeDefined();
runsArr.push(run);
iters += 1;
if (iters > limit) {
throw new Error(
`More runs returned than expected.\nExpected: ${limit}\nReceived: ${iters}`
);
}
}
} catch (e: any) {
// cleanup by deleting the project
const projectExists = await client.hasProject({ projectName });
if (projectExists) {
await client.deleteProject({ projectName });
}

// Error thrown by test, rethrow
if (e.message.startsWith("More runs returned than expected.")) {
throw e;
} else {
console.error(e);
}
}

expect(runsArr.length).toBe(limit);
});
27 changes: 27 additions & 0 deletions js/src/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Client } from "../client.js";
import { v4 as uuidv4 } from "uuid";
// eslint-disable-next-line import/no-extraneous-dependencies
import { faker } from "@faker-js/faker";
import { RunCreate } from "../schemas.js";

export async function toArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
const result: T[] = [];
Expand Down Expand Up @@ -112,3 +116,26 @@ export function sanitizePresignedUrls(payload: unknown) {
return value;
});
}

/**
* Factory which returns a list of `RunCreate` objects.
* @param {number} count Number of runs to create (default: 10)
* @returns {Array<RunCreate>} List of `RunCreate` objects
*/
export function createRunsFactory(
projectName: string,
count = 10
): Array<RunCreate> {
return Array.from({ length: count }).map((_, idx) => ({
id: uuidv4(),
name: `${idx}-${faker.lorem.words()}`,
run_type: faker.helpers.arrayElement(["tool", "chain", "llm", "runnable"]),
inputs: {
question: faker.lorem.sentence(),
},
outputs: {
answer: faker.lorem.sentence(),
},
project_name: projectName,
}));
}
5 changes: 5 additions & 0 deletions js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@
resolved "https://registry.npmjs.org/@eslint/js/-/js-8.41.0.tgz"
integrity sha512-LxcyMGxwmTh2lY9FwHPGWOHmYFCZvbrFCBZL4FzSSsxsRPuhrYUg/49/0KDfW8tnIEaEHtfmn6+NPN+1DqaNmA==

"@faker-js/faker@^8.4.1":
version "8.4.1"
resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-8.4.1.tgz#5d5e8aee8fce48f5e189bf730ebd1f758f491451"
integrity sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==

"@humanwhocodes/config-array@^0.11.8":
version "0.11.8"
resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz"
Expand Down
Loading