distill-pipeline / tests /try_generator_prompt.test.mjs
htaf's picture
added new instruct pipeline for faster generation
2739b3a
import { describe, it, expect } from 'vitest';
import { mkdtempSync, writeFileSync, mkdirSync } from 'fs';
import { tmpdir } from 'os';
import path from 'path';
import { execFileSync } from 'child_process';
import { chunkIdFromContent } from '../src/pipeline/ids.mjs';
describe('scripts/try_generator_prompt.sh', () => {
it('prints the generator response using cached chunk/question', () => {
const workdir = mkdtempSync(path.join(tmpdir(), 'try-gen-'));
const cacheDir = path.join(workdir, 'cache');
mkdirSync(cacheDir, { recursive: true });
const content = 'Chunk content for testing.';
const sourceId = 'source-1';
const chunkId = chunkIdFromContent(content, sourceId);
const questionsFile = path.join(cacheDir, 'questions.jsonl');
writeFileSync(
questionsFile,
JSON.stringify({
chunk_id: chunkId,
questions: ['What is being asked?'],
question_ids: ['q1'],
provider: 'mock',
model: 'mock',
ts: Date.now(),
}) + '\n',
'utf8',
);
const ragFile = path.join(workdir, 'rag.jsonl');
writeFileSync(
ragFile,
JSON.stringify({ id: sourceId, content }) + '\n',
'utf8',
);
const promptFile = path.join(workdir, 'prompt.txt');
writeFileSync(
promptFile,
'Q: {{QUESTION}}\nCTX: {{CONTEXT}}',
'utf8',
);
// Mock Ollama response via file://
const mockApiDir = path.join(workdir, 'mock-ollama', 'api');
mkdirSync(mockApiDir, { recursive: true });
const mockResponsePath = path.join(mockApiDir, 'generate');
writeFileSync(
mockResponsePath,
JSON.stringify({ response: 'mock generator answer' }),
'utf8',
);
const scriptPath = path.join(
path.dirname(new URL(import.meta.url).pathname),
'..',
'scripts',
'try_generator_prompt.sh',
);
const env = {
...process.env,
PIPELINE_CACHE_DIR: cacheDir,
RAG_CHUNKS_PATH: ragFile,
PROMPT_FILE: promptFile,
OLLAMA_URL: `file://${path.join(workdir, 'mock-ollama')}`,
GENERATOR_MODEL: 'mock-model',
};
let output;
try {
output = execFileSync('bash', [scriptPath], {
env,
encoding: 'utf8',
});
} catch (err) {
output = err?.stdout?.toString?.() || '';
}
expect(output).toContain('mock generator answer');
expect(output).toContain(chunkId);
});
});