From 4748412bb774faa2982e48cd2ed8892c6f420852 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Thu, 22 Feb 2024 10:40:45 -0800 Subject: [PATCH 1/3] Allow manually specified client in run tree with no env vars --- js/src/run_trees.ts | 6 +++--- js/src/tests/run_trees.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 js/src/tests/run_trees.test.ts diff --git a/js/src/run_trees.ts b/js/src/run_trees.ts index f081593f1..d403fe2ee 100644 --- a/js/src/run_trees.ts +++ b/js/src/run_trees.ts @@ -62,7 +62,7 @@ export class RunTree implements BaseRun { dotted_order: string; constructor(config: RunTreeConfig) { - const defaultConfig = RunTree.getDefaultConfig(); + const defaultConfig = RunTree.getDefaultConfig(config.client); Object.assign(this, { ...defaultConfig, ...config }); if (!this.trace_id) { if (this.parent_run) { @@ -84,7 +84,7 @@ export class RunTree implements BaseRun { } } } - private static getDefaultConfig(): object { + private static getDefaultConfig(client?: Client): object { return { id: uuid.v4(), run_type: "chain", @@ -101,7 +101,7 @@ export class RunTree implements BaseRun { serialized: {}, inputs: {}, extra: {}, - client: new Client({}), + client: client ?? new Client({}), }; } diff --git a/js/src/tests/run_trees.test.ts b/js/src/tests/run_trees.test.ts new file mode 100644 index 000000000..2ffec4e41 --- /dev/null +++ b/js/src/tests/run_trees.test.ts @@ -0,0 +1,34 @@ +import { jest } from "@jest/globals"; +import { Client } from "../client.js"; +import { RunTree } from "../run_trees.js"; + +test.concurrent( + "Should work with manually set API key", + async () => { + const key = process.env.LANGCHAIN_API_KEY; + delete process.env.LANGCHAIN_API_KEY; + const langchainClient = new Client({ + autoBatchTracing: true, + callerOptions: { maxRetries: 0 }, + timeout_ms: 30_000, + apiKey: key, + }); + const callSpy = jest + .spyOn((langchainClient as any).caller, "call") + .mockResolvedValue({ + ok: true, + text: () => "", + }); + const projectName = "__test_persist_update_run_tree"; + const runTree = new RunTree({ + name: "Test Run Tree", + inputs: { input: "foo1" }, + client: langchainClient, + project_name: projectName, + }); + await runTree.postRun(); + await new Promise((resolve) => setTimeout(resolve, 1000)); + expect(callSpy).toHaveBeenCalled(); + }, + 180_000 +); From dca34d233fd0792a2c910e2fe32fe909efcfae97 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Thu, 22 Feb 2024 10:43:55 -0800 Subject: [PATCH 2/3] Fix lint --- js/src/tests/run_trees.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/src/tests/run_trees.test.ts b/js/src/tests/run_trees.test.ts index 2ffec4e41..813076e50 100644 --- a/js/src/tests/run_trees.test.ts +++ b/js/src/tests/run_trees.test.ts @@ -1,3 +1,5 @@ +/* eslint-disable no-process-env */ + import { jest } from "@jest/globals"; import { Client } from "../client.js"; import { RunTree } from "../run_trees.js"; From 69bfc582a50622b1e9c2a038f12b19312e17723a Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Thu, 22 Feb 2024 10:44:56 -0800 Subject: [PATCH 3/3] Remove constructor validation --- js/src/client.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/js/src/client.ts b/js/src/client.ts index 2d4e98717..aed82d159 100644 --- a/js/src/client.ts +++ b/js/src/client.ts @@ -294,7 +294,6 @@ export class Client { this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? ""; this.apiKey = trimQuotes(config.apiKey ?? defaultConfig.apiKey); this.webUrl = trimQuotes(config.webUrl ?? defaultConfig.webUrl); - this.validateApiKeyIfHosted(); this.timeout_ms = config.timeout_ms ?? 12_000; this.caller = new AsyncCaller(config.callerOptions ?? {}); this.hideInputs = config.hideInputs ?? defaultConfig.hideInputs; @@ -328,15 +327,6 @@ export class Client { }; } - private validateApiKeyIfHosted(): void { - const isLocal = isLocalhost(this.apiUrl); - if (!isLocal && !this.apiKey) { - throw new Error( - "API key must be provided when using hosted LangSmith API" - ); - } - } - private getHostUrl(): string { if (this.webUrl) { return this.webUrl;