feat: add typed components; add typing to model output thunks and sampling results #300
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.
Addresses: #236 and #291
For quick understanding, I created a temporary example file in another branch that highlights most use cases for the typing / most scenarios: https://github.com/generative-computing/mellea/blob/jal/typing-example/typing-examples.py
Changes:
Makes Component, ModelOutputThunk, and SamplingResult into Generics. These class can now take a type and our code will understand the relationship between this Type
Tand all these classes when calling functions.Components also now specify a
parsefunction that takes a ModelOutputThunk and parses a value of type T from it. This value gets set as the parsed_repr for that ModelOutputThunk.The general flow is:
Things show as
Component[Any]orModelOutputThunk[Any]when used for parameters, meaning that by default, functions can take an type of Component / ModelOutputThunk without needing the Type [T] type hints to be added.Had to make changes to existing code:
There's some open questions about tool calling that I'm leaving for when we revamp that section of our code base. We may eventually need some generic parsing code that always runs for those cases.
In order to support typed Components, there's a couple of tradeoffs that must be made. Bolded options are ones I opted for.
Testing:
Added new tests and all current tests pass.
Examples:
Details