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

Allow manually specified client in run tree with no env vars #463

Merged
merged 3 commits into from
Feb 22, 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
10 changes: 0 additions & 10 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions js/src/run_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand All @@ -101,7 +101,7 @@ export class RunTree implements BaseRun {
serialized: {},
inputs: {},
extra: {},
client: new Client({}),
client: client ?? new Client({}),
};
}

Expand Down
36 changes: 36 additions & 0 deletions js/src/tests/run_trees.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-process-env */

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
);
Loading