-
Notifications
You must be signed in to change notification settings - Fork 239
chore(compass-assistant): add all eval cases, tags and CSV conversion script COMPASS-9823 COMPASS-9758 #7304
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
194f6c4
chore: add all eval cases and CSV conversion script
gagik 921ad45
chore: add tags
gagik 52cdb80
chore: fix tls typo
gagik af841c0
chore: share types
gagik 4cb7750
chore: fix check, combine all cases
gagik 2b8cc28
Merge branch 'main' of github.com:mongodb-js/compass into gagik/eval-…
gagik 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
| test/eval-cases/eval_cases.csv |
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
196 changes: 196 additions & 0 deletions
196
packages/compass-assistant/scripts/convert-csv-to-eval-cases.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,196 @@ | ||
| #!/usr/bin/env ts-node | ||
| /* eslint-disable no-console */ | ||
| // eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
| import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'; | ||
| // eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
| import { join, resolve } from 'path'; | ||
| import { parse } from '@fast-csv/parse'; | ||
|
|
||
| /** This is copied from the Compass Assistant PD Eval Cases */ | ||
| type CSVRow = { | ||
| 'Your Name': string; | ||
| 'Interaction Type\n(can add other types)': string; | ||
| 'Input\nHighlighting key: \nHardcoded\n\nContextual passed from client to assistant\n\nUser-entered': string; | ||
| 'Expected Output\n(target 100-200 words, okay to go over if needed)': string; | ||
| 'Expected Links\n(comma separated please)': string; | ||
| Notes: string; | ||
| }; | ||
|
|
||
| type SimpleEvalCase = { | ||
| name?: string; | ||
| input: string; | ||
| expected: string; | ||
| expectedSources?: string[]; | ||
| tags?: string[]; | ||
| }; | ||
|
|
||
| const interactionTypeTags = { | ||
| 'End-User Input Only': 'end-user-input', | ||
| 'Connection Error': 'connection-error', | ||
| 'DNS Error': 'dns-error', | ||
| 'Explain Plan': 'explain-plan', | ||
| 'Proactive Perf': 'proactive-performance-insights', | ||
| 'General network error': 'general-network-error', | ||
| OIDC: 'oidc', | ||
| TSL: 'tsl-ssl', | ||
| SSL: 'tsl-ssl', | ||
gagik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| function escapeString(str: string): string { | ||
| return str | ||
| .replace(/\\/g, '\\\\') | ||
| .replace(/`/g, '\\`') | ||
| .replace(/\${/g, '\\${') | ||
| .replace(/\r?\n/g, '\\n') // Handle newlines | ||
| .replace(/[\u200B-\u200D\uFEFF\u2028\u2029]/g, '') // Remove zero-width spaces and other invisible characters | ||
| .replace(/[^\S ]/g, ' ') // Replace all whitespace except normal spaces with spaces | ||
| .replace(/\s+/g, ' ') // Collapse multiple spaces | ||
| .trim(); // Remove leading/trailing whitespace | ||
| } | ||
|
|
||
| function generateEvalCaseFile(cases: SimpleEvalCase[]): string { | ||
| const caseDefinitions = cases | ||
| .map((evalCase) => { | ||
| const sourcesPart = | ||
| evalCase.expectedSources && evalCase.expectedSources.length > 0 | ||
| ? ` expectedSources: [\n ${evalCase.expectedSources | ||
| .map((source) => `'${escapeString(source)}'`) | ||
| .join(',\n ')},\n ],` | ||
| : ''; | ||
|
|
||
| const tagsPart = | ||
| evalCase.tags && evalCase.tags.length > 0 | ||
| ? ` tags: [\n ${evalCase.tags | ||
| .map((tag) => `'${escapeString(tag)}'`) | ||
| .join(',\n ')},\n ],` | ||
| : ''; | ||
|
|
||
| return ` { | ||
| input: \`${escapeString(evalCase.input)}\`, | ||
| expected: \`${escapeString(evalCase.expected)}\`,${ | ||
| sourcesPart ? '\n' + sourcesPart : '' | ||
| }${tagsPart ? '\n' + tagsPart : ''} | ||
| }`; | ||
| }) | ||
| .join(',\n'); | ||
|
|
||
| return `/** This file is auto-generated by the convert-csv-to-eval-cases script. | ||
| Do not modify this file manually. */ | ||
| import type { SimpleEvalCase } from '../assistant.eval'; | ||
|
|
||
| export const generatedEvalCases: SimpleEvalCase[] = [ | ||
| ${caseDefinitions}, | ||
| ]; | ||
| `; | ||
| } | ||
|
|
||
| async function convertCSVToEvalCases() { | ||
| const scriptDir = __dirname; | ||
| const csvFilePath = resolve(scriptDir, '../test/eval-cases/eval_cases.csv'); | ||
| // Check that the CSV file exists | ||
| if (!existsSync(csvFilePath)) { | ||
| console.error( | ||
| `The CSV file does not exist: ${csvFilePath}. Please import it and try again.` | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| const outputDir = resolve(scriptDir, '../test/eval-cases'); | ||
|
|
||
| console.log('Converting CSV to eval cases...'); | ||
| console.log(`Reading from: ${csvFilePath}`); | ||
| console.log(`Output directory: ${outputDir}`); | ||
|
|
||
| // Ensure output directory exists | ||
| mkdirSync(outputDir, { recursive: true }); | ||
|
|
||
| const allCases: SimpleEvalCase[] = []; | ||
|
|
||
| // Read and parse CSV using async/await | ||
| const csvContent = readFileSync(csvFilePath, 'utf8'); | ||
|
|
||
| const rows = await new Promise<CSVRow[]>((resolve, reject) => { | ||
| const results: CSVRow[] = []; | ||
| const stream = parse({ | ||
| headers: true, | ||
| }) | ||
| .on('data', (row: CSVRow) => results.push(row)) | ||
| .on('end', () => resolve(results)) | ||
| .on('error', reject); | ||
|
|
||
| stream.write(csvContent); | ||
| stream.end(); | ||
| }); | ||
|
|
||
| // Process rows | ||
| for (const row of rows) { | ||
| // Skip empty rows or header-like rows | ||
| const input = | ||
| row[ | ||
| 'Input\nHighlighting key: \nHardcoded\n\nContextual passed from client to assistant\n\nUser-entered' | ||
| ]?.trim(); | ||
| const expected = | ||
| row[ | ||
| 'Expected Output\n(target 100-200 words, okay to go over if needed)' | ||
| ]?.trim(); | ||
| const yourName = row['Your Name']?.trim(); | ||
| const interactionType = | ||
| row['Interaction Type\n(can add other types)']?.trim(); | ||
|
|
||
| if (!input || !expected || !yourName || !interactionType) { | ||
| continue; // Skip incomplete rows | ||
| } | ||
|
|
||
| // Parse expected sources | ||
| const expectedLinksRaw = | ||
| row['Expected Links\n(comma separated please)']?.trim(); | ||
| let expectedSources: string[] = []; | ||
|
|
||
| if (expectedLinksRaw) { | ||
| expectedSources = expectedLinksRaw | ||
| .replace(/\r?\n/g, ' ') // Replace newlines with spaces first | ||
| .split(',') | ||
| .map((link) => link.trim()) | ||
| .filter((link) => link && link.startsWith('http')); | ||
| } | ||
|
|
||
| const tags: string[] = []; | ||
|
|
||
| if (interactionType) { | ||
| for (const tag of Object.keys(interactionTypeTags)) { | ||
| if (interactionType.includes(tag)) { | ||
| tags.push( | ||
| interactionTypeTags[tag as keyof typeof interactionTypeTags] | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const evalCase: SimpleEvalCase = { | ||
| input, | ||
| expected, | ||
| ...(expectedSources.length > 0 && { expectedSources }), | ||
| ...(tags.length > 0 && { tags }), | ||
| }; | ||
|
|
||
| allCases.push(evalCase); | ||
| } | ||
|
|
||
| console.log(`\nProcessed ${allCases.length} cases`); | ||
|
|
||
| // Generate single file with all cases | ||
| const filename = 'generated-cases'; | ||
| const filepath = join(outputDir, `${filename}.ts`); | ||
| const content = generateEvalCaseFile(allCases); | ||
|
|
||
| writeFileSync(filepath, content, 'utf8'); | ||
| console.log(`✓ Generated ${filename}.ts with ${allCases.length} cases`); | ||
|
|
||
| console.log('\n✅ Conversion completed successfully!'); | ||
| } | ||
|
|
||
| convertCSVToEvalCases().catch((error) => { | ||
| console.error('❌ Conversion failed:', error); | ||
| process.exit(1); | ||
| }); | ||
|
|
||
| export { convertCSVToEvalCases }; | ||
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.