Skip to content
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

core[minor]: Add v2 implementation of astream events #21638

Merged
merged 34 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/core/langchain_core/language_models/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from langchain_core.prompt_values import ChatPromptValue, PromptValue, StringPromptValue
from langchain_core.pydantic_v1 import Field, root_validator
from langchain_core.runnables.config import ensure_config, run_in_executor
from langchain_core.tracers.log_stream import LogStreamCallbackHandler
from langchain_core.tracers._streaming import _StreamingCallbackHandler

if TYPE_CHECKING:
from langchain_core.pydantic_v1 import BaseModel
Expand Down Expand Up @@ -608,7 +608,7 @@ def _generate_with_cache(
(
True
for h in run_manager.handlers
if isinstance(h, LogStreamCallbackHandler)
if isinstance(h, _StreamingCallbackHandler)
),
False,
)
Expand Down Expand Up @@ -691,7 +691,7 @@ async def _agenerate_with_cache(
(
True
for h in run_manager.handlers
if isinstance(h, LogStreamCallbackHandler)
if isinstance(h, _StreamingCallbackHandler)
),
False,
)
Expand Down
243 changes: 55 additions & 188 deletions libs/core/langchain_core/runnables/base.py

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions libs/core/langchain_core/tracers/_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Internal tracers used for stream_log and astream events implementations."""
import abc
from typing import AsyncIterator, TypeVar
from uuid import UUID

T = TypeVar("T")


class _StreamingCallbackHandler(abc.ABC):
"""For internal use.

This is a common mixin that the callback handlers
for both astream events and astream log inherit from.

The `tap_output_aiter` method is invoked in some contexts
to produce callbacks for intermediate results.
"""

@abc.abstractmethod
def tap_output_aiter(
self, run_id: UUID, output: AsyncIterator[T]
) -> AsyncIterator[T]:
"""Used for internal astream_log and astream events implementations."""


__all__ = [
"_StreamingCallbackHandler",
]