test_base_parsers.py — langchain Source File
Architecture documentation for test_base_parsers.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5ce0d7bb_e4d1_00fb_39b4_2709751dc986["test_base_parsers.py"] 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> 91721f45_4909_e489_8c1f_084f8bd87145 75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> 75137834_4ba7_dc43_7ec5_182c05eceedf ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> ba43b74d_3099_7e1c_aac3_cf594720469e d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> d758344f_537f_649e_f467_b9d7442e86df 83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"] 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58 style 5ce0d7bb_e4d1_00fb_39b4_2709751dc986 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Module to test base parser implementations."""
from typing_extensions import override
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import GenericFakeChatModel
from langchain_core.messages import AIMessage
from langchain_core.output_parsers import (
BaseGenerationOutputParser,
BaseTransformOutputParser,
)
from langchain_core.outputs import ChatGeneration, Generation
def test_base_generation_parser() -> None:
"""Test Base Generation Output Parser."""
class StrInvertCase(BaseGenerationOutputParser[str]):
"""An example parser that inverts the case of the characters in the message."""
@override
def parse_result(
self, result: list[Generation], *, partial: bool = False
) -> str:
"""Parse a list of model Generations into a specific format.
Args:
result: A list of `Generation` to be parsed. The Generations are assumed
to be different candidate outputs for a single model input.
Many parsers assume that only a single generation is passed it in.
We will assert for that
partial: Whether to allow partial results. This is used for parsers
that support streaming
"""
if len(result) != 1:
msg = "This output parser can only be used with a single generation."
raise NotImplementedError(msg)
generation = result[0]
if not isinstance(generation, ChatGeneration):
# Say that this one only works with chat generations
msg = "This output parser can only be used with a chat generation."
raise OutputParserException(msg)
content = generation.message.content
assert isinstance(content, str)
return content.swapcase()
model = GenericFakeChatModel(messages=iter([AIMessage(content="hEllo")]))
chain = model | StrInvertCase()
assert chain.invoke("") == "HeLLO"
def test_base_transform_output_parser() -> None:
"""Test base transform output parser."""
class StrInvertCase(BaseTransformOutputParser[str]):
"""An example parser that inverts the case of the characters in the message."""
def parse(self, text: str) -> str:
"""Parse a single string into a specific format."""
raise NotImplementedError
@override
def parse_result(
self, result: list[Generation], *, partial: bool = False
) -> str:
"""Parse a list of model Generations into a specific format.
Args:
result: A list of `Generation` to be parsed. The Generations are assumed
to be different candidate outputs for a single model input.
Many parsers assume that only a single generation is passed it in.
We will assert for that
partial: Whether to allow partial results. This is used for parsers
that support streaming
"""
if len(result) != 1:
msg = "This output parser can only be used with a single generation."
raise NotImplementedError(msg)
generation = result[0]
if not isinstance(generation, ChatGeneration):
# Say that this one only works with chat generations
msg = "This output parser can only be used with a chat generation."
raise OutputParserException(msg)
content = generation.message.content
assert isinstance(content, str)
return content.swapcase()
model = GenericFakeChatModel(messages=iter([AIMessage(content="hello world")]))
chain = model | StrInvertCase()
# inputs to models are ignored, response is hard-coded in model definition
chunks = list(chain.stream(""))
assert chunks == ["HELLO", " ", "WORLD"]
Domain
Subdomains
Classes
Dependencies
- langchain_core.exceptions
- langchain_core.language_models
- langchain_core.messages
- langchain_core.output_parsers
- langchain_core.outputs
- typing_extensions
Source
Frequently Asked Questions
What does test_base_parsers.py do?
test_base_parsers.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_base_parsers.py?
test_base_parsers.py defines 2 function(s): test_base_generation_parser, test_base_transform_output_parser.
What does test_base_parsers.py depend on?
test_base_parsers.py imports 6 module(s): langchain_core.exceptions, langchain_core.language_models, langchain_core.messages, langchain_core.output_parsers, langchain_core.outputs, typing_extensions.
Where is test_base_parsers.py in the architecture?
test_base_parsers.py is located at libs/core/tests/unit_tests/output_parsers/test_base_parsers.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/tests/unit_tests/output_parsers).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free