Source code for langchain_core.output_parsers.transform

from __future__ import annotations

from typing import (
    TYPE_CHECKING,
    Any,
    AsyncIterator,
    Iterator,
    Optional,
    Union,
)

from langchain_core.messages import BaseMessage, BaseMessageChunk
from langchain_core.output_parsers.base import BaseOutputParser, T
from langchain_core.outputs import (
    ChatGeneration,
    ChatGenerationChunk,
    Generation,
    GenerationChunk,
)

if TYPE_CHECKING:
    from langchain_core.runnables import RunnableConfig


[docs]class BaseTransformOutputParser(BaseOutputParser[T]): """用于处理流式输入的输出解析器的基类。""" def _transform(self, input: Iterator[Union[str, BaseMessage]]) -> Iterator[T]: for chunk in input: if isinstance(chunk, BaseMessage): yield self.parse_result([ChatGeneration(message=chunk)]) else: yield self.parse_result([Generation(text=chunk)]) async def _atransform( self, input: AsyncIterator[Union[str, BaseMessage]] ) -> AsyncIterator[T]: async for chunk in input: if isinstance(chunk, BaseMessage): yield self.parse_result([ChatGeneration(message=chunk)]) else: yield self.parse_result([Generation(text=chunk)])
[docs] def transform( self, input: Iterator[Union[str, BaseMessage]], config: Optional[RunnableConfig] = None, **kwargs: Any, ) -> Iterator[T]: yield from self._transform_stream_with_config( input, self._transform, config, run_type="parser" )
[docs] async def atransform( self, input: AsyncIterator[Union[str, BaseMessage]], config: Optional[RunnableConfig] = None, **kwargs: Any, ) -> AsyncIterator[T]: async for chunk in self._atransform_stream_with_config( input, self._atransform, config, run_type="parser" ): yield chunk
[docs]class BaseCumulativeTransformOutputParser(BaseTransformOutputParser[T]): """用于处理流式输入的输出解析器的基类。""" diff: bool = False """在流模式下,是要产生先前和当前解析输出之间的差异,还是只产生当前解析输出。""" def _diff(self, prev: Optional[T], next: T) -> T: """将解析后的输出转换为diff格式。其语义取决于输出解析器。 """ raise NotImplementedError() def _transform(self, input: Iterator[Union[str, BaseMessage]]) -> Iterator[Any]: prev_parsed = None acc_gen = None for chunk in input: if isinstance(chunk, BaseMessageChunk): chunk_gen: Generation = ChatGenerationChunk(message=chunk) elif isinstance(chunk, BaseMessage): chunk_gen = ChatGenerationChunk( message=BaseMessageChunk(**chunk.dict()) ) else: chunk_gen = GenerationChunk(text=chunk) if acc_gen is None: acc_gen = chunk_gen else: acc_gen = acc_gen + chunk_gen parsed = self.parse_result([acc_gen], partial=True) if parsed is not None and parsed != prev_parsed: if self.diff: yield self._diff(prev_parsed, parsed) else: yield parsed prev_parsed = parsed async def _atransform( self, input: AsyncIterator[Union[str, BaseMessage]] ) -> AsyncIterator[T]: prev_parsed = None acc_gen = None async for chunk in input: if isinstance(chunk, BaseMessageChunk): chunk_gen: Generation = ChatGenerationChunk(message=chunk) elif isinstance(chunk, BaseMessage): chunk_gen = ChatGenerationChunk( message=BaseMessageChunk(**chunk.dict()) ) else: chunk_gen = GenerationChunk(text=chunk) if acc_gen is None: acc_gen = chunk_gen else: acc_gen = acc_gen + chunk_gen parsed = await self.aparse_result([acc_gen], partial=True) if parsed is not None and parsed != prev_parsed: if self.diff: yield self._diff(prev_parsed, parsed) else: yield parsed prev_parsed = parsed