mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-24 16:42:24 +02:00
Rename Context channel
This commit is contained in:
@@ -28,7 +28,7 @@ Channels are used to communicate between chains. Each channel has a value type,
|
||||
- `Channels.Archive`: stores a persistent sequence of values sent to the channel, useful for accumulating data over multiple steps
|
||||
- `Channels.UniqueArchive`: same as Archive, but deduplicates values sent to the channel
|
||||
- `Channels.BinaryOperatorAggregate`: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps. eg. `total = Channels.BinaryOperatorAggregate(int, operator.add)`
|
||||
- `Channels.ContextManager`: exposes the value of a context manager, managing its lifecycle. Useful for accessing external resources that require setup and/or teardown. eg. `client = Channels.ContextManager(httpx.Client)`
|
||||
- `Channels.Context`: exposes the value of a context manager, managing its lifecycle. Useful for accessing external resources that require setup and/or teardown. eg. `client = Channels.Context(httpx.Client)`
|
||||
|
||||
### Chains
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ def recursive_web_loader(
|
||||
"next_urls": Channels.UniqueInbox(str),
|
||||
"documents": Channels.Archive(Document),
|
||||
"visited": Channels.UniqueArchive(str),
|
||||
"client": Channels.ContextManager(httpx_client, httpx_aclient),
|
||||
"client": Channels.Context(httpx_client, httpx_aclient),
|
||||
}
|
||||
# the main chain that gets executed recursively
|
||||
visitor = (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from permchain.channels.archive import Archive, UniqueArchive
|
||||
from permchain.channels.binop import BinaryOperatorAggregate
|
||||
from permchain.channels.context import ContextManager
|
||||
from permchain.channels.context import Context
|
||||
from permchain.channels.inbox import Inbox, UniqueInbox
|
||||
from permchain.channels.last_value import LastValue
|
||||
|
||||
@@ -11,5 +11,5 @@ __all__ = [
|
||||
"Archive",
|
||||
"UniqueArchive",
|
||||
"BinaryOperatorAggregate",
|
||||
"ContextManager",
|
||||
"Context",
|
||||
]
|
||||
|
||||
@@ -4,13 +4,13 @@ from typing import (
|
||||
AsyncContextManager,
|
||||
AsyncGenerator,
|
||||
Callable,
|
||||
ContextManager,
|
||||
Generator,
|
||||
Generic,
|
||||
Optional,
|
||||
Sequence,
|
||||
Type,
|
||||
)
|
||||
from typing import ContextManager as ContextManagerType
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
@@ -22,7 +22,7 @@ from permchain.channels.base import (
|
||||
)
|
||||
|
||||
|
||||
class ContextManager(Generic[Value], BaseChannel[Value, None]):
|
||||
class Context(Generic[Value], BaseChannel[Value, None]):
|
||||
"""Exposes the value of a context manager, for the duration of an invocation.
|
||||
Context manager is entered before the first step, and exited after the last step.
|
||||
Optionally, provide an equivalent async context manager, which will be used
|
||||
@@ -31,7 +31,7 @@ class ContextManager(Generic[Value], BaseChannel[Value, None]):
|
||||
```python
|
||||
import httpx
|
||||
|
||||
client = Channels.ContextManager(httpx.Client, httpx.AsyncClient)
|
||||
client = Channels.Context(httpx.Client, httpx.AsyncClient)
|
||||
```
|
||||
"""
|
||||
|
||||
@@ -39,7 +39,7 @@ class ContextManager(Generic[Value], BaseChannel[Value, None]):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ctx: Optional[Callable[[], ContextManagerType[Value]]] = None,
|
||||
ctx: Optional[Callable[[], ContextManager[Value]]] = None,
|
||||
actx: Optional[Callable[[], AsyncContextManager[Value]]] = None,
|
||||
typ: Optional[Type[Value]] = None,
|
||||
) -> None:
|
||||
|
||||
@@ -135,7 +135,7 @@ def test_ctx_manager(mocker: MockerFixture) -> None:
|
||||
finally:
|
||||
cleanup()
|
||||
|
||||
with Channels.ContextManager(an_int, None, int).empty() as channel:
|
||||
with Channels.Context(an_int, None, int).empty() as channel:
|
||||
assert setup.call_count == 1
|
||||
assert cleanup.call_count == 0
|
||||
|
||||
@@ -153,7 +153,7 @@ def test_ctx_manager(mocker: MockerFixture) -> None:
|
||||
|
||||
|
||||
def test_ctx_manager_ctx(mocker: MockerFixture) -> None:
|
||||
with Channels.ContextManager(httpx.Client).empty() as channel:
|
||||
with Channels.Context(httpx.Client).empty() as channel:
|
||||
assert channel.ValueType is httpx.Client
|
||||
with pytest.raises(InvalidUpdateError):
|
||||
assert channel.UpdateType is None
|
||||
@@ -183,7 +183,7 @@ async def test_ctx_manager_async(mocker: MockerFixture) -> None:
|
||||
finally:
|
||||
cleanup()
|
||||
|
||||
async with Channels.ContextManager(an_int_sync, an_int, int).aempty() as channel:
|
||||
async with Channels.Context(an_int_sync, an_int, int).aempty() as channel:
|
||||
assert setup.call_count == 1
|
||||
assert cleanup.call_count == 0
|
||||
|
||||
|
||||
@@ -426,7 +426,7 @@ def test_channel_enter_exit_timing(mocker: MockerFixture) -> None:
|
||||
"input": Channels.LastValue(int),
|
||||
"output": Channels.LastValue(int),
|
||||
"inbox": Channels.Inbox(int),
|
||||
"ctx": Channels.ContextManager(an_int, typ=int),
|
||||
"ctx": Channels.Context(an_int, typ=int),
|
||||
},
|
||||
input="input",
|
||||
output=["inbox", "output"],
|
||||
|
||||
@@ -427,7 +427,7 @@ async def test_channel_enter_exit_timing(mocker: MockerFixture) -> None:
|
||||
"input": Channels.LastValue(int),
|
||||
"output": Channels.LastValue(int),
|
||||
"inbox": Channels.Inbox(int),
|
||||
"ctx": Channels.ContextManager(an_int, an_int_async, typ=int),
|
||||
"ctx": Channels.Context(an_int, an_int_async, typ=int),
|
||||
},
|
||||
input="input",
|
||||
output=["inbox", "output"],
|
||||
|
||||
Reference in New Issue
Block a user