From 775167cba5808eddf66e46f46a5ab2726e476b05 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:25:25 -0800 Subject: [PATCH] Improve prompt presets and prompt var generation (#533) (#536) (cherry picked from commit 40966de77732dc75312ba44a2fe22f5aebc4f4b4) Signed-off-by: Tyler Ohlsen Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- common/constants.ts | 90 ++++++++++++++----- .../pages/workflow_detail/workflow_detail.tsx | 4 +- .../modals/configure_template_modal.tsx | 30 +++++++ .../new_workflow/quick_configure_modal.tsx | 4 +- 4 files changed, 100 insertions(+), 28 deletions(-) diff --git a/common/constants.ts b/common/constants.ts index 9e4453b4..d036669b 100644 --- a/common/constants.ts +++ b/common/constants.ts @@ -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 XML tags:\n\n\n\ +${parameters.' + + DEFAULT_PROMPT_RESULTS_FIELD + + ".toString()}\n\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 XML tags, and then answer the question:\ +\n\n\n\ +${parameters.' + + DEFAULT_PROMPT_RESULTS_FIELD + + '.toString()}\n\ +\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[]; diff --git a/public/pages/workflow_detail/workflow_detail.tsx b/public/pages/workflow_detail/workflow_detail.tsx index 2b201633..80c28ea9 100644 --- a/public/pages/workflow_detail/workflow_detail.tsx +++ b/public/pages/workflow_detail/workflow_detail.tsx @@ -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) ? ( ({ 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', @@ -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; +} diff --git a/public/pages/workflows/new_workflow/quick_configure_modal.tsx b/public/pages/workflows/new_workflow/quick_configure_modal.tsx index 44c46bc7..7949834c 100644 --- a/public/pages/workflows/new_workflow/quick_configure_modal.tsx +++ b/public/pages/workflows/new_workflow/quick_configure_modal.tsx @@ -34,7 +34,7 @@ import { OutputMapFormValue, PROCESSOR_TYPE, QuickConfigureFields, - SUMMARIZE_DOCS_PROMPT, + CLAUDE_SUMMARIZE_PROMPT, TEXT_FIELD_PATTERN, TRANSFORM_TYPE, VECTOR, @@ -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,