Skip to content

Commit

Permalink
[js] Support LangSmith-prefixed env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Aug 19, 2024
1 parent e485d4a commit 75f7db0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 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";
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 75f7db0

Please sign in to comment.