Home / Function/ parse() — langchain Function Reference

parse() — langchain Function Reference

Architecture documentation for the parse() function in trajectory_eval_chain.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8614258b_8a81_eb94_7789_5188d33b6103["parse()"]
  65c932f4_0640_c33d_d4f3_07196188eaa5["TrajectoryOutputParser"]
  8614258b_8a81_eb94_7789_5188d33b6103 -->|defined in| 65c932f4_0640_c33d_d4f3_07196188eaa5
  e1f178f6_7558_62be_5f9e_904484841e62["_call()"]
  e1f178f6_7558_62be_5f9e_904484841e62 -->|calls| 8614258b_8a81_eb94_7789_5188d33b6103
  bb74900d_feda_ef5a_51a9_181534374461["_acall()"]
  bb74900d_feda_ef5a_51a9_181534374461 -->|calls| 8614258b_8a81_eb94_7789_5188d33b6103
  style 8614258b_8a81_eb94_7789_5188d33b6103 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py lines 56–93

    def parse(self, text: str) -> TrajectoryEval:
        """Parse the output text and extract the score and reasoning.

        Args:
            text: The output text to parse.

        Returns:
            A named tuple containing the normalized score and reasoning.

        Raises:
            If the score is not found in the output text or if the LLM's score is not a
            digit in the range 1-5.
        """
        if "Score:" not in text:
            msg = f"Could not find score in model eval output: {text}"
            raise OutputParserException(msg)

        reasoning, score_str = text.split("Score: ", maxsplit=1)

        reasoning, score_str = reasoning.strip(), score_str.strip()

        # Use regex to extract the score.
        # This will get the number in the string, even if it is a float or more than 10.
        # E.g. "Score: 1" will return 1, "Score: 3.5" will return 3.5, and
        # "Score: 10" will return 10.
        # The score should be an integer digit in the range 1-5.
        _score = re.search(r"(\d+(\.\d+)?)", score_str)
        # If the score is not found or is a float, raise an exception.
        if _score is None or "." in _score.group(1):
            msg = f"Score is not an integer digit in the range 1-5: {text}"
            raise OutputParserException(msg)
        score = int(_score.group(1))
        # If the score is not in the range 1-5, raise an exception.
        if not 1 <= score <= _MAX_SCORE:
            msg = f"Score is not a digit in the range 1-5: {text}"
            raise OutputParserException(msg)
        normalized_score = (score - 1) / (_MAX_SCORE - 1)
        return TrajectoryEval(score=normalized_score, reasoning=reasoning)

Domain

Subdomains

Called By

Frequently Asked Questions

What does parse() do?
parse() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py.
Where is parse() defined?
parse() is defined in libs/langchain/langchain_classic/evaluation/agents/trajectory_eval_chain.py at line 56.
What calls parse()?
parse() is called by 2 function(s): _acall, _call.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free