ConditionalPromptSelector constructor

const ConditionalPromptSelector({
  1. required BasePromptTemplate defaultPrompt,
  2. List<PromptCondition> conditionals = const [],
})

Prompt collection that goes through conditionals to select the appropriate prompt template.

You can use this to select a prompt template based on the type of language model (LLM vs. ChatModel) or the specific model used (e.g. GPT-4 vs Gemini Pro).

Example: Selecting a prompt based on the type of language model.

final prompt = PromptTemplate.fromTemplate('''
Use the following pieces of context to answer the question at the end.
{context}
Question: {question}
Helpful Answer:
''');
final chatPrompt = ChatPromptTemplate.fromTemplates([
  (ChatMessageRole.system, 'Use the following pieces of context to answer the users question.\n\n{context}'),
  (ChatMessageRole.human, '{question}'),
]);
final promptSelector = ConditionalPromptSelector(
  defaultPrompt: prompt,
  conditionals: [PromptCondition.isChatModel(chatPrompt)],
);
final prompt = promptSelector.getPrompt(llm);

Implementation

const ConditionalPromptSelector({
  required this.defaultPrompt,
  this.conditionals = const [],
});