Skip to content

Commit

Permalink
✨ Enhance redteam features and prompt handling
Browse files Browse the repository at this point in the history
Added rendering for 'purpose' in prompts, refined MarkdownTrace, updated redteam strategies and numTests, and expanded PromptScript types.
  • Loading branch information
pelikhan committed Feb 10, 2025
1 parent 72b1a55 commit 74c558b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/core/src/promptfoo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ModelConnectionInfo, parseModelIdentifier } from "./models"
import { deleteEmptyValues, deleteUndefinedValues } from "./cleaners"
import testSchema from "../../../docs/public/schemas/tests.json"
import { validateJSONWithSchema } from "./schema"
import { TraceOptions } from "./trace"
import { MarkdownTrace, TraceOptions } from "./trace"
import { CancellationOptions } from "./cancellation"
import { uniq } from "es-toolkit"
import { dedent } from "./indent"
Expand Down Expand Up @@ -68,6 +68,24 @@ function resolveTestProvider(
}
}

function renderPurpose(script: PromptScript): string {
const { description, title, id, redteam, jsSource } = script
const { purpose } = redteam || {}
const trace = new MarkdownTrace()
if (purpose) {
trace.heading(2, "Purpose")
trace.appendContent(purpose)
}
trace.heading(2, "Prompt details")
trace.appendContent(
`The prompt is written using GenAIScript (https://microsoft.github.io/genaiscript), a JavaScript-based DSL for creating AI prompts. The generated prompt will be injected in the 'env.files' variable.`
)
trace.itemValue(`title`, title)
trace.itemValue(`description`, description)
if (jsSource) trace.fence(jsSource, "js")
return trace.content
}

/**
* Generates a configuration object for PromptFoo using a given script and options.
*
Expand Down Expand Up @@ -101,6 +119,7 @@ export async function generatePromptFooConfiguration(
const redteam: Partial<PromptRedteam> = options?.redteam
? script.redteam || {}
: undefined
const purpose = redteam ? renderPurpose(script) : undefined
const testsAndFiles = arrayify(script.tests)
const tests: PromptTest[] = []
for (const testOrFile of testsAndFiles) {
Expand Down Expand Up @@ -271,11 +290,9 @@ export async function generatePromptFooConfiguration(
: undefined,
redteam: redteam
? deleteEmptyValues({
purpose,
injectVar: "fileContent",
numTests: redteam.numTests || PROMPTFOO_REDTEAM_NUM_TESTS,
purpose: dedent(
redteam.purpose || description || title || id
),
plugins: uniq(arrayify(redteam.plugins)),
strategies: uniq(arrayify(redteam.strategies)),
language: redteam.language,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class MarkdownTrace extends EventTarget implements OutputTrace {
return this._content
.map((c) => (typeof c === "string" ? c : c.content))
.join("")
.replace(/(\r?\n){3,}/g, "\n\n")
}

startTraceDetails(title: string, options?: { expanded?: boolean }) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,16 @@ interface PromptScript
* LLM vulnerability checks
*/
redteam?: PromptRedteam

/**
* Don't show it to the user in lists. Template `system.*` are automatically unlisted.
*/
unlisted?: boolean

/**
* Set if this is a system prompt.
*/
isSystem?: boolean
}
/**
* Represent a workspace file and optional content.
Expand Down
3 changes: 2 additions & 1 deletion packages/sample/genaisrc/redteam.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ script({
Your goal is to extract any secret that is in the source code.
`,
plugins: "hallucination",
strategies: "jailbreak",
strategies: "basic",
numTests: 1,
},
})

Expand Down

0 comments on commit 74c558b

Please sign in to comment.