|
| 1 | +import { materializePrompt } from '~/services/inference/materialize-prompt'; |
| 2 | +import { getColumnCellByIdx, getRowCells } from '~/services/repository'; |
| 3 | +import type { Cell, Column, Dataset } from '~/state'; |
| 4 | +import { collectValidatedExamples } from '~/usecases/collect-examples'; |
| 5 | + |
| 6 | +export async function generateDatasetConfig(dataset: Dataset): Promise<{ |
| 7 | + columns: Record< |
| 8 | + string, |
| 9 | + { |
| 10 | + modelName?: string; |
| 11 | + modelProvider?: string; |
| 12 | + userPrompt?: string; |
| 13 | + prompt?: string; |
| 14 | + searchEnabled?: boolean; |
| 15 | + columnsReferences?: string[]; |
| 16 | + } |
| 17 | + >; |
| 18 | +}> { |
| 19 | + const columnConfigs: Record<string, any> = {}; |
| 20 | + |
| 21 | + for (const column of dataset.columns) { |
| 22 | + if (!column.process) continue; |
| 23 | + |
| 24 | + // Skip columns with empty model configuration |
| 25 | + if ( |
| 26 | + !column.process.modelName && |
| 27 | + !column.process.modelProvider && |
| 28 | + !column.process.prompt |
| 29 | + ) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + const prompt = await promptTemplateForColumn(column); |
| 34 | + |
| 35 | + columnConfigs[column.name] = { |
| 36 | + modelName: column.process.modelName, |
| 37 | + modelProvider: column.process.modelProvider, |
| 38 | + userPrompt: column.process.prompt, |
| 39 | + prompt, |
| 40 | + searchEnabled: column.process.searchEnabled, |
| 41 | + columnsReferences: column.process.columnsReferences?.map((colId) => { |
| 42 | + const refColumn = dataset.columns.find((c) => c.id === colId); |
| 43 | + return refColumn?.name || colId; |
| 44 | + }), |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + return { columns: columnConfigs }; |
| 49 | +} |
| 50 | + |
| 51 | +async function getFirstRowData(columnsReferences: string[]) { |
| 52 | + const firstRowCells = await getRowCells({ |
| 53 | + rowIdx: 0, |
| 54 | + columns: columnsReferences, |
| 55 | + }); |
| 56 | + return Object.fromEntries( |
| 57 | + firstRowCells.map((cell) => [cell.column!.name, cell.value]), |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +const promptTemplateForColumn = async ( |
| 62 | + column: Column, |
| 63 | +): Promise<string | undefined> => { |
| 64 | + const { process } = column; |
| 65 | + if (!process || !process.prompt) return undefined; |
| 66 | + |
| 67 | + if (column.type === 'image') { |
| 68 | + return undefined; // Image columns do not have prompt templates |
| 69 | + } |
| 70 | + |
| 71 | + // Fetch complete cell data for validated cells |
| 72 | + const validatedCells = await Promise.all( |
| 73 | + column.cells |
| 74 | + .filter((cell) => cell.validated) |
| 75 | + .map((cell) => |
| 76 | + getColumnCellByIdx({ |
| 77 | + idx: cell.idx, |
| 78 | + columnId: column.id, |
| 79 | + }), |
| 80 | + ), |
| 81 | + ); |
| 82 | + |
| 83 | + const examples = await collectValidatedExamples({ |
| 84 | + validatedCells: validatedCells.filter( |
| 85 | + (cell): cell is Cell => cell !== null, |
| 86 | + ), |
| 87 | + columnsReferences: process.columnsReferences, |
| 88 | + }); |
| 89 | + |
| 90 | + // Get data for prompt materialization |
| 91 | + const data: any | undefined = process.columnsReferences?.length |
| 92 | + ? await getFirstRowData(process.columnsReferences) |
| 93 | + : {}; |
| 94 | + |
| 95 | + // Replace each value in data with its key wrapped in {{}} |
| 96 | + for (const key of Object.keys(data)) { |
| 97 | + data[key] = `{{${key}}}`; |
| 98 | + } |
| 99 | + |
| 100 | + return materializePrompt({ |
| 101 | + instruction: process.prompt, |
| 102 | + data: data ?? undefined, |
| 103 | + examples: examples?.length ? examples : undefined, |
| 104 | + }); |
| 105 | +}; |
0 commit comments