Skip to content

Commit

Permalink
Improve prompt presets and prompt var generation (#533) (#536)
Browse files Browse the repository at this point in the history
(cherry picked from commit 40966de)

Signed-off-by: Tyler Ohlsen <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 7dc1d17 commit 775167c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 28 deletions.
90 changes: 66 additions & 24 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,43 +422,85 @@ export const QUERY_PRESETS = [
*/
export const DEFAULT_PROMPT_RESULTS_FIELD = 'results';
export const DEFAULT_PROMPT_QUESTION_FIELD = 'question';
export const DEFAULT_PROMPT_TEXT_CATEGORY_FIELD = 'textCategory';
export const DEFAULT_PROMPT_ROLE_FIELD = 'role';

/**
* PROMPT PRESETS
* PROMPT PRESETS. Based off of https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-templates-and-examples.html
*/
export const SUMMARIZE_DOCS_PROMPT =
"Human: You are a professional data analyst. \
You are given a list of document results. You will \
analyze the data and generate a human-readable summary of the results. If you don't \
know the answer, just say I don't know.\
\n\n Results: ${parameters." +
export const GENERAL_SUMMARIZE_PROMPT =
'Read the following text: \
\n\n${parameters.' +
DEFAULT_PROMPT_RESULTS_FIELD +
'.toString()} \
\n\n Human: Please summarize the results.\
\n\n Assistant:';

export const QA_WITH_DOCUMENTS_PROMPT =
"Human: You are a professional data analyst. \
You are given a list of document results, along with a question. You will \
analyze the results and generate a human-readable response to the question, \
based on the results. If you don't know the answer, just say I don't know.\
\n\n Results: ${parameters." +
\n\nSummarize the text in one sentence.';

export const GENERAL_QA_WITH_CONTEXT_PROMPT =
'Read the following text, and answer the question at the end: \
\n\n${parameters.' +
DEFAULT_PROMPT_RESULTS_FIELD +
'.toString()} \
\n\n Question: ${parameters.' +
\n\n${parameters.' +
DEFAULT_PROMPT_QUESTION_FIELD +
'.toString()} \
\n\n Human: Please answer the question using the provided results.\
\n\n Assistant:';
'.toString()}';

export const GENERAL_QA_NO_CONTEXT_PROMPT =
'Answer the following question: \
${parameters.' +
DEFAULT_PROMPT_QUESTION_FIELD +
'.toString()}';

export const GENERAL_TEXT_GENERATION_PROMPT =
'Please write a ${parameters.' +
DEFAULT_PROMPT_TEXT_CATEGORY_FIELD +
'.toString()} in the voice of ${parameters.' +
DEFAULT_PROMPT_ROLE_FIELD +
'.toString()}';

export const CLAUDE_SUMMARIZE_PROMPT =
'Human: read the following results inside the <text></text> XML tags:\n\n<text>\n\
${parameters.' +
DEFAULT_PROMPT_RESULTS_FIELD +
".toString()}\n</text>\n\n\
Summarize the above results in one sentence. If you don't know the answer, just \
say I don't know.\
\n\nAssistant:";

export const CLAUDE_QA_WITH_CONTEXT_PROMPT =
'Human: read the following results inside the <text></text> XML tags, and then answer the question:\
\n\n<text>\n\
${parameters.' +
DEFAULT_PROMPT_RESULTS_FIELD +
'.toString()}\n\
</text>\n\n' +
'${parameters.' +
DEFAULT_PROMPT_QUESTION_FIELD +
'.toString()}\n\nAssistant:';

export const PROMPT_PRESETS = [
{
name: 'Summarize documents',
prompt: SUMMARIZE_DOCS_PROMPT,
name: 'Summarize text',
prompt: GENERAL_SUMMARIZE_PROMPT,
},
{
name: 'Question-answer, with context',
prompt: GENERAL_QA_WITH_CONTEXT_PROMPT,
},
{
name: 'Question-answer, without context',
prompt: GENERAL_QA_NO_CONTEXT_PROMPT,
},
{
name: 'Text generation',
prompt: GENERAL_TEXT_GENERATION_PROMPT,
},
{
name: 'Summarize text (Claude)',
prompt: CLAUDE_SUMMARIZE_PROMPT,
},
{
name: 'QA with documents',
prompt: QA_WITH_DOCUMENTS_PROMPT,
name: 'Question-answer, with context (Claude)',
prompt: CLAUDE_QA_WITH_CONTEXT_PROMPT,
},
] as PromptPreset[];

Expand Down
4 changes: 2 additions & 2 deletions public/pages/workflow_detail/workflow_detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export function WorkflowDetail(props: WorkflowDetailProps) {
}
}, [uiConfig]);

return errorMessage.includes(ERROR_GETTING_WORKFLOW_MSG) ||
errorMessage.includes(NO_TEMPLATES_FOUND_MSG) ? (
return errorMessage?.includes(ERROR_GETTING_WORKFLOW_MSG) ||
errorMessage?.includes(NO_TEMPLATES_FOUND_MSG) ? (
<EuiFlexGroup direction="column" alignItems="center">
<EuiFlexItem grow={3}>
<EuiEmptyPrompt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,24 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
items: PROMPT_PRESETS.map(
(preset: PromptPreset) => ({
name: preset.name,
// If a new prompt is selected, update the prompt,
// and reset any nested vars.
onClick: () => {
try {
formikProps.setFieldValue(
'value',
preset.prompt
);
formikProps.setFieldValue(
'nestedVars',
extractParametersFromTemplate(
preset.prompt
)
);
formikProps.setFieldTouched(
'nestedVars',
false
);
} catch {}
formikProps.setFieldTouched(
'value',
Expand Down Expand Up @@ -850,3 +862,21 @@ function injectValuesIntoTemplate(

return finalTemplate;
}

function extractParametersFromTemplate(template: string): ExpressionVar[] {
const expressionVars = [] as ExpressionVar[];
const paramsFound = template.match(/\${([^}]+)}/g);
paramsFound?.forEach((param) => {
const curExpressionNames = expressionVars.map(
(expressionVar) => expressionVar.name
);
const curExpressionName = param.slice(2, -1).split('.')[1];
if (!curExpressionNames.includes(curExpressionName)) {
expressionVars.push({
name: curExpressionName,
transform: '',
});
}
});
return expressionVars;
}
4 changes: 2 additions & 2 deletions public/pages/workflows/new_workflow/quick_configure_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
OutputMapFormValue,
PROCESSOR_TYPE,
QuickConfigureFields,
SUMMARIZE_DOCS_PROMPT,
CLAUDE_SUMMARIZE_PROMPT,
TEXT_FIELD_PATTERN,
TRANSFORM_TYPE,
VECTOR,
Expand Down Expand Up @@ -438,7 +438,7 @@ function updateRAGSearchResponseProcessors(
...inputMap[0],
value: {
transformType: TRANSFORM_TYPE.TEMPLATE,
value: SUMMARIZE_DOCS_PROMPT,
value: CLAUDE_SUMMARIZE_PROMPT,
nestedVars: [
{
name: DEFAULT_PROMPT_RESULTS_FIELD,
Expand Down

0 comments on commit 775167c

Please sign in to comment.