Ideas for PromptTemplate
composition and type safety improvements
#5157
+118
−60
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a draft PR of some core improvements to typing of prompt templates. @functorism and I went down a rabbit hole trying to make prompt template input types inferred.
Background
I was trying to improve type safety when composing multiple prompts. For example I wanted to do something like:
Together w @functorism we created a class
ComposablePromptTemplate
that does this inference, which I'll gladly share later. Here's a simplifed version that paints the picture:Problem
The problem that I run into when using
ComposablePromptTemplate
is thatChatPromptTemplate
andSystemPromptTemplate
don't propagate inferred type parameters correctly. Similar lack of propagation has been acknowledge in other parts in this issue for example #4155Core parts of this PR
ChatPromptTemplate.fromTypedMessages
is a new better typed version ofChatPromptTemplate.fromMessages
that infers the ChatPromptTemplate RunInput based on the combined type of the messages' RunInputs. We couldn't update the existingfromMessages
function without breaking current usage, and therefore added this as a new function.new {System/Human/AI}PromptTemplate(prompt)
infers RunInput type from the child promptRunInput
defaulting toSymbol
inPromptTemplate
, which is used to infer if the PromptTemplate's type should be inferred using fstring generics or the explicit type, and tried to make that more explicit usingInputValues_FSTRING
. This is a vague suggestion and it might make sense for e.g. @jacoblee93 and @functorism to chat and share thoughts on typingsDisclaimer
Do note, this is very much a showcase PR. E.g.
InputValues_FSTRING
should probably not be in the file it's currently in.