-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(evals): add LangSmith gating helpers + config (no behavior change) #2351
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| let langSmithPromise: Promise<typeof import("langsmith")> | undefined; | ||
|
|
||
| export function hasLangSmithApiKey(): boolean { | ||
| return Boolean(process.env.LANGSMITH_API_KEY); | ||
| } | ||
|
|
||
| export function langSmithTracingEnabled(): boolean { | ||
| return hasLangSmithApiKey() && process.env.LANGSMITH_TRACING === "true"; | ||
| } | ||
|
|
||
| export function resolveTraceTransport(): "native" | "otel" { | ||
| return process.env.EVAL_TRACE_TRANSPORT === "otel" ? "otel" : "native"; | ||
| } | ||
|
|
||
| export function loadLangSmith(): Promise<typeof import("langsmith")> { | ||
| langSmithPromise ??= import("langsmith"); | ||
| return langSmithPromise; | ||
| } | ||
|
|
||
| export function assertLangSmithReady(): void { | ||
| if (!hasLangSmithApiKey()) { | ||
| throw new Error( | ||
| "LangSmith tracing was selected, but LANGSMITH_API_KEY is not set.", | ||
| ); | ||
| } | ||
| if (!langSmithTracingEnabled()) { | ||
| throw new Error( | ||
| 'LangSmith tracing was selected, but LANGSMITH_TRACING is not set to "true".', | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const LANGSMITH_ENV_KEYS = [ | ||
| "LANGSMITH_API_KEY", | ||
| "LANGSMITH_TRACING", | ||
| "EVAL_TRACE_TRANSPORT", | ||
| ] as const; | ||
|
|
||
| const savedEnv = new Map<string, string | undefined>(); | ||
|
|
||
| beforeEach(() => { | ||
| for (const key of LANGSMITH_ENV_KEYS) { | ||
| savedEnv.set(key, process.env[key]); | ||
| delete process.env[key]; | ||
| } | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| for (const key of LANGSMITH_ENV_KEYS) { | ||
| const value = savedEnv.get(key); | ||
| if (value === undefined) delete process.env[key]; | ||
| else process.env[key] = value; | ||
| } | ||
| savedEnv.clear(); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| async function loadLangSmithHelpers() { | ||
| return import("../../framework/langsmith.js"); | ||
| } | ||
|
|
||
| describe("hasLangSmithApiKey", () => { | ||
| it("returns false without LANGSMITH_API_KEY", async () => { | ||
| const { hasLangSmithApiKey } = await loadLangSmithHelpers(); | ||
| expect(hasLangSmithApiKey()).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true with LANGSMITH_API_KEY", async () => { | ||
| process.env.LANGSMITH_API_KEY = "ls-test"; | ||
| const { hasLangSmithApiKey } = await loadLangSmithHelpers(); | ||
| expect(hasLangSmithApiKey()).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("langSmithTracingEnabled", () => { | ||
| it.each([ | ||
| { key: true, flag: true, expected: true }, | ||
| { key: true, flag: false, expected: false }, | ||
| { key: false, flag: true, expected: false }, | ||
| { key: false, flag: false, expected: false }, | ||
| ])( | ||
| "returns $expected when key=$key and flag=$flag", | ||
| async ({ key, flag, expected }) => { | ||
| if (key) process.env.LANGSMITH_API_KEY = "ls-test"; | ||
| if (flag) process.env.LANGSMITH_TRACING = "true"; | ||
| const { langSmithTracingEnabled } = await loadLangSmithHelpers(); | ||
| expect(langSmithTracingEnabled()).toBe(expected); | ||
| }, | ||
| ); | ||
|
|
||
| it("reflects environment changes made after module import", async () => { | ||
| const { langSmithTracingEnabled } = await loadLangSmithHelpers(); | ||
| expect(langSmithTracingEnabled()).toBe(false); | ||
|
|
||
| process.env.LANGSMITH_API_KEY = "ls-test"; | ||
| process.env.LANGSMITH_TRACING = "true"; | ||
| expect(langSmithTracingEnabled()).toBe(true); | ||
|
|
||
| delete process.env.LANGSMITH_TRACING; | ||
| expect(langSmithTracingEnabled()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveTraceTransport", () => { | ||
| it("defaults to native", async () => { | ||
| const { resolveTraceTransport } = await loadLangSmithHelpers(); | ||
| expect(resolveTraceTransport()).toBe("native"); | ||
| }); | ||
|
|
||
| it('returns otel for the literal "otel" value', async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| const { resolveTraceTransport } = await loadLangSmithHelpers(); | ||
| expect(resolveTraceTransport()).toBe("otel"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("assertLangSmithReady", () => { | ||
| it("throws when tracing is enabled without an API key", async () => { | ||
| process.env.LANGSMITH_TRACING = "true"; | ||
| const { assertLangSmithReady } = await loadLangSmithHelpers(); | ||
| expect(() => assertLangSmithReady()).toThrow(/LANGSMITH_API_KEY/); | ||
| }); | ||
|
|
||
| it("throws when the API key is set but tracing is disabled", async () => { | ||
| process.env.LANGSMITH_API_KEY = "ls-test"; | ||
| const { assertLangSmithReady } = await loadLangSmithHelpers(); | ||
| expect(() => assertLangSmithReady()).toThrow(/LANGSMITH_TRACING/); | ||
| }); | ||
|
|
||
| it("does not throw when the API key and tracing flag are set", async () => { | ||
| process.env.LANGSMITH_API_KEY = "ls-test"; | ||
| process.env.LANGSMITH_TRACING = "true"; | ||
| const { assertLangSmithReady } = await loadLangSmithHelpers(); | ||
| expect(() => assertLangSmithReady()).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.