-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5474f39
commit 3f6e44a
Showing
12 changed files
with
149 additions
and
78 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
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
42 changes: 42 additions & 0 deletions
42
...ys-web-messaging-tester-cli/src/commands/aiTest/prompt/generation/promptGenerator.spec.ts
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,42 @@ | ||
import { AiScenarioFollowUpSection } from '../../testScript/modelTypes'; | ||
import { promptGenerator } from './promptGenerator'; | ||
|
||
test('Placeholders are replaced if value present', () => { | ||
const scenario: Pick<AiScenarioFollowUpSection, 'prompt' | 'placeholders'> = { | ||
placeholders: { | ||
FIRST_NAME: ['John'], | ||
SECOND_NAME: ['Doe'], | ||
}, | ||
prompt: 'Your first name is {FIRST_NAME} and your second name is {SECOND_NAME}', | ||
}; | ||
|
||
expect(promptGenerator(scenario)).toStrictEqual({ | ||
prompt: 'Your first name is John and your second name is Doe', | ||
placeholderValues: { FIRST_NAME: 'John', SECOND_NAME: 'Doe' }, | ||
}); | ||
}); | ||
|
||
test('Placeholders ignored if no values present', () => { | ||
const scenario: Pick<AiScenarioFollowUpSection, 'prompt' | 'placeholders'> = { | ||
placeholders: { | ||
FIRST_NAME: [], | ||
}, | ||
prompt: 'Your first name is {FIRST_NAME}', | ||
}; | ||
|
||
expect(promptGenerator(scenario)).toStrictEqual({ | ||
prompt: 'Your first name is {FIRST_NAME}', | ||
placeholderValues: {}, | ||
}); | ||
}); | ||
|
||
test('Original prompt returned if placeholder values not present', () => { | ||
const scenario: Pick<AiScenarioFollowUpSection, 'prompt' | 'placeholders'> = { | ||
prompt: 'Your first name is {FIRST_NAME}', | ||
}; | ||
|
||
expect(promptGenerator(scenario)).toStrictEqual({ | ||
prompt: 'Your first name is {FIRST_NAME}', | ||
placeholderValues: {}, | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
...genesys-web-messaging-tester-cli/src/commands/aiTest/prompt/generation/promptGenerator.ts
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,33 @@ | ||
import { AiScenarioFollowUpSection } from '../../testScript/modelTypes'; | ||
import { replacePlaceholders } from './replacePlaceholders'; | ||
|
||
export interface PromptGeneratorResult { | ||
placeholderValues: Record<string, string>; | ||
prompt: string; | ||
} | ||
|
||
export function promptGenerator( | ||
scenario: Pick<AiScenarioFollowUpSection, 'prompt' | 'placeholders'>, | ||
updatePrompt: typeof replacePlaceholders = replacePlaceholders, | ||
randomIndex = (max: number) => Math.floor(Math.random() * max), | ||
): PromptGeneratorResult { | ||
if (!scenario.placeholders) { | ||
return { | ||
placeholderValues: {}, | ||
prompt: scenario.prompt, | ||
}; | ||
} | ||
|
||
const chosenValues: Record<string, string> = Object.fromEntries( | ||
Object.entries(scenario.placeholders) | ||
.filter(([, values]) => values.length > 0) | ||
.map(([placeholder, values]) => { | ||
return [placeholder, values[randomIndex(values.length)]]; | ||
}), | ||
); | ||
|
||
return { | ||
placeholderValues: chosenValues, | ||
prompt: updatePrompt(scenario.prompt, chosenValues), | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
...eb-messaging-tester-cli/src/commands/aiTest/prompt/generation/replacePlaceholders.spec.ts
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,13 @@ | ||
import { replacePlaceholders } from './replacePlaceholders'; | ||
|
||
test('Placeholders are replaced', () => { | ||
expect( | ||
replacePlaceholders('{FIRST_NAME} {LAST_NAME} {ABC}', { FIRST_NAME: 'John', LAST_NAME: 'Doe' }), | ||
).toStrictEqual('John Doe {ABC}'); | ||
}); | ||
|
||
test('Prompt with missing placeholders are ignored', () => { | ||
expect(replacePlaceholders('{FIRST_NAME} {LAST_NAME}', { FIRST_NAME: 'John' })).toStrictEqual( | ||
'John {LAST_NAME}', | ||
); | ||
}); |
11 changes: 11 additions & 0 deletions
11
...sys-web-messaging-tester-cli/src/commands/aiTest/prompt/generation/replacePlaceholders.ts
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,11 @@ | ||
export function replacePlaceholders( | ||
prompt: string, | ||
placeholderValues: Record<string, string>, | ||
): string { | ||
return Object.entries(placeholderValues).reduce( | ||
(previousValue, [placeholderKey, placeholderValue]) => { | ||
return previousValue.replace(`{${placeholderKey}}`, placeholderValue); | ||
}, | ||
prompt, | ||
); | ||
} |
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