represents a prompt generator that uses the function f.
LLMPromptGenerator[f,inputspec]
provides the specified inputspec to f.
LLMPromptGenerator
represents a prompt generator that uses the function f.
LLMPromptGenerator[f,inputspec]
provides the specified inputspec to f.
Details
- LLMPromptGenerator is used to add message-dependent context to an LLM prompt.
- LLMPromptGenerator can be used in retrieval-augmented generation (RAG) workflows to construct prompts dynamically using retrieved documents, semantic indexes or other external knowledge sources.
- Possible values for inputspec are:
-
"Input" the last user input (default) "Messages" the list of messages "LLMEvaluator" the current LLMConfiguration[…] "ChatObject" the whole conversation as ChatObject[…] {spec1,…} an association of inputs - The result of f has to be a valid prompt. Possible values include:
-
"text" static text LLMPrompt["name"] a repository prompt StringTemplate[…] templated text TemplateObject[…] template for creating a prompt Image[…] an image SemanticSearchIndex[…] a semantic search index {prompt1,…} a list of prompts - Template objects are automatically converted to strings via TemplateObject[…][].
- A prompt created with TemplateObject can contain text and images.
- Not every LLM supports image input.
- If the result of f is not a string, a list of strings or an image, it will be converted using TextString.
Examples
open all close allBasic Examples (1)
Define a prompt generator that always prepends the current evaluation date and time:
gen = LLMPromptGenerator[{"Current date/time: ", DateString[]}&]gen["hello there"]Use the generator in an interaction with an LLM:
LLMSynthesize["what day is today?", LLMEvaluator -> <|"Prompts" -> gen|>]Scope (5)
Function (3)
LLMSynthesize["hi", LLMEvaluator -> <|"Prompts" -> LLMPromptGenerator@LLMPrompt["R2D2"]|>]Use a function to define the tone of the answer at evaluation time based on the input:
gen = LLMPromptGenerator["Talk like a " <> First[StringCases[#, StartOfString ~~ "@" ~~ role : (LetterCharacter..) ~~ WordBoundary :> role], "pirate"]&]LLMSynthesize["@cowboy hi!", LLMEvaluator -> <|"Prompts" -> gen|>]Define a generator on a SemanticSearchIndex:
gen = LLMPromptGenerator@CreateSemanticSearchIndex[WikipediaData["light"]]Use it to add content semantically relevant to the query:
LLMSynthesize["What is the connection between light and Aphrodite in one sentence", LLMEvaluator -> <|"Prompts" -> gen|>]Compare with the uninformed LLM answer:
LLMSynthesize["What is the connection between light and Aphrodite in one sentence"]Input Specifications (2)
Define a generator that echoes the calling function input (default argument):
LLMSynthesize["this is the input", "PromptText", LLMEvaluator -> <|"Prompts" -> LLMPromptGenerator[Function[Echo[#]; "the prompt"]]|>]Give a custom input specification:
LLMSynthesize["This is the input", "PromptText", LLMEvaluator -> <|"Prompts" -> LLMPromptGenerator[Function[StringRiffle[{"* User input is: \"" <> ToString[#Input] <> "\"", "* Current LLM model is: " <> #LLMEvaluator["Model"]["Name"]}, "
"]], {"LLMEvaluator", "Input"}]|>]Possible Issues (1)
A list of prompts does not evaluate when applied to the input:
LLMPromptGenerator[{"Current date/time: ", DateString[]}]["content"]Wrap the list using TemplateObject or Function:
LLMSynthesize["content", "PromptText", LLMEvaluator -> {"Prompts" -> LLMPromptGenerator[TemplateObject[{"Current date/time: ", TemplateExpression@DateString[]}]]}]Related Guides
History
Text
Wolfram Research (2024), LLMPromptGenerator, Wolfram Language function, https://reference.wolfram.com/language/ref/LLMPromptGenerator.html.
CMS
Wolfram Language. 2024. "LLMPromptGenerator." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LLMPromptGenerator.html.
APA
Wolfram Language. (2024). LLMPromptGenerator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LLMPromptGenerator.html
BibTeX
@misc{reference.wolfram_2026_llmpromptgenerator, author="Wolfram Research", title="{LLMPromptGenerator}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/LLMPromptGenerator.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_llmpromptgenerator, organization={Wolfram Research}, title={LLMPromptGenerator}, year={2024}, url={https://reference.wolfram.com/language/ref/LLMPromptGenerator.html}, note=[Accessed: 15-June-2026]}