forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for markdown prompts (promptfoo#1616)
- Loading branch information
Showing
8 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"Envar", | ||
"envars", | ||
"Evals", | ||
"globbed", | ||
"Groq", | ||
"mitigations", | ||
"openai", | ||
|
This file contains 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,5 @@ | ||
You're an angry pirate. | ||
|
||
Be concise and stay in character. | ||
|
||
Tell me about {{topic}} |
This file contains 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 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 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 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 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,12 @@ | ||
import fs from 'fs'; | ||
import type { Prompt } from '../../types'; | ||
|
||
export function processMarkdownFile(filePath: string, prompt: Partial<Prompt>): Prompt[] { | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
return [ | ||
{ | ||
raw: content, | ||
label: prompt.label || `${filePath}: ${content.slice(0, 50)}...`, | ||
}, | ||
]; | ||
} |
This file contains 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,61 @@ | ||
import * as fs from 'fs'; | ||
import { processMarkdownFile } from '../src/prompts/processors/markdown'; | ||
|
||
jest.mock('fs'); | ||
|
||
describe('processMarkdownFile', () => { | ||
const mockReadFileSync = jest.mocked(fs.readFileSync); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should process a valid Markdown file without a label', () => { | ||
const filePath = 'file.md'; | ||
const fileContent = '# Heading\n\nThis is some markdown content.'; | ||
mockReadFileSync.mockReturnValue(fileContent); | ||
expect(processMarkdownFile(filePath, {})).toEqual([ | ||
{ | ||
raw: fileContent, | ||
label: `${filePath}: # Heading\n\nThis is some markdown content....`, | ||
}, | ||
]); | ||
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8'); | ||
}); | ||
|
||
it('should process a valid Markdown file with a label', () => { | ||
const filePath = 'file.md'; | ||
const fileContent = '# Heading\n\nThis is some markdown content.'; | ||
mockReadFileSync.mockReturnValue(fileContent); | ||
expect(processMarkdownFile(filePath, { label: 'Custom Label' })).toEqual([ | ||
{ | ||
raw: fileContent, | ||
label: 'Custom Label', | ||
}, | ||
]); | ||
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8'); | ||
}); | ||
|
||
it('should truncate the label for long Markdown files', () => { | ||
const filePath = 'file.md'; | ||
const fileContent = '# ' + 'A'.repeat(100); | ||
mockReadFileSync.mockReturnValue(fileContent); | ||
expect(processMarkdownFile(filePath, {})).toEqual([ | ||
{ | ||
raw: fileContent, | ||
label: `${filePath}: # ${'A'.repeat(48)}...`, | ||
}, | ||
]); | ||
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8'); | ||
}); | ||
|
||
it('should throw an error if the file cannot be read', () => { | ||
const filePath = 'nonexistent.md'; | ||
mockReadFileSync.mockImplementation(() => { | ||
throw new Error('File not found'); | ||
}); | ||
|
||
expect(() => processMarkdownFile(filePath, {})).toThrow('File not found'); | ||
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8'); | ||
}); | ||
}); |