Skip to content

Commit

Permalink
[JS] Share client across run trees (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Sep 17, 2024
1 parent e32c3c1 commit 6fc09b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.1.56",
"version": "0.1.57",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "yarn@1.22.19",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export { RunTree, type RunTreeConfig } from "./run_trees.js";
export { overrideFetchImplementation } from "./singletons/fetch.js";

// Update using yarn bump-version
export const __version__ = "0.1.56";
export const __version__ = "0.1.57";
11 changes: 10 additions & 1 deletion js/src/run_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class Baggage {
}

export class RunTree implements BaseRun {
private static sharedClient: Client | null = null;

id: string;
name: RunTreeConfig["name"];
run_type: string;
Expand Down Expand Up @@ -173,7 +175,7 @@ export class RunTree implements BaseRun {
constructor(originalConfig: RunTreeConfig) {
const defaultConfig = RunTree.getDefaultConfig();
const { metadata, ...config } = originalConfig;
const client = config.client ?? new Client();
const client = config.client ?? RunTree.getSharedClient();
const dedupedMetadata = {
...metadata,
...config?.extra?.metadata,
Expand Down Expand Up @@ -226,6 +228,13 @@ export class RunTree implements BaseRun {
};
}

private static getSharedClient(): Client {
if (!RunTree.sharedClient) {
RunTree.sharedClient = new Client();
}
return RunTree.sharedClient;
}

public createChild(config: RunTreeConfig): RunTree {
const child_execution_order = this.child_execution_order + 1;

Expand Down
23 changes: 16 additions & 7 deletions js/src/tests/run_trees.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client } from "../client.js";
import * as uuid from "uuid";
import {
RunTree,
RunTreeConfig,
Expand All @@ -14,14 +15,8 @@ import {
test.concurrent(
"Test post and patch run",
async () => {
const projectName = `__test_run_tree`;
const projectName = `__test_run_tree_js ${uuid.v4()}`;
const langchainClient = new Client({ timeout_ms: 30000 });
try {
await langchainClient.readProject({ projectName });
await langchainClient.deleteProject({ projectName });
} catch (e) {
// Pass
}
const parentRunConfig: RunTreeConfig = {
name: "parent_run",
run_type: "chain",
Expand Down Expand Up @@ -113,6 +108,20 @@ test.concurrent(
runMap.get("parent_run")?.id
);
expect(runMap.get("parent_run")?.parent_run_id).toBeNull();
await waitUntil(
async () => {
try {
const runs_ = await toArray(
langchainClient.listRuns({ traceId: runs[0].trace_id })
);
return runs_.length === 5;
} catch (e) {
return false;
}
},
30_000, // Wait up to 30 seconds
3000 // every 3 second
);

const traceRunsIter = langchainClient.listRuns({
traceId: runs[0].trace_id,
Expand Down
9 changes: 9 additions & 0 deletions js/src/tests/run_trees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ test("distributed", () => {
"20210503T000000000001Z00000000-0000-0000-0000-00000000000.20210503T000001000002Z00000000-0000-0000-0000-00000000001",
});
});

test("shared client between run trees", () => {
const runTree1 = new RunTree({ name: "tree_1" });
const runTree2 = new RunTree({ name: "tree_2" });

expect(runTree1.client).toBeDefined();
expect(runTree2.client).toBeDefined();
expect(runTree1.client).toBe(runTree2.client);
});

0 comments on commit 6fc09b5

Please sign in to comment.