-
Notifications
You must be signed in to change notification settings - Fork 0
Extensions to graphcore #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from typing import Generic, TypeVar, Annotated, Any | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from langchain_core.tools import InjectedToolCallId | ||
| from langgraph.prebuilt import InjectedState | ||
| from langgraph.types import Command | ||
| from langchain_core.tools import StructuredTool, BaseTool | ||
|
|
||
| ST = TypeVar("ST") | ||
|
|
||
| T_RES = TypeVar("T_RES", bound=str | Command) | ||
|
|
||
| class WithInjectedState(BaseModel, Generic[ST]): | ||
| state: Annotated[ST, InjectedState] | ||
|
|
||
| class WithInjectedId(BaseModel): | ||
| tool_call_id: Annotated[str, InjectedToolCallId] | ||
|
|
||
| class WithImplementation(BaseModel, Generic[T_RES]): | ||
| def run(self) -> T_RES: | ||
| """Override this method to implement the tool logic.""" | ||
| raise NotImplementedError("Subclasses must implement run()") | ||
|
|
||
| @classmethod | ||
| def as_tool( | ||
| cls, | ||
| name: str | ||
| ) -> BaseTool: | ||
| impl_method = getattr(cls, "run") | ||
|
|
||
| # Simple wrapper - just accept kwargs, instantiate model, call method | ||
| def wrapper(**kwargs: Any) -> Any: | ||
| instance = cls(**kwargs) | ||
| return impl_method(instance) | ||
|
|
||
| return StructuredTool.from_function( | ||
| func=wrapper, | ||
| args_schema=cls, | ||
| description=cls.__doc__, | ||
| name=name, | ||
| ) | ||
|
|
||
| class WithAsyncImplementation(BaseModel, Generic[T_RES]): | ||
| async def run(self) -> T_RES: | ||
| """Override this method to implement the tool logic.""" | ||
| raise NotImplementedError("Subclasses must implement run()") | ||
|
|
||
| @classmethod | ||
| def as_tool( | ||
| cls, | ||
| name: str | ||
| ) -> BaseTool: | ||
| impl_method = getattr(cls, "run") | ||
|
|
||
| # Simple wrapper - just accept kwargs, instantiate model, call method | ||
| async def wrapper(**kwargs: Any) -> Any: | ||
| instance = cls(**kwargs) | ||
| d = await impl_method(instance) | ||
| return d | ||
|
|
||
| return StructuredTool.from_function( | ||
| coroutine=wrapper, | ||
| args_schema=cls, | ||
| description=cls.__doc__, | ||
| name=name, | ||
| ) | ||
|
|
||
|
|
||
| class InjectAll(WithInjectedState[ST], WithInjectedId): | ||
| pass |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that you plan to use this class as:
If that's the case,
new_instance_with_contextwould "inherit" the originalnew_instance's attributes by reference, due to_copy_{untyped,typed}_to_which instantiate an emptyBuilder()and populate it by reference. This pattern does not preserve immutability across instances, which is what you want I suppose, that is, I guess you wantnew_instance_with_contextto be a proxy ofnew_instance, with the former getting the attributes updates of the latter by reference.Stated differently, with your current pattern (simplified):
As a side note, with your current pattern, if you chain the methods "à la rust" like:
you'd instantiate 4 different objects, all of them eligible for GC, because in the end
buildonly returns a Tuple of different objects.My humble opinion is that you could get rid of the
_copy_*pattern by simply doing something like:if you don't need immutability. This would reduce the factory-like pattern boiler plate but also, more importantly, every time you add a new field, you don't need to remember to update these methods.
If instead you really intended to have immutable objects, then you could simply use
copyfor shallow copies or, better, dataclasses with.replace().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I do need immutability. there are many places where I build some "common" parameters of a builder and then use it multiple times to fill in the details. For example:
I then use
cvl_builderto build multiple workflows with different state types. Doing the changes by reference as you describe would be a disaster.The reason for doing the explicit copying is that I believe it makes the type system happy. if I do
copyorreplaceI don't think that let's me change the type parameters of the class I'm copying. I could always throw atype: ignoreon there, but I prefer to avoid that where possible. Am I wrong?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then in that case beware of the fact that Python objects (except for a few basic types such as strings and ints) are mutable by default. If a subset of builders inherit some attribute which is, say, dict-like or list-like, any modification to such attributes in
basic_builderwould propagate downstream (see my former example withbuilder1andbuilder2).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right. the only reference type I use is the tools list, which believe I am careful to copy.