Skip to content

Commit

Permalink
[js] Support LangSmith-prefixed env vars (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Aug 19, 2024
1 parent e485d4a commit 179d606
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 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.41",
"version": "0.1.42",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "[email protected]",
"files": [
Expand Down
18 changes: 9 additions & 9 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import {
isLangChainMessage,
} from "./utils/messages.js";
import {
getEnvironmentVariable,
getLangChainEnvVarsMetadata,
getLangSmithEnvironmentVariable,
getRuntimeEnvironment,
} from "./utils/env.js";

Expand Down Expand Up @@ -286,16 +286,16 @@ async function mergeRuntimeEnvIntoRunCreates(runs: RunCreate[]) {
}

const getTracingSamplingRate = () => {
const samplingRateStr = getEnvironmentVariable(
"LANGCHAIN_TRACING_SAMPLING_RATE"
const samplingRateStr = getLangSmithEnvironmentVariable(
"TRACING_SAMPLING_RATE"
);
if (samplingRateStr === undefined) {
return undefined;
}
const samplingRate = parseFloat(samplingRateStr);
if (samplingRate < 0 || samplingRate > 1) {
throw new Error(
`LANGCHAIN_TRACING_SAMPLING_RATE must be between 0 and 1 if set. Got: ${samplingRate}`
`LANGSMITH_TRACING_SAMPLING_RATE must be between 0 and 1 if set. Got: ${samplingRate}`
);
}
return samplingRate;
Expand Down Expand Up @@ -463,14 +463,14 @@ export class Client {
hideInputs?: boolean;
hideOutputs?: boolean;
} {
const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
const apiKey = getLangSmithEnvironmentVariable("API_KEY");
const apiUrl =
getEnvironmentVariable("LANGCHAIN_ENDPOINT") ??
getLangSmithEnvironmentVariable("ENDPOINT") ??
"https://api.smith.langchain.com";
const hideInputs =
getEnvironmentVariable("LANGCHAIN_HIDE_INPUTS") === "true";
getLangSmithEnvironmentVariable("HIDE_INPUTS") === "true";
const hideOutputs =
getEnvironmentVariable("LANGCHAIN_HIDE_OUTPUTS") === "true";
getLangSmithEnvironmentVariable("HIDE_OUTPUTS") === "true";
return {
apiUrl: apiUrl,
apiKey: apiKey,
Expand Down Expand Up @@ -1017,7 +1017,7 @@ export class Client {
sessionId = projectOpts?.projectId;
} else {
const project = await this.readProject({
projectName: getEnvironmentVariable("LANGCHAIN_PROJECT") || "default",
projectName: getLangSmithEnvironmentVariable("PROJECT") || "default",
});
sessionId = project.id;
}
Expand Down
13 changes: 5 additions & 8 deletions js/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { getEnvironmentVariable } from "./utils/env.js";
import { getLangSmithEnvironmentVariable } from "./utils/env.js";

export const isTracingEnabled = (tracingEnabled?: boolean): boolean => {
if (tracingEnabled !== undefined) {
return tracingEnabled;
}
const envVars = [
"LANGSMITH_TRACING_V2",
"LANGCHAIN_TRACING_V2",
"LANGSMITH_TRACING",
"LANGCHAIN_TRACING",
];
return !!envVars.find((envVar) => getEnvironmentVariable(envVar) === "true");
const envVars = ["TRACING_V2", "TRACING"];
return !!envVars.find(
(envVar) => getLangSmithEnvironmentVariable(envVar) === "true"
);
};
2 changes: 1 addition & 1 deletion js/src/evaluation/_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ async function _forward(
if (!run) {
throw new Error(`Run not created by target function.
This is most likely due to tracing not being enabled.\n
Try setting "LANGCHAIN_TRACING_V2=true" in your environment.`);
Try setting "LANGSMITH_TRACING=true" in your environment.`);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export type {
export { RunTree, type RunTreeConfig } from "./run_trees.js";

// Update using yarn bump-version
export const __version__ = "0.1.41";
export const __version__ = "0.1.42";
39 changes: 17 additions & 22 deletions js/src/tests/evaluate.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { Example, Run, TracerSession } from "../schemas.js";
import { Client } from "../index.js";
import { afterAll, beforeAll } from "@jest/globals";
import { RunnableLambda, RunnableSequence } from "@langchain/core/runnables";

const TESTING_DATASET_NAME = "test_dataset_js_evaluate_123";
import { v4 as uuidv4 } from "uuid";
const TESTING_DATASET_NAME = `test_dataset_js_evaluate_${uuidv4()}`;
const TESTING_DATASET_NAME2 = `my_splits_ds_${uuidv4()}`;

beforeAll(async () => {
const client = new Client();
Expand Down Expand Up @@ -46,7 +47,6 @@ afterAll(async () => {

test("evaluate can evaluate", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -84,7 +84,6 @@ test("evaluate can evaluate", async () => {

test("evaluate can repeat", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -184,7 +183,6 @@ test("evaluate can evaluate with RunEvaluator evaluators", async () => {

test("evaluate can evaluate with custom evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -256,7 +254,6 @@ test("evaluate can evaluate with custom evaluators", async () => {

test("evaluate can evaluate with summary evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -314,7 +311,6 @@ test("evaluate can evaluate with summary evaluators", async () => {

test.skip("can iterate over evaluate results", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -343,7 +339,6 @@ test.skip("can iterate over evaluate results", async () => {

test("can pass multiple evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -391,7 +386,7 @@ test("can pass multiple evaluators", async () => {
test("split info saved correctly", async () => {
const client = new Client();
// create a new dataset
await client.createDataset("my_splits_ds2", {
await client.createDataset(TESTING_DATASET_NAME2, {
description:
"For testing purposed. Is created & deleted for each test run.",
});
Expand All @@ -400,21 +395,22 @@ test("split info saved correctly", async () => {
inputs: [{ input: 1 }, { input: 2 }, { input: 3 }],
outputs: [{ output: 2 }, { output: 3 }, { output: 4 }],
splits: [["test"], ["train"], ["validation", "test"]],
datasetName: "my_splits_ds2",
datasetName: TESTING_DATASET_NAME2,
});

const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
};
await evaluate(targetFunc, {
data: client.listExamples({ datasetName: "my_splits_ds2" }),
data: client.listExamples({ datasetName: TESTING_DATASET_NAME2 }),
description: "splits info saved correctly",
});

const exp = client.listProjects({ referenceDatasetName: "my_splits_ds2" });
const exp = client.listProjects({
referenceDatasetName: TESTING_DATASET_NAME2,
});
let myExp: TracerSession | null = null;
for await (const session of exp) {
myExp = session;
Expand All @@ -425,13 +421,15 @@ test("split info saved correctly", async () => {

await evaluate(targetFunc, {
data: client.listExamples({
datasetName: "my_splits_ds2",
datasetName: TESTING_DATASET_NAME2,
splits: ["test"],
}),
description: "splits info saved correctly",
});

const exp2 = client.listProjects({ referenceDatasetName: "my_splits_ds2" });
const exp2 = client.listProjects({
referenceDatasetName: TESTING_DATASET_NAME2,
});
let myExp2: TracerSession | null = null;
for await (const session of exp2) {
if (myExp2 === null || session.start_time > myExp2.start_time) {
Expand All @@ -445,13 +443,15 @@ test("split info saved correctly", async () => {

await evaluate(targetFunc, {
data: client.listExamples({
datasetName: "my_splits_ds2",
datasetName: TESTING_DATASET_NAME2,
splits: ["train"],
}),
description: "splits info saved correctly",
});

const exp3 = client.listProjects({ referenceDatasetName: "my_splits_ds2" });
const exp3 = client.listProjects({
referenceDatasetName: TESTING_DATASET_NAME2,
});
let myExp3: TracerSession | null = null;
for await (const session of exp3) {
if (myExp3 === null || session.start_time > myExp3.start_time) {
Expand All @@ -466,7 +466,6 @@ test("split info saved correctly", async () => {

test("can pass multiple summary evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -517,7 +516,6 @@ test("can pass AsyncIterable of Example's to evaluator instead of dataset name",
});

const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -551,7 +549,6 @@ test("can pass AsyncIterable of Example's to evaluator instead of dataset name",

test("max concurrency works with custom evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -587,7 +584,6 @@ test("max concurrency works with custom evaluators", async () => {

test("max concurrency works with summary evaluators", async () => {
const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down Expand Up @@ -704,7 +700,6 @@ test("evaluate can accept array of examples", async () => {
}

const targetFunc = (input: Record<string, any>) => {
console.log("__input__", input);
return {
foo: input.input + 1,
};
Expand Down
9 changes: 9 additions & 0 deletions js/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ export function getEnvironmentVariable(name: string): string | undefined {
}
}

export function getLangSmithEnvironmentVariable(
name: string
): string | undefined {
return (
getEnvironmentVariable(`LANGSMITH_${name}`) ||
getEnvironmentVariable(`LANGCHAIN_${name}`)
);
}

export function setEnvironmentVariable(name: string, value: string): void {
if (typeof process !== "undefined") {
// eslint-disable-next-line no-process-env
Expand Down

0 comments on commit 179d606

Please sign in to comment.