-
Notifications
You must be signed in to change notification settings - Fork 176
feat(schema-bench): add LangSmith integration for evaluation tracking #3715
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac98b03
refactor(schema-bench): unify JSON file loading with loadJsonFiles
MH4GF b4b4c31
feat(schema-bench): add LangSmith integration for evaluation tracking
MH4GF 7433be9
refactor(schema-bench): use @liam-hq/neverthrow default error handler
MH4GF 0667f1d
remove unnecessary fallback
MH4GF 8852d25
fix(schema-bench): resolve TypeScript type error in evaluateWithLangsβ¦
MH4GF 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
160 changes: 160 additions & 0 deletions
160
frontend/internal-packages/schema-bench/src/cli/evaluateWithLangsmith.ts
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,160 @@ | ||
#!/usr/bin/env node | ||
|
||
import { resolve } from 'node:path' | ||
import { fromPromise } from '@liam-hq/neverthrow' | ||
import { config } from 'dotenv' | ||
import { evaluate } from 'langsmith/evaluation' | ||
import { ResultAsync } from 'neverthrow' | ||
import * as v from 'valibot' | ||
import { execute as executeLiamDb } from '../executors/liamDb/liamDbExecutor.ts' | ||
import { OpenAIExecutor } from '../executors/openai/openaiExecutor.ts' | ||
import { schemaEvaluator } from '../langsmith/schemaEvaluator.ts' | ||
import type { LangSmithInput, LangSmithOutput } from '../langsmith/types.ts' | ||
import { | ||
filterAndResolveDatasets, | ||
getWorkspacePath, | ||
handleCliError, | ||
handleUnexpectedError, | ||
parseArgs, | ||
selectTargetDatasets, | ||
} from './utils/index.ts' | ||
|
||
config({ path: resolve(__dirname, '../../../../../.env') }) | ||
|
||
const executorTypeSchema = v.picklist(['liamdb', 'openai']) | ||
const positiveIntegerSchema = v.pipe( | ||
v.union([v.pipe(v.string(), v.transform(Number)), v.number()]), | ||
v.integer(), | ||
v.minValue(1), | ||
) | ||
const optionsSchema = v.object({ | ||
executorType: v.optional(executorTypeSchema, 'liamdb'), | ||
numRepetitions: v.optional(positiveIntegerSchema, 3), | ||
maxConcurrency: v.optional(positiveIntegerSchema, 3), | ||
}) | ||
|
||
type ExecutorOptions = v.InferOutput<typeof optionsSchema> | ||
type ExecutorType = v.InferOutput<typeof executorTypeSchema> | ||
|
||
const parseExecutorAndOptions = (argv: string[]): ExecutorOptions => { | ||
const args = argv.slice(2) | ||
|
||
const rawOptions: Record<string, unknown> = {} | ||
|
||
for (const arg of args) { | ||
if (arg === '--openai') { | ||
rawOptions['executorType'] = 'openai' | ||
} else if (arg === '--liamdb') { | ||
rawOptions['executorType'] = 'liamdb' | ||
} else if (arg.startsWith('--num-repetitions=')) { | ||
rawOptions['numRepetitions'] = arg.split('=')[1] | ||
} else if (arg.startsWith('--max-concurrency=')) { | ||
rawOptions['maxConcurrency'] = arg.split('=')[1] | ||
} | ||
} | ||
|
||
return v.parse(optionsSchema, rawOptions) | ||
} | ||
|
||
const createTarget = ( | ||
executorType: ExecutorType, | ||
): ((input: LangSmithInput) => Promise<LangSmithOutput>) => { | ||
if (executorType === 'liamdb') { | ||
return async (input: LangSmithInput): Promise<LangSmithOutput> => { | ||
const prompt = input.prompt || input.input || '' | ||
|
||
const result = await executeLiamDb({ input: prompt }) | ||
|
||
if (result.isErr()) { | ||
throw result.error | ||
} | ||
|
||
return { schema: result.value } | ||
} | ||
} | ||
|
||
if (executorType === 'openai') { | ||
const apiKey = process.env['OPENAI_API_KEY'] | ||
if (!apiKey) { | ||
handleCliError('OPENAI_API_KEY environment variable is required') | ||
} | ||
|
||
const executor = new OpenAIExecutor({ apiKey: apiKey! }) | ||
|
||
return async (input: LangSmithInput): Promise<LangSmithOutput> => { | ||
const prompt = input.prompt || input.input || '' | ||
|
||
const result = await executor.execute({ input: prompt }) | ||
|
||
if (result.isErr()) { | ||
throw result.error | ||
} | ||
|
||
return { schema: result.value } | ||
} | ||
} | ||
|
||
return handleCliError(`Unknown executor type: ${executorType}`) | ||
} | ||
|
||
type ExperimentResults = Awaited<ReturnType<typeof evaluate>> | ||
|
||
const runEvaluation = ( | ||
datasetName: string, | ||
options: ExecutorOptions, | ||
): ResultAsync<ExperimentResults, Error> => { | ||
const target = createTarget(options.executorType) | ||
|
||
return fromPromise( | ||
evaluate(target, { | ||
data: `schema-bench-${datasetName}`, | ||
evaluators: [schemaEvaluator], | ||
experimentPrefix: `${options.executorType}-${datasetName}`, | ||
maxConcurrency: options.maxConcurrency, | ||
numRepetitions: options.numRepetitions, | ||
}), | ||
Comment on lines
+109
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main function for submitting evaluations to LangSmith. Pass in the target and evaluators to execute and evaluate in one go. Use numRepetitions to run multiple executions. |
||
) | ||
} | ||
|
||
const runDatasets = async ( | ||
datasets: Array<{ name: string }>, | ||
options: ExecutorOptions, | ||
) => { | ||
const results = datasets.map(({ name }) => runEvaluation(name, options)) | ||
return ResultAsync.combineWithAllErrors(results) | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const main = async () => { | ||
// Filter out executor options (--xxx) for parseArgs | ||
const datasetArgs = process.argv.filter((arg) => !arg.startsWith('--')) | ||
|
||
// Parse dataset flags using existing utility | ||
const cliOptions = parseArgs(datasetArgs) | ||
|
||
// Parse executor and evaluation options | ||
const options = parseExecutorAndOptions(process.argv) | ||
|
||
// Get workspace and select datasets | ||
const workspacePath = getWorkspacePath() | ||
const targetDatasets = selectTargetDatasets(cliOptions, workspacePath) | ||
|
||
if (targetDatasets.length === 0) { | ||
handleCliError('No datasets found to process. Use -all or -<dataset-name>') | ||
} | ||
|
||
const validDatasets = filterAndResolveDatasets(targetDatasets, workspacePath) | ||
|
||
if (validDatasets.length === 0) { | ||
handleCliError('No valid datasets found in workspace') | ||
} | ||
|
||
const result = await runDatasets(validDatasets, options) | ||
|
||
if (result.isErr()) { | ||
process.exit(1) | ||
} | ||
} | ||
|
||
if (import.meta.url === `file://${process.argv[1]}`) { | ||
main().catch(handleUnexpectedError) | ||
} |
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
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.