Source code for langchain.schema.output
from __future__ import annotations
from copy import deepcopy
from typing import Any, Dict, List, Optional
from uuid import UUID
from langchain.load.serializable import Serializable
from langchain.pydantic_v1 import BaseModel, root_validator
from langchain.schema.messages import BaseMessage, BaseMessageChunk
[docs]class Generation(Serializable):
    """A single text generation output."""
    text: str
    """Generated text output."""
    generation_info: Optional[Dict[str, Any]] = None
    """Raw response from the provider. May include things like the 
        reason for finishing or token log probabilities.
    """
    # TODO: add log probs as separate attribute
[docs]    @classmethod
    def is_lc_serializable(cls) -> bool:
        """Return whether this class is serializable."""
        return True
[docs]class GenerationChunk(Generation):
    """A Generation chunk, which can be concatenated with other Generation chunks."""
    def __add__(self, other: GenerationChunk) -> GenerationChunk:
        if isinstance(other, GenerationChunk):
            generation_info = (
                {**(self.generation_info or {}), **(other.generation_info or {})}
                if self.generation_info is not None or other.generation_info is not None
                else None
            )
            return GenerationChunk(
                text=self.text + other.text,
                generation_info=generation_info,
            )
        else:
            raise TypeError(
                f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'"
            )
[docs]class ChatGeneration(Generation):
    """A single chat generation output."""
    text: str = ""
    """*SHOULD NOT BE SET DIRECTLY* The text contents of the output message."""