ConditionalPromptSelector class

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);
Implemented types

Constructors

ConditionalPromptSelector({required BasePromptTemplate defaultPrompt, List<PromptCondition> conditionals = const []})
Prompt collection that goes through conditionals to select the appropriate prompt template.
const

Properties

conditionals List<PromptCondition>
Conditional prompts to use.
final
defaultPrompt BasePromptTemplate
Default prompt to use if no conditionals match.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getPrompt(BaseLanguageModel<Object, LanguageModelOptions, LanguageModelResult<Object>> llm) BasePromptTemplate
Get default prompt for a language model.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited